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