Remove cruft left behind by removed stats_p_hook.
[seven-1.x.git] / modules / m_kline.c
blobf7633ad706522541d261f5824e90bf9c8ae740bf
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_kline.c: Bans/unbans a user.
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
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.
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.
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
25 #include "stdinc.h"
26 #include "tools.h"
27 #include "channel.h"
28 #include "class.h"
29 #include "client.h"
30 #include "common.h"
31 #include "irc_string.h"
32 #include "sprintf_irc.h"
33 #include "ircd.h"
34 #include "hostmask.h"
35 #include "numeric.h"
36 #include "commio.h"
37 #include "s_conf.h"
38 #include "s_newconf.h"
39 #include "s_log.h"
40 #include "send.h"
41 #include "hash.h"
42 #include "s_serv.h"
43 #include "msg.h"
44 #include "parse.h"
45 #include "modules.h"
46 #include "event.h"
48 static int mo_kline(struct Client *, struct Client *, int, const char **);
49 static int ms_kline(struct Client *, struct Client *, int, const char **);
50 static int me_kline(struct Client *, struct Client *, int, const char **);
51 static int mo_unkline(struct Client *, struct Client *, int, const char **);
52 static int ms_unkline(struct Client *, struct Client *, int, const char **);
53 static int me_unkline(struct Client *, struct Client *, int, const char **);
55 struct Message kline_msgtab = {
56 "KLINE", 0, 0, 0, MFLG_SLOW,
57 {mg_unreg, mg_not_oper, {ms_kline, 5}, {ms_kline, 5}, {me_kline, 5}, {mo_kline, 3}}
60 struct Message unkline_msgtab = {
61 "UNKLINE", 0, 0, 0, MFLG_SLOW,
62 {mg_unreg, mg_not_oper, {ms_unkline, 4}, {ms_unkline, 4}, {me_unkline, 3}, {mo_unkline, 2}}
65 mapi_clist_av1 kline_clist[] = { &kline_msgtab, &unkline_msgtab, NULL };
66 DECLARE_MODULE_AV1(kline, NULL, NULL, kline_clist, NULL, NULL, "$Revision: 125 $");
68 /* Local function prototypes */
69 static int find_user_host(struct Client *source_p, const char *userhost, char *user, char *host);
70 static int valid_comment(struct Client *source_p, char *comment);
71 static int valid_user_host(struct Client *source_p, const char *user, const char *host);
72 static int valid_wild_card(struct Client *source_p, const char *user, const char *host);
74 static void handle_remote_kline(struct Client *source_p, int tkline_time,
75 const char *user, const char *host, const char *reason);
76 static void apply_kline(struct Client *source_p, struct ConfItem *aconf,
77 const char *reason, const char *oper_reason, const char *current_date);
78 static void apply_tkline(struct Client *source_p, struct ConfItem *aconf,
79 const char *, const char *, const char *, int);
80 static int already_placed_kline(struct Client *, const char *, const char *, int);
82 static void handle_remote_unkline(struct Client *source_p,
83 const char *user, const char *host);
84 static void remove_permkline_match(struct Client *, const char *, const char *);
85 static int flush_write(struct Client *, FILE *, const char *, const char *);
86 static int remove_temp_kline(const char *, const char *);
88 /* mo_kline()
90 * parv[1] - temp time or user@host
91 * parv[2] - user@host, "ON", or reason
92 * parv[3] - "ON", reason, or server to target
93 * parv[4] - server to target, or reason
94 * parv[5] - reason
96 static int
97 mo_kline(struct Client *client_p, struct Client *source_p,
98 int parc, const char **parv)
100 char def[] = "No Reason";
101 char user[USERLEN + 2];
102 char host[HOSTLEN + 2];
103 char buffer[IRCD_BUFSIZE];
104 char *reason = def;
105 char *oper_reason;
106 const char *current_date;
107 const char *target_server = NULL;
108 struct ConfItem *aconf;
109 int tkline_time = 0;
110 int loc = 1;
112 if(!IsOperK(source_p))
114 sendto_one(source_p, form_str(ERR_NOPRIVS),
115 me.name, source_p->name, "kline");
116 return 0;
119 if((tkline_time = valid_temp_time(parv[loc])) >= 0)
120 loc++;
121 /* we just set tkline_time to -1! */
122 else
123 tkline_time = 0;
125 if(find_user_host(source_p, parv[loc], user, host) == 0)
126 return 0;
128 loc++;
130 if(parc >= loc+2 && !irccmp(parv[loc], "ON"))
132 target_server = parv[loc+1];
133 loc += 2;
136 if(parc <= loc || EmptyString(parv[loc]))
138 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
139 me.name, source_p->name, "KLINE");
140 return 0;
143 reason = LOCAL_COPY(parv[loc]);
145 if(target_server != NULL)
147 if(!ConfigFileEntry.allow_kline_on)
149 sendto_one(source_p, ":%s NOTICE %s :*** KLINE ON server.name is disabled.",
150 me.name, source_p->name);
151 return 0;
153 else if(!IsOperRemoteBan(source_p))
155 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name,
156 "remoteban");
157 return 0;
160 if(tkline_time > 0)
161 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s added temporary %d minute K-Line for [%s@%s] on %s [%s]",
162 get_oper_name(source_p), tkline_time / 60, user, host, target_server, reason);
163 else
164 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s added K-Line for [%s@%s] on %s [%s]",
165 get_oper_name(source_p), user, host, target_server, reason);
167 propagate_generic(source_p, "KLINE", target_server, CAP_KLN,
168 "%d %s %s :%s",
169 tkline_time, user, host, reason);
171 /* If we are sending it somewhere that doesnt include us, stop */
172 if(!match(target_server, me.name))
173 return 0;
175 /* if we have cluster servers, send it to them.. */
176 else if(dlink_list_length(&cluster_conf_list) > 0)
177 cluster_generic(source_p, "KLINE",
178 (tkline_time > 0) ? SHARED_TKLINE : SHARED_PKLINE, CAP_KLN,
179 "%lu %s %s :%s",
180 tkline_time, user, host, reason);
182 if(!valid_user_host(source_p, user, host) ||
183 !valid_wild_card(source_p, user, host) ||
184 !valid_comment(source_p, reason))
185 return 0;
187 if(already_placed_kline(source_p, user, host, tkline_time))
188 return 0;
190 set_time();
191 current_date = smalldate();
192 aconf = make_conf();
193 aconf->status = CONF_KILL;
194 DupString(aconf->host, host);
195 DupString(aconf->user, user);
196 aconf->port = 0;
198 /* Look for an oper reason */
199 if((oper_reason = strchr(reason, '|')) != NULL)
201 *oper_reason = '\0';
202 oper_reason++;
204 if(!EmptyString(oper_reason))
205 DupString(aconf->spasswd, oper_reason);
208 if(tkline_time > 0)
210 ircsnprintf(buffer, sizeof(buffer),
211 "Temporary K-line %d min. - %s (%s)",
212 (int) (tkline_time / 60), reason, current_date);
213 DupString(aconf->passwd, buffer);
214 apply_tkline(source_p, aconf, reason, oper_reason, current_date, tkline_time);
216 else
218 ircsnprintf(buffer, sizeof(buffer), "%s (%s)", reason, current_date);
219 DupString(aconf->passwd, buffer);
220 apply_kline(source_p, aconf, reason, oper_reason, current_date);
223 if(ConfigFileEntry.kline_delay)
225 if(kline_queued == 0)
227 eventAddOnce("check_klines", check_klines_event, NULL,
228 ConfigFileEntry.kline_delay);
229 kline_queued = 1;
232 else
233 check_klines();
235 return 0;
238 /* ms_kline()
240 * parv[1] - server targeted at
241 * parv[2] - tkline time (0 if perm)
242 * parv[3] - user
243 * parv[4] - host
244 * parv[5] - reason
246 static int
247 ms_kline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
249 int tkline_time = atoi(parv[2]);
251 /* 1.5-3 and earlier contains a bug that allows remote klines to be
252 * sent with an empty reason field. This is a protocol violation,
253 * but its not worth dropping the link over.. --anfl
255 if(parc < 6 || EmptyString(parv[5]))
256 return 0;
258 propagate_generic(source_p, "KLINE", parv[1], CAP_KLN,
259 "%d %s %s :%s",
260 tkline_time, parv[3], parv[4], parv[5]);
262 if(!match(parv[1], me.name))
263 return 0;
265 if(!IsPerson(source_p))
266 return 0;
268 handle_remote_kline(source_p, tkline_time, parv[3], parv[4], parv[5]);
269 return 0;
272 static int
273 me_kline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
275 /* <tkline_time> <user> <host> :<reason> */
276 if(!IsPerson(source_p))
277 return 0;
279 handle_remote_kline(source_p, atoi(parv[1]), parv[2], parv[3], parv[4]);
280 return 0;
283 static void
284 handle_remote_kline(struct Client *source_p, int tkline_time,
285 const char *user, const char *host, const char *kreason)
287 char buffer[BUFSIZE];
288 const char *current_date;
289 char *reason = LOCAL_COPY(kreason);
290 struct ConfItem *aconf = NULL;
291 char *oper_reason;
293 if (!find_client_shared_conf(source_p, tkline_time > 0 ? SHARED_TKLINE : SHARED_PKLINE))
294 return;
296 if(!valid_user_host(source_p, user, host) ||
297 !valid_wild_card(source_p, user, host) ||
298 !valid_comment(source_p, reason))
299 return;
301 if(already_placed_kline(source_p, user, host, tkline_time))
302 return;
304 aconf = make_conf();
306 aconf->status = CONF_KILL;
307 DupString(aconf->user, user);
308 DupString(aconf->host, host);
310 /* Look for an oper reason */
311 if((oper_reason = strchr(reason, '|')) != NULL)
313 *oper_reason = '\0';
314 oper_reason++;
316 if(!EmptyString(oper_reason))
317 DupString(aconf->spasswd, oper_reason);
320 current_date = smalldate();
322 if(tkline_time > 0)
324 ircsnprintf(buffer, sizeof(buffer),
325 "Temporary K-line %d min. - %s (%s)",
326 (int) (tkline_time / 60), reason, current_date);
327 DupString(aconf->passwd, buffer);
328 apply_tkline(source_p, aconf, reason, oper_reason, current_date, tkline_time);
330 else
332 ircsnprintf(buffer, sizeof(buffer), "%s (%s)", reason, current_date);
333 DupString(aconf->passwd, buffer);
334 apply_kline(source_p, aconf, reason, oper_reason, current_date);
337 if(ConfigFileEntry.kline_delay)
339 if(kline_queued == 0)
341 eventAddOnce("check_klines", check_klines_event, NULL,
342 ConfigFileEntry.kline_delay);
343 kline_queued = 1;
346 else
347 check_klines();
349 return;
352 /* mo_unkline()
354 * parv[1] - kline to remove
355 * parv[2] - optional "ON"
356 * parv[3] - optional target server
358 static int
359 mo_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
361 const char *user;
362 char *host;
363 char splat[] = "*";
364 char *h = LOCAL_COPY(parv[1]);
366 if(!IsOperUnkline(source_p))
368 sendto_one(source_p, form_str(ERR_NOPRIVS),
369 me.name, source_p->name, "unkline");
370 return 0;
373 if((host = strchr(h, '@')) || *h == '*')
375 /* Explicit user@host mask given */
377 if(host) /* Found user@host */
379 *host++ = '\0';
381 /* check for @host */
382 if(*h)
383 user = h;
384 else
385 user = splat;
387 /* check for user@ */
388 if(!*host)
389 host = splat;
391 else
393 user = splat; /* no @ found, assume its *@somehost */
394 host = h;
397 else
399 sendto_one(source_p, ":%s NOTICE %s :Invalid parameters", me.name, source_p->name);
400 return 0;
403 /* possible remote kline.. */
404 if((parc > 3) && (irccmp(parv[2], "ON") == 0))
406 if (!IsOperRemoteBan(source_p))
408 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name,
409 "remoteban");
411 return 0;
414 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
415 "%s is removing K-Line for [%s@%s] on %s",
416 get_oper_name(source_p), user, host, parv[3]);
418 propagate_generic(source_p, "UNKLINE", parv[3], CAP_UNKLN,
419 "%s %s", user, host);
421 if(match(parv[3], me.name) == 0)
422 return 0;
424 else if(dlink_list_length(&cluster_conf_list) > 0)
425 cluster_generic(source_p, "UNKLINE", SHARED_UNKLINE, CAP_UNKLN,
426 "%s %s", user, host);
428 if(remove_temp_kline(user, host))
430 sendto_one(source_p,
431 ":%s NOTICE %s :Un-klined [%s@%s] from temporary k-lines",
432 me.name, parv[0], user, host);
433 sendto_realops_snomask(SNO_GENERAL, L_ALL,
434 "%s has removed the temporary K-Line for: [%s@%s]",
435 get_oper_name(source_p), user, host);
436 ilog(L_KLINE, "UK %s %s %s",
437 get_oper_name(source_p), user, host);
438 return 0;
441 remove_permkline_match(source_p, host, user);
443 return 0;
446 /* ms_unkline()
448 * parv[1] - target server
449 * parv[2] - user to unkline
450 * parv[3] - host to unkline
452 static int
453 ms_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
455 /* parv[0] parv[1] parv[2] parv[3]
456 * oper target server user host */
457 propagate_generic(source_p, "UNKLINE", parv[1], CAP_UNKLN,
458 "%s %s", parv[2], parv[3]);
460 if(!match(parv[1], me.name))
461 return 0;
463 if(!IsPerson(source_p))
464 return 0;
466 handle_remote_unkline(source_p, parv[2], parv[3]);
467 return 0;
470 static int
471 me_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
473 /* user host */
474 if(!IsPerson(source_p))
475 return 0;
477 handle_remote_unkline(source_p, parv[1], parv[2]);
478 return 0;
481 static void
482 handle_remote_unkline(struct Client *source_p, const char *user, const char *host)
484 if (!find_client_shared_conf(source_p, SHARED_UNKLINE))
485 return;
487 if(remove_temp_kline(user, host))
489 sendto_one_notice(source_p,
490 ":Un-klined [%s@%s] from temporary k-lines",
491 user, host);
493 sendto_realops_snomask(SNO_GENERAL, L_ALL,
494 "%s has removed the temporary K-Line for: [%s@%s]",
495 get_oper_name(source_p), user, host);
497 ilog(L_KLINE, "UK %s %s %s",
498 get_oper_name(source_p), user, host);
499 return;
502 remove_permkline_match(source_p, host, user);
505 /* apply_kline()
507 * inputs -
508 * output - NONE
509 * side effects - kline as given, is added to the hashtable
510 * and conf file
512 static void
513 apply_kline(struct Client *source_p, struct ConfItem *aconf,
514 const char *reason, const char *oper_reason, const char *current_date)
516 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, aconf);
517 write_confitem(KLINE_TYPE, source_p, aconf->user, aconf->host,
518 reason, oper_reason, current_date, 0);
521 /* apply_tkline()
523 * inputs -
524 * output - NONE
525 * side effects - tkline as given is placed
527 static void
528 apply_tkline(struct Client *source_p, struct ConfItem *aconf,
529 const char *reason, const char *oper_reason, const char *current_date, int tkline_time)
531 aconf->hold = CurrentTime + tkline_time;
532 add_temp_kline(aconf);
534 /* no oper reason.. */
535 if(EmptyString(oper_reason))
537 sendto_realops_snomask(SNO_GENERAL, L_ALL,
538 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
539 get_oper_name(source_p), tkline_time / 60,
540 aconf->user, aconf->host, reason);
541 ilog(L_KLINE, "K %s %d %s %s %s",
542 get_oper_name(source_p), tkline_time / 60,
543 aconf->user, aconf->host, reason);
545 else
547 sendto_realops_snomask(SNO_GENERAL, L_ALL,
548 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
549 get_oper_name(source_p), tkline_time / 60,
550 aconf->user, aconf->host, reason, oper_reason);
551 ilog(L_KLINE, "K %s %d %s %s %s|%s",
552 get_oper_name(source_p), tkline_time / 60,
553 aconf->user, aconf->host, reason, oper_reason);
556 sendto_one_notice(source_p, ":Added temporary %d min. K-Line [%s@%s]",
557 tkline_time / 60, aconf->user, aconf->host);
560 /* find_user_host()
562 * inputs - client placing kline, user@host, user buffer, host buffer
563 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
564 * side effects -
566 static int
567 find_user_host(struct Client *source_p, const char *userhost, char *luser, char *lhost)
569 char *hostp;
571 hostp = strchr(userhost, '@');
573 if(hostp != NULL) /* I'm a little user@host */
575 *(hostp++) = '\0'; /* short and squat */
576 if(*userhost)
577 strlcpy(luser, userhost, USERLEN + 1); /* here is my user */
578 else
579 strcpy(luser, "*");
580 if(*hostp)
581 strlcpy(lhost, hostp, HOSTLEN + 1); /* here is my host */
582 else
583 strcpy(lhost, "*");
585 else
587 /* no '@', no '.', so its not a user@host or host, therefore
588 * its a nick, which support was removed for.
590 if(strchr(userhost, '.') == NULL)
591 return 0;
593 luser[0] = '*'; /* no @ found, assume its *@somehost */
594 luser[1] = '\0';
595 strlcpy(lhost, userhost, HOSTLEN + 1);
598 return 1;
601 /* valid_user_host()
603 * inputs - user buffer, host buffer
604 * output - 0 if invalid, 1 if valid
605 * side effects -
607 static int
608 valid_user_host(struct Client *source_p, const char *luser, const char *lhost)
610 /* # is invalid, as is '!' (n!u@h kline) */
611 if(strchr(lhost, '#') || strchr(luser, '#') || strchr(luser, '!'))
613 sendto_one_notice(source_p, ":Invalid K-Line");
614 return 0;
617 return 1;
620 /* valid_wild_card()
622 * input - user buffer, host buffer
623 * output - 0 if invalid, 1 if valid
624 * side effects -
626 static int
627 valid_wild_card(struct Client *source_p, const char *luser, const char *lhost)
629 const char *p;
630 char tmpch;
631 int nonwild = 0;
633 /* check there are enough non wildcard chars */
634 p = luser;
635 while ((tmpch = *p++))
637 if(!IsKWildChar(tmpch))
639 /* found enough chars, return */
640 if(++nonwild >= ConfigFileEntry.min_nonwildcard)
641 return 1;
645 /* try host, as user didnt contain enough */
646 p = lhost;
647 while ((tmpch = *p++))
649 if(!IsKWildChar(tmpch))
650 if(++nonwild >= ConfigFileEntry.min_nonwildcard)
651 return 1;
654 sendto_one_notice(source_p,
655 ":Please include at least %d non-wildcard "
656 "characters with the user@host",
657 ConfigFileEntry.min_nonwildcard);
658 return 0;
662 * valid_comment
663 * inputs - pointer to client
664 * - pointer to comment
665 * output - 0 if no valid comment, 1 if valid
666 * side effects - NONE
668 static int
669 valid_comment(struct Client *source_p, char *comment)
671 if(strchr(comment, '"'))
673 sendto_one_notice(source_p, ":Invalid character '\"' in comment");
674 return 0;
677 if(strlen(comment) > REASONLEN)
678 comment[REASONLEN] = '\0';
680 return 1;
683 /* already_placed_kline()
685 * inputs - source to notify, user@host to check, tkline time
686 * outputs - 1 if a perm kline or a tkline when a tkline is being
687 * set exists, else 0
688 * side effects - notifies source_p kline exists
690 /* Note: This currently works if the new K-line is a special case of an
691 * existing K-line, but not the other way round. To do that we would
692 * have to walk the hash and check every existing K-line. -A1kmm.
694 static int
695 already_placed_kline(struct Client *source_p, const char *luser, const char *lhost, int tkline)
697 const char *reason;
698 struct irc_sockaddr_storage iphost, *piphost;
699 struct ConfItem *aconf;
700 int t;
701 if(ConfigFileEntry.non_redundant_klines)
703 if((t = parse_netmask(lhost, (struct sockaddr *)&iphost, NULL)) != HM_HOST)
705 #ifdef IPV6
706 if(t == HM_IPV6)
707 t = AF_INET6;
708 else
709 #endif
710 t = AF_INET;
712 piphost = &iphost;
714 else
715 piphost = NULL;
717 if((aconf = find_conf_by_address(lhost, NULL, NULL, (struct sockaddr *)piphost, CONF_KILL, t, luser)))
719 /* setting a tkline, or existing one is perm */
720 if(tkline || ((aconf->flags & CONF_FLAGS_TEMPORARY) == 0))
722 reason = aconf->passwd ? aconf->passwd : "<No Reason>";
724 sendto_one_notice(source_p,
725 ":[%s@%s] already K-Lined by [%s@%s] - %s",
726 luser, lhost, aconf->user,
727 aconf->host, reason);
728 return 1;
733 return 0;
736 /* remove_permkline_match()
738 * hunts for a permanent kline, and removes it.
740 static void
741 remove_permkline_match(struct Client *source_p, const char *host, const char *user)
743 FILE *in, *out;
744 int pairme = 0;
745 int error_on_write = NO;
746 char buf[BUFSIZE];
747 char matchbuf[BUFSIZE];
748 char temppath[BUFSIZE];
749 const char *filename;
750 mode_t oldumask;
751 int matchlen;
753 ircsnprintf(temppath, sizeof(temppath),
754 "%s.tmp", ConfigFileEntry.klinefile);
756 filename = get_conf_name(KLINE_TYPE);
758 if((in = fopen(filename, "r")) == 0)
760 sendto_one_notice(source_p, ":Cannot open %s", filename);
761 return;
764 oldumask = umask(0);
765 if((out = fopen(temppath, "w")) == 0)
767 sendto_one_notice(source_p, ":Cannot open %s", temppath);
768 fclose(in);
769 umask(oldumask);
770 return;
773 umask(oldumask);
775 snprintf(matchbuf, sizeof(matchbuf), "\"%s\",\"%s\"", user, host);
776 matchlen = strlen(matchbuf);
778 while (fgets(buf, sizeof(buf), in))
780 if(error_on_write)
781 break;
783 if(!strncasecmp(buf, matchbuf, matchlen))
785 pairme++;
786 break;
788 else
789 error_on_write = flush_write(source_p, out, buf, temppath);
792 /* we dropped out of the loop early because we found a match,
793 * to drop into this somewhat faster loop as we presume we'll never
794 * have two matching klines --anfl
796 if(pairme && !error_on_write)
798 while(fgets(buf, sizeof(buf), in))
800 if(error_on_write)
801 break;
803 error_on_write = flush_write(source_p, out, buf, temppath);
807 fclose(in);
808 fclose(out);
810 /* The result of the rename should be checked too... oh well */
811 /* If there was an error on a write above, then its been reported
812 * and I am not going to trash the original kline /conf file
814 if(error_on_write)
816 sendto_one_notice(source_p, ":Couldn't write temp kline file, aborted");
817 return;
819 else if(!pairme)
821 sendto_one_notice(source_p, ":No K-Line for %s@%s",
822 user, host);
824 if(temppath != NULL)
825 (void) unlink(temppath);
827 return;
830 (void) rename(temppath, filename);
831 rehash_bans(0);
833 sendto_one_notice(source_p, ":K-Line for [%s@%s] is removed",
834 user, host);
836 sendto_realops_snomask(SNO_GENERAL, L_ALL,
837 "%s has removed the K-Line for: [%s@%s]",
838 get_oper_name(source_p), user, host);
840 ilog(L_KLINE, "UK %s %s %s",
841 get_oper_name(source_p), user, host);
842 return;
846 * flush_write()
848 * inputs - pointer to client structure of oper requesting unkline
849 * - out is the file descriptor
850 * - buf is the buffer to write
851 * - ntowrite is the expected number of character to be written
852 * - temppath is the temporary file name to be written
853 * output - YES for error on write
854 * - NO for success
855 * side effects - if successful, the buf is written to output file
856 * if a write failure happesn, and the file pointed to
857 * by temppath, if its non NULL, is removed.
859 * The idea here is, to be as robust as possible when writing to the
860 * kline file.
862 * -Dianora
865 static int
866 flush_write(struct Client *source_p, FILE * out, const char *buf, const char *temppath)
868 int error_on_write = (fputs(buf, out) < 0) ? YES : NO;
870 if(error_on_write)
872 sendto_one_notice(source_p, ":Unable to write to %s",
873 temppath);
874 if(temppath != NULL)
875 (void) unlink(temppath);
877 return (error_on_write);
880 /* remove_temp_kline()
882 * inputs - username, hostname to unkline
883 * outputs -
884 * side effects - tries to unkline anything that matches
886 static int
887 remove_temp_kline(const char *user, const char *host)
889 struct ConfItem *aconf;
890 dlink_node *ptr;
891 struct irc_sockaddr_storage addr, caddr;
892 int bits, cbits;
893 int mtype, ktype;
894 int i;
896 mtype = parse_netmask(host, (struct sockaddr *)&addr, &bits);
898 for (i = 0; i < LAST_TEMP_TYPE; i++)
900 DLINK_FOREACH(ptr, temp_klines[i].head)
902 aconf = ptr->data;
904 ktype = parse_netmask(aconf->host, (struct sockaddr *)&caddr, &cbits);
906 if(ktype != mtype || (user && irccmp(user, aconf->user)))
907 continue;
909 if(ktype == HM_HOST)
911 if(irccmp(aconf->host, host))
912 continue;
914 else if(bits != cbits ||
915 !comp_with_mask_sock((struct sockaddr *)&addr,
916 (struct sockaddr *)&caddr, bits))
917 continue;
919 dlinkDestroy(ptr, &temp_klines[i]);
920 delete_one_address_conf(aconf->host, aconf);
921 return YES;
925 return NO;