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