1*5f4c09ddSEd Maste /* $NetBSD: blacklistd.c,v 1.38 2019/02/27 02:20:18 christos Exp $ */
2*5f4c09ddSEd Maste
3*5f4c09ddSEd Maste /*-
4*5f4c09ddSEd Maste * Copyright (c) 2015 The NetBSD Foundation, Inc.
5*5f4c09ddSEd Maste * All rights reserved.
6*5f4c09ddSEd Maste *
7*5f4c09ddSEd Maste * This code is derived from software contributed to The NetBSD Foundation
8*5f4c09ddSEd Maste * by Christos Zoulas.
9*5f4c09ddSEd Maste *
10*5f4c09ddSEd Maste * Redistribution and use in source and binary forms, with or without
11*5f4c09ddSEd Maste * modification, are permitted provided that the following conditions
12*5f4c09ddSEd Maste * are met:
13*5f4c09ddSEd Maste * 1. Redistributions of source code must retain the above copyright
14*5f4c09ddSEd Maste * notice, this list of conditions and the following disclaimer.
15*5f4c09ddSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
16*5f4c09ddSEd Maste * notice, this list of conditions and the following disclaimer in the
17*5f4c09ddSEd Maste * documentation and/or other materials provided with the distribution.
18*5f4c09ddSEd Maste *
19*5f4c09ddSEd Maste * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*5f4c09ddSEd Maste * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*5f4c09ddSEd Maste * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*5f4c09ddSEd Maste * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*5f4c09ddSEd Maste * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*5f4c09ddSEd Maste * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*5f4c09ddSEd Maste * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*5f4c09ddSEd Maste * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*5f4c09ddSEd Maste * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*5f4c09ddSEd Maste * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*5f4c09ddSEd Maste * POSSIBILITY OF SUCH DAMAGE.
30*5f4c09ddSEd Maste */
31*5f4c09ddSEd Maste #ifdef HAVE_CONFIG_H
32*5f4c09ddSEd Maste #include "config.h"
33*5f4c09ddSEd Maste #endif
34*5f4c09ddSEd Maste #include <sys/cdefs.h>
35*5f4c09ddSEd Maste __RCSID("$NetBSD: blacklistd.c,v 1.38 2019/02/27 02:20:18 christos Exp $");
36*5f4c09ddSEd Maste
37*5f4c09ddSEd Maste #include <sys/types.h>
38*5f4c09ddSEd Maste #include <sys/socket.h>
39*5f4c09ddSEd Maste #include <sys/queue.h>
40*5f4c09ddSEd Maste
41*5f4c09ddSEd Maste #ifdef HAVE_LIBUTIL_H
42*5f4c09ddSEd Maste #include <libutil.h>
43*5f4c09ddSEd Maste #endif
44*5f4c09ddSEd Maste #ifdef HAVE_UTIL_H
45*5f4c09ddSEd Maste #include <util.h>
46*5f4c09ddSEd Maste #endif
47*5f4c09ddSEd Maste #include <string.h>
48*5f4c09ddSEd Maste #include <signal.h>
49*5f4c09ddSEd Maste #include <netdb.h>
50*5f4c09ddSEd Maste #include <stdio.h>
51*5f4c09ddSEd Maste #include <stdbool.h>
52*5f4c09ddSEd Maste #include <string.h>
53*5f4c09ddSEd Maste #include <inttypes.h>
54*5f4c09ddSEd Maste #include <syslog.h>
55*5f4c09ddSEd Maste #include <ctype.h>
56*5f4c09ddSEd Maste #include <limits.h>
57*5f4c09ddSEd Maste #include <errno.h>
58*5f4c09ddSEd Maste #include <poll.h>
59*5f4c09ddSEd Maste #include <fcntl.h>
60*5f4c09ddSEd Maste #include <err.h>
61*5f4c09ddSEd Maste #include <stdlib.h>
62*5f4c09ddSEd Maste #include <unistd.h>
63*5f4c09ddSEd Maste #include <time.h>
64*5f4c09ddSEd Maste #include <ifaddrs.h>
65*5f4c09ddSEd Maste #include <netinet/in.h>
66*5f4c09ddSEd Maste
67*5f4c09ddSEd Maste #include "bl.h"
68*5f4c09ddSEd Maste #include "internal.h"
69*5f4c09ddSEd Maste #include "conf.h"
70*5f4c09ddSEd Maste #include "run.h"
71*5f4c09ddSEd Maste #include "state.h"
72*5f4c09ddSEd Maste #include "support.h"
73*5f4c09ddSEd Maste
74*5f4c09ddSEd Maste static const char *configfile = _PATH_BLCONF;
75*5f4c09ddSEd Maste static DB *state;
76*5f4c09ddSEd Maste static const char *dbfile = _PATH_BLSTATE;
77*5f4c09ddSEd Maste static sig_atomic_t readconf;
78*5f4c09ddSEd Maste static sig_atomic_t done;
79*5f4c09ddSEd Maste static int vflag;
80*5f4c09ddSEd Maste
81*5f4c09ddSEd Maste static void
sigusr1(int n __unused)82*5f4c09ddSEd Maste sigusr1(int n __unused)
83*5f4c09ddSEd Maste {
84*5f4c09ddSEd Maste debug++;
85*5f4c09ddSEd Maste }
86*5f4c09ddSEd Maste
87*5f4c09ddSEd Maste static void
sigusr2(int n __unused)88*5f4c09ddSEd Maste sigusr2(int n __unused)
89*5f4c09ddSEd Maste {
90*5f4c09ddSEd Maste debug--;
91*5f4c09ddSEd Maste }
92*5f4c09ddSEd Maste
93*5f4c09ddSEd Maste static void
sighup(int n __unused)94*5f4c09ddSEd Maste sighup(int n __unused)
95*5f4c09ddSEd Maste {
96*5f4c09ddSEd Maste readconf++;
97*5f4c09ddSEd Maste }
98*5f4c09ddSEd Maste
99*5f4c09ddSEd Maste static void
sigdone(int n __unused)100*5f4c09ddSEd Maste sigdone(int n __unused)
101*5f4c09ddSEd Maste {
102*5f4c09ddSEd Maste done++;
103*5f4c09ddSEd Maste }
104*5f4c09ddSEd Maste
105*5f4c09ddSEd Maste static __dead void
usage(int c)106*5f4c09ddSEd Maste usage(int c)
107*5f4c09ddSEd Maste {
108*5f4c09ddSEd Maste if (c != '?')
109*5f4c09ddSEd Maste warnx("Unknown option `%c'", (char)c);
110*5f4c09ddSEd Maste fprintf(stderr, "Usage: %s [-vdfr] [-c <config>] [-R <rulename>] "
111*5f4c09ddSEd Maste "[-P <sockpathsfile>] [-C <controlprog>] [-D <dbfile>] "
112*5f4c09ddSEd Maste "[-s <sockpath>] [-t <timeout>]\n", getprogname());
113*5f4c09ddSEd Maste exit(EXIT_FAILURE);
114*5f4c09ddSEd Maste }
115*5f4c09ddSEd Maste
116*5f4c09ddSEd Maste static int
getremoteaddress(bl_info_t * bi,struct sockaddr_storage * rss,socklen_t * rsl)117*5f4c09ddSEd Maste getremoteaddress(bl_info_t *bi, struct sockaddr_storage *rss, socklen_t *rsl)
118*5f4c09ddSEd Maste {
119*5f4c09ddSEd Maste *rsl = sizeof(*rss);
120*5f4c09ddSEd Maste memset(rss, 0, *rsl);
121*5f4c09ddSEd Maste
122*5f4c09ddSEd Maste if (getpeername(bi->bi_fd, (void *)rss, rsl) != -1)
123*5f4c09ddSEd Maste return 0;
124*5f4c09ddSEd Maste
125*5f4c09ddSEd Maste if (errno != ENOTCONN) {
126*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "getpeername failed (%m)");
127*5f4c09ddSEd Maste return -1;
128*5f4c09ddSEd Maste }
129*5f4c09ddSEd Maste
130*5f4c09ddSEd Maste if (bi->bi_slen == 0) {
131*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "unconnected socket with no peer in message");
132*5f4c09ddSEd Maste return -1;
133*5f4c09ddSEd Maste }
134*5f4c09ddSEd Maste
135*5f4c09ddSEd Maste switch (bi->bi_ss.ss_family) {
136*5f4c09ddSEd Maste case AF_INET:
137*5f4c09ddSEd Maste *rsl = sizeof(struct sockaddr_in);
138*5f4c09ddSEd Maste break;
139*5f4c09ddSEd Maste case AF_INET6:
140*5f4c09ddSEd Maste *rsl = sizeof(struct sockaddr_in6);
141*5f4c09ddSEd Maste break;
142*5f4c09ddSEd Maste default:
143*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "bad client passed socket family %u",
144*5f4c09ddSEd Maste (unsigned)bi->bi_ss.ss_family);
145*5f4c09ddSEd Maste return -1;
146*5f4c09ddSEd Maste }
147*5f4c09ddSEd Maste
148*5f4c09ddSEd Maste if (*rsl != bi->bi_slen) {
149*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "bad client passed socket length %u != %u",
150*5f4c09ddSEd Maste (unsigned)*rsl, (unsigned)bi->bi_slen);
151*5f4c09ddSEd Maste return -1;
152*5f4c09ddSEd Maste }
153*5f4c09ddSEd Maste
154*5f4c09ddSEd Maste memcpy(rss, &bi->bi_ss, *rsl);
155*5f4c09ddSEd Maste
156*5f4c09ddSEd Maste #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
157*5f4c09ddSEd Maste if (*rsl != rss->ss_len) {
158*5f4c09ddSEd Maste (*lfun)(LOG_ERR,
159*5f4c09ddSEd Maste "bad client passed socket internal length %u != %u",
160*5f4c09ddSEd Maste (unsigned)*rsl, (unsigned)rss->ss_len);
161*5f4c09ddSEd Maste return -1;
162*5f4c09ddSEd Maste }
163*5f4c09ddSEd Maste #endif
164*5f4c09ddSEd Maste return 0;
165*5f4c09ddSEd Maste }
166*5f4c09ddSEd Maste
167*5f4c09ddSEd Maste static void
process(bl_t bl)168*5f4c09ddSEd Maste process(bl_t bl)
169*5f4c09ddSEd Maste {
170*5f4c09ddSEd Maste struct sockaddr_storage rss;
171*5f4c09ddSEd Maste socklen_t rsl;
172*5f4c09ddSEd Maste char rbuf[BUFSIZ];
173*5f4c09ddSEd Maste bl_info_t *bi;
174*5f4c09ddSEd Maste struct conf c;
175*5f4c09ddSEd Maste struct dbinfo dbi;
176*5f4c09ddSEd Maste struct timespec ts;
177*5f4c09ddSEd Maste
178*5f4c09ddSEd Maste if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
179*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "clock_gettime failed (%m)");
180*5f4c09ddSEd Maste return;
181*5f4c09ddSEd Maste }
182*5f4c09ddSEd Maste
183*5f4c09ddSEd Maste if ((bi = bl_recv(bl)) == NULL) {
184*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "no message (%m)");
185*5f4c09ddSEd Maste return;
186*5f4c09ddSEd Maste }
187*5f4c09ddSEd Maste
188*5f4c09ddSEd Maste if (getremoteaddress(bi, &rss, &rsl) == -1)
189*5f4c09ddSEd Maste goto out;
190*5f4c09ddSEd Maste
191*5f4c09ddSEd Maste if (debug) {
192*5f4c09ddSEd Maste sockaddr_snprintf(rbuf, sizeof(rbuf), "%a:%p", (void *)&rss);
193*5f4c09ddSEd Maste (*lfun)(LOG_DEBUG, "processing type=%d fd=%d remote=%s msg=%s"
194*5f4c09ddSEd Maste " uid=%lu gid=%lu", bi->bi_type, bi->bi_fd, rbuf,
195*5f4c09ddSEd Maste bi->bi_msg, (unsigned long)bi->bi_uid,
196*5f4c09ddSEd Maste (unsigned long)bi->bi_gid);
197*5f4c09ddSEd Maste }
198*5f4c09ddSEd Maste
199*5f4c09ddSEd Maste if (conf_find(bi->bi_fd, bi->bi_uid, &rss, &c) == NULL) {
200*5f4c09ddSEd Maste (*lfun)(LOG_DEBUG, "no rule matched");
201*5f4c09ddSEd Maste goto out;
202*5f4c09ddSEd Maste }
203*5f4c09ddSEd Maste
204*5f4c09ddSEd Maste
205*5f4c09ddSEd Maste if (state_get(state, &c, &dbi) == -1)
206*5f4c09ddSEd Maste goto out;
207*5f4c09ddSEd Maste
208*5f4c09ddSEd Maste if (debug) {
209*5f4c09ddSEd Maste char b1[128], b2[128];
210*5f4c09ddSEd Maste (*lfun)(LOG_DEBUG, "%s: initial db state for %s: count=%d/%d "
211*5f4c09ddSEd Maste "last=%s now=%s", __func__, rbuf, dbi.count, c.c_nfail,
212*5f4c09ddSEd Maste fmttime(b1, sizeof(b1), dbi.last),
213*5f4c09ddSEd Maste fmttime(b2, sizeof(b2), ts.tv_sec));
214*5f4c09ddSEd Maste }
215*5f4c09ddSEd Maste
216*5f4c09ddSEd Maste switch (bi->bi_type) {
217*5f4c09ddSEd Maste case BL_ABUSE:
218*5f4c09ddSEd Maste /*
219*5f4c09ddSEd Maste * If the application has signaled abusive behavior,
220*5f4c09ddSEd Maste * set the number of fails to be one less than the
221*5f4c09ddSEd Maste * configured limit. Fallthrough to the normal BL_ADD
222*5f4c09ddSEd Maste * processing, which will increment the failure count
223*5f4c09ddSEd Maste * to the threshhold, and block the abusive address.
224*5f4c09ddSEd Maste */
225*5f4c09ddSEd Maste if (c.c_nfail != -1)
226*5f4c09ddSEd Maste dbi.count = c.c_nfail - 1;
227*5f4c09ddSEd Maste /*FALLTHROUGH*/
228*5f4c09ddSEd Maste case BL_ADD:
229*5f4c09ddSEd Maste dbi.count++;
230*5f4c09ddSEd Maste dbi.last = ts.tv_sec;
231*5f4c09ddSEd Maste if (c.c_nfail != -1 && dbi.count >= c.c_nfail) {
232*5f4c09ddSEd Maste /*
233*5f4c09ddSEd Maste * No point in re-adding the rule.
234*5f4c09ddSEd Maste * It might exist already due to latency in processing
235*5f4c09ddSEd Maste * and removing the rule is the wrong thing to do as
236*5f4c09ddSEd Maste * it allows a window to attack again.
237*5f4c09ddSEd Maste */
238*5f4c09ddSEd Maste if (dbi.id[0] == '\0') {
239*5f4c09ddSEd Maste int res = run_change("add", &c,
240*5f4c09ddSEd Maste dbi.id, sizeof(dbi.id));
241*5f4c09ddSEd Maste if (res == -1)
242*5f4c09ddSEd Maste goto out;
243*5f4c09ddSEd Maste }
244*5f4c09ddSEd Maste sockaddr_snprintf(rbuf, sizeof(rbuf), "%a",
245*5f4c09ddSEd Maste (void *)&rss);
246*5f4c09ddSEd Maste (*lfun)(LOG_INFO,
247*5f4c09ddSEd Maste "blocked %s/%d:%d for %d seconds",
248*5f4c09ddSEd Maste rbuf, c.c_lmask, c.c_port, c.c_duration);
249*5f4c09ddSEd Maste }
250*5f4c09ddSEd Maste break;
251*5f4c09ddSEd Maste case BL_DELETE:
252*5f4c09ddSEd Maste if (dbi.last == 0)
253*5f4c09ddSEd Maste goto out;
254*5f4c09ddSEd Maste dbi.count = 0;
255*5f4c09ddSEd Maste dbi.last = 0;
256*5f4c09ddSEd Maste break;
257*5f4c09ddSEd Maste case BL_BADUSER:
258*5f4c09ddSEd Maste /* ignore for now */
259*5f4c09ddSEd Maste break;
260*5f4c09ddSEd Maste default:
261*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "unknown message %d", bi->bi_type);
262*5f4c09ddSEd Maste }
263*5f4c09ddSEd Maste state_put(state, &c, &dbi);
264*5f4c09ddSEd Maste
265*5f4c09ddSEd Maste out:
266*5f4c09ddSEd Maste close(bi->bi_fd);
267*5f4c09ddSEd Maste
268*5f4c09ddSEd Maste if (debug) {
269*5f4c09ddSEd Maste char b1[128], b2[128];
270*5f4c09ddSEd Maste (*lfun)(LOG_DEBUG, "%s: final db state for %s: count=%d/%d "
271*5f4c09ddSEd Maste "last=%s now=%s", __func__, rbuf, dbi.count, c.c_nfail,
272*5f4c09ddSEd Maste fmttime(b1, sizeof(b1), dbi.last),
273*5f4c09ddSEd Maste fmttime(b2, sizeof(b2), ts.tv_sec));
274*5f4c09ddSEd Maste }
275*5f4c09ddSEd Maste }
276*5f4c09ddSEd Maste
277*5f4c09ddSEd Maste static void
update_interfaces(void)278*5f4c09ddSEd Maste update_interfaces(void)
279*5f4c09ddSEd Maste {
280*5f4c09ddSEd Maste struct ifaddrs *oifas, *nifas;
281*5f4c09ddSEd Maste
282*5f4c09ddSEd Maste if (getifaddrs(&nifas) == -1)
283*5f4c09ddSEd Maste return;
284*5f4c09ddSEd Maste
285*5f4c09ddSEd Maste oifas = ifas;
286*5f4c09ddSEd Maste ifas = nifas;
287*5f4c09ddSEd Maste
288*5f4c09ddSEd Maste if (oifas)
289*5f4c09ddSEd Maste freeifaddrs(oifas);
290*5f4c09ddSEd Maste }
291*5f4c09ddSEd Maste
292*5f4c09ddSEd Maste static void
update(void)293*5f4c09ddSEd Maste update(void)
294*5f4c09ddSEd Maste {
295*5f4c09ddSEd Maste struct timespec ts;
296*5f4c09ddSEd Maste struct conf c;
297*5f4c09ddSEd Maste struct dbinfo dbi;
298*5f4c09ddSEd Maste unsigned int f, n;
299*5f4c09ddSEd Maste char buf[128];
300*5f4c09ddSEd Maste void *ss = &c.c_ss;
301*5f4c09ddSEd Maste
302*5f4c09ddSEd Maste if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
303*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "clock_gettime failed (%m)");
304*5f4c09ddSEd Maste return;
305*5f4c09ddSEd Maste }
306*5f4c09ddSEd Maste
307*5f4c09ddSEd Maste again:
308*5f4c09ddSEd Maste for (n = 0, f = 1; state_iterate(state, &c, &dbi, f) == 1;
309*5f4c09ddSEd Maste f = 0, n++)
310*5f4c09ddSEd Maste {
311*5f4c09ddSEd Maste time_t when = c.c_duration + dbi.last;
312*5f4c09ddSEd Maste if (debug > 1) {
313*5f4c09ddSEd Maste char b1[64], b2[64];
314*5f4c09ddSEd Maste sockaddr_snprintf(buf, sizeof(buf), "%a:%p", ss);
315*5f4c09ddSEd Maste (*lfun)(LOG_DEBUG, "%s:[%u] %s count=%d duration=%d "
316*5f4c09ddSEd Maste "last=%s " "now=%s", __func__, n, buf, dbi.count,
317*5f4c09ddSEd Maste c.c_duration, fmttime(b1, sizeof(b1), dbi.last),
318*5f4c09ddSEd Maste fmttime(b2, sizeof(b2), ts.tv_sec));
319*5f4c09ddSEd Maste }
320*5f4c09ddSEd Maste if (c.c_duration == -1 || when >= ts.tv_sec)
321*5f4c09ddSEd Maste continue;
322*5f4c09ddSEd Maste if (dbi.id[0]) {
323*5f4c09ddSEd Maste run_change("rem", &c, dbi.id, 0);
324*5f4c09ddSEd Maste sockaddr_snprintf(buf, sizeof(buf), "%a", ss);
325*5f4c09ddSEd Maste (*lfun)(LOG_INFO, "released %s/%d:%d after %d seconds",
326*5f4c09ddSEd Maste buf, c.c_lmask, c.c_port, c.c_duration);
327*5f4c09ddSEd Maste }
328*5f4c09ddSEd Maste state_del(state, &c);
329*5f4c09ddSEd Maste goto again;
330*5f4c09ddSEd Maste }
331*5f4c09ddSEd Maste }
332*5f4c09ddSEd Maste
333*5f4c09ddSEd Maste static void
addfd(struct pollfd ** pfdp,bl_t ** blp,size_t * nfd,size_t * maxfd,const char * path)334*5f4c09ddSEd Maste addfd(struct pollfd **pfdp, bl_t **blp, size_t *nfd, size_t *maxfd,
335*5f4c09ddSEd Maste const char *path)
336*5f4c09ddSEd Maste {
337*5f4c09ddSEd Maste bl_t bl = bl_create(true, path, vflag ? vdlog : vsyslog);
338*5f4c09ddSEd Maste if (bl == NULL || !bl_isconnected(bl))
339*5f4c09ddSEd Maste exit(EXIT_FAILURE);
340*5f4c09ddSEd Maste if (*nfd >= *maxfd) {
341*5f4c09ddSEd Maste *maxfd += 10;
342*5f4c09ddSEd Maste *blp = realloc(*blp, sizeof(**blp) * *maxfd);
343*5f4c09ddSEd Maste if (*blp == NULL)
344*5f4c09ddSEd Maste err(EXIT_FAILURE, "malloc");
345*5f4c09ddSEd Maste *pfdp = realloc(*pfdp, sizeof(**pfdp) * *maxfd);
346*5f4c09ddSEd Maste if (*pfdp == NULL)
347*5f4c09ddSEd Maste err(EXIT_FAILURE, "malloc");
348*5f4c09ddSEd Maste }
349*5f4c09ddSEd Maste
350*5f4c09ddSEd Maste (*pfdp)[*nfd].fd = bl_getfd(bl);
351*5f4c09ddSEd Maste (*pfdp)[*nfd].events = POLLIN;
352*5f4c09ddSEd Maste (*blp)[*nfd] = bl;
353*5f4c09ddSEd Maste *nfd += 1;
354*5f4c09ddSEd Maste }
355*5f4c09ddSEd Maste
356*5f4c09ddSEd Maste static void
uniqueadd(struct conf *** listp,size_t * nlist,size_t * mlist,struct conf * c)357*5f4c09ddSEd Maste uniqueadd(struct conf ***listp, size_t *nlist, size_t *mlist, struct conf *c)
358*5f4c09ddSEd Maste {
359*5f4c09ddSEd Maste struct conf **list = *listp;
360*5f4c09ddSEd Maste
361*5f4c09ddSEd Maste if (c->c_name[0] == '\0')
362*5f4c09ddSEd Maste return;
363*5f4c09ddSEd Maste for (size_t i = 0; i < *nlist; i++) {
364*5f4c09ddSEd Maste if (strcmp(list[i]->c_name, c->c_name) == 0)
365*5f4c09ddSEd Maste return;
366*5f4c09ddSEd Maste }
367*5f4c09ddSEd Maste if (*nlist == *mlist) {
368*5f4c09ddSEd Maste *mlist += 10;
369*5f4c09ddSEd Maste void *p = realloc(*listp, *mlist * sizeof(*list));
370*5f4c09ddSEd Maste if (p == NULL)
371*5f4c09ddSEd Maste err(EXIT_FAILURE, "Can't allocate for rule list");
372*5f4c09ddSEd Maste list = *listp = p;
373*5f4c09ddSEd Maste }
374*5f4c09ddSEd Maste list[(*nlist)++] = c;
375*5f4c09ddSEd Maste }
376*5f4c09ddSEd Maste
377*5f4c09ddSEd Maste static void
rules_flush(void)378*5f4c09ddSEd Maste rules_flush(void)
379*5f4c09ddSEd Maste {
380*5f4c09ddSEd Maste struct conf **list;
381*5f4c09ddSEd Maste size_t nlist, mlist;
382*5f4c09ddSEd Maste
383*5f4c09ddSEd Maste list = NULL;
384*5f4c09ddSEd Maste mlist = nlist = 0;
385*5f4c09ddSEd Maste for (size_t i = 0; i < rconf.cs_n; i++)
386*5f4c09ddSEd Maste uniqueadd(&list, &nlist, &mlist, &rconf.cs_c[i]);
387*5f4c09ddSEd Maste for (size_t i = 0; i < lconf.cs_n; i++)
388*5f4c09ddSEd Maste uniqueadd(&list, &nlist, &mlist, &lconf.cs_c[i]);
389*5f4c09ddSEd Maste
390*5f4c09ddSEd Maste for (size_t i = 0; i < nlist; i++)
391*5f4c09ddSEd Maste run_flush(list[i]);
392*5f4c09ddSEd Maste free(list);
393*5f4c09ddSEd Maste }
394*5f4c09ddSEd Maste
395*5f4c09ddSEd Maste static void
rules_restore(void)396*5f4c09ddSEd Maste rules_restore(void)
397*5f4c09ddSEd Maste {
398*5f4c09ddSEd Maste struct conf c;
399*5f4c09ddSEd Maste struct dbinfo dbi;
400*5f4c09ddSEd Maste unsigned int f;
401*5f4c09ddSEd Maste
402*5f4c09ddSEd Maste for (f = 1; state_iterate(state, &c, &dbi, f) == 1; f = 0) {
403*5f4c09ddSEd Maste if (dbi.id[0] == '\0')
404*5f4c09ddSEd Maste continue;
405*5f4c09ddSEd Maste (void)run_change("add", &c, dbi.id, sizeof(dbi.id));
406*5f4c09ddSEd Maste }
407*5f4c09ddSEd Maste }
408*5f4c09ddSEd Maste
409*5f4c09ddSEd Maste int
main(int argc,char * argv[])410*5f4c09ddSEd Maste main(int argc, char *argv[])
411*5f4c09ddSEd Maste {
412*5f4c09ddSEd Maste int c, tout, flags, flush, restore, ret;
413*5f4c09ddSEd Maste const char *spath, **blsock;
414*5f4c09ddSEd Maste size_t nblsock, maxblsock;
415*5f4c09ddSEd Maste
416*5f4c09ddSEd Maste setprogname(argv[0]);
417*5f4c09ddSEd Maste
418*5f4c09ddSEd Maste spath = NULL;
419*5f4c09ddSEd Maste blsock = NULL;
420*5f4c09ddSEd Maste maxblsock = nblsock = 0;
421*5f4c09ddSEd Maste flush = 0;
422*5f4c09ddSEd Maste restore = 0;
423*5f4c09ddSEd Maste tout = 0;
424*5f4c09ddSEd Maste flags = O_RDWR|O_EXCL|O_CLOEXEC;
425*5f4c09ddSEd Maste while ((c = getopt(argc, argv, "C:c:D:dfP:rR:s:t:v")) != -1) {
426*5f4c09ddSEd Maste switch (c) {
427*5f4c09ddSEd Maste case 'C':
428*5f4c09ddSEd Maste controlprog = optarg;
429*5f4c09ddSEd Maste break;
430*5f4c09ddSEd Maste case 'c':
431*5f4c09ddSEd Maste configfile = optarg;
432*5f4c09ddSEd Maste break;
433*5f4c09ddSEd Maste case 'D':
434*5f4c09ddSEd Maste dbfile = optarg;
435*5f4c09ddSEd Maste break;
436*5f4c09ddSEd Maste case 'd':
437*5f4c09ddSEd Maste debug++;
438*5f4c09ddSEd Maste break;
439*5f4c09ddSEd Maste case 'f':
440*5f4c09ddSEd Maste flush++;
441*5f4c09ddSEd Maste break;
442*5f4c09ddSEd Maste case 'P':
443*5f4c09ddSEd Maste spath = optarg;
444*5f4c09ddSEd Maste break;
445*5f4c09ddSEd Maste case 'R':
446*5f4c09ddSEd Maste rulename = optarg;
447*5f4c09ddSEd Maste break;
448*5f4c09ddSEd Maste case 'r':
449*5f4c09ddSEd Maste restore++;
450*5f4c09ddSEd Maste break;
451*5f4c09ddSEd Maste case 's':
452*5f4c09ddSEd Maste if (nblsock >= maxblsock) {
453*5f4c09ddSEd Maste maxblsock += 10;
454*5f4c09ddSEd Maste void *p = realloc(blsock,
455*5f4c09ddSEd Maste sizeof(*blsock) * maxblsock);
456*5f4c09ddSEd Maste if (p == NULL)
457*5f4c09ddSEd Maste err(EXIT_FAILURE,
458*5f4c09ddSEd Maste "Can't allocate memory for %zu sockets",
459*5f4c09ddSEd Maste maxblsock);
460*5f4c09ddSEd Maste blsock = p;
461*5f4c09ddSEd Maste }
462*5f4c09ddSEd Maste blsock[nblsock++] = optarg;
463*5f4c09ddSEd Maste break;
464*5f4c09ddSEd Maste case 't':
465*5f4c09ddSEd Maste tout = atoi(optarg) * 1000;
466*5f4c09ddSEd Maste break;
467*5f4c09ddSEd Maste case 'v':
468*5f4c09ddSEd Maste vflag++;
469*5f4c09ddSEd Maste break;
470*5f4c09ddSEd Maste default:
471*5f4c09ddSEd Maste usage(c);
472*5f4c09ddSEd Maste }
473*5f4c09ddSEd Maste }
474*5f4c09ddSEd Maste
475*5f4c09ddSEd Maste argc -= optind;
476*5f4c09ddSEd Maste if (argc)
477*5f4c09ddSEd Maste usage('?');
478*5f4c09ddSEd Maste
479*5f4c09ddSEd Maste signal(SIGHUP, sighup);
480*5f4c09ddSEd Maste signal(SIGINT, sigdone);
481*5f4c09ddSEd Maste signal(SIGQUIT, sigdone);
482*5f4c09ddSEd Maste signal(SIGTERM, sigdone);
483*5f4c09ddSEd Maste signal(SIGUSR1, sigusr1);
484*5f4c09ddSEd Maste signal(SIGUSR2, sigusr2);
485*5f4c09ddSEd Maste
486*5f4c09ddSEd Maste openlog(getprogname(), LOG_PID, LOG_DAEMON);
487*5f4c09ddSEd Maste
488*5f4c09ddSEd Maste if (debug) {
489*5f4c09ddSEd Maste lfun = dlog;
490*5f4c09ddSEd Maste if (tout == 0)
491*5f4c09ddSEd Maste tout = 5000;
492*5f4c09ddSEd Maste } else {
493*5f4c09ddSEd Maste if (tout == 0)
494*5f4c09ddSEd Maste tout = 15000;
495*5f4c09ddSEd Maste }
496*5f4c09ddSEd Maste
497*5f4c09ddSEd Maste update_interfaces();
498*5f4c09ddSEd Maste conf_parse(configfile);
499*5f4c09ddSEd Maste if (flush) {
500*5f4c09ddSEd Maste rules_flush();
501*5f4c09ddSEd Maste if (!restore)
502*5f4c09ddSEd Maste flags |= O_TRUNC;
503*5f4c09ddSEd Maste }
504*5f4c09ddSEd Maste
505*5f4c09ddSEd Maste struct pollfd *pfd = NULL;
506*5f4c09ddSEd Maste bl_t *bl = NULL;
507*5f4c09ddSEd Maste size_t nfd = 0;
508*5f4c09ddSEd Maste size_t maxfd = 0;
509*5f4c09ddSEd Maste
510*5f4c09ddSEd Maste for (size_t i = 0; i < nblsock; i++)
511*5f4c09ddSEd Maste addfd(&pfd, &bl, &nfd, &maxfd, blsock[i]);
512*5f4c09ddSEd Maste free(blsock);
513*5f4c09ddSEd Maste
514*5f4c09ddSEd Maste if (spath) {
515*5f4c09ddSEd Maste FILE *fp = fopen(spath, "r");
516*5f4c09ddSEd Maste char *line;
517*5f4c09ddSEd Maste if (fp == NULL)
518*5f4c09ddSEd Maste err(EXIT_FAILURE, "Can't open `%s'", spath);
519*5f4c09ddSEd Maste for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL;
520*5f4c09ddSEd Maste free(line))
521*5f4c09ddSEd Maste addfd(&pfd, &bl, &nfd, &maxfd, line);
522*5f4c09ddSEd Maste fclose(fp);
523*5f4c09ddSEd Maste }
524*5f4c09ddSEd Maste if (nfd == 0)
525*5f4c09ddSEd Maste addfd(&pfd, &bl, &nfd, &maxfd, _PATH_BLSOCK);
526*5f4c09ddSEd Maste
527*5f4c09ddSEd Maste state = state_open(dbfile, flags, 0600);
528*5f4c09ddSEd Maste if (state == NULL)
529*5f4c09ddSEd Maste state = state_open(dbfile, flags | O_CREAT, 0600);
530*5f4c09ddSEd Maste if (state == NULL)
531*5f4c09ddSEd Maste return EXIT_FAILURE;
532*5f4c09ddSEd Maste
533*5f4c09ddSEd Maste if (restore) {
534*5f4c09ddSEd Maste if (!flush)
535*5f4c09ddSEd Maste rules_flush();
536*5f4c09ddSEd Maste rules_restore();
537*5f4c09ddSEd Maste }
538*5f4c09ddSEd Maste
539*5f4c09ddSEd Maste if (!debug) {
540*5f4c09ddSEd Maste if (daemon(0, 0) == -1)
541*5f4c09ddSEd Maste err(EXIT_FAILURE, "daemon failed");
542*5f4c09ddSEd Maste if (pidfile(NULL) == -1)
543*5f4c09ddSEd Maste err(EXIT_FAILURE, "Can't create pidfile");
544*5f4c09ddSEd Maste }
545*5f4c09ddSEd Maste
546*5f4c09ddSEd Maste for (size_t t = 0; !done; t++) {
547*5f4c09ddSEd Maste if (readconf) {
548*5f4c09ddSEd Maste readconf = 0;
549*5f4c09ddSEd Maste conf_parse(configfile);
550*5f4c09ddSEd Maste }
551*5f4c09ddSEd Maste ret = poll(pfd, (nfds_t)nfd, tout);
552*5f4c09ddSEd Maste if (debug)
553*5f4c09ddSEd Maste (*lfun)(LOG_DEBUG, "received %d from poll()", ret);
554*5f4c09ddSEd Maste switch (ret) {
555*5f4c09ddSEd Maste case -1:
556*5f4c09ddSEd Maste if (errno == EINTR)
557*5f4c09ddSEd Maste continue;
558*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "poll (%m)");
559*5f4c09ddSEd Maste return EXIT_FAILURE;
560*5f4c09ddSEd Maste case 0:
561*5f4c09ddSEd Maste state_sync(state);
562*5f4c09ddSEd Maste break;
563*5f4c09ddSEd Maste default:
564*5f4c09ddSEd Maste for (size_t i = 0; i < nfd; i++)
565*5f4c09ddSEd Maste if (pfd[i].revents & POLLIN)
566*5f4c09ddSEd Maste process(bl[i]);
567*5f4c09ddSEd Maste }
568*5f4c09ddSEd Maste if (t % 100 == 0)
569*5f4c09ddSEd Maste state_sync(state);
570*5f4c09ddSEd Maste if (t % 10000 == 0)
571*5f4c09ddSEd Maste update_interfaces();
572*5f4c09ddSEd Maste update();
573*5f4c09ddSEd Maste }
574*5f4c09ddSEd Maste state_close(state);
575*5f4c09ddSEd Maste return 0;
576*5f4c09ddSEd Maste }
577