]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * m_dline.c: Bans/unbans a user. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
23 | */ | |
24 | ||
25 | #include "stdinc.h" | |
26 | #include "channel.h" | |
27 | #include "class.h" | |
28 | #include "client.h" | |
29 | #include "match.h" | |
30 | #include "ircd.h" | |
31 | #include "hostmask.h" | |
32 | #include "numeric.h" | |
33 | #include "s_conf.h" | |
34 | #include "s_newconf.h" | |
35 | #include "logger.h" | |
36 | #include "send.h" | |
37 | #include "hash.h" | |
38 | #include "s_serv.h" | |
39 | #include "msg.h" | |
40 | #include "parse.h" | |
41 | #include "modules.h" | |
42 | #include "bandbi.h" | |
43 | #include "operhash.h" | |
44 | ||
45 | static const char dline_desc[] = "Provides the DLINE facility to ban users via IP address"; | |
46 | ||
47 | static void mo_dline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
48 | static void me_dline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
49 | static void mo_undline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
50 | static void me_undline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
51 | ||
52 | struct Message dline_msgtab = { | |
53 | "DLINE", 0, 0, 0, 0, | |
54 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_dline, 4}, {mo_dline, 2}} | |
55 | }; | |
56 | ||
57 | struct Message undline_msgtab = { | |
58 | "UNDLINE", 0, 0, 0, 0, | |
59 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_undline, 2}, {mo_undline, 2}} | |
60 | }; | |
61 | ||
62 | mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL }; | |
63 | ||
64 | DECLARE_MODULE_AV2(dline, NULL, NULL, dline_clist, NULL, NULL, NULL, NULL, dline_desc); | |
65 | ||
66 | static bool remove_temp_dline(struct ConfItem *); | |
67 | static void apply_dline(struct Client *, const char *, int, char *); | |
68 | static void apply_undline(struct Client *, const char *); | |
69 | ||
70 | /* mo_dline() | |
71 | * | |
72 | * parv[1] - dline to add | |
73 | * parv[2] - reason | |
74 | */ | |
75 | static void | |
76 | mo_dline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
77 | { | |
78 | char def[] = "No Reason"; | |
79 | const char *dlhost; | |
80 | char *reason = def; | |
81 | int tdline_time = 0; | |
82 | const char *target_server = NULL; | |
83 | int loc = 1; | |
84 | ||
85 | if(!IsOperK(source_p)) | |
86 | { | |
87 | sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "kline"); | |
88 | return; | |
89 | } | |
90 | ||
91 | if((tdline_time = valid_temp_time(parv[loc])) >= 0) | |
92 | loc++; | |
93 | ||
94 | if (loc >= parc) | |
95 | { | |
96 | sendto_one_notice(source_p, ":Need an IP to D-Line"); | |
97 | return; | |
98 | } | |
99 | ||
100 | dlhost = parv[loc]; | |
101 | loc++; | |
102 | ||
103 | /* would break the protocol */ | |
104 | if (*dlhost == ':') | |
105 | { | |
106 | sendto_one_notice(source_p, ":Invalid D-Line [%s] - IP cannot start with :", dlhost); | |
107 | return; | |
108 | } | |
109 | ||
110 | int ty = parse_netmask_strict(dlhost, NULL, NULL); | |
111 | if (ty != HM_IPV4 && ty != HM_IPV6) | |
112 | { | |
113 | sendto_one_notice(source_p, ":Invalid D-Line [%s] - doesn't look like IP[/cidr]", dlhost); | |
114 | return; | |
115 | } | |
116 | ||
117 | if(parc >= loc + 2 && !irccmp(parv[loc], "ON")) | |
118 | { | |
119 | if(!IsOperRemoteBan(source_p)) | |
120 | { | |
121 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
122 | me.name, source_p->name, "remoteban"); | |
123 | return; | |
124 | } | |
125 | ||
126 | target_server = parv[loc + 1]; | |
127 | loc += 2; | |
128 | } | |
129 | ||
130 | if(parc >= loc + 1 && !EmptyString(parv[loc])) | |
131 | reason = LOCAL_COPY(parv[loc]); | |
132 | ||
133 | if(target_server != NULL) | |
134 | { | |
135 | sendto_match_servs(source_p, target_server, | |
136 | CAP_ENCAP, NOCAPS, | |
137 | "ENCAP %s DLINE %d %s :%s", | |
138 | target_server, tdline_time, dlhost, reason); | |
139 | ||
140 | if(!match(target_server, me.name)) | |
141 | return; | |
142 | } | |
143 | ||
144 | apply_dline(source_p, dlhost, tdline_time, reason); | |
145 | ||
146 | check_dlines(); | |
147 | } | |
148 | ||
149 | /* mo_undline() | |
150 | * | |
151 | * parv[1] = dline to remove | |
152 | */ | |
153 | static void | |
154 | mo_undline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
155 | { | |
156 | const char *cidr; | |
157 | const char *target_server = NULL; | |
158 | ||
159 | if(!IsOperK(source_p)) | |
160 | { | |
161 | sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "unkline"); | |
162 | return; | |
163 | } | |
164 | ||
165 | cidr = parv[1]; | |
166 | ||
167 | if(parc >= 4 && !irccmp(parv[2], "ON")) | |
168 | { | |
169 | if(!IsOperRemoteBan(source_p)) | |
170 | { | |
171 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
172 | me.name, source_p->name, "remoteban"); | |
173 | return; | |
174 | } | |
175 | ||
176 | target_server = parv[3]; | |
177 | sendto_match_servs(source_p, target_server, | |
178 | CAP_ENCAP, NOCAPS, "ENCAP %s UNDLINE %s", target_server, cidr); | |
179 | ||
180 | if(!match(target_server, me.name)) | |
181 | return; | |
182 | } | |
183 | ||
184 | apply_undline(source_p, cidr); | |
185 | } | |
186 | ||
187 | static void | |
188 | me_dline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
189 | { | |
190 | int tdline_time = atoi(parv[1]); | |
191 | /* Since this is coming over a server link, assume that the originating | |
192 | * server did the relevant permission/sanity checks... | |
193 | */ | |
194 | ||
195 | if(!IsPerson(source_p)) | |
196 | return; | |
197 | ||
198 | apply_dline(source_p, parv[2], tdline_time, LOCAL_COPY(parv[3])); | |
199 | ||
200 | check_dlines(); | |
201 | } | |
202 | ||
203 | static void | |
204 | me_undline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
205 | { | |
206 | if(!IsPerson(source_p)) | |
207 | return; | |
208 | ||
209 | apply_undline(source_p, parv[1]); | |
210 | } | |
211 | ||
212 | static void | |
213 | apply_dline(struct Client *source_p, const char *dlhost, int tdline_time, char *reason) | |
214 | { | |
215 | struct ConfItem *aconf; | |
216 | char *oper_reason; | |
217 | struct rb_sockaddr_storage daddr; | |
218 | int t = AF_INET, ty, b; | |
219 | const char *creason; | |
220 | ||
221 | ty = parse_netmask_strict(dlhost, &daddr, &b); | |
222 | if(ty != HM_IPV4 && ty != HM_IPV6) | |
223 | { | |
224 | sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line", me.name, source_p->name); | |
225 | return; | |
226 | } | |
227 | if(ty == HM_IPV6) | |
228 | t = AF_INET6; | |
229 | else | |
230 | t = AF_INET; | |
231 | ||
232 | /* This means dlines wider than /16 cannot be set remotely */ | |
233 | if(IsOperAdmin(source_p)) | |
234 | { | |
235 | if(b < 8) | |
236 | { | |
237 | sendto_one_notice(source_p, | |
238 | ":For safety, bitmasks less than 8 require conf access."); | |
239 | return; | |
240 | } | |
241 | } | |
242 | else | |
243 | { | |
244 | if(b < 16) | |
245 | { | |
246 | sendto_one_notice(source_p, | |
247 | ":Dline bitmasks less than 16 are for admins only."); | |
248 | return; | |
249 | } | |
250 | } | |
251 | ||
252 | if(ConfigFileEntry.non_redundant_klines) | |
253 | { | |
254 | if((aconf = find_dline((struct sockaddr *) &daddr, t)) != NULL) | |
255 | { | |
256 | int bx; | |
257 | int masktype = parse_netmask_strict(aconf->host, NULL, &bx); | |
258 | ||
259 | if (masktype != HM_ERROR && b >= bx) | |
260 | { | |
261 | creason = aconf->passwd ? aconf->passwd : "<No Reason>"; | |
262 | if(IsConfExemptKline(aconf)) | |
263 | sendto_one(source_p, | |
264 | ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s", | |
265 | me.name, source_p->name, dlhost, aconf->host, | |
266 | creason); | |
267 | else | |
268 | sendto_one(source_p, | |
269 | ":%s NOTICE %s :[%s] already D-lined by [%s] - %s", | |
270 | me.name, source_p->name, dlhost, aconf->host, | |
271 | creason); | |
272 | return; | |
273 | } | |
274 | } | |
275 | } | |
276 | ||
277 | rb_set_time(); | |
278 | ||
279 | aconf = make_conf(); | |
280 | aconf->status = CONF_DLINE; | |
281 | aconf->created = rb_current_time(); | |
282 | aconf->host = rb_strdup(dlhost); | |
283 | aconf->info.oper = operhash_add(get_oper_name(source_p)); | |
284 | ||
285 | if(strlen(reason) > BANREASONLEN) | |
286 | reason[BANREASONLEN] = '\0'; | |
287 | ||
288 | /* Look for an oper reason */ | |
289 | if((oper_reason = strchr(reason, '|')) != NULL) | |
290 | { | |
291 | *oper_reason = '\0'; | |
292 | oper_reason++; | |
293 | ||
294 | if(!EmptyString(oper_reason)) | |
295 | aconf->spasswd = rb_strdup(oper_reason); | |
296 | } | |
297 | ||
298 | aconf->passwd = rb_strdup(reason); | |
299 | ||
300 | if(tdline_time > 0) | |
301 | { | |
302 | aconf->hold = rb_current_time() + tdline_time; | |
303 | add_temp_dline(aconf); | |
304 | ||
305 | if(EmptyString(oper_reason)) | |
306 | { | |
307 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
308 | "%s added temporary %d min. D-Line for [%s] [%s]", | |
309 | get_oper_name(source_p), tdline_time / 60, | |
310 | aconf->host, reason); | |
311 | ilog(L_KLINE, "D %s %d %s %s", | |
312 | get_oper_name(source_p), tdline_time / 60, aconf->host, reason); | |
313 | } | |
314 | else | |
315 | { | |
316 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
317 | "%s added temporary %d min. D-Line for [%s] [%s|%s]", | |
318 | get_oper_name(source_p), tdline_time / 60, | |
319 | aconf->host, reason, oper_reason); | |
320 | ilog(L_KLINE, "D %s %d %s %s|%s", | |
321 | get_oper_name(source_p), tdline_time / 60, | |
322 | aconf->host, reason, oper_reason); | |
323 | } | |
324 | ||
325 | sendto_one(source_p, ":%s NOTICE %s :Added temporary %d min. D-Line for [%s]", | |
326 | me.name, source_p->name, tdline_time / 60, aconf->host); | |
327 | } | |
328 | else | |
329 | { | |
330 | add_conf_by_address(aconf->host, CONF_DLINE, NULL, NULL, aconf); | |
331 | ||
332 | bandb_add(BANDB_DLINE, source_p, aconf->host, NULL, | |
333 | reason, EmptyString(aconf->spasswd) ? NULL : aconf->spasswd, 0); | |
334 | ||
335 | if(EmptyString(oper_reason)) | |
336 | { | |
337 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
338 | "%s added D-Line for [%s] [%s]", | |
339 | get_oper_name(source_p), aconf->host, reason); | |
340 | ilog(L_KLINE, "D %s 0 %s %s", | |
341 | get_oper_name(source_p), aconf->host, reason); | |
342 | } | |
343 | else | |
344 | { | |
345 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
346 | "%s added D-Line for [%s] [%s|%s]", | |
347 | get_oper_name(source_p), aconf->host, reason, oper_reason); | |
348 | ilog(L_KLINE, "D %s 0 %s %s|%s", | |
349 | get_oper_name(source_p), | |
350 | aconf->host, reason, oper_reason); | |
351 | } | |
352 | } | |
353 | } | |
354 | ||
355 | static void | |
356 | apply_undline(struct Client *source_p, const char *cidr) | |
357 | { | |
358 | char buf[BUFSIZE]; | |
359 | struct ConfItem *aconf; | |
360 | ||
361 | int masktype = parse_netmask(cidr, NULL, NULL); | |
362 | ||
363 | if(masktype != HM_IPV4 && masktype != HM_IPV6) | |
364 | { | |
365 | sendto_one_notice(source_p, ":Invalid D-Line [%s] - doesn't look like IP[/cidr]", cidr); | |
366 | return; | |
367 | } | |
368 | ||
369 | aconf = find_exact_conf_by_address(cidr, CONF_DLINE, NULL); | |
370 | if(aconf == NULL) | |
371 | { | |
372 | sendto_one_notice(source_p, ":No D-Line for %s", cidr); | |
373 | return; | |
374 | } | |
375 | ||
376 | rb_strlcpy(buf, aconf->host, sizeof buf); | |
377 | if(remove_temp_dline(aconf)) | |
378 | { | |
379 | sendto_one(source_p, | |
380 | ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines", | |
381 | me.name, source_p->name, buf); | |
382 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
383 | "%s has removed the temporary D-Line for: [%s]", | |
384 | get_oper_name(source_p), buf); | |
385 | ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), buf); | |
386 | return; | |
387 | } | |
388 | ||
389 | bandb_del(BANDB_DLINE, aconf->host, NULL); | |
390 | ||
391 | sendto_one(source_p, ":%s NOTICE %s :D-Line for [%s] is removed", me.name, source_p->name, | |
392 | aconf->host); | |
393 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s has removed the D-Line for: [%s]", | |
394 | get_oper_name(source_p), aconf->host); | |
395 | ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), aconf->host); | |
396 | delete_one_address_conf(aconf->host, aconf); | |
397 | } | |
398 | ||
399 | /* remove_temp_dline() | |
400 | * | |
401 | * inputs - confitem to undline | |
402 | * outputs - true if removed, false otherwise. | |
403 | * side effects - tries to undline anything that matches | |
404 | */ | |
405 | static bool | |
406 | remove_temp_dline(struct ConfItem *aconf) | |
407 | { | |
408 | rb_dlink_node *ptr; | |
409 | int i; | |
410 | ||
411 | for(i = 0; i < LAST_TEMP_TYPE; i++) | |
412 | { | |
413 | RB_DLINK_FOREACH(ptr, temp_dlines[i].head) | |
414 | { | |
415 | if(aconf == ptr->data) | |
416 | { | |
417 | rb_dlinkDestroy(ptr, &temp_dlines[i]); | |
418 | delete_one_address_conf(aconf->host, aconf); | |
419 | return true; | |
420 | } | |
421 | } | |
422 | } | |
423 | ||
424 | return false; | |
425 | } |