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