]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/blacklist/bin/blacklistctl.c
MFV 364467:
[FreeBSD/FreeBSD.git] / contrib / blacklist / bin / blacklistctl.c
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
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                         break;
127                 }
128
129         db = state_open(dbname, O_RDONLY, 0);
130         if (db == NULL)
131                 err(EXIT_FAILURE, "Can't open `%s'", dbname);
132
133         clock_gettime(CLOCK_REALTIME, &ts);
134         wide = wide ? 8 * 4 + 7 : 4 * 3 + 3;
135         if (!noheader)
136                 printf("%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide,
137                     "address", remain ? "remaining time" : "last access");
138         for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) {
139                 char buf[BUFSIZ];
140                 char mbuf[64], pbuf[64];
141                 if (!all) {
142                         if (blocked) {
143                                 if (c.c_nfail == -1 || dbi.count < c.c_nfail)
144                                         continue;
145                         } else {
146                                 if (dbi.count >= c.c_nfail)
147                                         continue;
148                         }
149                 }
150                 sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss);
151                 printf("%*.*s/%s:%s\t", wide, wide, buf,
152                     star(mbuf, sizeof(mbuf), c.c_lmask),
153                     star(pbuf, sizeof(pbuf), c.c_port));
154                 if (c.c_duration == -1) {
155                         strlcpy(buf, "never", sizeof(buf));
156                 } else {
157                         if (remain)
158                                 fmtydhms(buf, sizeof(buf),
159                                     c.c_duration - (ts.tv_sec - dbi.last));
160                         else
161                                 fmttime(buf, sizeof(buf), dbi.last);
162                 }
163                 printf("%s\t%d/%s\t%-s\n", dbi.id, dbi.count,
164                     star(mbuf, sizeof(mbuf), c.c_nfail), buf);
165         }
166         state_close(db);
167         return EXIT_SUCCESS;
168 }