bl.c revision 1.2 1 1.2 christos /* $NetBSD: bl.c,v 1.2 2022/06/12 17:54:15 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos #ifdef HAVE_CONFIG_H
32 1.1 christos #include "config.h"
33 1.1 christos #endif
34 1.1 christos
35 1.1 christos #include <sys/cdefs.h>
36 1.2 christos __RCSID("$NetBSD: bl.c,v 1.2 2022/06/12 17:54:15 christos Exp $");
37 1.1 christos
38 1.1 christos #include <sys/param.h>
39 1.1 christos #include <sys/types.h>
40 1.1 christos #include <sys/socket.h>
41 1.1 christos #include <sys/stat.h>
42 1.1 christos #include <sys/un.h>
43 1.1 christos
44 1.1 christos #include <stdio.h>
45 1.1 christos #include <string.h>
46 1.1 christos #include <syslog.h>
47 1.1 christos #include <signal.h>
48 1.1 christos #include <fcntl.h>
49 1.1 christos #include <stdlib.h>
50 1.1 christos #include <unistd.h>
51 1.1 christos #include <stdint.h>
52 1.1 christos #include <stdbool.h>
53 1.1 christos #include <errno.h>
54 1.1 christos #include <stdarg.h>
55 1.1 christos #include <netinet/in.h>
56 1.1 christos #ifdef _REENTRANT
57 1.1 christos #include <pthread.h>
58 1.1 christos #endif
59 1.1 christos
60 1.1 christos #include "bl.h"
61 1.1 christos
62 1.1 christos typedef struct {
63 1.1 christos uint32_t bl_len;
64 1.1 christos uint32_t bl_version;
65 1.1 christos uint32_t bl_type;
66 1.1 christos uint32_t bl_salen;
67 1.1 christos struct sockaddr_storage bl_ss;
68 1.1 christos char bl_data[];
69 1.1 christos } bl_message_t;
70 1.1 christos
71 1.1 christos struct blocklist {
72 1.1 christos #ifdef _REENTRANT
73 1.1 christos pthread_mutex_t b_mutex;
74 1.1 christos # define BL_INIT(b) pthread_mutex_init(&b->b_mutex, NULL)
75 1.1 christos # define BL_LOCK(b) pthread_mutex_lock(&b->b_mutex)
76 1.1 christos # define BL_UNLOCK(b) pthread_mutex_unlock(&b->b_mutex)
77 1.1 christos #else
78 1.1 christos # define BL_INIT(b) do {} while(/*CONSTCOND*/0)
79 1.1 christos # define BL_LOCK(b) BL_INIT(b)
80 1.1 christos # define BL_UNLOCK(b) BL_INIT(b)
81 1.1 christos #endif
82 1.1 christos int b_fd;
83 1.1 christos int b_connected;
84 1.1 christos struct sockaddr_un b_sun;
85 1.1 christos void (*b_fun)(int, const char *, va_list);
86 1.1 christos bl_info_t b_info;
87 1.1 christos };
88 1.1 christos
89 1.1 christos #define BL_VERSION 1
90 1.1 christos
91 1.1 christos bool
92 1.1 christos bl_isconnected(bl_t b)
93 1.1 christos {
94 1.1 christos return b->b_connected == 0;
95 1.1 christos }
96 1.1 christos
97 1.1 christos int
98 1.1 christos bl_getfd(bl_t b)
99 1.1 christos {
100 1.1 christos return b->b_fd;
101 1.1 christos }
102 1.1 christos
103 1.1 christos static void
104 1.1 christos bl_reset(bl_t b, bool locked)
105 1.1 christos {
106 1.1 christos int serrno = errno;
107 1.1 christos if (!locked)
108 1.1 christos BL_LOCK(b);
109 1.1 christos close(b->b_fd);
110 1.1 christos errno = serrno;
111 1.1 christos b->b_fd = -1;
112 1.1 christos b->b_connected = -1;
113 1.1 christos if (!locked)
114 1.1 christos BL_UNLOCK(b);
115 1.1 christos }
116 1.1 christos
117 1.1 christos static void
118 1.1 christos bl_log(void (*fun)(int, const char *, va_list), int level,
119 1.1 christos const char *fmt, ...)
120 1.1 christos {
121 1.1 christos va_list ap;
122 1.1 christos int serrno = errno;
123 1.1 christos
124 1.1 christos va_start(ap, fmt);
125 1.1 christos (*fun)(level, fmt, ap);
126 1.1 christos va_end(ap);
127 1.1 christos errno = serrno;
128 1.1 christos }
129 1.1 christos
130 1.1 christos static int
131 1.1 christos bl_init(bl_t b, bool srv)
132 1.1 christos {
133 1.1 christos static int one = 1;
134 1.1 christos /* AF_UNIX address of local logger */
135 1.1 christos mode_t om;
136 1.1 christos int rv, serrno;
137 1.1 christos struct sockaddr_un *sun = &b->b_sun;
138 1.1 christos
139 1.1 christos #ifndef SOCK_NONBLOCK
140 1.1 christos #define SOCK_NONBLOCK 0
141 1.1 christos #endif
142 1.1 christos #ifndef SOCK_CLOEXEC
143 1.1 christos #define SOCK_CLOEXEC 0
144 1.1 christos #endif
145 1.1 christos #ifndef SOCK_NOSIGPIPE
146 1.1 christos #define SOCK_NOSIGPIPE 0
147 1.1 christos #endif
148 1.1 christos
149 1.1 christos BL_LOCK(b);
150 1.1 christos
151 1.1 christos if (b->b_fd == -1) {
152 1.1 christos b->b_fd = socket(PF_LOCAL,
153 1.1 christos SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK|SOCK_NOSIGPIPE, 0);
154 1.1 christos if (b->b_fd == -1) {
155 1.1 christos bl_log(b->b_fun, LOG_ERR, "%s: socket failed (%s)",
156 1.1 christos __func__, strerror(errno));
157 1.1 christos BL_UNLOCK(b);
158 1.1 christos return -1;
159 1.1 christos }
160 1.1 christos #if SOCK_CLOEXEC == 0
161 1.1 christos fcntl(b->b_fd, F_SETFD, FD_CLOEXEC);
162 1.1 christos #endif
163 1.1 christos #if SOCK_NONBLOCK == 0
164 1.1 christos fcntl(b->b_fd, F_SETFL, fcntl(b->b_fd, F_GETFL) | O_NONBLOCK);
165 1.1 christos #endif
166 1.1 christos #if SOCK_NOSIGPIPE == 0
167 1.1 christos #ifdef SO_NOSIGPIPE
168 1.1 christos int o = 1;
169 1.1 christos setsockopt(b->b_fd, SOL_SOCKET, SO_NOSIGPIPE, &o, sizeof(o));
170 1.1 christos #else
171 1.1 christos signal(SIGPIPE, SIG_IGN);
172 1.1 christos #endif
173 1.1 christos #endif
174 1.1 christos }
175 1.1 christos
176 1.1 christos if (bl_isconnected(b)) {
177 1.1 christos BL_UNLOCK(b);
178 1.1 christos return 0;
179 1.1 christos }
180 1.1 christos
181 1.1 christos /*
182 1.1 christos * We try to connect anyway even when we are a server to verify
183 1.1 christos * that no other server is listening to the socket. If we succeed
184 1.1 christos * to connect and we are a server, someone else owns it.
185 1.1 christos */
186 1.1 christos rv = connect(b->b_fd, (const void *)sun, (socklen_t)sizeof(*sun));
187 1.1 christos if (rv == 0) {
188 1.1 christos if (srv) {
189 1.1 christos bl_log(b->b_fun, LOG_ERR,
190 1.1 christos "%s: another daemon is handling `%s'",
191 1.1 christos __func__, sun->sun_path);
192 1.1 christos goto out;
193 1.1 christos }
194 1.1 christos } else {
195 1.1 christos if (!srv) {
196 1.1 christos /*
197 1.1 christos * If the daemon is not running, we just try a
198 1.1 christos * connect, so leave the socket alone until it does
199 1.1 christos * and only log once.
200 1.1 christos */
201 1.1 christos if (b->b_connected != 1) {
202 1.1 christos bl_log(b->b_fun, LOG_DEBUG,
203 1.1 christos "%s: connect failed for `%s' (%s)",
204 1.1 christos __func__, sun->sun_path, strerror(errno));
205 1.1 christos b->b_connected = 1;
206 1.1 christos }
207 1.1 christos BL_UNLOCK(b);
208 1.1 christos return -1;
209 1.1 christos }
210 1.1 christos bl_log(b->b_fun, LOG_DEBUG, "Connected to blocklist server",
211 1.1 christos __func__);
212 1.1 christos }
213 1.1 christos
214 1.1 christos if (srv) {
215 1.1 christos (void)unlink(sun->sun_path);
216 1.1 christos om = umask(0);
217 1.1 christos rv = bind(b->b_fd, (const void *)sun, (socklen_t)sizeof(*sun));
218 1.1 christos serrno = errno;
219 1.1 christos (void)umask(om);
220 1.1 christos errno = serrno;
221 1.1 christos if (rv == -1) {
222 1.1 christos bl_log(b->b_fun, LOG_ERR,
223 1.1 christos "%s: bind failed for `%s' (%s)",
224 1.1 christos __func__, sun->sun_path, strerror(errno));
225 1.1 christos goto out;
226 1.1 christos }
227 1.1 christos }
228 1.1 christos
229 1.1 christos b->b_connected = 0;
230 1.1 christos #define GOT_FD 1
231 1.1 christos #if defined(LOCAL_CREDS)
232 1.1 christos #define CRED_LEVEL 0
233 1.1 christos #define CRED_NAME LOCAL_CREDS
234 1.1 christos #define CRED_SC_UID sc_euid
235 1.1 christos #define CRED_SC_GID sc_egid
236 1.1 christos #define CRED_MESSAGE SCM_CREDS
237 1.1 christos #define CRED_SIZE SOCKCREDSIZE(NGROUPS_MAX)
238 1.1 christos #define CRED_TYPE struct sockcred
239 1.1 christos #define GOT_CRED 2
240 1.1 christos #elif defined(SO_PASSCRED)
241 1.1 christos #define CRED_LEVEL SOL_SOCKET
242 1.1 christos #define CRED_NAME SO_PASSCRED
243 1.1 christos #define CRED_SC_UID uid
244 1.1 christos #define CRED_SC_GID gid
245 1.1 christos #define CRED_MESSAGE SCM_CREDENTIALS
246 1.1 christos #define CRED_SIZE sizeof(struct ucred)
247 1.1 christos #define CRED_TYPE struct ucred
248 1.1 christos #define GOT_CRED 2
249 1.1 christos #else
250 1.1 christos #define GOT_CRED 0
251 1.1 christos /*
252 1.1 christos * getpeereid() and LOCAL_PEERCRED don't help here
253 1.1 christos * because we are not a stream socket!
254 1.1 christos */
255 1.1 christos #define CRED_SIZE 0
256 1.1 christos #define CRED_TYPE void * __unused
257 1.1 christos #endif
258 1.1 christos
259 1.1 christos #ifdef CRED_LEVEL
260 1.1 christos if (setsockopt(b->b_fd, CRED_LEVEL, CRED_NAME,
261 1.1 christos &one, (socklen_t)sizeof(one)) == -1) {
262 1.1 christos bl_log(b->b_fun, LOG_ERR, "%s: setsockopt %s "
263 1.1 christos "failed (%s)", __func__, __STRING(CRED_NAME),
264 1.1 christos strerror(errno));
265 1.1 christos goto out;
266 1.1 christos }
267 1.1 christos #endif
268 1.1 christos
269 1.1 christos BL_UNLOCK(b);
270 1.1 christos return 0;
271 1.1 christos out:
272 1.1 christos bl_reset(b, true);
273 1.1 christos BL_UNLOCK(b);
274 1.1 christos return -1;
275 1.1 christos }
276 1.1 christos
277 1.1 christos bl_t
278 1.1 christos bl_create(bool srv, const char *path, void (*fun)(int, const char *, va_list))
279 1.1 christos {
280 1.1 christos bl_t b = calloc(1, sizeof(*b));
281 1.1 christos if (b == NULL)
282 1.1 christos goto out;
283 1.1 christos b->b_fun = fun == NULL ? vsyslog : fun;
284 1.1 christos b->b_fd = -1;
285 1.1 christos b->b_connected = -1;
286 1.1 christos BL_INIT(b);
287 1.1 christos
288 1.1 christos memset(&b->b_sun, 0, sizeof(b->b_sun));
289 1.1 christos b->b_sun.sun_family = AF_LOCAL;
290 1.1 christos #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
291 1.1 christos b->b_sun.sun_len = sizeof(b->b_sun);
292 1.1 christos #endif
293 1.1 christos strlcpy(b->b_sun.sun_path,
294 1.1 christos path ? path : _PATH_BLSOCK, sizeof(b->b_sun.sun_path));
295 1.1 christos
296 1.1 christos bl_init(b, srv);
297 1.1 christos return b;
298 1.1 christos out:
299 1.1 christos free(b);
300 1.1 christos bl_log(fun, LOG_ERR, "%s: malloc failed (%s)", __func__,
301 1.1 christos strerror(errno));
302 1.1 christos return NULL;
303 1.1 christos }
304 1.1 christos
305 1.1 christos void
306 1.1 christos bl_destroy(bl_t b)
307 1.1 christos {
308 1.1 christos bl_reset(b, false);
309 1.1 christos free(b);
310 1.1 christos }
311 1.1 christos
312 1.1 christos static int
313 1.1 christos bl_getsock(bl_t b, struct sockaddr_storage *ss, const struct sockaddr *sa,
314 1.1 christos socklen_t slen, const char *ctx)
315 1.1 christos {
316 1.1 christos uint8_t family;
317 1.1 christos
318 1.1 christos memset(ss, 0, sizeof(*ss));
319 1.1 christos
320 1.1 christos switch (slen) {
321 1.1 christos case 0:
322 1.1 christos return 0;
323 1.1 christos case sizeof(struct sockaddr_in):
324 1.1 christos family = AF_INET;
325 1.1 christos break;
326 1.1 christos case sizeof(struct sockaddr_in6):
327 1.1 christos family = AF_INET6;
328 1.1 christos break;
329 1.1 christos default:
330 1.1 christos bl_log(b->b_fun, LOG_ERR, "%s: invalid socket len %u (%s)",
331 1.1 christos __func__, (unsigned)slen, ctx);
332 1.1 christos errno = EINVAL;
333 1.1 christos return -1;
334 1.1 christos }
335 1.1 christos
336 1.1 christos memcpy(ss, sa, slen);
337 1.1 christos
338 1.1 christos if (ss->ss_family != family) {
339 1.1 christos bl_log(b->b_fun, LOG_INFO,
340 1.1 christos "%s: correcting socket family %d to %d (%s)",
341 1.1 christos __func__, ss->ss_family, family, ctx);
342 1.1 christos ss->ss_family = family;
343 1.1 christos }
344 1.1 christos
345 1.1 christos #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
346 1.1 christos if (ss->ss_len != slen) {
347 1.1 christos bl_log(b->b_fun, LOG_INFO,
348 1.1 christos "%s: correcting socket len %u to %u (%s)",
349 1.1 christos __func__, ss->ss_len, (unsigned)slen, ctx);
350 1.1 christos ss->ss_len = (uint8_t)slen;
351 1.1 christos }
352 1.1 christos #endif
353 1.1 christos return 0;
354 1.1 christos }
355 1.1 christos
356 1.1 christos int
357 1.1 christos bl_send(bl_t b, bl_type_t e, int pfd, const struct sockaddr *sa,
358 1.1 christos socklen_t slen, const char *ctx)
359 1.1 christos {
360 1.1 christos struct msghdr msg;
361 1.1 christos struct iovec iov;
362 1.1 christos union {
363 1.1 christos char ctrl[CMSG_SPACE(sizeof(int))];
364 1.1 christos uint32_t fd;
365 1.1 christos } ua;
366 1.1 christos struct cmsghdr *cmsg;
367 1.1 christos union {
368 1.1 christos bl_message_t bl;
369 1.1 christos char buf[512];
370 1.1 christos } ub;
371 1.1 christos size_t ctxlen, tried;
372 1.1 christos #define NTRIES 5
373 1.1 christos
374 1.1 christos ctxlen = strlen(ctx);
375 1.1 christos if (ctxlen > 128)
376 1.1 christos ctxlen = 128;
377 1.1 christos
378 1.1 christos iov.iov_base = ub.buf;
379 1.1 christos iov.iov_len = sizeof(bl_message_t) + ctxlen;
380 1.1 christos ub.bl.bl_len = (uint32_t)iov.iov_len;
381 1.1 christos ub.bl.bl_version = BL_VERSION;
382 1.1 christos ub.bl.bl_type = (uint32_t)e;
383 1.1 christos
384 1.1 christos if (bl_getsock(b, &ub.bl.bl_ss, sa, slen, ctx) == -1)
385 1.1 christos return -1;
386 1.1 christos
387 1.1 christos
388 1.1 christos ub.bl.bl_salen = slen;
389 1.1 christos memcpy(ub.bl.bl_data, ctx, ctxlen);
390 1.1 christos
391 1.1 christos msg.msg_name = NULL;
392 1.1 christos msg.msg_namelen = 0;
393 1.1 christos msg.msg_iov = &iov;
394 1.1 christos msg.msg_iovlen = 1;
395 1.1 christos msg.msg_flags = 0;
396 1.1 christos
397 1.1 christos msg.msg_control = ua.ctrl;
398 1.1 christos msg.msg_controllen = sizeof(ua.ctrl);
399 1.1 christos
400 1.1 christos cmsg = CMSG_FIRSTHDR(&msg);
401 1.1 christos cmsg->cmsg_len = CMSG_LEN(sizeof(int));
402 1.1 christos cmsg->cmsg_level = SOL_SOCKET;
403 1.1 christos cmsg->cmsg_type = SCM_RIGHTS;
404 1.1 christos
405 1.1 christos memcpy(CMSG_DATA(cmsg), &pfd, sizeof(pfd));
406 1.1 christos
407 1.1 christos tried = 0;
408 1.1 christos again:
409 1.1 christos if (bl_init(b, false) == -1)
410 1.1 christos return -1;
411 1.1 christos
412 1.1 christos if ((sendmsg(b->b_fd, &msg, 0) == -1) && tried++ < NTRIES) {
413 1.1 christos bl_reset(b, false);
414 1.1 christos goto again;
415 1.1 christos }
416 1.1 christos return tried >= NTRIES ? -1 : 0;
417 1.1 christos }
418 1.1 christos
419 1.1 christos bl_info_t *
420 1.1 christos bl_recv(bl_t b)
421 1.1 christos {
422 1.1 christos struct msghdr msg;
423 1.1 christos struct iovec iov;
424 1.1 christos union {
425 1.1 christos char ctrl[CMSG_SPACE(sizeof(int)) + CMSG_SPACE(CRED_SIZE)];
426 1.1 christos uint32_t fd;
427 1.1 christos CRED_TYPE sc;
428 1.1 christos } ua;
429 1.1 christos struct cmsghdr *cmsg;
430 1.1 christos CRED_TYPE *sc;
431 1.1 christos union {
432 1.1 christos bl_message_t bl;
433 1.1 christos char buf[512];
434 1.1 christos } ub;
435 1.1 christos int got;
436 1.1 christos ssize_t rlen;
437 1.2 christos size_t rem;
438 1.1 christos bl_info_t *bi = &b->b_info;
439 1.1 christos
440 1.1 christos got = 0;
441 1.1 christos memset(bi, 0, sizeof(*bi));
442 1.1 christos
443 1.1 christos iov.iov_base = ub.buf;
444 1.1 christos iov.iov_len = sizeof(ub);
445 1.1 christos
446 1.1 christos msg.msg_name = NULL;
447 1.1 christos msg.msg_namelen = 0;
448 1.1 christos msg.msg_iov = &iov;
449 1.1 christos msg.msg_iovlen = 1;
450 1.1 christos msg.msg_flags = 0;
451 1.1 christos
452 1.1 christos msg.msg_control = ua.ctrl;
453 1.1 christos msg.msg_controllen = sizeof(ua.ctrl) + 100;
454 1.1 christos
455 1.1 christos rlen = recvmsg(b->b_fd, &msg, 0);
456 1.1 christos if (rlen == -1) {
457 1.1 christos bl_log(b->b_fun, LOG_ERR, "%s: recvmsg failed (%s)", __func__,
458 1.1 christos strerror(errno));
459 1.1 christos return NULL;
460 1.1 christos }
461 1.1 christos
462 1.1 christos for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
463 1.1 christos if (cmsg->cmsg_level != SOL_SOCKET) {
464 1.1 christos bl_log(b->b_fun, LOG_ERR,
465 1.1 christos "%s: unexpected cmsg_level %d",
466 1.1 christos __func__, cmsg->cmsg_level);
467 1.1 christos continue;
468 1.1 christos }
469 1.1 christos switch (cmsg->cmsg_type) {
470 1.1 christos case SCM_RIGHTS:
471 1.1 christos if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
472 1.1 christos bl_log(b->b_fun, LOG_ERR,
473 1.1 christos "%s: unexpected cmsg_len %d != %zu",
474 1.1 christos __func__, cmsg->cmsg_len,
475 1.1 christos CMSG_LEN(2 * sizeof(int)));
476 1.1 christos continue;
477 1.1 christos }
478 1.1 christos memcpy(&bi->bi_fd, CMSG_DATA(cmsg), sizeof(bi->bi_fd));
479 1.1 christos got |= GOT_FD;
480 1.1 christos break;
481 1.1 christos #ifdef CRED_MESSAGE
482 1.1 christos case CRED_MESSAGE:
483 1.1 christos sc = (void *)CMSG_DATA(cmsg);
484 1.1 christos bi->bi_uid = sc->CRED_SC_UID;
485 1.1 christos bi->bi_gid = sc->CRED_SC_GID;
486 1.1 christos got |= GOT_CRED;
487 1.1 christos break;
488 1.1 christos #endif
489 1.1 christos default:
490 1.1 christos bl_log(b->b_fun, LOG_ERR,
491 1.1 christos "%s: unexpected cmsg_type %d",
492 1.1 christos __func__, cmsg->cmsg_type);
493 1.1 christos continue;
494 1.1 christos }
495 1.1 christos
496 1.1 christos }
497 1.1 christos
498 1.1 christos if (got != (GOT_CRED|GOT_FD)) {
499 1.1 christos bl_log(b->b_fun, LOG_ERR, "message missing %s %s",
500 1.1 christos #if GOT_CRED != 0
501 1.1 christos (got & GOT_CRED) == 0 ? "cred" :
502 1.1 christos #endif
503 1.1 christos "", (got & GOT_FD) == 0 ? "fd" : "");
504 1.1 christos return NULL;
505 1.1 christos }
506 1.1 christos
507 1.2 christos rem = (size_t)rlen;
508 1.2 christos if (rem < sizeof(ub.bl)) {
509 1.1 christos bl_log(b->b_fun, LOG_ERR, "message too short %zd", rlen);
510 1.1 christos return NULL;
511 1.1 christos }
512 1.2 christos rem -= sizeof(ub.bl);
513 1.1 christos
514 1.1 christos if (ub.bl.bl_version != BL_VERSION) {
515 1.1 christos bl_log(b->b_fun, LOG_ERR, "bad version %d", ub.bl.bl_version);
516 1.1 christos return NULL;
517 1.1 christos }
518 1.1 christos
519 1.1 christos bi->bi_type = ub.bl.bl_type;
520 1.1 christos bi->bi_slen = ub.bl.bl_salen;
521 1.1 christos bi->bi_ss = ub.bl.bl_ss;
522 1.1 christos #ifndef CRED_MESSAGE
523 1.1 christos bi->bi_uid = -1;
524 1.1 christos bi->bi_gid = -1;
525 1.1 christos #endif
526 1.2 christos rem = MIN(sizeof(bi->bi_msg), rem);
527 1.2 christos if (rem == 0)
528 1.2 christos bi->bi_msg[0] = '\0';
529 1.2 christos else
530 1.2 christos strlcpy(bi->bi_msg, ub.bl.bl_data, rem);
531 1.1 christos return bi;
532 1.1 christos }
533