]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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 | * | |
61569b65 | 24 | * $Id: m_dline.c 3225 2007-03-04 23:42:55Z jilles $ |
212380e3 AC |
25 | */ |
26 | ||
27 | #include "stdinc.h" | |
212380e3 AC |
28 | #include "channel.h" |
29 | #include "class.h" | |
30 | #include "client.h" | |
31 | #include "common.h" | |
4562c604 | 32 | #include "match.h" |
212380e3 AC |
33 | #include "ircd.h" |
34 | #include "hostmask.h" | |
35 | #include "numeric.h" | |
212380e3 AC |
36 | #include "s_conf.h" |
37 | #include "s_newconf.h" | |
4016731b | 38 | #include "logger.h" |
212380e3 AC |
39 | #include "send.h" |
40 | #include "hash.h" | |
41 | #include "s_serv.h" | |
42 | #include "msg.h" | |
43 | #include "parse.h" | |
44 | #include "modules.h" | |
45 | ||
46 | static int mo_dline(struct Client *, struct Client *, int, const char **); | |
f5cb68d5 | 47 | static int me_dline(struct Client *, struct Client *, int, const char **); |
212380e3 | 48 | static int mo_undline(struct Client *, struct Client *, int, const char **); |
f5cb68d5 | 49 | static int me_undline(struct Client *, struct Client *, int, const char **); |
212380e3 AC |
50 | |
51 | struct Message dline_msgtab = { | |
52 | "DLINE", 0, 0, 0, MFLG_SLOW, | |
f5cb68d5 | 53 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_dline, 3}, {mo_dline, 2}} |
212380e3 AC |
54 | }; |
55 | struct Message undline_msgtab = { | |
56 | "UNDLINE", 0, 0, 0, MFLG_SLOW, | |
f5cb68d5 | 57 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_undline, 1}, {mo_undline, 2}} |
212380e3 AC |
58 | }; |
59 | ||
60 | mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL }; | |
61569b65 | 61 | DECLARE_MODULE_AV1(dline, NULL, NULL, dline_clist, NULL, NULL, "$Revision: 3225 $"); |
212380e3 AC |
62 | |
63 | static int valid_comment(char *comment); | |
64 | static int flush_write(struct Client *, FILE *, char *, char *); | |
6f3a09ff | 65 | static int remove_temp_dline(struct ConfItem *); |
5f7a6a3d | 66 | static int apply_dline(struct Client *, const char *, int, char *); |
f5cb68d5 | 67 | static int apply_undline(struct Client *, const char *); |
212380e3 AC |
68 | |
69 | /* mo_dline() | |
70 | * | |
71 | * parv[1] - dline to add | |
72 | * parv[2] - reason | |
73 | */ | |
74 | static int | |
75 | mo_dline(struct Client *client_p, struct Client *source_p, | |
76 | int parc, const char *parv[]) | |
77 | { | |
78 | char def[] = "No Reason"; | |
79 | const char *dlhost; | |
212380e3 | 80 | char *reason = def; |
212380e3 | 81 | char cidr_form_host[HOSTLEN + 1]; |
212380e3 | 82 | int tdline_time = 0; |
f5cb68d5 | 83 | const char *target_server = NULL; |
212380e3 AC |
84 | int loc = 1; |
85 | ||
86 | if(!IsOperK(source_p)) | |
87 | { | |
88 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
89 | me.name, source_p->name, "kline"); | |
90 | return 0; | |
91 | } | |
92 | ||
93 | if((tdline_time = valid_temp_time(parv[loc])) >= 0) | |
94 | loc++; | |
95 | ||
212380e3 | 96 | dlhost = parv[loc]; |
f427c8b0 | 97 | rb_strlcpy(cidr_form_host, dlhost, sizeof(cidr_form_host)); |
212380e3 | 98 | |
f5cb68d5 VY |
99 | loc++; |
100 | ||
101 | if(parc >= loc+2 && !irccmp(parv[loc], "ON")) | |
102 | { | |
103 | target_server = parv[loc+1]; | |
104 | loc += 2; | |
105 | } | |
106 | ||
107 | if(parc >= loc + 1 && !EmptyString(parv[loc])) | |
108 | reason = LOCAL_COPY(parv[loc]); | |
109 | ||
110 | if(target_server != NULL) | |
212380e3 | 111 | { |
f5cb68d5 VY |
112 | sendto_match_servs(source_p, target_server, |
113 | CAP_ENCAP, NOCAPS, | |
114 | "ENCAP %s DLINE %d %s :%s", | |
115 | target_server, tdline_time, dlhost, reason); | |
116 | ||
117 | if(!match(target_server, me.name)) | |
118 | return 0; | |
212380e3 AC |
119 | } |
120 | ||
f5cb68d5 VY |
121 | apply_dline(source_p, dlhost, tdline_time, reason); |
122 | ||
123 | check_dlines(); | |
124 | return 0; | |
125 | } | |
212380e3 | 126 | |
f5cb68d5 VY |
127 | /* mo_undline() |
128 | * | |
129 | * parv[1] = dline to remove | |
130 | */ | |
131 | static int | |
132 | mo_undline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
133 | { | |
134 | const char *cidr; | |
135 | const char *target_server = NULL; | |
136 | ||
137 | if(!IsOperK(source_p)) | |
138 | { | |
139 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
140 | me.name, source_p->name, "unkline"); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | cidr = parv[1]; | |
145 | ||
146 | if(parc >= 4 && !irccmp(parv[2], "ON")) | |
212380e3 | 147 | { |
f5cb68d5 VY |
148 | target_server = parv[3]; |
149 | sendto_match_servs(source_p, target_server, | |
150 | CAP_ENCAP, NOCAPS, | |
151 | "ENCAP %s UNDLINE %s", | |
152 | target_server, cidr); | |
212380e3 | 153 | |
f5cb68d5 | 154 | if(!match(target_server, me.name)) |
212380e3 | 155 | return 0; |
212380e3 AC |
156 | } |
157 | ||
f5cb68d5 VY |
158 | apply_undline(source_p, cidr); |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
5f7a6a3d JT |
163 | static int |
164 | me_dline(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
165 | { | |
166 | int tdline_time = atoi(parv[1]); | |
167 | /* Since this is coming over a server link, assume that the originating | |
168 | * server did the relevant permission/sanity checks... | |
169 | */ | |
170 | ||
171 | if(!IsPerson(source_p)) | |
172 | return 0; | |
173 | ||
174 | if(!find_shared_conf(source_p->username, source_p->host, | |
175 | source_p->servptr->name, tdline_time > 0 ? SHARED_TDLINE : SHARED_PDLINE)) | |
5f7a6a3d | 176 | return 0; |
5f7a6a3d JT |
177 | |
178 | apply_dline(source_p, parv[2], tdline_time, LOCAL_COPY(parv[3])); | |
179 | ||
180 | check_dlines(); | |
181 | return 0; | |
182 | } | |
183 | ||
184 | static int | |
185 | me_undline(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
186 | { | |
187 | if(!IsPerson(source_p)) | |
188 | return 0; | |
189 | ||
190 | if(!find_shared_conf(source_p->username, source_p->host, | |
191 | source_p->servptr->name, SHARED_UNDLINE)) | |
192 | return 0; | |
193 | ||
194 | apply_undline(source_p, parv[1]); | |
195 | ||
196 | return 0; | |
f5cb68d5 VY |
197 | } |
198 | ||
199 | static int | |
200 | apply_dline(struct Client *source_p, const char *dlhost, int tdline_time, char *reason) | |
201 | { | |
202 | struct ConfItem *aconf; | |
203 | char *oper_reason; | |
204 | char dlbuffer[IRCD_BUFSIZE]; | |
205 | const char *current_date; | |
206 | struct rb_sockaddr_storage daddr; | |
207 | int t = AF_INET, ty, b; | |
208 | const char *creason; | |
209 | ||
210 | ty = parse_netmask(dlhost, (struct sockaddr *)&daddr, &b); | |
211 | if(ty == HM_HOST) | |
212 | { | |
213 | sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line", | |
214 | me.name, source_p->name); | |
215 | return 0; | |
216 | } | |
217 | #ifdef RB_IPV6 | |
218 | if(ty == HM_IPV6) | |
219 | t = AF_INET6; | |
220 | else | |
221 | #endif | |
222 | t = AF_INET; | |
223 | ||
224 | /* This means dlines wider than /16 cannot be set remotely */ | |
212380e3 AC |
225 | if(IsOperAdmin(source_p)) |
226 | { | |
f5cb68d5 | 227 | if(b < 8) |
212380e3 | 228 | { |
f5cb68d5 VY |
229 | sendto_one_notice(source_p, |
230 | ":For safety, bitmasks less than 8 require conf access."); | |
212380e3 AC |
231 | return 0; |
232 | } | |
233 | } | |
234 | else | |
235 | { | |
f5cb68d5 | 236 | if(b < 16) |
212380e3 | 237 | { |
f5cb68d5 VY |
238 | sendto_one_notice(source_p, |
239 | ":Dline bitmasks less than 16 are for admins only."); | |
212380e3 AC |
240 | return 0; |
241 | } | |
242 | } | |
243 | ||
f5cb68d5 VY |
244 | if(!valid_comment(reason)) |
245 | { | |
246 | sendto_one(source_p, | |
247 | ":%s NOTICE %s :Invalid character '\"' in comment", | |
248 | me.name, source_p->name); | |
249 | return 0; | |
250 | } | |
251 | ||
252 | /* Look for an oper reason */ | |
253 | if((oper_reason = strchr(reason, '|')) != NULL) | |
254 | { | |
255 | *oper_reason = '\0'; | |
256 | oper_reason++; | |
257 | ||
258 | if(!EmptyString(oper_reason)) | |
259 | aconf->spasswd = rb_strdup(oper_reason); | |
260 | } | |
261 | ||
54ac8b60 VY |
262 | if(ConfigFileEntry.non_redundant_klines) |
263 | { | |
54ac8b60 VY |
264 | if((aconf = find_dline((struct sockaddr *)&daddr, t)) != NULL) |
265 | { | |
266 | int bx; | |
267 | parse_netmask(aconf->host, NULL, &bx); | |
268 | if(b >= bx) | |
269 | { | |
270 | creason = aconf->passwd ? aconf->passwd : "<No Reason>"; | |
271 | if(IsConfExemptKline(aconf)) | |
272 | sendto_one(source_p, | |
273 | ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s", | |
f5cb68d5 | 274 | me.name, source_p->name, dlhost, aconf->host, creason); |
54ac8b60 VY |
275 | else |
276 | sendto_one(source_p, | |
277 | ":%s NOTICE %s :[%s] already D-lined by [%s] - %s", | |
f5cb68d5 | 278 | me.name, source_p->name, dlhost, aconf->host, creason); |
54ac8b60 VY |
279 | return 0; |
280 | } | |
281 | } | |
212380e3 AC |
282 | } |
283 | ||
7df5fcfb | 284 | rb_set_time(); |
212380e3 AC |
285 | current_date = smalldate(); |
286 | ||
287 | aconf = make_conf(); | |
288 | aconf->status = CONF_DLINE; | |
47a03750 | 289 | aconf->host = rb_strdup(dlhost); |
212380e3 | 290 | |
212380e3 AC |
291 | if(tdline_time > 0) |
292 | { | |
7cdb0a09 | 293 | rb_snprintf(dlbuffer, sizeof(dlbuffer), |
212380e3 AC |
294 | "Temporary D-line %d min. - %s (%s)", |
295 | (int) (tdline_time / 60), reason, current_date); | |
47a03750 | 296 | aconf->passwd = rb_strdup(dlbuffer); |
e3354945 | 297 | aconf->hold = rb_current_time() + tdline_time; |
212380e3 AC |
298 | add_temp_dline(aconf); |
299 | ||
300 | if(EmptyString(oper_reason)) | |
301 | { | |
61c096aa | 302 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
212380e3 AC |
303 | "%s added temporary %d min. D-Line for [%s] [%s]", |
304 | get_oper_name(source_p), tdline_time / 60, | |
305 | aconf->host, reason); | |
306 | ilog(L_KLINE, "D %s %d %s %s", | |
307 | get_oper_name(source_p), tdline_time / 60, | |
308 | aconf->host, reason); | |
309 | } | |
310 | else | |
311 | { | |
61c096aa | 312 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
212380e3 AC |
313 | "%s added temporary %d min. D-Line for [%s] [%s|%s]", |
314 | get_oper_name(source_p), tdline_time / 60, | |
315 | aconf->host, reason, oper_reason); | |
316 | ilog(L_KLINE, "D %s %d %s %s|%s", | |
317 | get_oper_name(source_p), tdline_time / 60, | |
318 | aconf->host, reason, oper_reason); | |
319 | } | |
320 | ||
321 | sendto_one(source_p, ":%s NOTICE %s :Added temporary %d min. D-Line for [%s]", | |
322 | me.name, source_p->name, tdline_time / 60, aconf->host); | |
323 | } | |
324 | else | |
325 | { | |
7cdb0a09 | 326 | rb_snprintf(dlbuffer, sizeof(dlbuffer), "%s (%s)", reason, current_date); |
47a03750 | 327 | aconf->passwd = rb_strdup(dlbuffer); |
212380e3 AC |
328 | add_conf_by_address(aconf->host, CONF_DLINE, NULL, aconf); |
329 | write_confitem(DLINE_TYPE, source_p, NULL, aconf->host, reason, | |
330 | oper_reason, current_date, 0); | |
331 | } | |
332 | ||
212380e3 AC |
333 | return 0; |
334 | } | |
335 | ||
212380e3 | 336 | static int |
f5cb68d5 | 337 | apply_undline(struct Client *source_p, const char *cidr) |
212380e3 AC |
338 | { |
339 | FILE *in; | |
340 | FILE *out; | |
341 | char buf[BUFSIZE], buff[BUFSIZE], temppath[BUFSIZE], *p; | |
342 | const char *filename, *found_cidr; | |
212380e3 AC |
343 | int pairme = NO, error_on_write = NO; |
344 | mode_t oldumask; | |
f5cb68d5 | 345 | struct ConfItem *aconf; |
212380e3 AC |
346 | |
347 | if(parse_netmask(cidr, NULL, NULL) == HM_HOST) | |
348 | { | |
5366977b | 349 | sendto_one_notice(source_p, ":Invalid D-Line"); |
212380e3 AC |
350 | return 0; |
351 | } | |
352 | ||
f5cb68d5 VY |
353 | rb_snprintf(temppath, sizeof(temppath), "%s.tmp", ConfigFileEntry.dlinefile); |
354 | ||
6f3a09ff JT |
355 | aconf = find_exact_conf_by_address(cidr, CONF_DLINE, NULL); |
356 | if(aconf == NULL) | |
357 | { | |
358 | sendto_one_notice(source_p, ":No D-Line for %s", cidr); | |
359 | return 0; | |
360 | } | |
361 | ||
f427c8b0 | 362 | rb_strlcpy(buf, aconf->host, sizeof buf); |
6f3a09ff | 363 | if(remove_temp_dline(aconf)) |
212380e3 AC |
364 | { |
365 | sendto_one(source_p, | |
366 | ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines", | |
f5cb68d5 | 367 | me.name, source_p->name, buf); |
212380e3 AC |
368 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
369 | "%s has removed the temporary D-Line for: [%s]", | |
6f3a09ff JT |
370 | get_oper_name(source_p), buf); |
371 | ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), buf); | |
212380e3 AC |
372 | return 0; |
373 | } | |
374 | ||
375 | filename = get_conf_name(DLINE_TYPE); | |
376 | ||
377 | if((in = fopen(filename, "r")) == 0) | |
378 | { | |
f5cb68d5 | 379 | sendto_one(source_p, ":%s NOTICE %s :Cannot open %s", me.name, source_p->name, filename); |
212380e3 AC |
380 | return 0; |
381 | } | |
382 | ||
383 | oldumask = umask(0); | |
384 | if((out = fopen(temppath, "w")) == 0) | |
385 | { | |
f5cb68d5 | 386 | sendto_one(source_p, ":%s NOTICE %s :Cannot open %s", me.name, source_p->name, temppath); |
212380e3 AC |
387 | fclose(in); |
388 | umask(oldumask); | |
389 | return 0; | |
390 | } | |
391 | ||
392 | umask(oldumask); | |
393 | ||
394 | while (fgets(buf, sizeof(buf), in)) | |
395 | { | |
f427c8b0 | 396 | rb_strlcpy(buff, buf, sizeof(buff)); |
212380e3 AC |
397 | |
398 | if((p = strchr(buff, '\n')) != NULL) | |
399 | *p = '\0'; | |
400 | ||
401 | if((*buff == '\0') || (*buff == '#')) | |
402 | { | |
403 | if(!error_on_write) | |
404 | flush_write(source_p, out, buf, temppath); | |
405 | continue; | |
406 | } | |
407 | ||
408 | if((found_cidr = getfield(buff)) == NULL) | |
409 | { | |
410 | if(!error_on_write) | |
411 | flush_write(source_p, out, buf, temppath); | |
412 | continue; | |
413 | } | |
414 | ||
37f6cc05 | 415 | if(irccmp(found_cidr, aconf->host) == 0) |
212380e3 AC |
416 | { |
417 | pairme++; | |
418 | } | |
419 | else | |
420 | { | |
421 | if(!error_on_write) | |
422 | flush_write(source_p, out, buf, temppath); | |
423 | continue; | |
424 | } | |
425 | } | |
426 | ||
427 | fclose(in); | |
428 | if (fclose(out)) | |
429 | error_on_write = YES; | |
430 | ||
431 | if(error_on_write) | |
432 | { | |
433 | sendto_one(source_p, | |
434 | ":%s NOTICE %s :Couldn't write D-line file, aborted", | |
f5cb68d5 | 435 | me.name, source_p->name); |
212380e3 AC |
436 | return 0; |
437 | } | |
438 | else if(!pairme) | |
439 | { | |
37f6cc05 JT |
440 | sendto_one_notice(source_p, ":Cannot find D-Line for %s in file", |
441 | aconf->host); | |
212380e3 AC |
442 | |
443 | if(temppath != NULL) | |
444 | (void) unlink(temppath); | |
445 | ||
446 | return 0; | |
447 | } | |
448 | ||
449 | if (rename(temppath, filename)) | |
450 | { | |
451 | sendto_one_notice(source_p, ":Couldn't rename temp file, aborted"); | |
452 | return 0; | |
453 | } | |
212380e3 | 454 | |
37f6cc05 | 455 | sendto_one(source_p, ":%s NOTICE %s :D-Line for [%s] is removed", me.name, source_p->name, aconf->host); |
61c096aa | 456 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
37f6cc05 JT |
457 | "%s has removed the D-Line for: [%s]", get_oper_name(source_p), aconf->host); |
458 | ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), aconf->host); | |
459 | delete_one_address_conf(aconf->host, aconf); | |
212380e3 AC |
460 | |
461 | return 0; | |
462 | } | |
463 | ||
464 | /* | |
465 | * valid_comment | |
466 | * inputs - pointer to client | |
467 | * - pointer to comment | |
468 | * output - 0 if no valid comment, 1 if valid | |
469 | * side effects - NONE | |
470 | */ | |
471 | static int | |
472 | valid_comment(char *comment) | |
473 | { | |
474 | if(strchr(comment, '"')) | |
475 | return 0; | |
476 | ||
61569b65 JT |
477 | if(strlen(comment) > BANREASONLEN) |
478 | comment[BANREASONLEN] = '\0'; | |
212380e3 AC |
479 | |
480 | return 1; | |
481 | } | |
482 | ||
483 | /* | |
484 | * flush_write() | |
485 | * | |
486 | * inputs - pointer to client structure of oper requesting unkline | |
487 | * - out is the file descriptor | |
488 | * - buf is the buffer to write | |
489 | * - ntowrite is the expected number of character to be written | |
490 | * - temppath is the temporary file name to be written | |
491 | * output - YES for error on write | |
492 | * - NO for success | |
493 | * side effects - if successful, the buf is written to output file | |
494 | * if a write failure happesn, and the file pointed to | |
495 | * by temppath, if its non NULL, is removed. | |
496 | * | |
497 | * The idea here is, to be as robust as possible when writing to the | |
498 | * kline file. | |
499 | * | |
500 | * -Dianora | |
501 | */ | |
502 | static int | |
503 | flush_write(struct Client *source_p, FILE * out, char *buf, char *temppath) | |
504 | { | |
505 | int error_on_write = (fputs(buf, out) < 0) ? YES : NO; | |
506 | ||
507 | if(error_on_write) | |
508 | { | |
5366977b | 509 | sendto_one_notice(source_p, ":Unable to write to %s", temppath); |
212380e3 AC |
510 | fclose(out); |
511 | if(temppath != NULL) | |
512 | (void) unlink(temppath); | |
513 | } | |
514 | return (error_on_write); | |
515 | } | |
516 | ||
517 | /* remove_temp_dline() | |
518 | * | |
6f3a09ff | 519 | * inputs - confitem to undline |
212380e3 AC |
520 | * outputs - |
521 | * side effects - tries to undline anything that matches | |
522 | */ | |
523 | static int | |
6f3a09ff | 524 | remove_temp_dline(struct ConfItem *aconf) |
212380e3 | 525 | { |
5b96d9a6 | 526 | rb_dlink_node *ptr; |
212380e3 AC |
527 | int i; |
528 | ||
212380e3 AC |
529 | for (i = 0; i < LAST_TEMP_TYPE; i++) |
530 | { | |
5b96d9a6 | 531 | RB_DLINK_FOREACH(ptr, temp_dlines[i].head) |
212380e3 | 532 | { |
6f3a09ff | 533 | if (aconf == ptr->data) |
212380e3 | 534 | { |
555ac41f | 535 | rb_dlinkDestroy(ptr, &temp_dlines[i]); |
212380e3 AC |
536 | delete_one_address_conf(aconf->host, aconf); |
537 | return YES; | |
538 | } | |
539 | } | |
540 | } | |
541 | ||
542 | return NO; | |
543 | } |