bl.c revision 1.3 1 1.3 christos /* $NetBSD: bl.c,v 1.3 2024/08/02 17:11:55 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.3 christos __RCSID("$NetBSD: bl.c,v 1.3 2024/08/02 17:11:55 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.3 christos #ifndef SYSLOG_DATA_INIT
63 1.3 christos struct syslog_data {
64 1.3 christos int dummy;
65 1.3 christos };
66 1.3 christos #define SYSLOG_DATA_INIT { 0 }
67 1.3 christos
68 1.3 christos static void
69 1.3 christos vsyslog_r(int priority, struct syslog_data *sd, const char *fmt, va_list ap)
70 1.3 christos {
71 1.3 christos vsyslog(priority, fmt, ap);
72 1.3 christos }
73 1.3 christos #endif
74 1.3 christos
75 1.1 christos typedef struct {
76 1.1 christos uint32_t bl_len;
77 1.1 christos uint32_t bl_version;
78 1.1 christos uint32_t bl_type;
79 1.1 christos uint32_t bl_salen;
80 1.1 christos struct sockaddr_storage bl_ss;
81 1.1 christos char bl_data[];
82 1.1 christos } bl_message_t;
83 1.1 christos
84 1.1 christos struct blocklist {
85 1.1 christos #ifdef _REENTRANT
86 1.1 christos pthread_mutex_t b_mutex;
87 1.1 christos # define BL_INIT(b) pthread_mutex_init(&b->b_mutex, NULL)
88 1.1 christos # define BL_LOCK(b) pthread_mutex_lock(&b->b_mutex)
89 1.1 christos # define BL_UNLOCK(b) pthread_mutex_unlock(&b->b_mutex)
90 1.1 christos #else
91 1.1 christos # define BL_INIT(b) do {} while(/*CONSTCOND*/0)
92 1.1 christos # define BL_LOCK(b) BL_INIT(b)
93 1.1 christos # define BL_UNLOCK(b) BL_INIT(b)
94 1.1 christos #endif
95 1.1 christos int b_fd;
96 1.1 christos int b_connected;
97 1.1 christos struct sockaddr_un b_sun;
98 1.3 christos struct syslog_data b_syslog_data;
99 1.3 christos void (*b_fun)(int, struct syslog_data *, const char *, va_list);
100 1.1 christos bl_info_t b_info;
101 1.1 christos };
102 1.1 christos
103 1.1 christos #define BL_VERSION 1
104 1.1 christos
105 1.1 christos bool
106 1.1 christos bl_isconnected(bl_t b)
107 1.1 christos {
108 1.1 christos return b->b_connected == 0;
109 1.1 christos }
110 1.1 christos
111 1.1 christos int
112 1.1 christos bl_getfd(bl_t b)
113 1.1 christos {
114 1.1 christos return b->b_fd;
115 1.1 christos }
116 1.1 christos
117 1.1 christos static void
118 1.1 christos bl_reset(bl_t b, bool locked)
119 1.1 christos {
120 1.1 christos int serrno = errno;
121 1.1 christos if (!locked)
122 1.1 christos BL_LOCK(b);
123 1.1 christos close(b->b_fd);
124 1.1 christos errno = serrno;
125 1.1 christos b->b_fd = -1;
126 1.1 christos b->b_connected = -1;
127 1.1 christos if (!locked)
128 1.1 christos BL_UNLOCK(b);
129 1.1 christos }
130 1.1 christos
131 1.1 christos static void
132 1.3 christos bl_log(bl_t b, int level, const char *fmt, ...)
133 1.1 christos {
134 1.1 christos va_list ap;
135 1.1 christos int serrno = errno;
136 1.1 christos
137 1.3 christos if (b->b_fun == NULL)
138 1.3 christos return;
139 1.3 christos
140 1.1 christos va_start(ap, fmt);
141 1.3 christos (*b->b_fun)(level, &b->b_syslog_data, fmt, ap);
142 1.1 christos va_end(ap);
143 1.1 christos errno = serrno;
144 1.1 christos }
145 1.1 christos
146 1.1 christos static int
147 1.1 christos bl_init(bl_t b, bool srv)
148 1.1 christos {
149 1.1 christos static int one = 1;
150 1.1 christos /* AF_UNIX address of local logger */
151 1.1 christos mode_t om;
152 1.1 christos int rv, serrno;
153 1.1 christos struct sockaddr_un *sun = &b->b_sun;
154 1.1 christos
155 1.1 christos #ifndef SOCK_NONBLOCK
156 1.1 christos #define SOCK_NONBLOCK 0
157 1.1 christos #endif
158 1.1 christos #ifndef SOCK_CLOEXEC
159 1.1 christos #define SOCK_CLOEXEC 0
160 1.1 christos #endif
161 1.1 christos #ifndef SOCK_NOSIGPIPE
162 1.1 christos #define SOCK_NOSIGPIPE 0
163 1.1 christos #endif
164 1.1 christos
165 1.1 christos BL_LOCK(b);
166 1.1 christos
167 1.1 christos if (b->b_fd == -1) {
168 1.1 christos b->b_fd = socket(PF_LOCAL,
169 1.1 christos SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK|SOCK_NOSIGPIPE, 0);
170 1.1 christos if (b->b_fd == -1) {
171 1.3 christos bl_log(b, LOG_ERR, "%s: socket failed (%s)",
172 1.1 christos __func__, strerror(errno));
173 1.1 christos BL_UNLOCK(b);
174 1.1 christos return -1;
175 1.1 christos }
176 1.1 christos #if SOCK_CLOEXEC == 0
177 1.1 christos fcntl(b->b_fd, F_SETFD, FD_CLOEXEC);
178 1.1 christos #endif
179 1.1 christos #if SOCK_NONBLOCK == 0
180 1.1 christos fcntl(b->b_fd, F_SETFL, fcntl(b->b_fd, F_GETFL) | O_NONBLOCK);
181 1.1 christos #endif
182 1.1 christos #if SOCK_NOSIGPIPE == 0
183 1.1 christos #ifdef SO_NOSIGPIPE
184 1.1 christos int o = 1;
185 1.1 christos setsockopt(b->b_fd, SOL_SOCKET, SO_NOSIGPIPE, &o, sizeof(o));
186 1.1 christos #else
187 1.1 christos signal(SIGPIPE, SIG_IGN);
188 1.1 christos #endif
189 1.1 christos #endif
190 1.1 christos }
191 1.1 christos
192 1.1 christos if (bl_isconnected(b)) {
193 1.1 christos BL_UNLOCK(b);
194 1.1 christos return 0;
195 1.1 christos }
196 1.1 christos
197 1.1 christos /*
198 1.1 christos * We try to connect anyway even when we are a server to verify
199 1.1 christos * that no other server is listening to the socket. If we succeed
200 1.1 christos * to connect and we are a server, someone else owns it.
201 1.1 christos */
202 1.1 christos rv = connect(b->b_fd, (const void *)sun, (socklen_t)sizeof(*sun));
203 1.1 christos if (rv == 0) {
204 1.1 christos if (srv) {
205 1.3 christos bl_log(b, LOG_ERR,
206 1.1 christos "%s: another daemon is handling `%s'",
207 1.1 christos __func__, sun->sun_path);
208 1.1 christos goto out;
209 1.1 christos }
210 1.1 christos } else {
211 1.1 christos if (!srv) {
212 1.1 christos /*
213 1.1 christos * If the daemon is not running, we just try a
214 1.1 christos * connect, so leave the socket alone until it does
215 1.1 christos * and only log once.
216 1.1 christos */
217 1.1 christos if (b->b_connected != 1) {
218 1.3 christos bl_log(b, LOG_DEBUG,
219 1.1 christos "%s: connect failed for `%s' (%s)",
220 1.1 christos __func__, sun->sun_path, strerror(errno));
221 1.1 christos b->b_connected = 1;
222 1.1 christos }
223 1.1 christos BL_UNLOCK(b);
224 1.1 christos return -1;
225 1.1 christos }
226 1.3 christos bl_log(b, LOG_DEBUG, "Connected to blocklist server", __func__);
227 1.1 christos }
228 1.1 christos
229 1.1 christos if (srv) {
230 1.1 christos (void)unlink(sun->sun_path);
231 1.1 christos om = umask(0);
232 1.1 christos rv = bind(b->b_fd, (const void *)sun, (socklen_t)sizeof(*sun));
233 1.1 christos serrno = errno;
234 1.1 christos (void)umask(om);
235 1.1 christos errno = serrno;
236 1.1 christos if (rv == -1) {
237 1.3 christos bl_log(b, LOG_ERR, "%s: bind failed for `%s' (%s)",
238 1.1 christos __func__, sun->sun_path, strerror(errno));
239 1.1 christos goto out;
240 1.1 christos }
241 1.1 christos }
242 1.1 christos
243 1.1 christos b->b_connected = 0;
244 1.1 christos #define GOT_FD 1
245 1.1 christos #if defined(LOCAL_CREDS)
246 1.1 christos #define CRED_LEVEL 0
247 1.1 christos #define CRED_NAME LOCAL_CREDS
248 1.1 christos #define CRED_SC_UID sc_euid
249 1.1 christos #define CRED_SC_GID sc_egid
250 1.1 christos #define CRED_MESSAGE SCM_CREDS
251 1.1 christos #define CRED_SIZE SOCKCREDSIZE(NGROUPS_MAX)
252 1.1 christos #define CRED_TYPE struct sockcred
253 1.1 christos #define GOT_CRED 2
254 1.1 christos #elif defined(SO_PASSCRED)
255 1.1 christos #define CRED_LEVEL SOL_SOCKET
256 1.1 christos #define CRED_NAME SO_PASSCRED
257 1.1 christos #define CRED_SC_UID uid
258 1.1 christos #define CRED_SC_GID gid
259 1.1 christos #define CRED_MESSAGE SCM_CREDENTIALS
260 1.1 christos #define CRED_SIZE sizeof(struct ucred)
261 1.1 christos #define CRED_TYPE struct ucred
262 1.1 christos #define GOT_CRED 2
263 1.1 christos #else
264 1.1 christos #define GOT_CRED 0
265 1.1 christos /*
266 1.1 christos * getpeereid() and LOCAL_PEERCRED don't help here
267 1.1 christos * because we are not a stream socket!
268 1.1 christos */
269 1.1 christos #define CRED_SIZE 0
270 1.1 christos #define CRED_TYPE void * __unused
271 1.1 christos #endif
272 1.1 christos
273 1.1 christos #ifdef CRED_LEVEL
274 1.1 christos if (setsockopt(b->b_fd, CRED_LEVEL, CRED_NAME,
275 1.1 christos &one, (socklen_t)sizeof(one)) == -1) {
276 1.3 christos bl_log(b, LOG_ERR, "%s: setsockopt %s "
277 1.1 christos "failed (%s)", __func__, __STRING(CRED_NAME),
278 1.1 christos strerror(errno));
279 1.1 christos goto out;
280 1.1 christos }
281 1.1 christos #endif
282 1.1 christos
283 1.1 christos BL_UNLOCK(b);
284 1.1 christos return 0;
285 1.1 christos out:
286 1.1 christos bl_reset(b, true);
287 1.1 christos BL_UNLOCK(b);
288 1.1 christos return -1;
289 1.1 christos }
290 1.1 christos
291 1.1 christos bl_t
292 1.3 christos bl_create(bool srv, const char *path,
293 1.3 christos void (*fun)(int, struct syslog_data *, const char *, va_list))
294 1.1 christos {
295 1.3 christos static struct syslog_data sd = SYSLOG_DATA_INIT;
296 1.1 christos bl_t b = calloc(1, sizeof(*b));
297 1.1 christos if (b == NULL)
298 1.3 christos return NULL;
299 1.3 christos b->b_fun = fun;
300 1.3 christos b->b_syslog_data = sd;
301 1.1 christos b->b_fd = -1;
302 1.1 christos b->b_connected = -1;
303 1.1 christos BL_INIT(b);
304 1.1 christos
305 1.1 christos memset(&b->b_sun, 0, sizeof(b->b_sun));
306 1.1 christos b->b_sun.sun_family = AF_LOCAL;
307 1.1 christos #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
308 1.1 christos b->b_sun.sun_len = sizeof(b->b_sun);
309 1.1 christos #endif
310 1.1 christos strlcpy(b->b_sun.sun_path,
311 1.1 christos path ? path : _PATH_BLSOCK, sizeof(b->b_sun.sun_path));
312 1.1 christos
313 1.1 christos bl_init(b, srv);
314 1.1 christos return b;
315 1.1 christos }
316 1.1 christos
317 1.1 christos void
318 1.1 christos bl_destroy(bl_t b)
319 1.1 christos {
320 1.1 christos bl_reset(b, false);
321 1.1 christos free(b);
322 1.1 christos }
323 1.1 christos
324 1.1 christos static int
325 1.1 christos bl_getsock(bl_t b, struct sockaddr_storage *ss, const struct sockaddr *sa,
326 1.1 christos socklen_t slen, const char *ctx)
327 1.1 christos {
328 1.1 christos uint8_t family;
329 1.1 christos
330 1.1 christos memset(ss, 0, sizeof(*ss));
331 1.1 christos
332 1.1 christos switch (slen) {
333 1.1 christos case 0:
334 1.1 christos return 0;
335 1.1 christos case sizeof(struct sockaddr_in):
336 1.1 christos family = AF_INET;
337 1.1 christos break;
338 1.1 christos case sizeof(struct sockaddr_in6):
339 1.1 christos family = AF_INET6;
340 1.1 christos break;
341 1.1 christos default:
342 1.3 christos bl_log(b, LOG_ERR, "%s: invalid socket len %u (%s)",
343 1.1 christos __func__, (unsigned)slen, ctx);
344 1.1 christos errno = EINVAL;
345 1.1 christos return -1;
346 1.1 christos }
347 1.1 christos
348 1.1 christos memcpy(ss, sa, slen);
349 1.1 christos
350 1.1 christos if (ss->ss_family != family) {
351 1.3 christos bl_log(b, LOG_INFO,
352 1.1 christos "%s: correcting socket family %d to %d (%s)",
353 1.1 christos __func__, ss->ss_family, family, ctx);
354 1.1 christos ss->ss_family = family;
355 1.1 christos }
356 1.1 christos
357 1.1 christos #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
358 1.1 christos if (ss->ss_len != slen) {
359 1.3 christos bl_log(b, LOG_INFO,
360 1.1 christos "%s: correcting socket len %u to %u (%s)",
361 1.1 christos __func__, ss->ss_len, (unsigned)slen, ctx);
362 1.1 christos ss->ss_len = (uint8_t)slen;
363 1.1 christos }
364 1.1 christos #endif
365 1.1 christos return 0;
366 1.1 christos }
367 1.1 christos
368 1.1 christos int
369 1.1 christos bl_send(bl_t b, bl_type_t e, int pfd, const struct sockaddr *sa,
370 1.1 christos socklen_t slen, const char *ctx)
371 1.1 christos {
372 1.1 christos struct msghdr msg;
373 1.1 christos struct iovec iov;
374 1.1 christos union {
375 1.1 christos char ctrl[CMSG_SPACE(sizeof(int))];
376 1.1 christos uint32_t fd;
377 1.1 christos } ua;
378 1.1 christos struct cmsghdr *cmsg;
379 1.1 christos union {
380 1.1 christos bl_message_t bl;
381 1.1 christos char buf[512];
382 1.1 christos } ub;
383 1.1 christos size_t ctxlen, tried;
384 1.1 christos #define NTRIES 5
385 1.1 christos
386 1.1 christos ctxlen = strlen(ctx);
387 1.1 christos if (ctxlen > 128)
388 1.1 christos ctxlen = 128;
389 1.1 christos
390 1.1 christos iov.iov_base = ub.buf;
391 1.1 christos iov.iov_len = sizeof(bl_message_t) + ctxlen;
392 1.1 christos ub.bl.bl_len = (uint32_t)iov.iov_len;
393 1.1 christos ub.bl.bl_version = BL_VERSION;
394 1.1 christos ub.bl.bl_type = (uint32_t)e;
395 1.1 christos
396 1.1 christos if (bl_getsock(b, &ub.bl.bl_ss, sa, slen, ctx) == -1)
397 1.1 christos return -1;
398 1.1 christos
399 1.1 christos
400 1.1 christos ub.bl.bl_salen = slen;
401 1.1 christos memcpy(ub.bl.bl_data, ctx, ctxlen);
402 1.1 christos
403 1.1 christos msg.msg_name = NULL;
404 1.1 christos msg.msg_namelen = 0;
405 1.1 christos msg.msg_iov = &iov;
406 1.1 christos msg.msg_iovlen = 1;
407 1.1 christos msg.msg_flags = 0;
408 1.1 christos
409 1.1 christos msg.msg_control = ua.ctrl;
410 1.1 christos msg.msg_controllen = sizeof(ua.ctrl);
411 1.1 christos
412 1.1 christos cmsg = CMSG_FIRSTHDR(&msg);
413 1.1 christos cmsg->cmsg_len = CMSG_LEN(sizeof(int));
414 1.1 christos cmsg->cmsg_level = SOL_SOCKET;
415 1.1 christos cmsg->cmsg_type = SCM_RIGHTS;
416 1.1 christos
417 1.1 christos memcpy(CMSG_DATA(cmsg), &pfd, sizeof(pfd));
418 1.1 christos
419 1.1 christos tried = 0;
420 1.1 christos again:
421 1.1 christos if (bl_init(b, false) == -1)
422 1.1 christos return -1;
423 1.1 christos
424 1.1 christos if ((sendmsg(b->b_fd, &msg, 0) == -1) && tried++ < NTRIES) {
425 1.1 christos bl_reset(b, false);
426 1.1 christos goto again;
427 1.1 christos }
428 1.1 christos return tried >= NTRIES ? -1 : 0;
429 1.1 christos }
430 1.1 christos
431 1.1 christos bl_info_t *
432 1.1 christos bl_recv(bl_t b)
433 1.1 christos {
434 1.1 christos struct msghdr msg;
435 1.1 christos struct iovec iov;
436 1.1 christos union {
437 1.1 christos char ctrl[CMSG_SPACE(sizeof(int)) + CMSG_SPACE(CRED_SIZE)];
438 1.1 christos uint32_t fd;
439 1.1 christos CRED_TYPE sc;
440 1.1 christos } ua;
441 1.1 christos struct cmsghdr *cmsg;
442 1.1 christos CRED_TYPE *sc;
443 1.1 christos union {
444 1.1 christos bl_message_t bl;
445 1.1 christos char buf[512];
446 1.1 christos } ub;
447 1.1 christos int got;
448 1.1 christos ssize_t rlen;
449 1.2 christos size_t rem;
450 1.1 christos bl_info_t *bi = &b->b_info;
451 1.1 christos
452 1.1 christos got = 0;
453 1.1 christos memset(bi, 0, sizeof(*bi));
454 1.1 christos
455 1.1 christos iov.iov_base = ub.buf;
456 1.1 christos iov.iov_len = sizeof(ub);
457 1.1 christos
458 1.1 christos msg.msg_name = NULL;
459 1.1 christos msg.msg_namelen = 0;
460 1.1 christos msg.msg_iov = &iov;
461 1.1 christos msg.msg_iovlen = 1;
462 1.1 christos msg.msg_flags = 0;
463 1.1 christos
464 1.1 christos msg.msg_control = ua.ctrl;
465 1.1 christos msg.msg_controllen = sizeof(ua.ctrl) + 100;
466 1.1 christos
467 1.1 christos rlen = recvmsg(b->b_fd, &msg, 0);
468 1.1 christos if (rlen == -1) {
469 1.3 christos bl_log(b, LOG_ERR, "%s: recvmsg failed (%s)", __func__,
470 1.1 christos strerror(errno));
471 1.1 christos return NULL;
472 1.1 christos }
473 1.1 christos
474 1.1 christos for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
475 1.1 christos if (cmsg->cmsg_level != SOL_SOCKET) {
476 1.3 christos bl_log(b, LOG_ERR,
477 1.1 christos "%s: unexpected cmsg_level %d",
478 1.1 christos __func__, cmsg->cmsg_level);
479 1.1 christos continue;
480 1.1 christos }
481 1.1 christos switch (cmsg->cmsg_type) {
482 1.1 christos case SCM_RIGHTS:
483 1.1 christos if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
484 1.3 christos bl_log(b, LOG_ERR,
485 1.1 christos "%s: unexpected cmsg_len %d != %zu",
486 1.1 christos __func__, cmsg->cmsg_len,
487 1.1 christos CMSG_LEN(2 * sizeof(int)));
488 1.1 christos continue;
489 1.1 christos }
490 1.1 christos memcpy(&bi->bi_fd, CMSG_DATA(cmsg), sizeof(bi->bi_fd));
491 1.1 christos got |= GOT_FD;
492 1.1 christos break;
493 1.1 christos #ifdef CRED_MESSAGE
494 1.1 christos case CRED_MESSAGE:
495 1.1 christos sc = (void *)CMSG_DATA(cmsg);
496 1.1 christos bi->bi_uid = sc->CRED_SC_UID;
497 1.1 christos bi->bi_gid = sc->CRED_SC_GID;
498 1.1 christos got |= GOT_CRED;
499 1.1 christos break;
500 1.1 christos #endif
501 1.1 christos default:
502 1.3 christos bl_log(b, LOG_ERR,
503 1.1 christos "%s: unexpected cmsg_type %d",
504 1.1 christos __func__, cmsg->cmsg_type);
505 1.1 christos continue;
506 1.1 christos }
507 1.1 christos
508 1.1 christos }
509 1.1 christos
510 1.1 christos if (got != (GOT_CRED|GOT_FD)) {
511 1.3 christos bl_log(b, LOG_ERR, "message missing %s %s",
512 1.1 christos #if GOT_CRED != 0
513 1.1 christos (got & GOT_CRED) == 0 ? "cred" :
514 1.1 christos #endif
515 1.1 christos "", (got & GOT_FD) == 0 ? "fd" : "");
516 1.1 christos return NULL;
517 1.1 christos }
518 1.1 christos
519 1.2 christos rem = (size_t)rlen;
520 1.2 christos if (rem < sizeof(ub.bl)) {
521 1.3 christos bl_log(b, LOG_ERR, "message too short %zd", rlen);
522 1.1 christos return NULL;
523 1.1 christos }
524 1.2 christos rem -= sizeof(ub.bl);
525 1.1 christos
526 1.1 christos if (ub.bl.bl_version != BL_VERSION) {
527 1.3 christos bl_log(b, LOG_ERR, "bad version %d", ub.bl.bl_version);
528 1.1 christos return NULL;
529 1.1 christos }
530 1.1 christos
531 1.1 christos bi->bi_type = ub.bl.bl_type;
532 1.1 christos bi->bi_slen = ub.bl.bl_salen;
533 1.1 christos bi->bi_ss = ub.bl.bl_ss;
534 1.1 christos #ifndef CRED_MESSAGE
535 1.1 christos bi->bi_uid = -1;
536 1.1 christos bi->bi_gid = -1;
537 1.1 christos #endif
538 1.2 christos rem = MIN(sizeof(bi->bi_msg), rem);
539 1.2 christos if (rem == 0)
540 1.2 christos bi->bi_msg[0] = '\0';
541 1.2 christos else
542 1.2 christos strlcpy(bi->bi_msg, ub.bl.bl_data, rem);
543 1.1 christos return bi;
544 1.1 christos }
545