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