1*5f4c09ddSEd Maste /* $NetBSD: blacklistctl.c,v 1.23 2018/05/24 19:21:01 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
35*5f4c09ddSEd Maste #include <sys/cdefs.h>
36*5f4c09ddSEd Maste __RCSID("$NetBSD: blacklistctl.c,v 1.23 2018/05/24 19:21:01 christos Exp $");
37*5f4c09ddSEd Maste
38*5f4c09ddSEd Maste #include <stdio.h>
39*5f4c09ddSEd Maste #include <time.h>
40*5f4c09ddSEd Maste #ifdef HAVE_LIBUTIL_H
41*5f4c09ddSEd Maste #include <libutil.h>
42*5f4c09ddSEd Maste #endif
43*5f4c09ddSEd Maste #ifdef HAVE_UTIL_H
44*5f4c09ddSEd Maste #include <util.h>
45*5f4c09ddSEd Maste #endif
46*5f4c09ddSEd Maste #include <fcntl.h>
47*5f4c09ddSEd Maste #include <string.h>
48*5f4c09ddSEd Maste #include <syslog.h>
49*5f4c09ddSEd Maste #include <err.h>
50*5f4c09ddSEd Maste #include <stdlib.h>
51*5f4c09ddSEd Maste #include <unistd.h>
52*5f4c09ddSEd Maste #include <sys/socket.h>
53*5f4c09ddSEd Maste
54*5f4c09ddSEd Maste #include "conf.h"
55*5f4c09ddSEd Maste #include "state.h"
56*5f4c09ddSEd Maste #include "internal.h"
57*5f4c09ddSEd Maste #include "support.h"
58*5f4c09ddSEd Maste
59*5f4c09ddSEd Maste static __dead void
usage(int c)60*5f4c09ddSEd Maste usage(int c)
61*5f4c09ddSEd Maste {
62*5f4c09ddSEd Maste if (c == 0)
63*5f4c09ddSEd Maste warnx("Missing/unknown command");
64*5f4c09ddSEd Maste else if (c != '?')
65*5f4c09ddSEd Maste warnx("Unknown option `%c'", (char)c);
66*5f4c09ddSEd Maste fprintf(stderr, "Usage: %s dump [-abdnrw]\n", getprogname());
67*5f4c09ddSEd Maste exit(EXIT_FAILURE);
68*5f4c09ddSEd Maste }
69*5f4c09ddSEd Maste
70*5f4c09ddSEd Maste static const char *
star(char * buf,size_t len,int val)71*5f4c09ddSEd Maste star(char *buf, size_t len, int val)
72*5f4c09ddSEd Maste {
73*5f4c09ddSEd Maste if (val == -1)
74*5f4c09ddSEd Maste return "*";
75*5f4c09ddSEd Maste snprintf(buf, len, "%d", val);
76*5f4c09ddSEd Maste return buf;
77*5f4c09ddSEd Maste }
78*5f4c09ddSEd Maste
79*5f4c09ddSEd Maste int
main(int argc,char * argv[])80*5f4c09ddSEd Maste main(int argc, char *argv[])
81*5f4c09ddSEd Maste {
82*5f4c09ddSEd Maste const char *dbname = _PATH_BLSTATE;
83*5f4c09ddSEd Maste DB *db;
84*5f4c09ddSEd Maste struct conf c;
85*5f4c09ddSEd Maste struct dbinfo dbi;
86*5f4c09ddSEd Maste unsigned int i;
87*5f4c09ddSEd Maste struct timespec ts;
88*5f4c09ddSEd Maste int all, blocked, remain, wide, noheader;
89*5f4c09ddSEd Maste int o;
90*5f4c09ddSEd Maste
91*5f4c09ddSEd Maste noheader = wide = blocked = all = remain = 0;
92*5f4c09ddSEd Maste lfun = dlog;
93*5f4c09ddSEd Maste
94*5f4c09ddSEd Maste if (argc == 1 || strcmp(argv[1], "dump") != 0)
95*5f4c09ddSEd Maste usage(0);
96*5f4c09ddSEd Maste
97*5f4c09ddSEd Maste argc--;
98*5f4c09ddSEd Maste argv++;
99*5f4c09ddSEd Maste
100*5f4c09ddSEd Maste while ((o = getopt(argc, argv, "abD:dnrw")) != -1)
101*5f4c09ddSEd Maste switch (o) {
102*5f4c09ddSEd Maste case 'a':
103*5f4c09ddSEd Maste all = 1;
104*5f4c09ddSEd Maste blocked = 0;
105*5f4c09ddSEd Maste break;
106*5f4c09ddSEd Maste case 'b':
107*5f4c09ddSEd Maste blocked = 1;
108*5f4c09ddSEd Maste break;
109*5f4c09ddSEd Maste case 'D':
110*5f4c09ddSEd Maste dbname = optarg;
111*5f4c09ddSEd Maste break;
112*5f4c09ddSEd Maste case 'd':
113*5f4c09ddSEd Maste debug++;
114*5f4c09ddSEd Maste break;
115*5f4c09ddSEd Maste case 'n':
116*5f4c09ddSEd Maste noheader = 1;
117*5f4c09ddSEd Maste break;
118*5f4c09ddSEd Maste case 'r':
119*5f4c09ddSEd Maste remain = 1;
120*5f4c09ddSEd Maste break;
121*5f4c09ddSEd Maste case 'w':
122*5f4c09ddSEd Maste wide = 1;
123*5f4c09ddSEd Maste break;
124*5f4c09ddSEd Maste default:
125*5f4c09ddSEd Maste usage(o);
126*5f4c09ddSEd Maste }
127*5f4c09ddSEd Maste
128*5f4c09ddSEd Maste db = state_open(dbname, O_RDONLY, 0);
129*5f4c09ddSEd Maste if (db == NULL)
130*5f4c09ddSEd Maste err(EXIT_FAILURE, "Can't open `%s'", dbname);
131*5f4c09ddSEd Maste
132*5f4c09ddSEd Maste clock_gettime(CLOCK_REALTIME, &ts);
133*5f4c09ddSEd Maste wide = wide ? 8 * 4 + 7 : 4 * 3 + 3;
134*5f4c09ddSEd Maste if (!noheader)
135*5f4c09ddSEd Maste printf("%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide,
136*5f4c09ddSEd Maste "address", remain ? "remaining time" : "last access");
137*5f4c09ddSEd Maste for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) {
138*5f4c09ddSEd Maste char buf[BUFSIZ];
139*5f4c09ddSEd Maste char mbuf[64], pbuf[64];
140*5f4c09ddSEd Maste if (!all) {
141*5f4c09ddSEd Maste if (blocked) {
142*5f4c09ddSEd Maste if (c.c_nfail == -1 || dbi.count < c.c_nfail)
143*5f4c09ddSEd Maste continue;
144*5f4c09ddSEd Maste } else {
145*5f4c09ddSEd Maste if (dbi.count >= c.c_nfail)
146*5f4c09ddSEd Maste continue;
147*5f4c09ddSEd Maste }
148*5f4c09ddSEd Maste }
149*5f4c09ddSEd Maste sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss);
150*5f4c09ddSEd Maste printf("%*.*s/%s:%s\t", wide, wide, buf,
151*5f4c09ddSEd Maste star(mbuf, sizeof(mbuf), c.c_lmask),
152*5f4c09ddSEd Maste star(pbuf, sizeof(pbuf), c.c_port));
153*5f4c09ddSEd Maste if (c.c_duration == -1) {
154*5f4c09ddSEd Maste strlcpy(buf, "never", sizeof(buf));
155*5f4c09ddSEd Maste } else {
156*5f4c09ddSEd Maste if (remain)
157*5f4c09ddSEd Maste fmtydhms(buf, sizeof(buf),
158*5f4c09ddSEd Maste c.c_duration - (ts.tv_sec - dbi.last));
159*5f4c09ddSEd Maste else
160*5f4c09ddSEd Maste fmttime(buf, sizeof(buf), dbi.last);
161*5f4c09ddSEd Maste }
162*5f4c09ddSEd Maste printf("%s\t%d/%s\t%-s\n", dbi.id, dbi.count,
163*5f4c09ddSEd Maste star(mbuf, sizeof(mbuf), c.c_nfail), buf);
164*5f4c09ddSEd Maste }
165*5f4c09ddSEd Maste state_close(db);
166*5f4c09ddSEd Maste return EXIT_SUCCESS;
167*5f4c09ddSEd Maste }
168