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 $
45 #include "irc_string.h"
46 #include "sprintf_irc.h"
52 #include "s_newconf.h"
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
[]);
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}}
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}}
71 mapi_clist_av1 xline_clist
[] = { &xline_msgtab
, &unxline_msgtab
, NULL
};
72 DECLARE_MODULE_AV1(xline
, NULL
, NULL
, xline_clist
, NULL
, NULL
, "$Revision: 3161 $");
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
);
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
);
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
);
94 * parv[1] - thing to xline
95 * parv[2] - optional type/reason
99 mo_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
101 struct ConfItem
*aconf
;
104 const char *target_server
= NULL
;
108 if(!IsOperXline(source_p
))
110 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
111 me
.name
, source_p
->name
, "xline");
115 if((temp_time
= valid_temp_time(parv
[loc
])) >= 0)
117 /* we just set temp_time to -1! */
124 /* XLINE <gecos> ON <server> :<reason> */
125 if(parc
>= loc
+2 && !irccmp(parv
[loc
], "ON"))
127 if(!IsOperRemoteBan(source_p
))
129 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
130 me
.name
, source_p
->name
, "remoteban");
134 target_server
= parv
[loc
+1];
138 if(parc
<= loc
|| EmptyString(parv
[loc
]))
140 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
141 me
.name
, source_p
->name
, "XLINE");
147 if(target_server
!= NULL
)
149 propagate_xline(source_p
, target_server
, temp_time
,
152 if(!match(target_server
, me
.name
))
155 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
156 cluster_xline(source_p
, temp_time
, name
, reason
);
158 if((aconf
= find_xline_mask(name
)) != NULL
)
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
);
165 if(!valid_xline(source_p
, name
, reason
))
168 apply_xline(source_p
, name
, reason
, temp_time
);
175 * handles a remote xline
178 ms_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
180 /* parv[0] parv[1] parv[2] parv[3] parv[4]
181 * oper target serv xline type reason
183 propagate_xline(source_p
, parv
[1], 0, parv
[2], parv
[3], parv
[4]);
185 if(!IsPerson(source_p
))
188 /* destined for me? */
189 if(!match(parv
[1], me
.name
))
192 handle_remote_xline(source_p
, 0, parv
[2], parv
[4]);
197 me_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
199 /* time name type :reason */
200 if(!IsPerson(source_p
))
203 handle_remote_xline(source_p
, atoi(parv
[1]), parv
[2], parv
[4]);
208 handle_remote_xline(struct Client
*source_p
, int temp_time
,
209 const char *name
, const char *reason
)
211 struct ConfItem
*aconf
;
213 if(!find_shared_conf(source_p
->username
, source_p
->host
,
214 source_p
->servptr
->name
,
215 (temp_time
> 0) ? SHARED_TXLINE
: SHARED_PXLINE
))
218 if(!valid_xline(source_p
, name
, reason
))
222 if((aconf
= find_xline_mask(name
)) != NULL
)
224 sendto_one_notice(source_p
, ":[%s] already X-Lined by [%s] - %s", name
, aconf
->name
, aconf
->passwd
);
228 apply_xline(source_p
, name
, reason
, temp_time
);
233 * inputs - client xlining, gecos, reason and whether to warn
235 * side effects - checks the xline for validity, erroring if needed
238 valid_xline(struct Client
*source_p
, const char *gecos
,
241 if(EmptyString(reason
))
243 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
244 get_id(&me
, source_p
),
245 get_id(source_p
, source_p
), "XLINE");
249 if(strchr(reason
, ':') != NULL
)
251 sendto_one_notice(source_p
,
252 ":Invalid character ':' in comment");
256 if(strchr(reason
, '"'))
258 sendto_one_notice(source_p
,
259 ":Invalid character '\"' in comment");
263 if(!valid_wild_card_simple(gecos
))
265 sendto_one_notice(source_p
,
266 ":Please include at least %d non-wildcard "
267 "characters with the xline",
268 ConfigFileEntry
.min_nonwildcard_simple
);
276 apply_xline(struct Client
*source_p
, const char *name
, const char *reason
,
279 struct ConfItem
*aconf
;
282 aconf
->status
= CONF_XLINE
;
284 if(strstr(name
, "\\s"))
286 char *tmp
= LOCAL_COPY(name
);
292 if(*orig
== '\\' && *(orig
+ 1) != '\0')
294 if(*(orig
+ 1) == 's')
299 /* otherwise skip that and the escaped
300 * character after it, so we dont mistake
314 aconf
->name
= rb_strdup(tmp
);
317 aconf
->name
= rb_strdup(name
);
319 aconf
->passwd
= rb_strdup(reason
);
320 collapse(aconf
->name
);
324 aconf
->hold
= rb_current_time() + temp_time
;
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,
333 sendto_one_notice(source_p
, ":Added temporary %d min. X-Line [%s]",
334 temp_time
/ 60, aconf
->name
);
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
);
348 rb_dlinkAddAlloc(aconf
, &xline_conf_list
);
354 * inputs - gecos, reason, xline type
355 * outputs - writes an xline to the config
359 write_xline(struct Client
*source_p
, struct ConfItem
*aconf
)
361 char buffer
[BUFSIZE
* 2];
363 const char *filename
;
365 filename
= ConfigFileEntry
.xlinefile
;
367 if((out
= fopen(filename
, "a")) == NULL
)
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");
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());
378 if(fputs(buffer
, out
) == -1)
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");
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");
395 propagate_xline(struct Client
*source_p
, const char *target
,
396 int temp_time
, const char *name
, const char *type
,
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
);
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
);
415 cluster_xline(struct Client
*source_p
, int temp_time
, const char *name
,
418 struct remote_conf
*shared_p
;
421 RB_DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
423 shared_p
= ptr
->data
;
425 /* old protocol cant handle temps, and we dont really want
426 * to convert them to perm.. --fl
430 if(!(shared_p
->flags
& SHARED_PXLINE
))
433 sendto_match_servs(source_p
, shared_p
->server
, CAP_CLUSTER
, NOCAPS
,
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
);
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
);
449 * parv[1] - thing to unxline
452 mo_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
454 if(!IsOperXline(source_p
))
456 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
457 me
.name
, source_p
->name
, "xline");
461 if(parc
== 4 && !(irccmp(parv
[2], "ON")))
463 if(!IsOperRemoteBan(source_p
))
465 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
466 me
.name
, source_p
->name
, "remoteban");
470 propagate_generic(source_p
, "UNXLINE", parv
[3], CAP_CLUSTER
,
473 if(match(parv
[3], me
.name
) == 0)
476 else if(rb_dlink_list_length(&cluster_conf_list
))
477 cluster_generic(source_p
, "UNXLINE", SHARED_UNXLINE
, CAP_CLUSTER
,
480 remove_xline(source_p
, parv
[1]);
487 * handles a remote unxline
490 ms_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
492 /* parv[0] parv[1] parv[2]
493 * oper target server gecos
495 propagate_generic(source_p
, "UNXLINE", parv
[1], CAP_CLUSTER
,
498 if(!match(parv
[1], me
.name
))
501 if(!IsPerson(source_p
))
504 handle_remote_unxline(source_p
, parv
[2]);
509 me_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
512 if(!IsPerson(source_p
))
515 handle_remote_unxline(source_p
, parv
[1]);
520 handle_remote_unxline(struct Client
*source_p
, const char *name
)
522 if(!find_shared_conf(source_p
->username
, source_p
->host
,
523 source_p
->servptr
->name
, SHARED_UNXLINE
))
526 remove_xline(source_p
, name
);
532 remove_xline(struct Client
*source_p
, const char *name
)
534 struct ConfItem
*aconf
;
537 RB_DLINK_FOREACH(ptr
, xline_conf_list
.head
)
541 if(!irccmp(aconf
->name
, name
))
545 if (!remove_xline_from_file(source_p
, name
))
550 sendto_one_notice(source_p
,
551 ":X-Line for [%s] is removed",
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
);
560 remove_reject_mask(aconf
->name
, NULL
);
562 rb_dlinkDestroy(ptr
, &xline_conf_list
);
567 sendto_one_notice(source_p
, ":No X-Line for %s", name
);
572 /* remove_xline_from_file()
574 * inputs - gecos to remove
576 * side effects - removes xline from conf, if exists
577 * - does not touch xline_conf_list
580 remove_xline_from_file(struct Client
*source_p
, const char *huntgecos
)
585 char temppath
[BUFSIZE
];
586 const char *filename
;
590 int error_on_write
= 0;
593 filename
= ConfigFileEntry
.xlinefile
;
594 rb_snprintf(temppath
, sizeof(temppath
),
595 "%s.tmp", ConfigFileEntry
.xlinefile
);
597 if((in
= fopen(filename
, "r")) == NULL
)
599 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
605 if((out
= fopen(temppath
, "w")) == NULL
)
607 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
615 while (fgets(buf
, sizeof(buf
), in
))
620 (void) unlink(temppath
);
625 strlcpy(buff
, buf
, sizeof(buff
));
627 if((p
= strchr(buff
, '\n')) != NULL
)
630 if((*buff
== '\0') || (*buff
== '#'))
632 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
636 if((gecos
= getfield(buff
)) == NULL
)
638 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
643 if(irccmp(gecos
, huntgecos
) == 0)
646 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
651 error_on_write
= YES
;
655 sendto_one_notice(source_p
,
656 ":Couldn't write temp xline file, aborted");
659 else if(found_xline
== 0)
661 sendto_one_notice(source_p
, ":Cannot find X-Line for %s in file", huntgecos
);
664 (void) unlink(temppath
);
668 if (rename(temppath
, filename
))
670 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
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
);