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