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
39 int
t_cmsgcred_client(int fd)40 t_cmsgcred_client(int fd)
41 {
42 struct msghdr msghdr;
43 struct iovec iov[1];
44 void *cmsg_data;
45 size_t cmsg_size;
46 int rv;
47
48 if (uc_sync_recv() < 0)
49 return (-2);
50
51 rv = -2;
52
53 cmsg_size = CMSG_SPACE(sizeof(struct cmsgcred));
54 cmsg_data = malloc(cmsg_size);
55 if (cmsg_data == NULL) {
56 uc_logmsg("malloc");
57 goto done;
58 }
59 uc_msghdr_init_client(&msghdr, iov, cmsg_data, cmsg_size,
60 SCM_CREDS, sizeof(struct cmsgcred));
61
62 if (uc_socket_connect(fd) < 0)
63 goto done;
64
65 if (uc_message_sendn(fd, &msghdr) < 0)
66 goto done;
67
68 rv = 0;
69 done:
70 free(cmsg_data);
71 return (rv);
72 }
73
74 static int
t_cmsgcred_server(int fd1)75 t_cmsgcred_server(int fd1)
76 {
77 struct msghdr msghdr;
78 struct iovec iov[1];
79 struct cmsghdr *cmsghdr;
80 void *cmsg_data;
81 size_t cmsg_size;
82 u_int i;
83 int fd2, rv;
84
85 if (uc_sync_send() < 0)
86 return (-2);
87
88 fd2 = -1;
89 rv = -2;
90
91 cmsg_size = CMSG_SPACE(sizeof(struct cmsgcred));
92 cmsg_data = malloc(cmsg_size);
93 if (cmsg_data == NULL) {
94 uc_logmsg("malloc");
95 goto done;
96 }
97
98 if (uc_cfg.sock_type == SOCK_STREAM) {
99 fd2 = uc_socket_accept(fd1);
100 if (fd2 < 0)
101 goto done;
102 } else
103 fd2 = fd1;
104
105 rv = -1;
106 for (i = 1; i <= uc_cfg.ipc_msg.msg_num; ++i) {
107 uc_dbgmsg("message #%u", i);
108
109 uc_msghdr_init_server(&msghdr, iov, cmsg_data, cmsg_size);
110 if (uc_message_recv(fd2, &msghdr) < 0) {
111 rv = -2;
112 break;
113 }
114
115 if (uc_check_msghdr(&msghdr, sizeof(*cmsghdr)) < 0)
116 break;
117
118 cmsghdr = CMSG_FIRSTHDR(&msghdr);
119 if (uc_check_scm_creds_cmsgcred(cmsghdr) < 0)
120 break;
121 }
122 if (i > uc_cfg.ipc_msg.msg_num)
123 rv = 0;
124 done:
125 free(cmsg_data);
126 if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)
127 if (uc_socket_close(fd2) < 0)
128 rv = -2;
129 return (rv);
130 }
131
132 int
t_cmsgcred(void)133 t_cmsgcred(void)
134 {
135 return (t_generic(t_cmsgcred_client, t_cmsgcred_server));
136 }
137