1301169Slidl/*	$NetBSD: blacklistctl.c,v 1.20 2016/04/04 15:52:56 christos Exp $	*/
2301169Slidl
3301169Slidl/*-
4301169Slidl * Copyright (c) 2015 The NetBSD Foundation, Inc.
5301169Slidl * All rights reserved.
6301169Slidl *
7301169Slidl * This code is derived from software contributed to The NetBSD Foundation
8301169Slidl * by Christos Zoulas.
9301169Slidl *
10301169Slidl * Redistribution and use in source and binary forms, with or without
11301169Slidl * modification, are permitted provided that the following conditions
12301169Slidl * are met:
13301169Slidl * 1. Redistributions of source code must retain the above copyright
14301169Slidl *    notice, this list of conditions and the following disclaimer.
15301169Slidl * 2. Redistributions in binary form must reproduce the above copyright
16301169Slidl *    notice, this list of conditions and the following disclaimer in the
17301169Slidl *    documentation and/or other materials provided with the distribution.
18301169Slidl *
19301169Slidl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20301169Slidl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21301169Slidl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22301169Slidl * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23301169Slidl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24301169Slidl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25301169Slidl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26301169Slidl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27301169Slidl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28301169Slidl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29301169Slidl * POSSIBILITY OF SUCH DAMAGE.
30301169Slidl */
31301169Slidl#ifdef HAVE_CONFIG_H
32301169Slidl#include "config.h"
33301169Slidl#endif
34301169Slidl
35301169Slidl#include <sys/cdefs.h>
36301169Slidl__RCSID("$NetBSD: blacklistctl.c,v 1.20 2016/04/04 15:52:56 christos Exp $");
37301169Slidl
38301169Slidl#include <stdio.h>
39301169Slidl#include <time.h>
40301169Slidl#ifdef HAVE_LIBUTIL_H
41301169Slidl#include <libutil.h>
42301169Slidl#endif
43301169Slidl#ifdef HAVE_UTIL_H
44301169Slidl#include <util.h>
45301169Slidl#endif
46301169Slidl#include <fcntl.h>
47301169Slidl#include <string.h>
48301169Slidl#include <syslog.h>
49301169Slidl#include <err.h>
50301169Slidl#include <stdlib.h>
51301169Slidl#include <unistd.h>
52301169Slidl#include <sys/socket.h>
53301169Slidl
54301169Slidl#include "conf.h"
55301169Slidl#include "state.h"
56301169Slidl#include "internal.h"
57301169Slidl#include "support.h"
58301169Slidl
59301169Slidlstatic __dead void
60301169Slidlusage(int c)
61301169Slidl{
62301169Slidl	if (c == 0)
63301169Slidl		warnx("Missing/unknown command");
64301169Slidl	else
65301169Slidl		warnx("Unknown option `%c'", (char)c);
66301169Slidl	fprintf(stderr, "Usage: %s dump [-abdnrw]\n", getprogname());
67301169Slidl	exit(EXIT_FAILURE);
68301169Slidl}
69301169Slidl
70301169Slidlint
71301169Slidlmain(int argc, char *argv[])
72301169Slidl{
73301169Slidl	const char *dbname = _PATH_BLSTATE;
74301169Slidl	DB *db;
75301169Slidl	struct conf c;
76301169Slidl	struct dbinfo dbi;
77301169Slidl	unsigned int i;
78301169Slidl	struct timespec ts;
79301169Slidl	int all, blocked, remain, wide, noheader;
80301169Slidl	int o;
81301169Slidl
82301169Slidl	noheader = wide = blocked = all = remain = 0;
83301169Slidl	lfun = dlog;
84301169Slidl
85301169Slidl	if (argc == 1 || strcmp(argv[1], "dump") != 0)
86301169Slidl		usage(0);
87301169Slidl
88301169Slidl	argc--;
89301169Slidl	argv++;
90301169Slidl
91301169Slidl	while ((o = getopt(argc, argv, "abD:dnrw")) != -1)
92301169Slidl		switch (o) {
93301169Slidl		case 'a':
94301169Slidl			all = 1;
95301169Slidl			blocked = 0;
96301169Slidl			break;
97301169Slidl		case 'b':
98301169Slidl			blocked = 1;
99301603Sgarga			break;
100301169Slidl		case 'D':
101301169Slidl			dbname = optarg;
102301169Slidl			break;
103301169Slidl		case 'd':
104301169Slidl			debug++;
105301169Slidl			break;
106301169Slidl		case 'n':
107301169Slidl			noheader = 1;
108301169Slidl			break;
109301169Slidl		case 'r':
110301169Slidl			remain = 1;
111301169Slidl			break;
112301169Slidl		case 'w':
113301169Slidl			wide = 1;
114301169Slidl			break;
115301169Slidl		default:
116301169Slidl			usage(o);
117301169Slidl			break;
118301169Slidl		}
119301169Slidl
120301169Slidl	db = state_open(dbname, O_RDONLY, 0);
121301169Slidl	if (db == NULL)
122301169Slidl		err(EXIT_FAILURE, "Can't open `%s'", dbname);
123301169Slidl
124301169Slidl	clock_gettime(CLOCK_REALTIME, &ts);
125301169Slidl	wide = wide ? 8 * 4 + 7 : 4 * 3 + 3;
126301169Slidl	if (!noheader)
127301169Slidl		printf("%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide,
128301169Slidl		    "address", remain ? "remaining time" : "last access");
129301169Slidl	for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) {
130301169Slidl		char buf[BUFSIZ];
131301169Slidl		if (!all) {
132301169Slidl			if (blocked) {
133301169Slidl				if (dbi.count < c.c_nfail)
134301169Slidl					continue;
135301169Slidl			} else {
136301169Slidl				if (dbi.count >= c.c_nfail)
137301169Slidl					continue;
138301169Slidl			}
139301169Slidl		}
140301169Slidl		sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss);
141301169Slidl		printf("%*.*s/%d:%d\t", wide, wide, buf, c.c_lmask, c.c_port);
142301169Slidl		if (remain)
143301169Slidl			fmtydhms(buf, sizeof(buf),
144301169Slidl			    c.c_duration - (ts.tv_sec - dbi.last));
145301169Slidl		else
146301169Slidl			fmttime(buf, sizeof(buf), dbi.last);
147301169Slidl		printf("%s\t%d/%d\t%-s\n", dbi.id, dbi.count, c.c_nfail, buf);
148301169Slidl	}
149301169Slidl	state_close(db);
150301169Slidl	return EXIT_SUCCESS;
151301169Slidl}
152