]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/blacklist/bin/blacklistctl.c
Import NetBSD's blacklist source from vendor tree
[FreeBSD/FreeBSD.git] / contrib / blacklist / bin / blacklistctl.c
1 /*      $NetBSD: blacklistctl.c,v 1.20 2016/04/04 15:52:56 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.20 2016/04/04 15:52:56 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 int
71 main(int argc, char *argv[])
72 {
73         const char *dbname = _PATH_BLSTATE;
74         DB *db;
75         struct conf c;
76         struct dbinfo dbi;
77         unsigned int i;
78         struct timespec ts;
79         int all, blocked, remain, wide, noheader;
80         int o;
81
82         noheader = wide = blocked = all = remain = 0;
83         lfun = dlog;
84
85         if (argc == 1 || strcmp(argv[1], "dump") != 0)
86                 usage(0);
87
88         argc--;
89         argv++;
90
91         while ((o = getopt(argc, argv, "abD:dnrw")) != -1)
92                 switch (o) {
93                 case 'a':
94                         all = 1;
95                         blocked = 0;
96                         break;
97                 case 'b':
98                         blocked = 1;
99                 case 'D':
100                         dbname = optarg;
101                         break;
102                         break;
103                 case 'd':
104                         debug++;
105                         break;
106                 case 'n':
107                         noheader = 1;
108                         break;
109                 case 'r':
110                         remain = 1;
111                         break;
112                 case 'w':
113                         wide = 1;
114                         break;
115                 default:
116                         usage(o);
117                         break;
118                 }
119
120         db = state_open(dbname, O_RDONLY, 0);
121         if (db == NULL)
122                 err(EXIT_FAILURE, "Can't open `%s'", dbname);
123
124         clock_gettime(CLOCK_REALTIME, &ts);
125         wide = wide ? 8 * 4 + 7 : 4 * 3 + 3;
126         if (!noheader)
127                 printf("%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide,
128                     "address", remain ? "remaining time" : "last access");
129         for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) {
130                 char buf[BUFSIZ];
131                 if (!all) {
132                         if (blocked) {
133                                 if (dbi.count < c.c_nfail)
134                                         continue;
135                         } else {
136                                 if (dbi.count >= c.c_nfail)
137                                         continue;
138                         }
139                 }
140                 sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss);
141                 printf("%*.*s/%d:%d\t", wide, wide, buf, c.c_lmask, c.c_port);
142                 if (remain)
143                         fmtydhms(buf, sizeof(buf),
144                             c.c_duration - (ts.tv_sec - dbi.last));
145                 else
146                         fmttime(buf, sizeof(buf), dbi.last);
147                 printf("%s\t%d/%d\t%-s\n", dbi.id, dbi.count, c.c_nfail, buf);
148         }
149         state_close(db);
150         return EXIT_SUCCESS;
151 }