/* $NetBSD: blocklist.c,v 1.4 2025/02/11 17:48:30 christos Exp $ */12/*-3* Copyright (c) 2014 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: blocklist.c,v 1.4 2025/02/11 17:48:30 christos Exp $");3839#include <stdio.h>40#include <bl.h>4142#include <stdarg.h>43#include <errno.h>44#include <string.h>45#include <stdlib.h>46#include <syslog.h>4748int49blocklist_sa(int action, int rfd, const struct sockaddr *sa, socklen_t salen,50const char *msg)51{52struct blocklist *bl;53int rv;54if ((bl = blocklist_open()) == NULL)55return -1;56rv = blocklist_sa_r(bl, action, rfd, sa, salen, msg);57blocklist_close(bl);58return rv;59}6061int62blocklist_sa_r(struct blocklist *bl, int action, int rfd,63const struct sockaddr *sa, socklen_t slen, const char *msg)64{65bl_type_t internal_action;6667/* internal values are not the same as user application values */68switch (action) {69case BLOCKLIST_AUTH_FAIL:70internal_action = BL_ADD;71break;72case BLOCKLIST_AUTH_OK:73internal_action = BL_DELETE;74break;75case BLOCKLIST_ABUSIVE_BEHAVIOR:76internal_action = BL_ABUSE;77break;78case BLOCKLIST_BAD_USER:79internal_action = BL_BADUSER;80break;81default:82internal_action = BL_INVALID;83break;84}85return bl_send(bl, internal_action, rfd, sa, slen, msg);86}8788int89blocklist(int action, int rfd, const char *msg)90{91return blocklist_sa(action, rfd, NULL, 0, msg);92}9394int95blocklist_r(struct blocklist *bl, int action, int rfd, const char *msg)96{97return blocklist_sa_r(bl, action, rfd, NULL, 0, msg);98}99100struct blocklist *101blocklist_open(void) {102return bl_create(false, NULL, vsyslog_r);103}104105struct blocklist *106blocklist_open2(107void (*logger)(int, struct syslog_data *, const char *, va_list))108{109return bl_create(false, NULL, logger);110}111112void113blocklist_close(struct blocklist *bl)114{115bl_destroy(bl);116}117118119