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 $
51 #include "s_newconf.h"
54 static int mo_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[]);
55 static int ms_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[]);
56 static int me_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[]);
57 static int mo_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[]);
58 static int ms_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[]);
59 static int me_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[]);
61 struct 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}}
65 struct 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}}
70 mapi_clist_av1 xline_clist
[] = { &xline_msgtab
, &unxline_msgtab
, NULL
};
71 DECLARE_MODULE_AV1(xline
, NULL
, NULL
, xline_clist
, NULL
, NULL
, "$Revision: 3161 $");
73 static int valid_xline(struct Client
*, const char *, const char *);
74 static void apply_xline(struct Client
*client_p
, const char *name
,
75 const char *reason
, int temp_time
);
76 static void write_xline(struct Client
*source_p
, struct ConfItem
*aconf
);
77 static void propagate_xline(struct Client
*source_p
, const char *target
,
78 int temp_time
, const char *name
,
79 const char *type
, const char *reason
);
80 static void cluster_xline(struct Client
*source_p
, int temp_time
,
81 const char *name
, const char *reason
);
83 static void handle_remote_xline(struct Client
*source_p
, int temp_time
,
84 const char *name
, const char *reason
);
85 static void handle_remote_unxline(struct Client
*source_p
, const char *name
);
87 static void remove_xline(struct Client
*source_p
, const char *name
);
88 static int remove_xline_from_file(struct Client
*source_p
, const char *gecos
);
93 * parv[1] - thing to xline
94 * parv[2] - optional type/reason
98 mo_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
100 struct ConfItem
*aconf
;
103 const char *target_server
= NULL
;
107 if(!IsOperXline(source_p
))
109 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
110 me
.name
, source_p
->name
, "xline");
114 if((temp_time
= valid_temp_time(parv
[loc
])) >= 0)
116 /* we just set temp_time to -1! */
123 /* XLINE <gecos> ON <server> :<reason> */
124 if(parc
>= loc
+2 && !irccmp(parv
[loc
], "ON"))
126 if(!IsOperRemoteBan(source_p
))
128 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
129 me
.name
, source_p
->name
, "remoteban");
133 target_server
= parv
[loc
+1];
137 if(parc
<= loc
|| EmptyString(parv
[loc
]))
139 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
140 me
.name
, source_p
->name
, "XLINE");
146 if(target_server
!= NULL
)
148 propagate_xline(source_p
, target_server
, temp_time
,
151 if(!match(target_server
, me
.name
))
154 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
155 cluster_xline(source_p
, temp_time
, name
, reason
);
157 if((aconf
= find_xline_mask(name
)) != NULL
)
159 sendto_one(source_p
, ":%s NOTICE %s :[%s] already X-Lined by [%s] - %s",
160 me
.name
, source_p
->name
, name
, aconf
->name
, aconf
->passwd
);
164 if(!valid_xline(source_p
, name
, reason
))
167 apply_xline(source_p
, name
, reason
, temp_time
);
174 * handles a remote xline
177 ms_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
179 /* source_p parv[1] parv[2] parv[3] parv[4]
180 * oper target serv xline type reason
182 propagate_xline(source_p
, parv
[1], 0, parv
[2], parv
[3], parv
[4]);
184 if(!IsPerson(source_p
))
187 /* destined for me? */
188 if(!match(parv
[1], me
.name
))
191 handle_remote_xline(source_p
, 0, parv
[2], parv
[4]);
196 me_xline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
198 /* time name type :reason */
199 if(!IsPerson(source_p
))
202 handle_remote_xline(source_p
, atoi(parv
[1]), parv
[2], parv
[4]);
207 handle_remote_xline(struct Client
*source_p
, int temp_time
,
208 const char *name
, const char *reason
)
210 struct ConfItem
*aconf
;
212 if(!find_shared_conf(source_p
->username
, source_p
->host
,
213 source_p
->servptr
->name
,
214 (temp_time
> 0) ? SHARED_TXLINE
: SHARED_PXLINE
))
217 if(!valid_xline(source_p
, name
, reason
))
221 if((aconf
= find_xline_mask(name
)) != NULL
)
223 sendto_one_notice(source_p
, ":[%s] already X-Lined by [%s] - %s", name
, aconf
->name
, aconf
->passwd
);
227 apply_xline(source_p
, name
, reason
, temp_time
);
232 * inputs - client xlining, gecos, reason and whether to warn
234 * side effects - checks the xline for validity, erroring if needed
237 valid_xline(struct Client
*source_p
, const char *gecos
,
240 if(EmptyString(reason
))
242 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
243 get_id(&me
, source_p
),
244 get_id(source_p
, source_p
), "XLINE");
248 if(strchr(reason
, ':') != NULL
)
250 sendto_one_notice(source_p
,
251 ":Invalid character ':' in comment");
255 if(strchr(reason
, '"'))
257 sendto_one_notice(source_p
,
258 ":Invalid character '\"' in comment");
262 if(!valid_wild_card_simple(gecos
))
264 sendto_one_notice(source_p
,
265 ":Please include at least %d non-wildcard "
266 "characters with the xline",
267 ConfigFileEntry
.min_nonwildcard_simple
);
275 apply_xline(struct Client
*source_p
, const char *name
, const char *reason
,
278 struct ConfItem
*aconf
;
281 aconf
->status
= CONF_XLINE
;
283 aconf
->name
= rb_strdup(name
);
284 aconf
->passwd
= rb_strdup(reason
);
285 collapse(aconf
->name
);
289 aconf
->hold
= rb_current_time() + temp_time
;
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,
298 sendto_one_notice(source_p
, ":Added temporary %d min. X-Line [%s]",
299 temp_time
/ 60, aconf
->name
);
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
);
313 rb_dlinkAddAlloc(aconf
, &xline_conf_list
);
319 * inputs - gecos, reason, xline type
320 * outputs - writes an xline to the config
324 write_xline(struct Client
*source_p
, struct ConfItem
*aconf
)
326 char buffer
[BUFSIZE
* 2];
328 const char *filename
;
330 filename
= ConfigFileEntry
.xlinefile
;
332 if((out
= fopen(filename
, "a")) == NULL
)
334 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "*** Problem opening %s ", filename
);
335 sendto_one_notice(source_p
, ":*** Problem opening file, xline added temporarily only");
339 rb_sprintf(buffer
, "\"%s\",\"0\",\"%s\",\"%s\",%ld\n",
340 aconf
->name
, aconf
->passwd
,
341 get_oper_name(source_p
), (long) rb_current_time());
343 if(fputs(buffer
, out
) == -1)
345 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "*** Problem writing to %s", filename
);
346 sendto_one_notice(source_p
, ":*** Problem writing to file, xline added temporarily only");
353 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "*** Problem writing to %s", filename
);
354 sendto_one_notice(source_p
, ":*** Problem writing to file, xline added temporarily only");
360 propagate_xline(struct Client
*source_p
, const char *target
,
361 int temp_time
, const char *name
, const char *type
,
366 sendto_match_servs(source_p
, target
, CAP_CLUSTER
, NOCAPS
,
367 "XLINE %s %s %s :%s",
368 target
, name
, type
, reason
);
369 sendto_match_servs(source_p
, target
, CAP_ENCAP
, CAP_CLUSTER
,
370 "ENCAP %s XLINE %d %s 2 :%s",
371 target
, temp_time
, name
, reason
);
374 sendto_match_servs(source_p
, target
, CAP_ENCAP
, NOCAPS
,
375 "ENCAP %s XLINE %d %s %s :%s",
376 target
, temp_time
, name
, type
, reason
);
380 cluster_xline(struct Client
*source_p
, int temp_time
, const char *name
,
383 struct remote_conf
*shared_p
;
386 RB_DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
388 shared_p
= ptr
->data
;
390 /* old protocol cant handle temps, and we dont really want
391 * to convert them to perm.. --fl
395 if(!(shared_p
->flags
& SHARED_PXLINE
))
398 sendto_match_servs(source_p
, shared_p
->server
, CAP_CLUSTER
, NOCAPS
,
400 shared_p
->server
, name
, reason
);
401 sendto_match_servs(source_p
, shared_p
->server
, CAP_ENCAP
, CAP_CLUSTER
,
402 "ENCAP %s XLINE 0 %s 2 :%s",
403 shared_p
->server
, name
, reason
);
405 else if(shared_p
->flags
& SHARED_TXLINE
)
406 sendto_match_servs(source_p
, shared_p
->server
, CAP_ENCAP
, NOCAPS
,
407 "ENCAP %s XLINE %d %s 2 :%s",
408 shared_p
->server
, temp_time
, name
, reason
);
414 * parv[1] - thing to unxline
417 mo_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
419 if(!IsOperXline(source_p
))
421 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
422 me
.name
, source_p
->name
, "xline");
426 if(parc
== 4 && !(irccmp(parv
[2], "ON")))
428 if(!IsOperRemoteBan(source_p
))
430 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
431 me
.name
, source_p
->name
, "remoteban");
435 propagate_generic(source_p
, "UNXLINE", parv
[3], CAP_CLUSTER
,
438 if(match(parv
[3], me
.name
) == 0)
441 else if(rb_dlink_list_length(&cluster_conf_list
))
442 cluster_generic(source_p
, "UNXLINE", SHARED_UNXLINE
, CAP_CLUSTER
,
445 remove_xline(source_p
, parv
[1]);
452 * handles a remote unxline
455 ms_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
457 /* source_p parv[1] parv[2]
458 * oper target server gecos
460 propagate_generic(source_p
, "UNXLINE", parv
[1], CAP_CLUSTER
,
463 if(!match(parv
[1], me
.name
))
466 if(!IsPerson(source_p
))
469 handle_remote_unxline(source_p
, parv
[2]);
474 me_unxline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
477 if(!IsPerson(source_p
))
480 handle_remote_unxline(source_p
, parv
[1]);
485 handle_remote_unxline(struct Client
*source_p
, const char *name
)
487 if(!find_shared_conf(source_p
->username
, source_p
->host
,
488 source_p
->servptr
->name
, SHARED_UNXLINE
))
491 remove_xline(source_p
, name
);
497 remove_xline(struct Client
*source_p
, const char *name
)
499 struct ConfItem
*aconf
;
503 encoded
= xline_encode_spaces(name
);
505 RB_DLINK_FOREACH(ptr
, xline_conf_list
.head
)
509 if(!irccmp(aconf
->name
, encoded
))
513 if (!remove_xline_from_file(source_p
, encoded
))
518 sendto_one_notice(source_p
,
519 ":X-Line for [%s] is removed",
521 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
522 "%s has removed the temporary X-Line for: [%s]",
523 get_oper_name(source_p
), encoded
);
524 ilog(L_KLINE
, "UX %s %s",
525 get_oper_name(source_p
), encoded
);
528 remove_reject_mask(aconf
->name
, NULL
);
530 rb_dlinkDestroy(ptr
, &xline_conf_list
);
536 sendto_one_notice(source_p
, ":No X-Line for %s", encoded
);
542 /* remove_xline_from_file()
544 * inputs - gecos to remove
546 * side effects - removes xline from conf, if exists
547 * - does not touch xline_conf_list
550 remove_xline_from_file(struct Client
*source_p
, const char *huntgecos
)
555 char temppath
[BUFSIZE
];
556 const char *filename
;
561 int error_on_write
= 0;
564 filename
= ConfigFileEntry
.xlinefile
;
565 rb_snprintf(temppath
, sizeof(temppath
),
566 "%s.tmp", ConfigFileEntry
.xlinefile
);
568 if((in
= fopen(filename
, "r")) == NULL
)
570 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
576 if((out
= fopen(temppath
, "w")) == NULL
)
578 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
586 while (fgets(buf
, sizeof(buf
), in
))
591 (void) unlink(temppath
);
596 rb_strlcpy(buff
, buf
, sizeof(buff
));
598 if((p
= strchr(buff
, '\n')) != NULL
)
601 if((*buff
== '\0') || (*buff
== '#'))
603 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
607 if((gecos
= getfield(buff
)) == NULL
)
609 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
614 encoded
= xline_encode_spaces(gecos
);
615 if(irccmp(encoded
, huntgecos
) == 0)
618 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
624 error_on_write
= YES
;
628 sendto_one_notice(source_p
,
629 ":Couldn't write temp xline file, aborted");
632 else if(found_xline
== 0)
634 sendto_one_notice(source_p
, ":Cannot find X-Line for %s in file", huntgecos
);
637 (void) unlink(temppath
);
641 if (rename(temppath
, filename
))
643 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
647 sendto_one_notice(source_p
, ":X-Line for [%s] is removed", huntgecos
);
648 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
649 "%s has removed the X-Line for: [%s]",
650 get_oper_name(source_p
), huntgecos
);
651 ilog(L_KLINE
, "UX %s %s", get_oper_name(source_p
), huntgecos
);