xref: /freebsd-src/tools/regression/sockets/unix_cmsg/t_cmsgcred_sockcred.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
1*1fab62b1SMaxim Sobolev /*-
2*1fab62b1SMaxim Sobolev  * Copyright (c) 2005 Andrey Simonenko
3*1fab62b1SMaxim Sobolev  * All rights reserved.
4*1fab62b1SMaxim Sobolev  *
5*1fab62b1SMaxim Sobolev  * Redistribution and use in source and binary forms, with or without
6*1fab62b1SMaxim Sobolev  * modification, are permitted provided that the following conditions
7*1fab62b1SMaxim Sobolev  * are met:
8*1fab62b1SMaxim Sobolev  * 1. Redistributions of source code must retain the above copyright
9*1fab62b1SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer.
10*1fab62b1SMaxim Sobolev  * 2. Redistributions in binary form must reproduce the above copyright
11*1fab62b1SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer in the
12*1fab62b1SMaxim Sobolev  *    documentation and/or other materials provided with the distribution.
13*1fab62b1SMaxim Sobolev  *
14*1fab62b1SMaxim Sobolev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*1fab62b1SMaxim Sobolev  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*1fab62b1SMaxim Sobolev  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*1fab62b1SMaxim Sobolev  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*1fab62b1SMaxim Sobolev  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*1fab62b1SMaxim Sobolev  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*1fab62b1SMaxim Sobolev  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*1fab62b1SMaxim Sobolev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*1fab62b1SMaxim Sobolev  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*1fab62b1SMaxim Sobolev  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*1fab62b1SMaxim Sobolev  * SUCH DAMAGE.
25*1fab62b1SMaxim Sobolev  */
26*1fab62b1SMaxim Sobolev 
27*1fab62b1SMaxim Sobolev #include <sys/types.h>
28*1fab62b1SMaxim Sobolev #include <sys/socket.h>
29*1fab62b1SMaxim Sobolev #include <sys/un.h>
30*1fab62b1SMaxim Sobolev #include <inttypes.h>
31*1fab62b1SMaxim Sobolev #include <stdarg.h>
32*1fab62b1SMaxim Sobolev #include <stdbool.h>
33*1fab62b1SMaxim Sobolev #include <stdlib.h>
34*1fab62b1SMaxim Sobolev 
35*1fab62b1SMaxim Sobolev #include "uc_common.h"
36*1fab62b1SMaxim Sobolev #include "t_generic.h"
37*1fab62b1SMaxim Sobolev #include "t_cmsgcred.h"
38*1fab62b1SMaxim Sobolev #include "t_cmsgcred_sockcred.h"
39*1fab62b1SMaxim Sobolev 
40*1fab62b1SMaxim Sobolev static int
t_cmsgcred_sockcred_server(int fd1)41*1fab62b1SMaxim Sobolev t_cmsgcred_sockcred_server(int fd1)
42*1fab62b1SMaxim Sobolev {
43*1fab62b1SMaxim Sobolev 	struct msghdr msghdr;
44*1fab62b1SMaxim Sobolev 	struct iovec iov[1];
45*1fab62b1SMaxim Sobolev 	struct cmsghdr *cmsghdr;
46*1fab62b1SMaxim Sobolev 	void *cmsg_data, *cmsg1_data, *cmsg2_data;
47*1fab62b1SMaxim Sobolev 	size_t cmsg_size, cmsg1_size, cmsg2_size;
48*1fab62b1SMaxim Sobolev 	u_int i;
49*1fab62b1SMaxim Sobolev 	int fd2, rv, val;
50*1fab62b1SMaxim Sobolev 
51*1fab62b1SMaxim Sobolev 	fd2 = -1;
52*1fab62b1SMaxim Sobolev 	rv = -2;
53*1fab62b1SMaxim Sobolev 
54*1fab62b1SMaxim Sobolev 	cmsg1_size = CMSG_SPACE(SOCKCREDSIZE(uc_cfg.proc_cred.gid_num));
55*1fab62b1SMaxim Sobolev 	cmsg2_size = CMSG_SPACE(sizeof(struct cmsgcred));
56*1fab62b1SMaxim Sobolev 	cmsg1_data = malloc(cmsg1_size);
57*1fab62b1SMaxim Sobolev 	cmsg2_data = malloc(cmsg2_size);
58*1fab62b1SMaxim Sobolev 	if (cmsg1_data == NULL || cmsg2_data == NULL) {
59*1fab62b1SMaxim Sobolev 		uc_logmsg("malloc");
60*1fab62b1SMaxim Sobolev 		goto done;
61*1fab62b1SMaxim Sobolev 	}
62*1fab62b1SMaxim Sobolev 
63*1fab62b1SMaxim Sobolev 	uc_dbgmsg("setting LOCAL_CREDS");
64*1fab62b1SMaxim Sobolev 	val = 1;
65*1fab62b1SMaxim Sobolev 	if (setsockopt(fd1, 0, LOCAL_CREDS, &val, sizeof(val)) < 0) {
66*1fab62b1SMaxim Sobolev 		uc_logmsg("setsockopt(LOCAL_CREDS)");
67*1fab62b1SMaxim Sobolev 		goto done;
68*1fab62b1SMaxim Sobolev 	}
69*1fab62b1SMaxim Sobolev 
70*1fab62b1SMaxim Sobolev 	if (uc_sync_send() < 0)
71*1fab62b1SMaxim Sobolev 		goto done;
72*1fab62b1SMaxim Sobolev 
73*1fab62b1SMaxim Sobolev 	if (uc_cfg.sock_type == SOCK_STREAM) {
74*1fab62b1SMaxim Sobolev 		fd2 = uc_socket_accept(fd1);
75*1fab62b1SMaxim Sobolev 		if (fd2 < 0)
76*1fab62b1SMaxim Sobolev 			goto done;
77*1fab62b1SMaxim Sobolev 	} else
78*1fab62b1SMaxim Sobolev 		fd2 = fd1;
79*1fab62b1SMaxim Sobolev 
80*1fab62b1SMaxim Sobolev 	cmsg_data = cmsg1_data;
81*1fab62b1SMaxim Sobolev 	cmsg_size = cmsg1_size;
82*1fab62b1SMaxim Sobolev 	rv = -1;
83*1fab62b1SMaxim Sobolev 	for (i = 1; i <= uc_cfg.ipc_msg.msg_num; ++i) {
84*1fab62b1SMaxim Sobolev 		uc_dbgmsg("message #%u", i);
85*1fab62b1SMaxim Sobolev 
86*1fab62b1SMaxim Sobolev 		uc_msghdr_init_server(&msghdr, iov, cmsg_data, cmsg_size);
87*1fab62b1SMaxim Sobolev 		if (uc_message_recv(fd2, &msghdr) < 0) {
88*1fab62b1SMaxim Sobolev 			rv = -2;
89*1fab62b1SMaxim Sobolev 			break;
90*1fab62b1SMaxim Sobolev 		}
91*1fab62b1SMaxim Sobolev 
92*1fab62b1SMaxim Sobolev 		if (uc_check_msghdr(&msghdr, sizeof(*cmsghdr)) < 0)
93*1fab62b1SMaxim Sobolev 			break;
94*1fab62b1SMaxim Sobolev 
95*1fab62b1SMaxim Sobolev 		cmsghdr = CMSG_FIRSTHDR(&msghdr);
96*1fab62b1SMaxim Sobolev 		if (i == 1 || uc_cfg.sock_type == SOCK_DGRAM) {
97*1fab62b1SMaxim Sobolev 			if (uc_check_scm_creds_sockcred(cmsghdr) < 0)
98*1fab62b1SMaxim Sobolev 				break;
99*1fab62b1SMaxim Sobolev 		} else {
100*1fab62b1SMaxim Sobolev 			if (uc_check_scm_creds_cmsgcred(cmsghdr) < 0)
101*1fab62b1SMaxim Sobolev 				break;
102*1fab62b1SMaxim Sobolev 		}
103*1fab62b1SMaxim Sobolev 
104*1fab62b1SMaxim Sobolev 		cmsg_data = cmsg2_data;
105*1fab62b1SMaxim Sobolev 		cmsg_size = cmsg2_size;
106*1fab62b1SMaxim Sobolev 	}
107*1fab62b1SMaxim Sobolev 	if (i > uc_cfg.ipc_msg.msg_num)
108*1fab62b1SMaxim Sobolev 		rv = 0;
109*1fab62b1SMaxim Sobolev done:
110*1fab62b1SMaxim Sobolev 	free(cmsg1_data);
111*1fab62b1SMaxim Sobolev 	free(cmsg2_data);
112*1fab62b1SMaxim Sobolev 	if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)
113*1fab62b1SMaxim Sobolev 		if (uc_socket_close(fd2) < 0)
114*1fab62b1SMaxim Sobolev 			rv = -2;
115*1fab62b1SMaxim Sobolev 	return (rv);
116*1fab62b1SMaxim Sobolev }
117*1fab62b1SMaxim Sobolev 
118*1fab62b1SMaxim Sobolev int
t_cmsgcred_sockcred(void)119*1fab62b1SMaxim Sobolev t_cmsgcred_sockcred(void)
120*1fab62b1SMaxim Sobolev {
121*1fab62b1SMaxim Sobolev 	return (t_generic(t_cmsgcred_client, t_cmsgcred_sockcred_server));
122*1fab62b1SMaxim Sobolev }
123