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