xref: /freebsd/crypto/openssh/blocklist.c (revision 7238317403b95a8e35cf0bc7cd66fbd78ecbe521)
1*72383174SJose Luis Duran /*-
2*72383174SJose Luis Duran  * Copyright (c) 2015 The NetBSD Foundation, Inc.
3*72383174SJose Luis Duran  * Copyright (c) 2016 The FreeBSD Foundation
4*72383174SJose Luis Duran  * All rights reserved.
5*72383174SJose Luis Duran  *
6*72383174SJose Luis Duran  * Portions of this software were developed by Kurt Lidl
7*72383174SJose Luis Duran  * under sponsorship from the FreeBSD Foundation.
8*72383174SJose Luis Duran  *
9*72383174SJose Luis Duran  * This code is derived from software contributed to The NetBSD Foundation
10*72383174SJose Luis Duran  * by Christos Zoulas.
11*72383174SJose Luis Duran  *
12*72383174SJose Luis Duran  * Redistribution and use in source and binary forms, with or without
13*72383174SJose Luis Duran  * modification, are permitted provided that the following conditions
14*72383174SJose Luis Duran  * are met:
15*72383174SJose Luis Duran  * 1. Redistributions of source code must retain the above copyright
16*72383174SJose Luis Duran  *    notice, this list of conditions and the following disclaimer.
17*72383174SJose Luis Duran  * 2. Redistributions in binary form must reproduce the above copyright
18*72383174SJose Luis Duran  *    notice, this list of conditions and the following disclaimer in the
19*72383174SJose Luis Duran  *    documentation and/or other materials provided with the distribution.
20*72383174SJose Luis Duran  *
21*72383174SJose Luis Duran  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22*72383174SJose Luis Duran  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23*72383174SJose Luis Duran  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24*72383174SJose Luis Duran  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25*72383174SJose Luis Duran  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26*72383174SJose Luis Duran  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27*72383174SJose Luis Duran  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*72383174SJose Luis Duran  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29*72383174SJose Luis Duran  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30*72383174SJose Luis Duran  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31*72383174SJose Luis Duran  * POSSIBILITY OF SUCH DAMAGE.
32*72383174SJose Luis Duran  */
33*72383174SJose Luis Duran 
34*72383174SJose Luis Duran #include "includes.h"
35*72383174SJose Luis Duran 
36*72383174SJose Luis Duran #include <ctype.h>
37*72383174SJose Luis Duran #include <stdarg.h>
38*72383174SJose Luis Duran #include <stdbool.h>
39*72383174SJose Luis Duran #include <stdio.h>
40*72383174SJose Luis Duran #include <stdlib.h>
41*72383174SJose Luis Duran #include <syslog.h>
42*72383174SJose Luis Duran #include <unistd.h>
43*72383174SJose Luis Duran 
44*72383174SJose Luis Duran #include "ssh.h"
45*72383174SJose Luis Duran #include "packet.h"
46*72383174SJose Luis Duran #include "log.h"
47*72383174SJose Luis Duran #include "misc.h"
48*72383174SJose Luis Duran #include "servconf.h"
49*72383174SJose Luis Duran #include <blocklist.h>
50*72383174SJose Luis Duran #include "blocklist_client.h"
51*72383174SJose Luis Duran 
52*72383174SJose Luis Duran static struct blocklist *blstate = NULL;
53*72383174SJose Luis Duran 
54*72383174SJose Luis Duran /* import */
55*72383174SJose Luis Duran extern ServerOptions options;
56*72383174SJose Luis Duran 
57*72383174SJose Luis Duran /* internal definition from bl.h */
58*72383174SJose Luis Duran struct blocklist *bl_create(bool, char *, void (*)(int, const char *, va_list));
59*72383174SJose Luis Duran 
60*72383174SJose Luis Duran /* impedence match vsyslog() to sshd's internal logging levels */
61*72383174SJose Luis Duran void
im_log(int priority,const char * message,va_list args)62*72383174SJose Luis Duran im_log(int priority, const char *message, va_list args)
63*72383174SJose Luis Duran {
64*72383174SJose Luis Duran 	LogLevel imlevel;
65*72383174SJose Luis Duran 
66*72383174SJose Luis Duran 	switch (priority) {
67*72383174SJose Luis Duran 	case LOG_ERR:
68*72383174SJose Luis Duran 		imlevel = SYSLOG_LEVEL_ERROR;
69*72383174SJose Luis Duran 		break;
70*72383174SJose Luis Duran 	case LOG_DEBUG:
71*72383174SJose Luis Duran 		imlevel = SYSLOG_LEVEL_DEBUG1;
72*72383174SJose Luis Duran 		break;
73*72383174SJose Luis Duran 	case LOG_INFO:
74*72383174SJose Luis Duran 		imlevel = SYSLOG_LEVEL_INFO;
75*72383174SJose Luis Duran 		break;
76*72383174SJose Luis Duran 	default:
77*72383174SJose Luis Duran 		imlevel = SYSLOG_LEVEL_DEBUG2;
78*72383174SJose Luis Duran 	}
79*72383174SJose Luis Duran 	do_log2(imlevel, message, args);
80*72383174SJose Luis Duran }
81*72383174SJose Luis Duran 
82*72383174SJose Luis Duran void
blocklist_init(void)83*72383174SJose Luis Duran blocklist_init(void)
84*72383174SJose Luis Duran {
85*72383174SJose Luis Duran 
86*72383174SJose Luis Duran 	if (options.use_blocklist)
87*72383174SJose Luis Duran 		blstate = bl_create(false, NULL, im_log);
88*72383174SJose Luis Duran }
89*72383174SJose Luis Duran 
90*72383174SJose Luis Duran void
blocklist_notify(struct ssh * ssh,int action,const char * msg)91*72383174SJose Luis Duran blocklist_notify(struct ssh *ssh, int action, const char *msg)
92*72383174SJose Luis Duran {
93*72383174SJose Luis Duran 
94*72383174SJose Luis Duran 	if (blstate != NULL && ssh_packet_connection_is_on_socket(ssh))
95*72383174SJose Luis Duran 		(void)blocklist_r(blstate, action,
96*72383174SJose Luis Duran 		ssh_packet_get_connection_in(ssh), msg);
97*72383174SJose Luis Duran }
98