Path: blob/main/contrib/blocklist/bin/blacklistctl.c
39478 views
/* $NetBSD: blacklistctl.c,v 1.23 2018/05/24 19:21:01 christos Exp $ */12/*-3* Copyright (c) 2015 The NetBSD Foundation, Inc.4* All rights reserved.5*6* This code is derived from software contributed to The NetBSD Foundation7* by Christos Zoulas.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS19* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED20* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS22* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/30#ifdef HAVE_CONFIG_H31#include "config.h"32#endif3334#include <sys/cdefs.h>35__RCSID("$NetBSD: blacklistctl.c,v 1.23 2018/05/24 19:21:01 christos Exp $");3637#include <stdio.h>38#include <time.h>39#ifdef HAVE_LIBUTIL_H40#include <libutil.h>41#endif42#ifdef HAVE_UTIL_H43#include <util.h>44#endif45#include <fcntl.h>46#include <string.h>47#include <syslog.h>48#include <err.h>49#include <stdlib.h>50#include <unistd.h>51#include <sys/socket.h>5253#include "conf.h"54#include "state.h"55#include "internal.h"56#include "support.h"5758static __dead void59usage(int c)60{61if (c == 0)62warnx("Missing/unknown command");63else if (c != '?')64warnx("Unknown option `%c'", (char)c);65fprintf(stderr, "Usage: %s dump [-abdnrw]\n", getprogname());66exit(EXIT_FAILURE);67}6869static const char *70star(char *buf, size_t len, int val)71{72if (val == -1)73return "*";74snprintf(buf, len, "%d", val);75return buf;76}7778int79main(int argc, char *argv[])80{81const char *dbname = _PATH_BLSTATE;82DB *db;83struct conf c;84struct dbinfo dbi;85unsigned int i;86struct timespec ts;87int all, blocked, remain, wide, noheader;88int o;8990noheader = wide = blocked = all = remain = 0;91lfun = dlog;9293if (argc == 1 || strcmp(argv[1], "dump") != 0)94usage(0);9596argc--;97argv++;9899while ((o = getopt(argc, argv, "abD:dnrw")) != -1)100switch (o) {101case 'a':102all = 1;103blocked = 0;104break;105case 'b':106blocked = 1;107break;108case 'D':109dbname = optarg;110break;111case 'd':112debug++;113break;114case 'n':115noheader = 1;116break;117case 'r':118remain = 1;119break;120case 'w':121wide = 1;122break;123default:124usage(o);125}126127db = state_open(dbname, O_RDONLY, 0);128if (db == NULL)129err(EXIT_FAILURE, "Can't open `%s'", dbname);130131clock_gettime(CLOCK_REALTIME, &ts);132wide = wide ? 8 * 4 + 7 : 4 * 3 + 3;133if (!noheader)134printf("%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide,135"address", remain ? "remaining time" : "last access");136for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) {137char buf[BUFSIZ];138char mbuf[64], pbuf[64];139if (!all) {140if (blocked) {141if (c.c_nfail == -1 || dbi.count < c.c_nfail)142continue;143} else {144if (dbi.count >= c.c_nfail)145continue;146}147}148sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss);149printf("%*.*s/%s:%s\t", wide, wide, buf,150star(mbuf, sizeof(mbuf), c.c_lmask),151star(pbuf, sizeof(pbuf), c.c_port));152if (c.c_duration == -1) {153strlcpy(buf, "never", sizeof(buf));154} else {155if (remain)156fmtydhms(buf, sizeof(buf),157c.c_duration - (ts.tv_sec - dbi.last));158else159fmttime(buf, sizeof(buf), dbi.last);160}161printf("%s\t%d/%s\t%-s\n", dbi.id, dbi.count,162star(mbuf, sizeof(mbuf), c.c_nfail), buf);163}164state_close(db);165return EXIT_SUCCESS;166}167168169