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