Path: blob/main/contrib/blocklist/bin/blocklistctl.c
48062 views
/* $NetBSD: blocklistctl.c,v 1.5 2025/10/25 16:56:10 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#ifdef HAVE_SYS_CDEFS_H35#include <sys/cdefs.h>36#endif37__RCSID("$NetBSD: blocklistctl.c,v 1.5 2025/10/25 16:56:10 christos Exp $");3839#include <stdio.h>40#include <time.h>41#ifdef HAVE_LIBUTIL_H42#include <libutil.h>43#endif44#ifdef HAVE_UTIL_H45#include <util.h>46#endif47#include <fcntl.h>48#include <string.h>49#include <syslog.h>50#include <err.h>51#include <stdlib.h>52#include <unistd.h>53#include <sys/socket.h>5455#include "conf.h"56#include "state.h"57#include "internal.h"58#include "support.h"5960static __dead void61usage(int c)62{63if (c == 0)64warnx("Missing/unknown command");65else if (c != '?')66warnx("Unknown option `%c'", (char)c);67fprintf(stderr,68"Usage: %s dump [-abdnrw] [-D dbname]\n", getprogname());69exit(EXIT_FAILURE);70}7172static const char *73star(char *buf, size_t len, int val)74{75if (val == -1)76return "*";77snprintf(buf, len, "%d", val);78return buf;79}8081int82main(int argc, char *argv[])83{84const char *dbname = _PATH_BLSTATE;85DB *db;86struct conf c;87struct dbinfo dbi;88unsigned int i;89struct timespec ts;90int all, blocked, remain, wide, noheader;91int o;9293noheader = wide = blocked = all = remain = 0;94lfun = dlog;9596if (argc == 1 || strcmp(argv[1], "dump") != 0)97usage(0);9899argc--;100argv++;101102while ((o = getopt(argc, argv, "abD:dnrw")) != -1)103switch (o) {104case 'a':105all = 1;106blocked = 0;107break;108case 'b':109blocked = 1;110break;111case 'D':112dbname = optarg;113break;114case 'd':115debug++;116break;117case 'n':118noheader = 1;119break;120case 'r':121remain = 1;122break;123case 'w':124wide = 1;125break;126default:127usage(o);128}129130db = state_open(dbname, O_RDONLY, 0);131if (db == NULL)132err(EXIT_FAILURE, "Can't open `%s'", dbname);133134clock_gettime(CLOCK_REALTIME, &ts);135wide = wide ? 8 * 4 + 7 : 4 * 3 + 3;136if (!noheader)137printf("rulename\t%*.*s/ma:port\tid\tnfail\t%s\n", wide, wide,138"address", remain ? "remaining time" : "last access");139for (i = 1; state_iterate(db, &c, &dbi, i) != 0; i = 0) {140char buf[BUFSIZ];141char mbuf[64], pbuf[64];142if (!all) {143if (blocked) {144if (c.c_nfail == -1 || dbi.count < c.c_nfail)145continue;146} else {147if (dbi.count >= c.c_nfail)148continue;149}150}151sockaddr_snprintf(buf, sizeof(buf), "%a", (void *)&c.c_ss);152printf("%s\t%*.*s/%s:%s\t", c.c_name, wide, wide, buf,153star(mbuf, sizeof(mbuf), c.c_lmask),154star(pbuf, sizeof(pbuf), c.c_port));155if (c.c_duration == -1) {156strlcpy(buf, "never", sizeof(buf));157} else {158if (remain)159fmtydhms(buf, sizeof(buf),160c.c_duration - (ts.tv_sec - dbi.last));161else162fmttime(buf, sizeof(buf), dbi.last);163}164printf("%s\t%d/%s\t%-s\n", dbi.id, dbi.count,165star(mbuf, sizeof(mbuf), c.c_nfail), buf);166}167state_close(db);168return EXIT_SUCCESS;169}170171172