xref: /freebsd/tools/regression/sockets/unix_cmsg/t_cmsgcred_sockcred.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
1 /*-
2  * Copyright (c) 2005 Andrey Simonenko
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33 #include <inttypes.h>
34 #include <stdarg.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 
38 #include "uc_common.h"
39 #include "t_generic.h"
40 #include "t_cmsgcred.h"
41 #include "t_cmsgcred_sockcred.h"
42 
43 static int
44 t_cmsgcred_sockcred_server(int fd1)
45 {
46 	struct msghdr msghdr;
47 	struct iovec iov[1];
48 	struct cmsghdr *cmsghdr;
49 	void *cmsg_data, *cmsg1_data, *cmsg2_data;
50 	size_t cmsg_size, cmsg1_size, cmsg2_size;
51 	u_int i;
52 	int fd2, rv, val;
53 
54 	fd2 = -1;
55 	rv = -2;
56 
57 	cmsg1_size = CMSG_SPACE(SOCKCREDSIZE(uc_cfg.proc_cred.gid_num));
58 	cmsg2_size = CMSG_SPACE(sizeof(struct cmsgcred));
59 	cmsg1_data = malloc(cmsg1_size);
60 	cmsg2_data = malloc(cmsg2_size);
61 	if (cmsg1_data == NULL || cmsg2_data == NULL) {
62 		uc_logmsg("malloc");
63 		goto done;
64 	}
65 
66 	uc_dbgmsg("setting LOCAL_CREDS");
67 	val = 1;
68 	if (setsockopt(fd1, 0, LOCAL_CREDS, &val, sizeof(val)) < 0) {
69 		uc_logmsg("setsockopt(LOCAL_CREDS)");
70 		goto done;
71 	}
72 
73 	if (uc_sync_send() < 0)
74 		goto done;
75 
76 	if (uc_cfg.sock_type == SOCK_STREAM) {
77 		fd2 = uc_socket_accept(fd1);
78 		if (fd2 < 0)
79 			goto done;
80 	} else
81 		fd2 = fd1;
82 
83 	cmsg_data = cmsg1_data;
84 	cmsg_size = cmsg1_size;
85 	rv = -1;
86 	for (i = 1; i <= uc_cfg.ipc_msg.msg_num; ++i) {
87 		uc_dbgmsg("message #%u", i);
88 
89 		uc_msghdr_init_server(&msghdr, iov, cmsg_data, cmsg_size);
90 		if (uc_message_recv(fd2, &msghdr) < 0) {
91 			rv = -2;
92 			break;
93 		}
94 
95 		if (uc_check_msghdr(&msghdr, sizeof(*cmsghdr)) < 0)
96 			break;
97 
98 		cmsghdr = CMSG_FIRSTHDR(&msghdr);
99 		if (i == 1 || uc_cfg.sock_type == SOCK_DGRAM) {
100 			if (uc_check_scm_creds_sockcred(cmsghdr) < 0)
101 				break;
102 		} else {
103 			if (uc_check_scm_creds_cmsgcred(cmsghdr) < 0)
104 				break;
105 		}
106 
107 		cmsg_data = cmsg2_data;
108 		cmsg_size = cmsg2_size;
109 	}
110 	if (i > uc_cfg.ipc_msg.msg_num)
111 		rv = 0;
112 done:
113 	free(cmsg1_data);
114 	free(cmsg2_data);
115 	if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)
116 		if (uc_socket_close(fd2) < 0)
117 			rv = -2;
118 	return (rv);
119 }
120 
121 int
122 t_cmsgcred_sockcred(void)
123 {
124 	return (t_generic(t_cmsgcred_client, t_cmsgcred_sockcred_server));
125 }
126