-
Notifications
You must be signed in to change notification settings - Fork 0
/
tor.c
186 lines (141 loc) · 4.95 KB
/
tor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* syn: a utility bot to manage IRC network access
* Copyright (C) 2009-2016 Stephen Bennett
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "atheme.h"
#include "syn.h"
DECLARE_MODULE_V1
(
"syn/tor", false, _modinit, _moddeinit,
"$Revision$",
"Stephen Bennett <stephen -at- freenode.net>"
);
static void tor_kline_check(void *);
static void tor_newuser(hook_user_nick_t *data);
static void syn_cmd_checktor(sourceinfo_t*, int, char**);
command_t syn_checktor = { "CHECKTOR", N_("Checks for tor nodes on a given IP."), "syn:general", 1, syn_cmd_checktor };
static void load_tor_list();
mowgli_patricia_t *torlist;
unsigned int kline_duration;
char *kline_reason;
const char *default_kline_reason = "Tor access to freenode is hidden service only. Mail [email protected] with questions.";
const unsigned int default_kline_duration = 24 * 3600;
mowgli_eventloop_timer_t *update_tor_timer;
void _modinit(module_t *m)
{
user_t *u;
mowgli_patricia_iteration_state_t state;
use_syn_main_symbols(m);
use_syn_kline_symbols(m);
service_named_bind_command("syn", &syn_checktor);
hook_add_event("user_add");
hook_add_user_add(tor_newuser);
hook_add_event("syn_kline_check");
hook_add_hook("syn_kline_check", tor_kline_check);
update_tor_timer = mowgli_timer_add(base_eventloop, "update_tor_list", load_tor_list, NULL, 120);
load_tor_list();
add_dupstr_conf_item("TOR_KLINE_REASON", &syn->conf_table, 0, &kline_reason, default_kline_reason);
add_uint_conf_item("TOR_KLINE_DURATION", &syn->conf_table, 0, &kline_duration, 1, (unsigned int)-1, default_kline_duration);
MOWGLI_PATRICIA_FOREACH(u, &state, userlist)
{
hook_user_nick_t data = { .u = u };
tor_newuser(&data);
}
}
void _moddeinit(module_unload_intent_t intent)
{
mowgli_patricia_destroy(torlist, NULL, NULL);
del_conf_item("TOR_KLINE_DURATION", &syn->conf_table);
del_conf_item("TOR_KLINE_REASON", &syn->conf_table);
service_named_unbind_command("syn", &syn_checktor);
hook_del_user_add(tor_newuser);
hook_del_hook("syn_kline_check", tor_kline_check);
mowgli_timer_destroy(base_eventloop, update_tor_timer);
}
static void tor_newuser(hook_user_nick_t *data)
{
user_t *u = data->u;
/* If the user has already been killed, don't try to do anything */
if (!u)
return;
if (is_internal_client(u) || !u->ip || *u->ip == '\0')
return;
syn_kline_check_data_t d = { u->ip, u, 0 };
tor_kline_check(&d);
}
static void tor_kline_check(void *v)
{
syn_kline_check_data_t *d = v;
if (!d->ip)
return;
if (NULL == mowgli_patricia_retrieve(torlist, d->ip))
return;
// IP was listed in the tor list.
syn_report("K:lining tor node %s (user %s)", d->ip, d->u->nick);
syn_kline(d->ip, kline_duration, kline_reason);
d->added = 1;
}
static void load_tor_list()
{
FILE *f;
char line[BUFSIZE];
if (torlist)
mowgli_patricia_destroy(torlist, NULL, NULL);
torlist = mowgli_patricia_create(noopcanon);
f = fopen(DATADIR "/tor.list", "r");
if (!f)
{
slog(LG_DEBUG, "load_tor_list(): cannot open tor node list: %s", strerror(errno));
return;
}
while (fgets(line, BUFSIZE, f))
{
strip(line);
mowgli_patricia_add(torlist, line, (void*)1);
}
fclose(f);
// List might have new entries. Check them.
user_t *u;
mowgli_patricia_iteration_state_t state;
MOWGLI_PATRICIA_FOREACH(u, &state, userlist)
{
hook_user_nick_t data = { .u = u };
tor_newuser(&data);
}
}
static void syn_cmd_checktor(sourceinfo_t *si, int parc, char **parv)
{
if (!parv[0])
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CHECKTOR");
command_fail(si, fault_needmoreparams, _("Syntax: CHECKTOR <IP|user>"));
return;
}
void *p;
const char *test = parv[0];
if (strchr(parv[0], '.') == NULL)
{
user_t *target_u = user_find_named(parv[0]);
if (!target_u)
{
command_fail(si, fault_nosuch_target, _("\2%s\2 is not online."), parv[0]);
return;
}
test = target_u->ip;
}
p = mowgli_patricia_retrieve(torlist, test);
command_success_nodata(si, _("\2%s\2 is %s as a tor node."), test, p != NULL ? "listed" : "not listed");
}