]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_xline.c
Automated merge with http://angelforce.ru:8000/
[irc/rqf/shadowircd.git] / modules / m_xline.c
1 /* modules/m_xline.c
2 *
3 * Copyright (C) 2002-2003 Lee Hardy <lee@leeh.co.uk>
4 * Copyright (C) 2002-2005 ircd-ratbox development team
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1.Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2.Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3.The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $Id: m_xline.c 3161 2007-01-25 07:23:01Z nenolod $
31 */
32
33 #include "stdinc.h"
34 #include "send.h"
35 #include "channel.h"
36 #include "client.h"
37 #include "common.h"
38 #include "config.h"
39 #include "class.h"
40 #include "ircd.h"
41 #include "numeric.h"
42 #include "s_log.h"
43 #include "s_serv.h"
44 #include "whowas.h"
45 #include "irc_string.h"
46 #include "sprintf_irc.h"
47 #include "hash.h"
48 #include "msg.h"
49 #include "parse.h"
50 #include "modules.h"
51 #include "s_conf.h"
52 #include "s_newconf.h"
53 #include "reject.h"
54
55 static int mo_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
56 static int ms_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
57 static int me_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
58 static int mo_unxline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
59 static int ms_unxline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
60 static int me_unxline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
61
62 struct Message xline_msgtab = {
63 "XLINE", 0, 0, 0, MFLG_SLOW,
64 {mg_unreg, mg_not_oper, {ms_xline, 5}, {ms_xline, 5}, {me_xline, 5}, {mo_xline, 3}}
65 };
66 struct Message unxline_msgtab = {
67 "UNXLINE", 0, 0, 0, MFLG_SLOW,
68 {mg_unreg, mg_not_oper, {ms_unxline, 3}, {ms_unxline, 3}, {me_unxline, 2}, {mo_unxline, 2}}
69 };
70
71 mapi_clist_av1 xline_clist[] = { &xline_msgtab, &unxline_msgtab, NULL };
72 DECLARE_MODULE_AV1(xline, NULL, NULL, xline_clist, NULL, NULL, "$Revision: 3161 $");
73
74 static int valid_xline(struct Client *, const char *, const char *);
75 static void apply_xline(struct Client *client_p, const char *name,
76 const char *reason, int temp_time);
77 static void write_xline(struct Client *source_p, struct ConfItem *aconf);
78 static void propagate_xline(struct Client *source_p, const char *target,
79 int temp_time, const char *name,
80 const char *type, const char *reason);
81 static void cluster_xline(struct Client *source_p, int temp_time,
82 const char *name, const char *reason);
83
84 static void handle_remote_xline(struct Client *source_p, int temp_time,
85 const char *name, const char *reason);
86 static void handle_remote_unxline(struct Client *source_p, const char *name);
87
88 static void remove_xline(struct Client *source_p, const char *name);
89 static int remove_xline_from_file(struct Client *source_p, const char *gecos);
90
91
92 /* m_xline()
93 *
94 * parv[1] - thing to xline
95 * parv[2] - optional type/reason
96 * parv[3] - reason
97 */
98 static int
99 mo_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
100 {
101 struct ConfItem *aconf;
102 const char *name;
103 const char *reason;
104 const char *target_server = NULL;
105 int temp_time;
106 int loc = 1;
107
108 if(!IsOperXline(source_p))
109 {
110 sendto_one(source_p, form_str(ERR_NOPRIVS),
111 me.name, source_p->name, "xline");
112 return 0;
113 }
114
115 if((temp_time = valid_temp_time(parv[loc])) >= 0)
116 loc++;
117 /* we just set temp_time to -1! */
118 else
119 temp_time = 0;
120
121 name = parv[loc];
122 loc++;
123
124 /* XLINE <gecos> ON <server> :<reason> */
125 if(parc >= loc+2 && !irccmp(parv[loc], "ON"))
126 {
127 if(!IsOperRemoteBan(source_p))
128 {
129 sendto_one(source_p, form_str(ERR_NOPRIVS),
130 me.name, source_p->name, "remoteban");
131 return 0;
132 }
133
134 target_server = parv[loc+1];
135 loc += 2;
136 }
137
138 if(parc <= loc || EmptyString(parv[loc]))
139 {
140 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
141 me.name, source_p->name, "XLINE");
142 return 0;
143 }
144
145 reason = parv[loc];
146
147 if(target_server != NULL)
148 {
149 propagate_xline(source_p, target_server, temp_time,
150 name, "2", reason);
151
152 if(!match(target_server, me.name))
153 return 0;
154 }
155 else if(rb_dlink_list_length(&cluster_conf_list) > 0)
156 cluster_xline(source_p, temp_time, name, reason);
157
158 if((aconf = find_xline_mask(name)) != NULL)
159 {
160 sendto_one(source_p, ":%s NOTICE %s :[%s] already X-Lined by [%s] - %s",
161 me.name, source_p->name, name, aconf->name, aconf->passwd);
162 return 0;
163 }
164
165 if(!valid_xline(source_p, name, reason))
166 return 0;
167
168 apply_xline(source_p, name, reason, temp_time);
169
170 return 0;
171 }
172
173 /* ms_xline()
174 *
175 * handles a remote xline
176 */
177 static int
178 ms_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
179 {
180 /* parv[0] parv[1] parv[2] parv[3] parv[4]
181 * oper target serv xline type reason
182 */
183 propagate_xline(source_p, parv[1], 0, parv[2], parv[3], parv[4]);
184
185 if(!IsPerson(source_p))
186 return 0;
187
188 /* destined for me? */
189 if(!match(parv[1], me.name))
190 return 0;
191
192 handle_remote_xline(source_p, 0, parv[2], parv[4]);
193 return 0;
194 }
195
196 static int
197 me_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
198 {
199 /* time name type :reason */
200 if(!IsPerson(source_p))
201 return 0;
202
203 handle_remote_xline(source_p, atoi(parv[1]), parv[2], parv[4]);
204 return 0;
205 }
206
207 static void
208 handle_remote_xline(struct Client *source_p, int temp_time,
209 const char *name, const char *reason)
210 {
211 struct ConfItem *aconf;
212
213 if(!find_shared_conf(source_p->username, source_p->host,
214 source_p->servptr->name,
215 (temp_time > 0) ? SHARED_TXLINE : SHARED_PXLINE))
216 return;
217
218 if(!valid_xline(source_p, name, reason))
219 return;
220
221 /* already xlined */
222 if((aconf = find_xline_mask(name)) != NULL)
223 {
224 sendto_one_notice(source_p, ":[%s] already X-Lined by [%s] - %s", name, aconf->name, aconf->passwd);
225 return;
226 }
227
228 apply_xline(source_p, name, reason, temp_time);
229 }
230
231 /* valid_xline()
232 *
233 * inputs - client xlining, gecos, reason and whether to warn
234 * outputs -
235 * side effects - checks the xline for validity, erroring if needed
236 */
237 static int
238 valid_xline(struct Client *source_p, const char *gecos,
239 const char *reason)
240 {
241 if(EmptyString(reason))
242 {
243 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
244 get_id(&me, source_p),
245 get_id(source_p, source_p), "XLINE");
246 return 0;
247 }
248
249 if(strchr(reason, ':') != NULL)
250 {
251 sendto_one_notice(source_p,
252 ":Invalid character ':' in comment");
253 return 0;
254 }
255
256 if(strchr(reason, '"'))
257 {
258 sendto_one_notice(source_p,
259 ":Invalid character '\"' in comment");
260 return 0;
261 }
262
263 if(!valid_wild_card_simple(gecos))
264 {
265 sendto_one_notice(source_p,
266 ":Please include at least %d non-wildcard "
267 "characters with the xline",
268 ConfigFileEntry.min_nonwildcard_simple);
269 return 0;
270 }
271
272 return 1;
273 }
274
275 void
276 apply_xline(struct Client *source_p, const char *name, const char *reason,
277 int temp_time)
278 {
279 struct ConfItem *aconf;
280
281 aconf = make_conf();
282 aconf->status = CONF_XLINE;
283
284 if(strstr(name, "\\s"))
285 {
286 char *tmp = LOCAL_COPY(name);
287 char *orig = tmp;
288 char *new = tmp;
289
290 while(*orig)
291 {
292 if(*orig == '\\' && *(orig + 1) != '\0')
293 {
294 if(*(orig + 1) == 's')
295 {
296 *new++ = ' ';
297 orig += 2;
298 }
299 /* otherwise skip that and the escaped
300 * character after it, so we dont mistake
301 * \\s as \s --fl
302 */
303 else
304 {
305 *new++ = *orig++;
306 *new++ = *orig++;
307 }
308 }
309 else
310 *new++ = *orig++;
311 }
312
313 *new = '\0';
314 aconf->name = rb_strdup(tmp);
315 }
316 else
317 aconf->name = rb_strdup(name);
318
319 aconf->passwd = rb_strdup(reason);
320 collapse(aconf->name);
321
322 if(temp_time > 0)
323 {
324 aconf->hold = rb_current_time() + temp_time;
325
326 sendto_realops_snomask(SNO_GENERAL, L_ALL,
327 "%s added temporary %d min. X-Line for [%s] [%s]",
328 get_oper_name(source_p), temp_time / 60,
329 aconf->name, reason);
330 ilog(L_KLINE, "X %s %d %s %s",
331 get_oper_name(source_p), temp_time / 60,
332 name, reason);
333 sendto_one_notice(source_p, ":Added temporary %d min. X-Line [%s]",
334 temp_time / 60, aconf->name);
335 }
336 else
337 {
338 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s added X-Line for [%s] [%s]",
339 get_oper_name(source_p),
340 aconf->name, aconf->passwd);
341 sendto_one_notice(source_p, ":Added X-Line for [%s] [%s]",
342 aconf->name, aconf->passwd);
343 write_xline(source_p, aconf);
344 ilog(L_KLINE, "X %s 0 %s %s",
345 get_oper_name(source_p), name, reason);
346 }
347
348 rb_dlinkAddAlloc(aconf, &xline_conf_list);
349 check_xlines();
350 }
351
352 /* write_xline()
353 *
354 * inputs - gecos, reason, xline type
355 * outputs - writes an xline to the config
356 * side effects -
357 */
358 static void
359 write_xline(struct Client *source_p, struct ConfItem *aconf)
360 {
361 char buffer[BUFSIZE * 2];
362 FILE *out;
363 const char *filename;
364
365 filename = ConfigFileEntry.xlinefile;
366
367 if((out = fopen(filename, "a")) == NULL)
368 {
369 sendto_realops_snomask(SNO_GENERAL, L_ALL, "*** Problem opening %s ", filename);
370 sendto_one_notice(source_p, ":*** Problem opening file, xline added temporarily only");
371 return;
372 }
373
374 rb_sprintf(buffer, "\"%s\",\"0\",\"%s\",\"%s\",%ld\n",
375 aconf->name, aconf->passwd,
376 get_oper_name(source_p), (long) rb_current_time());
377
378 if(fputs(buffer, out) == -1)
379 {
380 sendto_realops_snomask(SNO_GENERAL, L_ALL, "*** Problem writing to %s", filename);
381 sendto_one_notice(source_p, ":*** Problem writing to file, xline added temporarily only");
382 fclose(out);
383 return;
384 }
385
386 if(fclose(out))
387 {
388 sendto_realops_snomask(SNO_GENERAL, L_ALL, "*** Problem writing to %s", filename);
389 sendto_one_notice(source_p, ":*** Problem writing to file, xline added temporarily only");
390 return;
391 }
392 }
393
394 static void
395 propagate_xline(struct Client *source_p, const char *target,
396 int temp_time, const char *name, const char *type,
397 const char *reason)
398 {
399 if(!temp_time)
400 {
401 sendto_match_servs(source_p, target, CAP_CLUSTER, NOCAPS,
402 "XLINE %s %s %s :%s",
403 target, name, type, reason);
404 sendto_match_servs(source_p, target, CAP_ENCAP, CAP_CLUSTER,
405 "ENCAP %s XLINE %d %s 2 :%s",
406 target, temp_time, name, reason);
407 }
408 else
409 sendto_match_servs(source_p, target, CAP_ENCAP, NOCAPS,
410 "ENCAP %s XLINE %d %s %s :%s",
411 target, temp_time, name, type, reason);
412 }
413
414 static void
415 cluster_xline(struct Client *source_p, int temp_time, const char *name,
416 const char *reason)
417 {
418 struct remote_conf *shared_p;
419 rb_dlink_node *ptr;
420
421 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
422 {
423 shared_p = ptr->data;
424
425 /* old protocol cant handle temps, and we dont really want
426 * to convert them to perm.. --fl
427 */
428 if(!temp_time)
429 {
430 if(!(shared_p->flags & SHARED_PXLINE))
431 continue;
432
433 sendto_match_servs(source_p, shared_p->server, CAP_CLUSTER, NOCAPS,
434 "XLINE %s %s 2 :%s",
435 shared_p->server, name, reason);
436 sendto_match_servs(source_p, shared_p->server, CAP_ENCAP, CAP_CLUSTER,
437 "ENCAP %s XLINE 0 %s 2 :%s",
438 shared_p->server, name, reason);
439 }
440 else if(shared_p->flags & SHARED_TXLINE)
441 sendto_match_servs(source_p, shared_p->server, CAP_ENCAP, NOCAPS,
442 "ENCAP %s XLINE %d %s 2 :%s",
443 shared_p->server, temp_time, name, reason);
444 }
445 }
446
447 /* mo_unxline()
448 *
449 * parv[1] - thing to unxline
450 */
451 static int
452 mo_unxline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
453 {
454 if(!IsOperXline(source_p))
455 {
456 sendto_one(source_p, form_str(ERR_NOPRIVS),
457 me.name, source_p->name, "xline");
458 return 0;
459 }
460
461 if(parc == 4 && !(irccmp(parv[2], "ON")))
462 {
463 if(!IsOperRemoteBan(source_p))
464 {
465 sendto_one(source_p, form_str(ERR_NOPRIVS),
466 me.name, source_p->name, "remoteban");
467 return 0;
468 }
469
470 propagate_generic(source_p, "UNXLINE", parv[3], CAP_CLUSTER,
471 "%s", parv[1]);
472
473 if(match(parv[3], me.name) == 0)
474 return 0;
475 }
476 else if(rb_dlink_list_length(&cluster_conf_list))
477 cluster_generic(source_p, "UNXLINE", SHARED_UNXLINE, CAP_CLUSTER,
478 "%s", parv[1]);
479
480 remove_xline(source_p, parv[1]);
481
482 return 0;
483 }
484
485 /* ms_unxline()
486 *
487 * handles a remote unxline
488 */
489 static int
490 ms_unxline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
491 {
492 /* parv[0] parv[1] parv[2]
493 * oper target server gecos
494 */
495 propagate_generic(source_p, "UNXLINE", parv[1], CAP_CLUSTER,
496 "%s", parv[2]);
497
498 if(!match(parv[1], me.name))
499 return 0;
500
501 if(!IsPerson(source_p))
502 return 0;
503
504 handle_remote_unxline(source_p, parv[2]);
505 return 0;
506 }
507
508 static int
509 me_unxline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
510 {
511 /* name */
512 if(!IsPerson(source_p))
513 return 0;
514
515 handle_remote_unxline(source_p, parv[1]);
516 return 0;
517 }
518
519 static void
520 handle_remote_unxline(struct Client *source_p, const char *name)
521 {
522 if(!find_shared_conf(source_p->username, source_p->host,
523 source_p->servptr->name, SHARED_UNXLINE))
524 return;
525
526 remove_xline(source_p, name);
527
528 return;
529 }
530
531 static void
532 remove_xline(struct Client *source_p, const char *name)
533 {
534 struct ConfItem *aconf;
535 rb_dlink_node *ptr;
536
537 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
538 {
539 aconf = ptr->data;
540
541 if(!irccmp(aconf->name, name))
542 {
543 if (!aconf->hold)
544 {
545 if (!remove_xline_from_file(source_p, name))
546 return;
547 }
548 else
549 {
550 sendto_one_notice(source_p,
551 ":X-Line for [%s] is removed",
552 name);
553 sendto_realops_snomask(SNO_GENERAL, L_ALL,
554 "%s has removed the temporary X-Line for: [%s]",
555 get_oper_name(source_p), name);
556 ilog(L_KLINE, "UX %s %s",
557 get_oper_name(source_p), name);
558 }
559
560 remove_reject_mask(aconf->name, NULL);
561 free_conf(aconf);
562 rb_dlinkDestroy(ptr, &xline_conf_list);
563 return;
564 }
565 }
566
567 sendto_one_notice(source_p, ":No X-Line for %s", name);
568
569 return;
570 }
571
572 /* remove_xline_from_file()
573 *
574 * inputs - gecos to remove
575 * outputs -
576 * side effects - removes xline from conf, if exists
577 * - does not touch xline_conf_list
578 */
579 static int
580 remove_xline_from_file(struct Client *source_p, const char *huntgecos)
581 {
582 FILE *in, *out;
583 char buf[BUFSIZE];
584 char buff[BUFSIZE];
585 char temppath[BUFSIZE];
586 const char *filename;
587 const char *gecos;
588 mode_t oldumask;
589 char *p;
590 int error_on_write = 0;
591 int found_xline = 0;
592
593 filename = ConfigFileEntry.xlinefile;
594 rb_snprintf(temppath, sizeof(temppath),
595 "%s.tmp", ConfigFileEntry.xlinefile);
596
597 if((in = fopen(filename, "r")) == NULL)
598 {
599 sendto_one_notice(source_p, ":Cannot open %s", filename);
600 return 0;
601 }
602
603 oldumask = umask(0);
604
605 if((out = fopen(temppath, "w")) == NULL)
606 {
607 sendto_one_notice(source_p, ":Cannot open %s", temppath);
608 fclose(in);
609 umask(oldumask);
610 return 0;
611 }
612
613 umask(oldumask);
614
615 while (fgets(buf, sizeof(buf), in))
616 {
617 if(error_on_write)
618 {
619 if(temppath != NULL)
620 (void) unlink(temppath);
621
622 break;
623 }
624
625 strlcpy(buff, buf, sizeof(buff));
626
627 if((p = strchr(buff, '\n')) != NULL)
628 *p = '\0';
629
630 if((*buff == '\0') || (*buff == '#'))
631 {
632 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
633 continue;
634 }
635
636 if((gecos = getfield(buff)) == NULL)
637 {
638 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
639 continue;
640 }
641
642 /* matching.. */
643 if(irccmp(gecos, huntgecos) == 0)
644 found_xline++;
645 else
646 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
647 }
648
649 fclose(in);
650 if (fclose(out))
651 error_on_write = YES;
652
653 if(error_on_write)
654 {
655 sendto_one_notice(source_p,
656 ":Couldn't write temp xline file, aborted");
657 return 0;
658 }
659 else if(found_xline == 0)
660 {
661 sendto_one_notice(source_p, ":Cannot find X-Line for %s in file", huntgecos);
662
663 if(temppath != NULL)
664 (void) unlink(temppath);
665 return 0;
666 }
667
668 if (rename(temppath, filename))
669 {
670 sendto_one_notice(source_p, ":Couldn't rename temp file, aborted");
671 return 0;
672 }
673
674 sendto_one_notice(source_p, ":X-Line for [%s] is removed", huntgecos);
675 sendto_realops_snomask(SNO_GENERAL, L_ALL,
676 "%s has removed the X-Line for: [%s]",
677 get_oper_name(source_p), huntgecos);
678 ilog(L_KLINE, "UX %s %s", get_oper_name(source_p), huntgecos);
679
680 return 1;
681 }