]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_kline.c
Speed up /unkline
[irc/rqf/shadowircd.git] / modules / m_kline.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_kline.c: Bans/unbans a user.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 * $Id: m_kline.c 3466 2007-05-19 23:36:51Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "tools.h"
29 #include "channel.h"
30 #include "class.h"
31 #include "client.h"
32 #include "common.h"
33 #include "irc_string.h"
34 #include "sprintf_irc.h"
35 #include "ircd.h"
36 #include "hostmask.h"
37 #include "numeric.h"
38 #include "commio.h"
39 #include "s_conf.h"
40 #include "s_newconf.h"
41 #include "s_log.h"
42 #include "send.h"
43 #include "hash.h"
44 #include "s_serv.h"
45 #include "msg.h"
46 #include "parse.h"
47 #include "modules.h"
48 #include "event.h"
49
50 static int mo_kline(struct Client *, struct Client *, int, const char **);
51 static int ms_kline(struct Client *, struct Client *, int, const char **);
52 static int me_kline(struct Client *, struct Client *, int, const char **);
53 static int mo_unkline(struct Client *, struct Client *, int, const char **);
54 static int ms_unkline(struct Client *, struct Client *, int, const char **);
55 static int me_unkline(struct Client *, struct Client *, int, const char **);
56
57 struct Message kline_msgtab = {
58 "KLINE", 0, 0, 0, MFLG_SLOW,
59 {mg_unreg, mg_not_oper, {ms_kline, 5}, {ms_kline, 5}, {me_kline, 5}, {mo_kline, 3}}
60 };
61
62 struct Message unkline_msgtab = {
63 "UNKLINE", 0, 0, 0, MFLG_SLOW,
64 {mg_unreg, mg_not_oper, {ms_unkline, 4}, {ms_unkline, 4}, {me_unkline, 3}, {mo_unkline, 2}}
65 };
66
67 mapi_clist_av1 kline_clist[] = { &kline_msgtab, &unkline_msgtab, NULL };
68 DECLARE_MODULE_AV1(kline, NULL, NULL, kline_clist, NULL, NULL, "$Revision: 3466 $");
69
70 /* Local function prototypes */
71 static int find_user_host(struct Client *source_p, const char *userhost, char *user, char *host);
72 static int valid_comment(struct Client *source_p, char *comment);
73 static int valid_user_host(struct Client *source_p, const char *user, const char *host);
74 static int valid_wild_card(struct Client *source_p, const char *user, const char *host);
75
76 static void handle_remote_kline(struct Client *source_p, int tkline_time,
77 const char *user, const char *host, const char *reason);
78 static void apply_kline(struct Client *source_p, struct ConfItem *aconf,
79 const char *reason, const char *oper_reason, const char *current_date);
80 static void apply_tkline(struct Client *source_p, struct ConfItem *aconf,
81 const char *, const char *, const char *, int);
82 static int already_placed_kline(struct Client *, const char *, const char *, int);
83
84 static void handle_remote_unkline(struct Client *source_p,
85 const char *user, const char *host);
86 static void remove_permkline_match(struct Client *, struct ConfItem *);
87 static int flush_write(struct Client *, FILE *, const char *, const char *);
88 static int remove_temp_kline(struct ConfItem *);
89
90 /* mo_kline()
91 *
92 * parv[1] - temp time or user@host
93 * parv[2] - user@host, "ON", or reason
94 * parv[3] - "ON", reason, or server to target
95 * parv[4] - server to target, or reason
96 * parv[5] - reason
97 */
98 static int
99 mo_kline(struct Client *client_p, struct Client *source_p,
100 int parc, const char **parv)
101 {
102 char def[] = "No Reason";
103 char user[USERLEN + 2];
104 char host[HOSTLEN + 2];
105 char buffer[IRCD_BUFSIZE];
106 char *reason = def;
107 char *oper_reason;
108 const char *current_date;
109 const char *target_server = NULL;
110 struct ConfItem *aconf;
111 int tkline_time = 0;
112 int loc = 1;
113
114 if(!IsOperK(source_p))
115 {
116 sendto_one(source_p, form_str(ERR_NOPRIVS),
117 me.name, source_p->name, "kline");
118 return 0;
119 }
120
121 if((tkline_time = valid_temp_time(parv[loc])) >= 0)
122 loc++;
123 /* we just set tkline_time to -1! */
124 else
125 tkline_time = 0;
126
127 if(find_user_host(source_p, parv[loc], user, host) == 0)
128 return 0;
129
130 loc++;
131
132 if(parc >= loc+2 && !irccmp(parv[loc], "ON"))
133 {
134 if(!IsOperRemoteBan(source_p))
135 {
136 sendto_one(source_p, form_str(ERR_NOPRIVS),
137 me.name, source_p->name, "remoteban");
138 return 0;
139 }
140
141 target_server = parv[loc+1];
142 loc += 2;
143 }
144
145 if(parc <= loc || EmptyString(parv[loc]))
146 {
147 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
148 me.name, source_p->name, "KLINE");
149 return 0;
150 }
151
152 reason = LOCAL_COPY(parv[loc]);
153
154 if(target_server != NULL)
155 {
156 propagate_generic(source_p, "KLINE", target_server, CAP_KLN,
157 "%d %s %s :%s",
158 tkline_time, user, host, reason);
159
160 /* If we are sending it somewhere that doesnt include us, stop */
161 if(!match(target_server, me.name))
162 return 0;
163 }
164 /* if we have cluster servers, send it to them.. */
165 else if(dlink_list_length(&cluster_conf_list) > 0)
166 cluster_generic(source_p, "KLINE",
167 (tkline_time > 0) ? SHARED_TKLINE : SHARED_PKLINE, CAP_KLN,
168 "%lu %s %s :%s",
169 tkline_time, user, host, reason);
170
171 if(!valid_user_host(source_p, user, host) ||
172 !valid_wild_card(source_p, user, host) ||
173 !valid_comment(source_p, reason))
174 return 0;
175
176 if(already_placed_kline(source_p, user, host, tkline_time))
177 return 0;
178
179 set_time();
180 current_date = smalldate();
181 aconf = make_conf();
182 aconf->status = CONF_KILL;
183 DupString(aconf->host, host);
184 DupString(aconf->user, user);
185 aconf->port = 0;
186
187 /* Look for an oper reason */
188 if((oper_reason = strchr(reason, '|')) != NULL)
189 {
190 *oper_reason = '\0';
191 oper_reason++;
192
193 if(!EmptyString(oper_reason))
194 DupString(aconf->spasswd, oper_reason);
195 }
196
197 if(tkline_time > 0)
198 {
199 ircsnprintf(buffer, sizeof(buffer),
200 "Temporary K-line %d min. - %s (%s)",
201 (int) (tkline_time / 60), reason, current_date);
202 DupString(aconf->passwd, buffer);
203 apply_tkline(source_p, aconf, reason, oper_reason, current_date, tkline_time);
204 }
205 else
206 {
207 ircsnprintf(buffer, sizeof(buffer), "%s (%s)", reason, current_date);
208 DupString(aconf->passwd, buffer);
209 apply_kline(source_p, aconf, reason, oper_reason, current_date);
210 }
211
212 if(ConfigFileEntry.kline_delay)
213 {
214 if(kline_queued == 0)
215 {
216 eventAddOnce("check_klines", check_klines_event, NULL,
217 ConfigFileEntry.kline_delay);
218 kline_queued = 1;
219 }
220 }
221 else
222 check_klines();
223
224 return 0;
225 }
226
227 /* ms_kline()
228 *
229 * parv[1] - server targeted at
230 * parv[2] - tkline time (0 if perm)
231 * parv[3] - user
232 * parv[4] - host
233 * parv[5] - reason
234 */
235 static int
236 ms_kline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
237 {
238 int tkline_time = atoi(parv[2]);
239
240 /* 1.5-3 and earlier contains a bug that allows remote klines to be
241 * sent with an empty reason field. This is a protocol violation,
242 * but its not worth dropping the link over.. --anfl
243 */
244 if(parc < 6 || EmptyString(parv[5]))
245 return 0;
246
247 propagate_generic(source_p, "KLINE", parv[1], CAP_KLN,
248 "%d %s %s :%s",
249 tkline_time, parv[3], parv[4], parv[5]);
250
251 if(!match(parv[1], me.name))
252 return 0;
253
254 if(!IsPerson(source_p))
255 return 0;
256
257 handle_remote_kline(source_p, tkline_time, parv[3], parv[4], parv[5]);
258 return 0;
259 }
260
261 static int
262 me_kline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
263 {
264 /* <tkline_time> <user> <host> :<reason> */
265 if(!IsPerson(source_p))
266 return 0;
267
268 handle_remote_kline(source_p, atoi(parv[1]), parv[2], parv[3], parv[4]);
269 return 0;
270 }
271
272 static void
273 handle_remote_kline(struct Client *source_p, int tkline_time,
274 const char *user, const char *host, const char *kreason)
275 {
276 char buffer[BUFSIZE];
277 const char *current_date;
278 char *reason = LOCAL_COPY(kreason);
279 struct ConfItem *aconf = NULL;
280 char *oper_reason;
281
282 if(!find_shared_conf(source_p->username, source_p->host,
283 source_p->servptr->name,
284 (tkline_time > 0) ? SHARED_TKLINE : SHARED_PKLINE))
285 return;
286
287 if(!valid_user_host(source_p, user, host) ||
288 !valid_wild_card(source_p, user, host) ||
289 !valid_comment(source_p, reason))
290 return;
291
292 if(already_placed_kline(source_p, user, host, tkline_time))
293 return;
294
295 aconf = make_conf();
296
297 aconf->status = CONF_KILL;
298 DupString(aconf->user, user);
299 DupString(aconf->host, host);
300
301 /* Look for an oper reason */
302 if((oper_reason = strchr(reason, '|')) != NULL)
303 {
304 *oper_reason = '\0';
305 oper_reason++;
306
307 if(!EmptyString(oper_reason))
308 DupString(aconf->spasswd, oper_reason);
309 }
310
311 current_date = smalldate();
312
313 if(tkline_time > 0)
314 {
315 ircsnprintf(buffer, sizeof(buffer),
316 "Temporary K-line %d min. - %s (%s)",
317 (int) (tkline_time / 60), reason, current_date);
318 DupString(aconf->passwd, buffer);
319 apply_tkline(source_p, aconf, reason, oper_reason, current_date, tkline_time);
320 }
321 else
322 {
323 ircsnprintf(buffer, sizeof(buffer), "%s (%s)", reason, current_date);
324 DupString(aconf->passwd, buffer);
325 apply_kline(source_p, aconf, reason, oper_reason, current_date);
326 }
327
328 if(ConfigFileEntry.kline_delay)
329 {
330 if(kline_queued == 0)
331 {
332 eventAddOnce("check_klines", check_klines_event, NULL,
333 ConfigFileEntry.kline_delay);
334 kline_queued = 1;
335 }
336 }
337 else
338 check_klines();
339
340 return;
341 }
342
343 /* mo_unkline()
344 *
345 * parv[1] - kline to remove
346 * parv[2] - optional "ON"
347 * parv[3] - optional target server
348 */
349 static int
350 mo_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
351 {
352 const char *user;
353 char *host;
354 char splat[] = "*";
355 char *h = LOCAL_COPY(parv[1]);
356 struct ConfItem *aconf;
357
358 if(!IsOperUnkline(source_p))
359 {
360 sendto_one(source_p, form_str(ERR_NOPRIVS),
361 me.name, source_p->name, "unkline");
362 return 0;
363 }
364
365 if((host = strchr(h, '@')) || *h == '*' || strchr(h, '.') || strchr(h, ':'))
366 {
367 /* Explicit user@host mask given */
368
369 if(host) /* Found user@host */
370 {
371 *host++ = '\0';
372
373 /* check for @host */
374 if(*h)
375 user = h;
376 else
377 user = splat;
378
379 /* check for user@ */
380 if(!*host)
381 host = splat;
382 }
383 else
384 {
385 user = splat; /* no @ found, assume its *@somehost */
386 host = h;
387 }
388 }
389 else
390 {
391 sendto_one_notice(source_p, ":Invalid parameters");
392 return 0;
393 }
394
395 /* possible remote kline.. */
396 if((parc > 3) && (irccmp(parv[2], "ON") == 0))
397 {
398 if(!IsOperRemoteBan(source_p))
399 {
400 sendto_one(source_p, form_str(ERR_NOPRIVS),
401 me.name, source_p->name, "remoteban");
402 return 0;
403 }
404
405 propagate_generic(source_p, "UNKLINE", parv[3], CAP_UNKLN,
406 "%s %s", user, host);
407
408 if(match(parv[3], me.name) == 0)
409 return 0;
410 }
411 else if(dlink_list_length(&cluster_conf_list) > 0)
412 cluster_generic(source_p, "UNKLINE", SHARED_UNKLINE, CAP_UNKLN,
413 "%s %s", user, host);
414
415 aconf = find_exact_conf_by_address(host, CONF_KILL, user);
416 if(aconf == NULL)
417 {
418 sendto_one_notice(source_p, ":No K-Line for %s@%s", user, host);
419 return 0;
420 }
421
422 if(remove_temp_kline(aconf))
423 {
424 sendto_one_notice(source_p, ":Un-klined [%s@%s] from temporary k-lines", user, host);
425 sendto_realops_snomask(SNO_GENERAL, L_ALL,
426 "%s has removed the temporary K-Line for: [%s@%s]",
427 get_oper_name(source_p), user, host);
428 ilog(L_KLINE, "UK %s %s %s",
429 get_oper_name(source_p), user, host);
430 return 0;
431 }
432
433 remove_permkline_match(source_p, aconf);
434
435 return 0;
436 }
437
438 /* ms_unkline()
439 *
440 * parv[1] - target server
441 * parv[2] - user to unkline
442 * parv[3] - host to unkline
443 */
444 static int
445 ms_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
446 {
447 /* parv[0] parv[1] parv[2] parv[3]
448 * oper target server user host */
449 propagate_generic(source_p, "UNKLINE", parv[1], CAP_UNKLN,
450 "%s %s", parv[2], parv[3]);
451
452 if(!match(parv[1], me.name))
453 return 0;
454
455 if(!IsPerson(source_p))
456 return 0;
457
458 handle_remote_unkline(source_p, parv[2], parv[3]);
459 return 0;
460 }
461
462 static int
463 me_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
464 {
465 /* user host */
466 if(!IsPerson(source_p))
467 return 0;
468
469 handle_remote_unkline(source_p, parv[1], parv[2]);
470 return 0;
471 }
472
473 static void
474 handle_remote_unkline(struct Client *source_p, const char *user, const char *host)
475 {
476 struct ConfItem *aconf;
477
478 if(!find_shared_conf(source_p->username, source_p->host,
479 source_p->servptr->name, SHARED_UNKLINE))
480 return;
481
482 aconf = find_exact_conf_by_address(host, CONF_KILL, user);
483 if(aconf == NULL)
484 {
485 sendto_one_notice(source_p, ":No K-Line for %s@%s", user, host);
486 return;
487 }
488
489 if(remove_temp_kline(aconf))
490 {
491 sendto_one_notice(source_p,
492 ":Un-klined [%s@%s] from temporary k-lines",
493 user, host);
494
495 sendto_realops_snomask(SNO_GENERAL, L_ALL,
496 "%s has removed the temporary K-Line for: [%s@%s]",
497 get_oper_name(source_p), user, host);
498
499 ilog(L_KLINE, "UK %s %s %s",
500 get_oper_name(source_p), user, host);
501 return;
502 }
503
504 remove_permkline_match(source_p, aconf);
505 }
506
507 /* apply_kline()
508 *
509 * inputs -
510 * output - NONE
511 * side effects - kline as given, is added to the hashtable
512 * and conf file
513 */
514 static void
515 apply_kline(struct Client *source_p, struct ConfItem *aconf,
516 const char *reason, const char *oper_reason, const char *current_date)
517 {
518 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, aconf);
519 write_confitem(KLINE_TYPE, source_p, aconf->user, aconf->host,
520 reason, oper_reason, current_date, 0);
521 }
522
523 /* apply_tkline()
524 *
525 * inputs -
526 * output - NONE
527 * side effects - tkline as given is placed
528 */
529 static void
530 apply_tkline(struct Client *source_p, struct ConfItem *aconf,
531 const char *reason, const char *oper_reason, const char *current_date, int tkline_time)
532 {
533 aconf->hold = CurrentTime + tkline_time;
534 add_temp_kline(aconf);
535
536 /* no oper reason.. */
537 if(EmptyString(oper_reason))
538 {
539 sendto_realops_snomask(SNO_GENERAL, L_ALL,
540 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
541 get_oper_name(source_p), tkline_time / 60,
542 aconf->user, aconf->host, reason);
543 ilog(L_KLINE, "K %s %d %s %s %s",
544 get_oper_name(source_p), tkline_time / 60,
545 aconf->user, aconf->host, reason);
546 }
547 else
548 {
549 sendto_realops_snomask(SNO_GENERAL, L_ALL,
550 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
551 get_oper_name(source_p), tkline_time / 60,
552 aconf->user, aconf->host, reason, oper_reason);
553 ilog(L_KLINE, "K %s %d %s %s %s|%s",
554 get_oper_name(source_p), tkline_time / 60,
555 aconf->user, aconf->host, reason, oper_reason);
556 }
557
558 sendto_one_notice(source_p, ":Added temporary %d min. K-Line [%s@%s]",
559 tkline_time / 60, aconf->user, aconf->host);
560 }
561
562 /* find_user_host()
563 *
564 * inputs - client placing kline, user@host, user buffer, host buffer
565 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
566 * side effects -
567 */
568 static int
569 find_user_host(struct Client *source_p, const char *userhost, char *luser, char *lhost)
570 {
571 char *hostp;
572
573 hostp = strchr(userhost, '@');
574
575 if(hostp != NULL) /* I'm a little user@host */
576 {
577 *(hostp++) = '\0'; /* short and squat */
578 if(*userhost)
579 strlcpy(luser, userhost, USERLEN + 1); /* here is my user */
580 else
581 strcpy(luser, "*");
582 if(*hostp)
583 strlcpy(lhost, hostp, HOSTLEN + 1); /* here is my host */
584 else
585 strcpy(lhost, "*");
586 }
587 else
588 {
589 /* no '@', no '.', so its not a user@host or host, therefore
590 * its a nick, which support was removed for.
591 */
592 if(strchr(userhost, '.') == NULL && strchr(userhost, ':') == NULL)
593 return 0;
594
595 luser[0] = '*'; /* no @ found, assume its *@somehost */
596 luser[1] = '\0';
597 strlcpy(lhost, userhost, HOSTLEN + 1);
598 }
599
600 return 1;
601 }
602
603 /* valid_user_host()
604 *
605 * inputs - user buffer, host buffer
606 * output - 0 if invalid, 1 if valid
607 * side effects -
608 */
609 static int
610 valid_user_host(struct Client *source_p, const char *luser, const char *lhost)
611 {
612 /* # is invalid, as is '!' (n!u@h kline) */
613 if(strchr(lhost, '#') || strchr(luser, '#') || strchr(luser, '!'))
614 {
615 sendto_one_notice(source_p, ":Invalid K-Line");
616 return 0;
617 }
618
619 return 1;
620 }
621
622 /* valid_wild_card()
623 *
624 * input - user buffer, host buffer
625 * output - 0 if invalid, 1 if valid
626 * side effects -
627 */
628 static int
629 valid_wild_card(struct Client *source_p, const char *luser, const char *lhost)
630 {
631 const char *p;
632 char tmpch;
633 int nonwild = 0;
634 int bitlen;
635
636 /* user has no wildcards, always accept -- jilles */
637 if (!strchr(luser, '?') && !strchr(luser, '*'))
638 return 1;
639
640 /* check there are enough non wildcard chars */
641 p = luser;
642 while ((tmpch = *p++))
643 {
644 if(!IsKWildChar(tmpch))
645 {
646 /* found enough chars, return */
647 if(++nonwild >= ConfigFileEntry.min_nonwildcard)
648 return 1;
649 }
650 }
651
652 /* try host, as user didnt contain enough */
653 /* special case for cidr masks -- jilles */
654 if ((p = strrchr(lhost, '/')) != NULL && IsDigit(p[1]))
655 {
656 bitlen = atoi(p + 1);
657 /* much like non-cidr for ipv6, rather arbitrary for ipv4 */
658 if (bitlen > 0 && bitlen >= (strchr(lhost, ':') ? 4 * (ConfigFileEntry.min_nonwildcard - nonwild) : 6 - 2 * nonwild))
659 return 1;
660 }
661 else
662 {
663 p = lhost;
664 while ((tmpch = *p++))
665 {
666 if(!IsKWildChar(tmpch))
667 if(++nonwild >= ConfigFileEntry.min_nonwildcard)
668 return 1;
669 }
670 }
671
672 sendto_one_notice(source_p,
673 ":Please include at least %d non-wildcard "
674 "characters with the user@host",
675 ConfigFileEntry.min_nonwildcard);
676 return 0;
677 }
678
679 /*
680 * valid_comment
681 * inputs - pointer to client
682 * - pointer to comment
683 * output - 0 if no valid comment, 1 if valid
684 * side effects - NONE
685 */
686 static int
687 valid_comment(struct Client *source_p, char *comment)
688 {
689 if(strchr(comment, '"'))
690 {
691 sendto_one_notice(source_p, ":Invalid character '\"' in comment");
692 return 0;
693 }
694
695 if(strlen(comment) > BANREASONLEN)
696 comment[BANREASONLEN] = '\0';
697
698 return 1;
699 }
700
701 /* already_placed_kline()
702 *
703 * inputs - source to notify, user@host to check, tkline time
704 * outputs - 1 if a perm kline or a tkline when a tkline is being
705 * set exists, else 0
706 * side effects - notifies source_p kline exists
707 */
708 /* Note: This currently works if the new K-line is a special case of an
709 * existing K-line, but not the other way round. To do that we would
710 * have to walk the hash and check every existing K-line. -A1kmm.
711 */
712 static int
713 already_placed_kline(struct Client *source_p, const char *luser, const char *lhost, int tkline)
714 {
715 const char *reason;
716 struct irc_sockaddr_storage iphost, *piphost;
717 struct ConfItem *aconf;
718 int t;
719 if(ConfigFileEntry.non_redundant_klines)
720 {
721 if((t = parse_netmask(lhost, (struct sockaddr *)&iphost, NULL)) != HM_HOST)
722 {
723 #ifdef IPV6
724 if(t == HM_IPV6)
725 t = AF_INET6;
726 else
727 #endif
728 t = AF_INET;
729
730 piphost = &iphost;
731 }
732 else
733 piphost = NULL;
734
735 if((aconf = find_conf_by_address(lhost, NULL, NULL, (struct sockaddr *)piphost, CONF_KILL, t, luser)))
736 {
737 /* setting a tkline, or existing one is perm */
738 if(tkline || ((aconf->flags & CONF_FLAGS_TEMPORARY) == 0))
739 {
740 reason = aconf->passwd ? aconf->passwd : "<No Reason>";
741
742 sendto_one_notice(source_p,
743 ":[%s@%s] already K-Lined by [%s@%s] - %s",
744 luser, lhost, aconf->user,
745 aconf->host, reason);
746 return 1;
747 }
748 }
749 }
750
751 return 0;
752 }
753
754 /* remove_permkline_match()
755 *
756 * hunts for a permanent kline, and removes it.
757 */
758 static void
759 remove_permkline_match(struct Client *source_p, struct ConfItem *aconf)
760 {
761 FILE *in, *out;
762 int pairme = 0;
763 int error_on_write = NO;
764 char buf[BUFSIZE];
765 char matchbuf[BUFSIZE];
766 char temppath[BUFSIZE];
767 const char *filename;
768 const char *host, *user;
769 mode_t oldumask;
770 int matchlen;
771
772 host = aconf->host;
773 user = aconf->user;
774
775 ircsnprintf(temppath, sizeof(temppath),
776 "%s.tmp", ConfigFileEntry.klinefile);
777
778 filename = get_conf_name(KLINE_TYPE);
779
780 if((in = fopen(filename, "r")) == 0)
781 {
782 sendto_one_notice(source_p, ":Cannot open %s", filename);
783 return;
784 }
785
786 oldumask = umask(0);
787 if((out = fopen(temppath, "w")) == 0)
788 {
789 sendto_one_notice(source_p, ":Cannot open %s", temppath);
790 fclose(in);
791 umask(oldumask);
792 return;
793 }
794
795 umask(oldumask);
796
797 snprintf(matchbuf, sizeof(matchbuf), "\"%s\",\"%s\"", user, host);
798 matchlen = strlen(matchbuf);
799
800 while (fgets(buf, sizeof(buf), in))
801 {
802 if(error_on_write)
803 break;
804
805 if(!strncasecmp(buf, matchbuf, matchlen))
806 {
807 pairme++;
808 break;
809 }
810 else
811 error_on_write = flush_write(source_p, out, buf, temppath);
812 }
813
814 /* we dropped out of the loop early because we found a match,
815 * to drop into this somewhat faster loop as we presume we'll never
816 * have two matching klines --anfl
817 */
818 if(pairme && !error_on_write)
819 {
820 while(fgets(buf, sizeof(buf), in))
821 {
822 if(error_on_write)
823 break;
824
825 error_on_write = flush_write(source_p, out, buf, temppath);
826 }
827 }
828
829 fclose(in);
830 if (fclose(out))
831 error_on_write = YES;
832
833 /* The result of the rename should be checked too... oh well */
834 /* If there was an error on a write above, then its been reported
835 * and I am not going to trash the original kline /conf file
836 */
837 if(error_on_write)
838 {
839 sendto_one_notice(source_p, ":Couldn't write temp kline file, aborted");
840 return;
841 }
842 else if(!pairme)
843 {
844 sendto_one_notice(source_p, ":Cannot find K-Line for %s@%s in file",
845 user, host);
846
847 if(temppath != NULL)
848 (void) unlink(temppath);
849
850 return;
851 }
852
853 if (rename(temppath, filename))
854 {
855 sendto_one_notice(source_p, ":Couldn't rename temp file, aborted");
856 return;
857 }
858
859 sendto_one_notice(source_p, ":K-Line for [%s@%s] is removed",
860 user, host);
861
862 sendto_realops_snomask(SNO_GENERAL, L_ALL,
863 "%s has removed the K-Line for: [%s@%s]",
864 get_oper_name(source_p), user, host);
865
866 ilog(L_KLINE, "UK %s %s %s",
867 get_oper_name(source_p), user, host);
868
869 delete_one_address_conf(aconf->host, aconf);
870
871 return;
872 }
873
874 /*
875 * flush_write()
876 *
877 * inputs - pointer to client structure of oper requesting unkline
878 * - out is the file descriptor
879 * - buf is the buffer to write
880 * - ntowrite is the expected number of character to be written
881 * - temppath is the temporary file name to be written
882 * output - YES for error on write
883 * - NO for success
884 * side effects - if successful, the buf is written to output file
885 * if a write failure happesn, and the file pointed to
886 * by temppath, if its non NULL, is removed.
887 *
888 * The idea here is, to be as robust as possible when writing to the
889 * kline file.
890 *
891 * -Dianora
892 */
893
894 static int
895 flush_write(struct Client *source_p, FILE * out, const char *buf, const char *temppath)
896 {
897 int error_on_write = (fputs(buf, out) < 0) ? YES : NO;
898
899 if(error_on_write)
900 {
901 sendto_one_notice(source_p, ":Unable to write to %s",
902 temppath);
903 if(temppath != NULL)
904 (void) unlink(temppath);
905 }
906 return (error_on_write);
907 }
908
909 /* remove_temp_kline()
910 *
911 * inputs - username, hostname to unkline
912 * outputs -
913 * side effects - tries to unkline anything that matches
914 */
915 static int
916 remove_temp_kline(struct ConfItem *aconf)
917 {
918 dlink_node *ptr;
919 int i;
920
921 for (i = 0; i < LAST_TEMP_TYPE; i++)
922 {
923 DLINK_FOREACH(ptr, temp_klines[i].head)
924 {
925 if (aconf == ptr->data)
926 {
927 dlinkDestroy(ptr, &temp_klines[i]);
928 delete_one_address_conf(aconf->host, aconf);
929 return YES;
930 }
931 }
932 }
933
934 return NO;
935 }