15f4c09ddSEd Maste /* $NetBSD: blacklistctl.c,v 1.23 2018/05/24 19:21:01 christos Exp $ */
25f4c09ddSEd Maste
35f4c09ddSEd Maste /*-
45f4c09ddSEd Maste * Copyright (c) 2015 The NetBSD Foundation, Inc.
55f4c09ddSEd Maste * All rights reserved.
65f4c09ddSEd Maste *
75f4c09ddSEd Maste * This code is derived from software contributed to The NetBSD Foundation
85f4c09ddSEd Maste * by Christos Zoulas.
95f4c09ddSEd Maste *
105f4c09ddSEd Maste * Redistribution and use in source and binary forms, with or without
115f4c09ddSEd Maste * modification, are permitted provided that the following conditions
125f4c09ddSEd Maste * are met:
135f4c09ddSEd Maste * 1. Redistributions of source code must retain the above copyright
145f4c09ddSEd Maste * notice, this list of conditions and the following disclaimer.
155f4c09ddSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
165f4c09ddSEd Maste * notice, this list of conditions and the following disclaimer in the
175f4c09ddSEd Maste * documentation and/or other materials provided with the distribution.
185f4c09ddSEd Maste *
195f4c09ddSEd Maste * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
205f4c09ddSEd Maste * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
215f4c09ddSEd Maste * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
225f4c09ddSEd Maste * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
235f4c09ddSEd Maste * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
245f4c09ddSEd Maste * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
255f4c09ddSEd Maste * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
265f4c09ddSEd Maste * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
275f4c09ddSEd Maste * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
285f4c09ddSEd Maste * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
295f4c09ddSEd Maste * POSSIBILITY OF SUCH DAMAGE.
305f4c09ddSEd Maste */
315f4c09ddSEd Maste #ifdef HAVE_CONFIG_H
325f4c09ddSEd Maste #include "config.h"
335f4c09ddSEd Maste #endif
345f4c09ddSEd Maste
355f4c09ddSEd Maste #include <sys/cdefs.h>
365f4c09ddSEd Maste __RCSID("$NetBSD: blacklistctl.c,v 1.23 2018/05/24 19:21:01 christos Exp $");
375f4c09ddSEd Maste
385f4c09ddSEd Maste #include <stdio.h>
395f4c09ddSEd Maste #include <time.h>
405f4c09ddSEd Maste #ifdef HAVE_LIBUTIL_H
415f4c09ddSEd Maste #include <libutil.h>
425f4c09ddSEd Maste #endif
435f4c09ddSEd Maste #ifdef HAVE_UTIL_H
445f4c09ddSEd Maste #include <util.h>
455f4c09ddSEd Maste #endif
465f4c09ddSEd Maste #include <fcntl.h>
475f4c09ddSEd Maste #include <string.h>
485f4c09ddSEd Maste #include <syslog.h>
495f4c09ddSEd Maste #include <err.h>
505f4c09ddSEd Maste #include <stdlib.h>
515f4c09ddSEd Maste #include <unistd.h>
525f4c09ddSEd Maste #include <sys/socket.h>
535f4c09ddSEd Maste
545f4c09ddSEd Maste #include "conf.h"
555f4c09ddSEd Maste #include "state.h"
565f4c09ddSEd Maste #include "internal.h"
575f4c09ddSEd Maste #include "support.h"
585f4c09ddSEd Maste
595f4c09ddSEd Maste static __dead void
usage(int c)605f4c09ddSEd Maste usage(int c)
615f4c09ddSEd Maste {
625f4c09ddSEd Maste if (c == 0)
635f4c09ddSEd Maste warnx("Missing/unknown command");
645f4c09ddSEd Maste else if (c != '?')
655f4c09ddSEd Maste warnx("Unknown option `%c'", (char)c);
665f4c09ddSEd Maste fprintf(stderr, "Usage: %s dump [-abdnrw]\n", getprogname());
675f4c09ddSEd Maste exit(EXIT_FAILURE);
685f4c09ddSEd Maste }
695f4c09ddSEd Maste
705f4c09ddSEd Maste static const char *
star(char * buf,size_t len,int val)715f4c09ddSEd Maste star(char *buf, size_t len, int val)
725f4c09ddSEd Maste {
735f4c09ddSEd Maste if (val == -1)
745f4c09ddSEd Maste return "*";
755f4c09ddSEd Maste snprintf(buf, len, "%d", val);
765f4c09ddSEd Maste return buf;
775f4c09ddSEd Maste }
785f4c09ddSEd Maste
795f4c09ddSEd Maste int
main(int argc,char * argv[])805f4c09ddSEd Maste main(int argc, char *argv[])
815f4c09ddSEd Maste {
825f4c09ddSEd Maste const char *dbname = _PATH_BLSTATE;
835f4c09ddSEd Maste DB *db;
845f4c09ddSEd Maste struct conf c;
855f4c09ddSEd Maste struct dbinfo dbi;
865f4c09ddSEd Maste unsigned int i;
875f4c09ddSEd Maste struct timespec ts;
885f4c09ddSEd Maste int all, blocked, remain, wide, noheader;
895f4c09ddSEd Maste int o;
905f4c09ddSEd Maste
915f4c09ddSEd Maste noheader = wide = blocked = all = remain = 0;
925f4c09ddSEd Maste lfun = dlog;
935f4c09ddSEd Maste
945f4c09ddSEd Maste if (argc == 1 || strcmp(argv[1], "dump") != 0)
955f4c09ddSEd Maste usage(0);
965f4c09ddSEd Maste
975f4c09ddSEd Maste argc--;
985f4c09ddSEd Maste argv++;
995f4c09ddSEd Maste
1005f4c09ddSEd Maste while ((o = getopt(argc, argv, "abD:dnrw")) != -1)
1015f4c09ddSEd Maste switch (o) {
1025f4c09ddSEd Maste case 'a':
1035f4c09ddSEd Maste all = 1;
1045f4c09ddSEd Maste blocked = 0;
1055f4c09ddSEd Maste break;
1065f4c09ddSEd Maste case 'b':
1075f4c09ddSEd Maste blocked = 1;
1085f4c09ddSEd Maste break;
1095f4c09ddSEd Maste case 'D':
1105f4c09ddSEd Maste dbname = optarg;
1115f4c09ddSEd Maste break;
1125f4c09ddSEd Maste case 'd':
1135f4c09ddSEd Maste debug++;
1145f4c09ddSEd Maste break;
1155f4c09ddSEd Maste case 'n':
1165f4c09ddSEd Maste noheader = 1;
1175f4c09ddSEd Maste break;
1185f4c09ddSEd Maste case 'r':
1195f4c09ddSEd Maste remain = 1;
1205f4c09ddSEd Maste break;
1215f4c09ddSEd Maste case 'w':
1225f4c09ddSEd Maste wide = 1;
1235f4c09ddSEd Maste break;
1245f4c09ddSEd Maste default:
1255f4c09ddSEd Maste usage(o);
1265f4c09ddSEd Maste }
1275f4c09ddSEd Maste
1285f4c09ddSEd Maste db = state_open(dbname, O_RDONLY, 0);
1295f4c09ddSEd Maste if (db == NULL)
1305f4c09ddSEd Maste err(EXIT_FAILURE, "Can't open `%s'", dbname);
1315f4c09ddSEd Maste
1325f4c09ddSEd Maste clock_gettime(CLOCK_REALTIME, &ts);
1335f4c09ddSEd Maste wide = wide ? 8 * 4 + 7 : 4 * 3 + 3;
1345f4c09ddSEd Maste if (!noheader)
1355f4c09ddSEd Maste printf("%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide,
1365f4c09ddSEd Maste "address", remain ? "remaining time" : "last access");
1375f4c09ddSEd Maste for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) {
1385f4c09ddSEd Maste char buf[BUFSIZ];
1395f4c09ddSEd Maste char mbuf[64], pbuf[64];
1405f4c09ddSEd Maste if (!all) {
1415f4c09ddSEd Maste if (blocked) {
1425f4c09ddSEd Maste if (c.c_nfail == -1 || dbi.count < c.c_nfail)
1435f4c09ddSEd Maste continue;
1445f4c09ddSEd Maste } else {
1455f4c09ddSEd Maste if (dbi.count >= c.c_nfail)
1465f4c09ddSEd Maste continue;
1475f4c09ddSEd Maste }
1485f4c09ddSEd Maste }
1495f4c09ddSEd Maste sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss);
1505f4c09ddSEd Maste printf("%*.*s/%s:%s\t", wide, wide, buf,
1515f4c09ddSEd Maste star(mbuf, sizeof(mbuf), c.c_lmask),
1525f4c09ddSEd Maste star(pbuf, sizeof(pbuf), c.c_port));
1535f4c09ddSEd Maste if (c.c_duration == -1) {
1545f4c09ddSEd Maste strlcpy(buf, "never", sizeof(buf));
1555f4c09ddSEd Maste } else {
1565f4c09ddSEd Maste if (remain)
1575f4c09ddSEd Maste fmtydhms(buf, sizeof(buf),
1585f4c09ddSEd Maste c.c_duration - (ts.tv_sec - dbi.last));
1595f4c09ddSEd Maste else
1605f4c09ddSEd Maste fmttime(buf, sizeof(buf), dbi.last);
1615f4c09ddSEd Maste }
1625f4c09ddSEd Maste printf("%s\t%d/%s\t%-s\n", dbi.id, dbi.count,
1635f4c09ddSEd Maste star(mbuf, sizeof(mbuf), c.c_nfail), buf);
1645f4c09ddSEd Maste }
1655f4c09ddSEd Maste state_close(db);
1665f4c09ddSEd Maste return EXIT_SUCCESS;
1675f4c09ddSEd Maste }
168