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