1 /* Blacklist module for srvx 1.x
2 * Copyright 2007 Michael Poole <mdpoole@troilus.org>
4 * This file is part of srvx.
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 const char *blacklist_module_deps[] = { NULL };
28 static struct log_type *bl_log;
29 static dict_t blacklist_hosts; /* maps IPs or hostnames to reasons from blacklist_reasons */
30 static dict_t blacklist_reasons; /* maps strings to themselves (poor man's data sharing) */
33 unsigned long gline_duration;
37 blacklist_check_user(struct userNode *user)
41 char ip[IRC_NTOP_MAX_SIZE];
43 irc_ntop(ip, sizeof(ip), &user->ip);
44 reason = dict_find(blacklist_hosts, host = ip, NULL);
46 reason = dict_find(blacklist_hosts, host = user->hostname, NULL);
50 target = alloca(strlen(host) + 3);
53 strcpy(target + 2, host);
54 gline_add(self->name, target, conf.gline_duration, reason, now, now, 1);
60 blacklist_load_file(const char *filename, const char *default_reason)
72 default_reason = "client is blacklisted";
73 file = fopen(filename, "r");
75 log_module(bl_log, LOG_ERROR, "Unable to open %s for reading: %s", filename, strerror(errno));
78 log_module(bl_log, LOG_DEBUG, "Loading blacklist from %s.", filename);
79 while (fgets(linebuf, sizeof(linebuf), file)) {
80 /* Trim whitespace from end of line. */
81 len = strlen(linebuf);
82 while (isspace(linebuf[len-1]))
83 linebuf[--len] = '\0';
85 /* Figure out which reason string we should use. */
86 reason = default_reason;
87 sep = strchr(linebuf, ' ');
96 /* See if the reason string is already known. */
97 mapped_reason = dict_find(blacklist_reasons, reason, NULL);
99 mapped_reason = strdup(reason);
100 dict_insert(blacklist_reasons, mapped_reason, (char*)mapped_reason);
103 /* Store the blacklist entry. */
104 dict_insert(blacklist_hosts, strdup(linebuf), mapped_reason);
110 blacklist_conf_read(void)
116 dict_delete(blacklist_hosts);
117 blacklist_hosts = dict_new();
118 dict_set_free_keys(blacklist_hosts, free);
120 dict_delete(blacklist_reasons);
121 blacklist_reasons = dict_new();
122 dict_set_free_keys(blacklist_reasons, free);
124 node = conf_get_data("modules/blacklist", RECDB_OBJECT);
128 str1 = database_get_data(node, "file", RECDB_QSTRING);
129 str2 = database_get_data(node, "file_reason", RECDB_QSTRING);
130 blacklist_load_file(str1, str2);
132 str1 = database_get_data(node, "gline_duration", RECDB_QSTRING);
135 conf.gline_duration = ParseInterval(str1);
139 blacklist_cleanup(void)
141 dict_delete(blacklist_hosts);
142 dict_delete(blacklist_reasons);
148 bl_log = log_register_type("Blacklist", "file:blacklist.log");
149 conf_register_reload(blacklist_conf_read);
150 reg_new_user_func(blacklist_check_user);
151 reg_exit_func(blacklist_cleanup);
156 blacklist_finalize(void)