xref: /freebsd/contrib/blocklist/bin/blocklistctl.c (revision 48e64ca13d4f36795ac718911b805e3e9a726f1b)
1*48e64ca1SJose Luis Duran /*	$NetBSD: blocklistctl.c,v 1.4 2025/02/11 17:48:30 christos Exp $	*/
2*48e64ca1SJose Luis Duran 
3*48e64ca1SJose Luis Duran /*-
4*48e64ca1SJose Luis Duran  * Copyright (c) 2015 The NetBSD Foundation, Inc.
5*48e64ca1SJose Luis Duran  * All rights reserved.
6*48e64ca1SJose Luis Duran  *
7*48e64ca1SJose Luis Duran  * This code is derived from software contributed to The NetBSD Foundation
8*48e64ca1SJose Luis Duran  * by Christos Zoulas.
9*48e64ca1SJose Luis Duran  *
10*48e64ca1SJose Luis Duran  * Redistribution and use in source and binary forms, with or without
11*48e64ca1SJose Luis Duran  * modification, are permitted provided that the following conditions
12*48e64ca1SJose Luis Duran  * are met:
13*48e64ca1SJose Luis Duran  * 1. Redistributions of source code must retain the above copyright
14*48e64ca1SJose Luis Duran  *    notice, this list of conditions and the following disclaimer.
15*48e64ca1SJose Luis Duran  * 2. Redistributions in binary form must reproduce the above copyright
16*48e64ca1SJose Luis Duran  *    notice, this list of conditions and the following disclaimer in the
17*48e64ca1SJose Luis Duran  *    documentation and/or other materials provided with the distribution.
18*48e64ca1SJose Luis Duran  *
19*48e64ca1SJose Luis Duran  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*48e64ca1SJose Luis Duran  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*48e64ca1SJose Luis Duran  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*48e64ca1SJose Luis Duran  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*48e64ca1SJose Luis Duran  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*48e64ca1SJose Luis Duran  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*48e64ca1SJose Luis Duran  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*48e64ca1SJose Luis Duran  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*48e64ca1SJose Luis Duran  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*48e64ca1SJose Luis Duran  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*48e64ca1SJose Luis Duran  * POSSIBILITY OF SUCH DAMAGE.
30*48e64ca1SJose Luis Duran  */
31*48e64ca1SJose Luis Duran #ifdef HAVE_CONFIG_H
32*48e64ca1SJose Luis Duran #include "config.h"
33*48e64ca1SJose Luis Duran #endif
34*48e64ca1SJose Luis Duran 
35*48e64ca1SJose Luis Duran #ifdef HAVE_SYS_CDEFS_H
36*48e64ca1SJose Luis Duran #include <sys/cdefs.h>
37*48e64ca1SJose Luis Duran #endif
38*48e64ca1SJose Luis Duran __RCSID("$NetBSD: blocklistctl.c,v 1.4 2025/02/11 17:48:30 christos Exp $");
39*48e64ca1SJose Luis Duran 
40*48e64ca1SJose Luis Duran #include <stdio.h>
41*48e64ca1SJose Luis Duran #include <time.h>
42*48e64ca1SJose Luis Duran #ifdef HAVE_LIBUTIL_H
43*48e64ca1SJose Luis Duran #include <libutil.h>
44*48e64ca1SJose Luis Duran #endif
45*48e64ca1SJose Luis Duran #ifdef HAVE_UTIL_H
46*48e64ca1SJose Luis Duran #include <util.h>
47*48e64ca1SJose Luis Duran #endif
48*48e64ca1SJose Luis Duran #include <fcntl.h>
49*48e64ca1SJose Luis Duran #include <string.h>
50*48e64ca1SJose Luis Duran #include <syslog.h>
51*48e64ca1SJose Luis Duran #include <err.h>
52*48e64ca1SJose Luis Duran #include <stdlib.h>
53*48e64ca1SJose Luis Duran #include <unistd.h>
54*48e64ca1SJose Luis Duran #include <sys/socket.h>
55*48e64ca1SJose Luis Duran 
56*48e64ca1SJose Luis Duran #include "conf.h"
57*48e64ca1SJose Luis Duran #include "state.h"
58*48e64ca1SJose Luis Duran #include "internal.h"
59*48e64ca1SJose Luis Duran #include "support.h"
60*48e64ca1SJose Luis Duran 
61*48e64ca1SJose Luis Duran static __dead void
usage(int c)62*48e64ca1SJose Luis Duran usage(int c)
63*48e64ca1SJose Luis Duran {
64*48e64ca1SJose Luis Duran 	if (c == 0)
65*48e64ca1SJose Luis Duran 		warnx("Missing/unknown command");
66*48e64ca1SJose Luis Duran 	else if (c != '?')
67*48e64ca1SJose Luis Duran 		warnx("Unknown option `%c'", (char)c);
68*48e64ca1SJose Luis Duran 	fprintf(stderr,
69*48e64ca1SJose Luis Duran 	    "Usage: %s dump [-abdnrw] [-D dbname]\n", getprogname());
70*48e64ca1SJose Luis Duran 	exit(EXIT_FAILURE);
71*48e64ca1SJose Luis Duran }
72*48e64ca1SJose Luis Duran 
73*48e64ca1SJose Luis Duran static const char *
star(char * buf,size_t len,int val)74*48e64ca1SJose Luis Duran star(char *buf, size_t len, int val)
75*48e64ca1SJose Luis Duran {
76*48e64ca1SJose Luis Duran 	if (val == -1)
77*48e64ca1SJose Luis Duran 		return "*";
78*48e64ca1SJose Luis Duran 	snprintf(buf, len, "%d", val);
79*48e64ca1SJose Luis Duran 	return buf;
80*48e64ca1SJose Luis Duran }
81*48e64ca1SJose Luis Duran 
82*48e64ca1SJose Luis Duran int
main(int argc,char * argv[])83*48e64ca1SJose Luis Duran main(int argc, char *argv[])
84*48e64ca1SJose Luis Duran {
85*48e64ca1SJose Luis Duran 	const char *dbname = _PATH_BLSTATE;
86*48e64ca1SJose Luis Duran 	DB *db;
87*48e64ca1SJose Luis Duran 	struct conf c;
88*48e64ca1SJose Luis Duran 	struct dbinfo dbi;
89*48e64ca1SJose Luis Duran 	unsigned int i;
90*48e64ca1SJose Luis Duran 	struct timespec ts;
91*48e64ca1SJose Luis Duran 	int all, blocked, remain, wide, noheader;
92*48e64ca1SJose Luis Duran 	int o;
93*48e64ca1SJose Luis Duran 
94*48e64ca1SJose Luis Duran 	noheader = wide = blocked = all = remain = 0;
95*48e64ca1SJose Luis Duran 	lfun = dlog;
96*48e64ca1SJose Luis Duran 
97*48e64ca1SJose Luis Duran 	if (argc == 1 || strcmp(argv[1], "dump") != 0)
98*48e64ca1SJose Luis Duran 		usage(0);
99*48e64ca1SJose Luis Duran 
100*48e64ca1SJose Luis Duran 	argc--;
101*48e64ca1SJose Luis Duran 	argv++;
102*48e64ca1SJose Luis Duran 
103*48e64ca1SJose Luis Duran 	while ((o = getopt(argc, argv, "abD:dnrw")) != -1)
104*48e64ca1SJose Luis Duran 		switch (o) {
105*48e64ca1SJose Luis Duran 		case 'a':
106*48e64ca1SJose Luis Duran 			all = 1;
107*48e64ca1SJose Luis Duran 			blocked = 0;
108*48e64ca1SJose Luis Duran 			break;
109*48e64ca1SJose Luis Duran 		case 'b':
110*48e64ca1SJose Luis Duran 			blocked = 1;
111*48e64ca1SJose Luis Duran 			break;
112*48e64ca1SJose Luis Duran 		case 'D':
113*48e64ca1SJose Luis Duran 			dbname = optarg;
114*48e64ca1SJose Luis Duran 			break;
115*48e64ca1SJose Luis Duran 		case 'd':
116*48e64ca1SJose Luis Duran 			debug++;
117*48e64ca1SJose Luis Duran 			break;
118*48e64ca1SJose Luis Duran 		case 'n':
119*48e64ca1SJose Luis Duran 			noheader = 1;
120*48e64ca1SJose Luis Duran 			break;
121*48e64ca1SJose Luis Duran 		case 'r':
122*48e64ca1SJose Luis Duran 			remain = 1;
123*48e64ca1SJose Luis Duran 			break;
124*48e64ca1SJose Luis Duran 		case 'w':
125*48e64ca1SJose Luis Duran 			wide = 1;
126*48e64ca1SJose Luis Duran 			break;
127*48e64ca1SJose Luis Duran 		default:
128*48e64ca1SJose Luis Duran 			usage(o);
129*48e64ca1SJose Luis Duran 		}
130*48e64ca1SJose Luis Duran 
131*48e64ca1SJose Luis Duran 	db = state_open(dbname, O_RDONLY, 0);
132*48e64ca1SJose Luis Duran 	if (db == NULL)
133*48e64ca1SJose Luis Duran 		err(EXIT_FAILURE, "Can't open `%s'", dbname);
134*48e64ca1SJose Luis Duran 
135*48e64ca1SJose Luis Duran 	clock_gettime(CLOCK_REALTIME, &ts);
136*48e64ca1SJose Luis Duran 	wide = wide ? 8 * 4 + 7 : 4 * 3 + 3;
137*48e64ca1SJose Luis Duran 	if (!noheader)
138*48e64ca1SJose Luis Duran 		printf("%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide,
139*48e64ca1SJose Luis Duran 		    "address", remain ? "remaining time" : "last access");
140*48e64ca1SJose Luis Duran 	for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) {
141*48e64ca1SJose Luis Duran 		char buf[BUFSIZ];
142*48e64ca1SJose Luis Duran 		char mbuf[64], pbuf[64];
143*48e64ca1SJose Luis Duran 		if (!all) {
144*48e64ca1SJose Luis Duran 			if (blocked) {
145*48e64ca1SJose Luis Duran 				if (c.c_nfail == -1 || dbi.count < c.c_nfail)
146*48e64ca1SJose Luis Duran 					continue;
147*48e64ca1SJose Luis Duran 			} else {
148*48e64ca1SJose Luis Duran 				if (dbi.count >= c.c_nfail)
149*48e64ca1SJose Luis Duran 					continue;
150*48e64ca1SJose Luis Duran 			}
151*48e64ca1SJose Luis Duran 		}
152*48e64ca1SJose Luis Duran 		sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss);
153*48e64ca1SJose Luis Duran 		printf("%*.*s/%s:%s\t", wide, wide, buf,
154*48e64ca1SJose Luis Duran 		    star(mbuf, sizeof(mbuf), c.c_lmask),
155*48e64ca1SJose Luis Duran 		    star(pbuf, sizeof(pbuf), c.c_port));
156*48e64ca1SJose Luis Duran 		if (c.c_duration == -1) {
157*48e64ca1SJose Luis Duran 			strlcpy(buf, "never", sizeof(buf));
158*48e64ca1SJose Luis Duran 		} else {
159*48e64ca1SJose Luis Duran 			if (remain)
160*48e64ca1SJose Luis Duran 				fmtydhms(buf, sizeof(buf),
161*48e64ca1SJose Luis Duran 				    c.c_duration - (ts.tv_sec - dbi.last));
162*48e64ca1SJose Luis Duran 			else
163*48e64ca1SJose Luis Duran 				fmttime(buf, sizeof(buf), dbi.last);
164*48e64ca1SJose Luis Duran 		}
165*48e64ca1SJose Luis Duran 		printf("%s\t%d/%s\t%-s\n", dbi.id, dbi.count,
166*48e64ca1SJose Luis Duran 		    star(mbuf, sizeof(mbuf), c.c_nfail), buf);
167*48e64ca1SJose Luis Duran 	}
168*48e64ca1SJose Luis Duran 	state_close(db);
169*48e64ca1SJose Luis Duran 	return EXIT_SUCCESS;
170*48e64ca1SJose Luis Duran }
171