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