1*48e64ca1SJose Luis Duran /* $NetBSD: blocklist.c,v 1.4 2025/02/11 17:48:30 christos Exp $ */
2*48e64ca1SJose Luis Duran
3*48e64ca1SJose Luis Duran /*-
4*48e64ca1SJose Luis Duran * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*48e64ca1SJose Luis Duran * All rights reserved.
6*48e64ca1SJose Luis Duran *
7*48e64ca1SJose Luis Duran * This code is derived from software contributed to The NetBSD Foundation
8*48e64ca1SJose Luis Duran * by Christos Zoulas.
9*48e64ca1SJose Luis Duran *
10*48e64ca1SJose Luis Duran * Redistribution and use in source and binary forms, with or without
11*48e64ca1SJose Luis Duran * modification, are permitted provided that the following conditions
12*48e64ca1SJose Luis Duran * are met:
13*48e64ca1SJose Luis Duran * 1. Redistributions of source code must retain the above copyright
14*48e64ca1SJose Luis Duran * notice, this list of conditions and the following disclaimer.
15*48e64ca1SJose Luis Duran * 2. Redistributions in binary form must reproduce the above copyright
16*48e64ca1SJose Luis Duran * notice, this list of conditions and the following disclaimer in the
17*48e64ca1SJose Luis Duran * documentation and/or other materials provided with the distribution.
18*48e64ca1SJose Luis Duran *
19*48e64ca1SJose Luis Duran * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*48e64ca1SJose Luis Duran * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*48e64ca1SJose Luis Duran * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*48e64ca1SJose Luis Duran * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*48e64ca1SJose Luis Duran * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*48e64ca1SJose Luis Duran * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*48e64ca1SJose Luis Duran * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*48e64ca1SJose Luis Duran * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*48e64ca1SJose Luis Duran * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*48e64ca1SJose Luis Duran * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*48e64ca1SJose Luis Duran * POSSIBILITY OF SUCH DAMAGE.
30*48e64ca1SJose Luis Duran */
31*48e64ca1SJose Luis Duran #ifdef HAVE_CONFIG_H
32*48e64ca1SJose Luis Duran #include "config.h"
33*48e64ca1SJose Luis Duran #endif
34*48e64ca1SJose Luis Duran
35*48e64ca1SJose Luis Duran #ifdef HAVE_SYS_CDEFS_H
36*48e64ca1SJose Luis Duran #include <sys/cdefs.h>
37*48e64ca1SJose Luis Duran #endif
38*48e64ca1SJose Luis Duran __RCSID("$NetBSD: blocklist.c,v 1.4 2025/02/11 17:48:30 christos Exp $");
39*48e64ca1SJose Luis Duran
40*48e64ca1SJose Luis Duran #include <stdio.h>
41*48e64ca1SJose Luis Duran #include <bl.h>
42*48e64ca1SJose Luis Duran
43*48e64ca1SJose Luis Duran #include <stdarg.h>
44*48e64ca1SJose Luis Duran #include <errno.h>
45*48e64ca1SJose Luis Duran #include <string.h>
46*48e64ca1SJose Luis Duran #include <stdlib.h>
47*48e64ca1SJose Luis Duran #include <syslog.h>
48*48e64ca1SJose Luis Duran
49*48e64ca1SJose Luis Duran int
blocklist_sa(int action,int rfd,const struct sockaddr * sa,socklen_t salen,const char * msg)50*48e64ca1SJose Luis Duran blocklist_sa(int action, int rfd, const struct sockaddr *sa, socklen_t salen,
51*48e64ca1SJose Luis Duran const char *msg)
52*48e64ca1SJose Luis Duran {
53*48e64ca1SJose Luis Duran struct blocklist *bl;
54*48e64ca1SJose Luis Duran int rv;
55*48e64ca1SJose Luis Duran if ((bl = blocklist_open()) == NULL)
56*48e64ca1SJose Luis Duran return -1;
57*48e64ca1SJose Luis Duran rv = blocklist_sa_r(bl, action, rfd, sa, salen, msg);
58*48e64ca1SJose Luis Duran blocklist_close(bl);
59*48e64ca1SJose Luis Duran return rv;
60*48e64ca1SJose Luis Duran }
61*48e64ca1SJose Luis Duran
62*48e64ca1SJose Luis Duran int
blocklist_sa_r(struct blocklist * bl,int action,int rfd,const struct sockaddr * sa,socklen_t slen,const char * msg)63*48e64ca1SJose Luis Duran blocklist_sa_r(struct blocklist *bl, int action, int rfd,
64*48e64ca1SJose Luis Duran const struct sockaddr *sa, socklen_t slen, const char *msg)
65*48e64ca1SJose Luis Duran {
66*48e64ca1SJose Luis Duran bl_type_t internal_action;
67*48e64ca1SJose Luis Duran
68*48e64ca1SJose Luis Duran /* internal values are not the same as user application values */
69*48e64ca1SJose Luis Duran switch (action) {
70*48e64ca1SJose Luis Duran case BLOCKLIST_AUTH_FAIL:
71*48e64ca1SJose Luis Duran internal_action = BL_ADD;
72*48e64ca1SJose Luis Duran break;
73*48e64ca1SJose Luis Duran case BLOCKLIST_AUTH_OK:
74*48e64ca1SJose Luis Duran internal_action = BL_DELETE;
75*48e64ca1SJose Luis Duran break;
76*48e64ca1SJose Luis Duran case BLOCKLIST_ABUSIVE_BEHAVIOR:
77*48e64ca1SJose Luis Duran internal_action = BL_ABUSE;
78*48e64ca1SJose Luis Duran break;
79*48e64ca1SJose Luis Duran case BLOCKLIST_BAD_USER:
80*48e64ca1SJose Luis Duran internal_action = BL_BADUSER;
81*48e64ca1SJose Luis Duran break;
82*48e64ca1SJose Luis Duran default:
83*48e64ca1SJose Luis Duran internal_action = BL_INVALID;
84*48e64ca1SJose Luis Duran break;
85*48e64ca1SJose Luis Duran }
86*48e64ca1SJose Luis Duran return bl_send(bl, internal_action, rfd, sa, slen, msg);
87*48e64ca1SJose Luis Duran }
88*48e64ca1SJose Luis Duran
89*48e64ca1SJose Luis Duran int
blocklist(int action,int rfd,const char * msg)90*48e64ca1SJose Luis Duran blocklist(int action, int rfd, const char *msg)
91*48e64ca1SJose Luis Duran {
92*48e64ca1SJose Luis Duran return blocklist_sa(action, rfd, NULL, 0, msg);
93*48e64ca1SJose Luis Duran }
94*48e64ca1SJose Luis Duran
95*48e64ca1SJose Luis Duran int
blocklist_r(struct blocklist * bl,int action,int rfd,const char * msg)96*48e64ca1SJose Luis Duran blocklist_r(struct blocklist *bl, int action, int rfd, const char *msg)
97*48e64ca1SJose Luis Duran {
98*48e64ca1SJose Luis Duran return blocklist_sa_r(bl, action, rfd, NULL, 0, msg);
99*48e64ca1SJose Luis Duran }
100*48e64ca1SJose Luis Duran
101*48e64ca1SJose Luis Duran struct blocklist *
blocklist_open(void)102*48e64ca1SJose Luis Duran blocklist_open(void) {
103*48e64ca1SJose Luis Duran return bl_create(false, NULL, vsyslog_r);
104*48e64ca1SJose Luis Duran }
105*48e64ca1SJose Luis Duran
106*48e64ca1SJose Luis Duran struct blocklist *
blocklist_open2(void (* logger)(int,struct syslog_data *,const char *,va_list))107*48e64ca1SJose Luis Duran blocklist_open2(
108*48e64ca1SJose Luis Duran void (*logger)(int, struct syslog_data *, const char *, va_list))
109*48e64ca1SJose Luis Duran {
110*48e64ca1SJose Luis Duran return bl_create(false, NULL, logger);
111*48e64ca1SJose Luis Duran }
112*48e64ca1SJose Luis Duran
113*48e64ca1SJose Luis Duran void
blocklist_close(struct blocklist * bl)114*48e64ca1SJose Luis Duran blocklist_close(struct blocklist *bl)
115*48e64ca1SJose Luis Duran {
116*48e64ca1SJose Luis Duran bl_destroy(bl);
117*48e64ca1SJose Luis Duran }
118