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