3 * Copyright (C) 2002-2003 Lee Hardy <lee@leeh.co.uk>
4 * Copyright (C) 2002-2005 ircd-ratbox development team
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
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.
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.
30 * $Id: m_xline.c 3161 2007-01-25 07:23:01Z nenolod $
47 #include "irc_string.h"
48 #include "sprintf_irc.h"
54 #include "s_newconf.h"
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
[]);
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}}
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}}
73 mapi_clist_av1 xline_clist
[] = { &xline_msgtab
, &unxline_msgtab
, NULL
};
74 DECLARE_MODULE_AV1(xline
, NULL
, NULL
, xline_clist
, NULL
, NULL
, "$Revision: 3161 $");
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
);
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
);
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
);
96 * parv[1] - thing to xline
97 * parv[2] - optional type/reason
101 mo_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
103 struct ConfItem
*aconf
;
106 const char *target_server
= NULL
;
110 if(!IsOperXline(source_p
))
112 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
113 me
.name
, source_p
->name
, "xline");
117 if((temp_time
= valid_temp_time(parv
[loc
])) >= 0)
119 /* we just set temp_time to -1! */
126 /* XLINE <gecos> ON <server> :<reason> */
127 if(parc
>= loc
+2 && !irccmp(parv
[loc
], "ON"))
129 if(!IsOperRemoteBan(source_p
))
131 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
132 me
.name
, source_p
->name
, "remoteban");
136 target_server
= parv
[loc
+1];
140 if(parc
<= loc
|| EmptyString(parv
[loc
]))
142 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
143 me
.name
, source_p
->name
, "XLINE");
149 if(target_server
!= NULL
)
151 propagate_xline(source_p
, target_server
, temp_time
,
154 if(!match(target_server
, me
.name
))
157 else if(dlink_list_length(&cluster_conf_list
) > 0)
158 cluster_xline(source_p
, temp_time
, name
, reason
);
160 if((aconf
= find_xline_mask(name
)) != NULL
)
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
);
167 if(!valid_xline(source_p
, name
, reason
))
170 apply_xline(source_p
, name
, reason
, temp_time
);
177 * handles a remote xline
180 ms_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
182 /* parv[0] parv[1] parv[2] parv[3] parv[4]
183 * oper target serv xline type reason
185 propagate_xline(source_p
, parv
[1], 0, parv
[2], parv
[3], parv
[4]);
187 if(!IsPerson(source_p
))
190 /* destined for me? */
191 if(!match(parv
[1], me
.name
))
194 handle_remote_xline(source_p
, 0, parv
[2], parv
[4]);
199 me_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
201 /* time name type :reason */
202 if(!IsPerson(source_p
))
205 handle_remote_xline(source_p
, atoi(parv
[1]), parv
[2], parv
[4]);
210 handle_remote_xline(struct Client
*source_p
, int temp_time
,
211 const char *name
, const char *reason
)
213 struct ConfItem
*aconf
;
215 if(!find_shared_conf(source_p
->username
, source_p
->host
,
216 source_p
->servptr
->name
,
217 (temp_time
> 0) ? SHARED_TXLINE
: SHARED_PXLINE
))
220 if(!valid_xline(source_p
, name
, reason
))
224 if((aconf
= find_xline_mask(name
)) != NULL
)
226 sendto_one_notice(source_p
, ":[%s] already X-Lined by [%s] - %s", name
, aconf
->name
, aconf
->passwd
);
230 apply_xline(source_p
, name
, reason
, temp_time
);
235 * inputs - client xlining, gecos, reason and whether to warn
237 * side effects - checks the xline for validity, erroring if needed
240 valid_xline(struct Client
*source_p
, const char *gecos
,
243 if(EmptyString(reason
))
245 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
246 get_id(&me
, source_p
),
247 get_id(source_p
, source_p
), "XLINE");
251 if(strchr(reason
, ':') != NULL
)
253 sendto_one_notice(source_p
,
254 ":Invalid character ':' in comment");
258 if(strchr(reason
, '"'))
260 sendto_one_notice(source_p
,
261 ":Invalid character '\"' in comment");
265 if(!valid_wild_card_simple(gecos
))
267 sendto_one_notice(source_p
,
268 ":Please include at least %d non-wildcard "
269 "characters with the xline",
270 ConfigFileEntry
.min_nonwildcard_simple
);
278 apply_xline(struct Client
*source_p
, const char *name
, const char *reason
,
281 struct ConfItem
*aconf
;
284 aconf
->status
= CONF_XLINE
;
286 if(strstr(name
, "\\s"))
288 char *tmp
= LOCAL_COPY(name
);
294 if(*orig
== '\\' && *(orig
+ 1) != '\0')
296 if(*(orig
+ 1) == 's')
301 /* otherwise skip that and the escaped
302 * character after it, so we dont mistake
316 DupString(aconf
->name
, tmp
);
319 DupString(aconf
->name
, name
);
321 DupString(aconf
->passwd
, reason
);
322 collapse(aconf
->name
);
326 aconf
->hold
= CurrentTime
+ temp_time
;
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,
335 sendto_one_notice(source_p
, ":Added temporary %d min. X-Line [%s]",
336 temp_time
/ 60, aconf
->name
);
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
);
350 dlinkAddAlloc(aconf
, &xline_conf_list
);
356 * inputs - gecos, reason, xline type
357 * outputs - writes an xline to the config
361 write_xline(struct Client
*source_p
, struct ConfItem
*aconf
)
363 char buffer
[BUFSIZE
* 2];
365 const char *filename
;
367 filename
= ConfigFileEntry
.xlinefile
;
369 if((out
= fopen(filename
, "a")) == NULL
)
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");
376 ircsprintf(buffer
, "\"%s\",\"0\",\"%s\",\"%s\",%ld\n",
377 aconf
->name
, aconf
->passwd
,
378 get_oper_name(source_p
), CurrentTime
);
380 if(fputs(buffer
, out
) == -1)
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");
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");
397 propagate_xline(struct Client
*source_p
, const char *target
,
398 int temp_time
, const char *name
, const char *type
,
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
);
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
);
417 cluster_xline(struct Client
*source_p
, int temp_time
, const char *name
,
420 struct remote_conf
*shared_p
;
423 DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
425 shared_p
= ptr
->data
;
427 /* old protocol cant handle temps, and we dont really want
428 * to convert them to perm.. --fl
432 if(!(shared_p
->flags
& SHARED_PXLINE
))
435 sendto_match_servs(source_p
, shared_p
->server
, CAP_CLUSTER
, NOCAPS
,
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
);
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
);
451 * parv[1] - thing to unxline
454 mo_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
456 if(!IsOperXline(source_p
))
458 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
459 me
.name
, source_p
->name
, "xline");
463 if(parc
== 4 && !(irccmp(parv
[2], "ON")))
465 if(!IsOperRemoteBan(source_p
))
467 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
468 me
.name
, source_p
->name
, "remoteban");
472 propagate_generic(source_p
, "UNXLINE", parv
[3], CAP_CLUSTER
,
475 if(match(parv
[3], me
.name
) == 0)
478 else if(dlink_list_length(&cluster_conf_list
))
479 cluster_generic(source_p
, "UNXLINE", SHARED_UNXLINE
, CAP_CLUSTER
,
482 remove_xline(source_p
, parv
[1]);
489 * handles a remote unxline
492 ms_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
494 /* parv[0] parv[1] parv[2]
495 * oper target server gecos
497 propagate_generic(source_p
, "UNXLINE", parv
[1], CAP_CLUSTER
,
500 if(!match(parv
[1], me
.name
))
503 if(!IsPerson(source_p
))
506 handle_remote_unxline(source_p
, parv
[2]);
511 me_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
514 if(!IsPerson(source_p
))
517 handle_remote_unxline(source_p
, parv
[1]);
522 handle_remote_unxline(struct Client
*source_p
, const char *name
)
524 if(!find_shared_conf(source_p
->username
, source_p
->host
,
525 source_p
->servptr
->name
, SHARED_UNXLINE
))
528 remove_xline(source_p
, name
);
534 remove_xline(struct Client
*source_p
, const char *name
)
536 struct ConfItem
*aconf
;
539 DLINK_FOREACH(ptr
, xline_conf_list
.head
)
543 if(!irccmp(aconf
->name
, name
))
547 if (!remove_xline_from_file(source_p
, name
))
552 sendto_one_notice(source_p
,
553 ":X-Line for [%s] is removed",
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
);
562 remove_reject_mask(aconf
->name
, NULL
);
564 dlinkDestroy(ptr
, &xline_conf_list
);
569 sendto_one_notice(source_p
, ":No X-Line for %s", name
);
574 /* remove_xline_from_file()
576 * inputs - gecos to remove
578 * side effects - removes xline from conf, if exists
579 * - does not touch xline_conf_list
582 remove_xline_from_file(struct Client
*source_p
, const char *huntgecos
)
587 char temppath
[BUFSIZE
];
588 const char *filename
;
592 int error_on_write
= 0;
595 filename
= ConfigFileEntry
.xlinefile
;
596 ircsnprintf(temppath
, sizeof(temppath
),
597 "%s.tmp", ConfigFileEntry
.xlinefile
);
599 if((in
= fopen(filename
, "r")) == NULL
)
601 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
607 if((out
= fopen(temppath
, "w")) == NULL
)
609 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
617 while (fgets(buf
, sizeof(buf
), in
))
622 (void) unlink(temppath
);
627 strlcpy(buff
, buf
, sizeof(buff
));
629 if((p
= strchr(buff
, '\n')) != NULL
)
632 if((*buff
== '\0') || (*buff
== '#'))
634 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
638 if((gecos
= getfield(buff
)) == NULL
)
640 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
645 if(irccmp(gecos
, huntgecos
) == 0)
648 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
653 error_on_write
= YES
;
657 sendto_one_notice(source_p
,
658 ":Couldn't write temp xline file, aborted");
661 else if(found_xline
== 0)
663 sendto_one_notice(source_p
, ":Cannot find X-Line for %s in file", huntgecos
);
666 (void) unlink(temppath
);
670 if (rename(temppath
, filename
))
672 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
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
);