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