2 * Copyright (c) 2005 Andrey Simonenko
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/ucred.h>
37 #include "uc_common.h"
38 #include "t_generic.h"
39 #include "t_peercred.h"
42 check_xucred(const struct xucred *xucred, socklen_t len)
46 if (len != sizeof(*xucred)) {
47 uc_logmsgx("option value size %zu != %zu",
48 (size_t)len, sizeof(*xucred));
52 uc_dbgmsg("xucred.cr_version %u", xucred->cr_version);
53 uc_dbgmsg("xucred.cr_uid %lu", (u_long)xucred->cr_uid);
54 uc_dbgmsg("xucred.cr_ngroups %d", xucred->cr_ngroups);
58 if (xucred->cr_version != XUCRED_VERSION) {
59 uc_logmsgx("xucred.cr_version %u != %d",
60 xucred->cr_version, XUCRED_VERSION);
63 if (xucred->cr_uid != uc_cfg.proc_cred.euid) {
64 uc_logmsgx("xucred.cr_uid %lu != %lu (EUID)",
65 (u_long)xucred->cr_uid, (u_long)uc_cfg.proc_cred.euid);
68 if (xucred->cr_ngroups == 0) {
69 uc_logmsgx("xucred.cr_ngroups == 0");
72 if (xucred->cr_ngroups < 0) {
73 uc_logmsgx("xucred.cr_ngroups < 0");
76 if (xucred->cr_ngroups > XU_NGROUPS) {
77 uc_logmsgx("xucred.cr_ngroups %hu > %u (max)",
78 xucred->cr_ngroups, XU_NGROUPS);
81 if (xucred->cr_groups[0] != uc_cfg.proc_cred.egid) {
82 uc_logmsgx("xucred.cr_groups[0] %lu != %lu (EGID)",
83 (u_long)xucred->cr_groups[0], (u_long)uc_cfg.proc_cred.egid);
86 if (uc_check_groups("xucred.cr_groups", xucred->cr_groups,
87 "xucred.cr_ngroups", xucred->cr_ngroups, false) < 0)
93 t_peercred_client(int fd)
98 if (uc_sync_recv() < 0)
101 if (uc_socket_connect(fd) < 0)
104 len = sizeof(xucred);
105 if (getsockopt(fd, 0, LOCAL_PEERCRED, &xucred, &len) < 0) {
106 uc_logmsg("getsockopt(LOCAL_PEERCRED)");
110 if (check_xucred(&xucred, len) < 0)
117 t_peercred_server(int fd1)
119 struct xucred xucred;
123 if (uc_sync_send() < 0)
126 fd2 = uc_socket_accept(fd1);
130 len = sizeof(xucred);
131 if (getsockopt(fd2, 0, LOCAL_PEERCRED, &xucred, &len) < 0) {
132 uc_logmsg("getsockopt(LOCAL_PEERCRED)");
137 if (check_xucred(&xucred, len) < 0) {
144 if (uc_socket_close(fd2) < 0)
152 return (t_generic(t_peercred_client, t_peercred_server));