Import from http://svn.freenode.net/ircd-seven/private/beu/seven (r196).
[seven-1.x.git] / modules / m_kline.c
blob356c1a1c5be096d82b40cb2de4641e2df4597c93
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
24 * $Id: m_kline.c 125 2006-11-12 19:39:18Z spb $
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"
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 **);
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}}
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}}
67 mapi_clist_av1 kline_clist[] = { &kline_msgtab, &unkline_msgtab, NULL };
68 DECLARE_MODULE_AV1(kline, NULL, NULL, kline_clist, NULL, NULL, "$Revision: 125 $");
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);
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);
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 *, const char *, const char *);
87 static int flush_write(struct Client *, FILE *, const char *, const char *);
88 static int remove_temp_kline(const char *, const char *);
90 /* mo_kline()
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
98 static int
99 mo_kline(struct Client *client_p, struct Client *source_p,
100 int parc, const char **parv)
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;
114 if(!IsOperK(source_p))
116 sendto_one(source_p, form_str(ERR_NOPRIVS),
117 me.name, source_p->name, "kline");
118 return 0;
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;
127 if(find_user_host(source_p, parv[loc], user, host) == 0)
128 return 0;
130 loc++;
132 if(parc >= loc+2 && !irccmp(parv[loc], "ON"))
134 target_server = parv[loc+1];
135 loc += 2;
138 if(parc <= loc || EmptyString(parv[loc]))
140 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
141 me.name, source_p->name, "KLINE");
142 return 0;
145 reason = LOCAL_COPY(parv[loc]);
147 if(target_server != NULL)
149 if(!ConfigFileEntry.allow_kline_on)
151 sendto_one(source_p, ":%s NOTICE %s :*** KLINE ON server.name is disabled.",
152 me.name, source_p->name);
153 return 0;
155 else if(!IsOperRemoteBan(source_p))
157 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name,
158 "remoteban");
159 return 0;
162 if(tkline_time > 0)
163 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s added temporary %d minute K-Line for [%s@%s] on %s [%s]",
164 get_oper_name(source_p), tkline_time / 60, user, host, target_server, reason);
165 else
166 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s added K-Line for [%s@%s] on %s [%s]",
167 get_oper_name(source_p), user, host, target_server, reason);
169 propagate_generic(source_p, "KLINE", target_server, CAP_KLN,
170 "%d %s %s :%s",
171 tkline_time, user, host, reason);
173 /* If we are sending it somewhere that doesnt include us, stop */
174 if(!match(target_server, me.name))
175 return 0;
177 /* if we have cluster servers, send it to them.. */
178 else if(dlink_list_length(&cluster_conf_list) > 0)
179 cluster_generic(source_p, "KLINE",
180 (tkline_time > 0) ? SHARED_TKLINE : SHARED_PKLINE, CAP_KLN,
181 "%lu %s %s :%s",
182 tkline_time, user, host, reason);
184 if(!valid_user_host(source_p, user, host) ||
185 !valid_wild_card(source_p, user, host) ||
186 !valid_comment(source_p, reason))
187 return 0;
189 if(already_placed_kline(source_p, user, host, tkline_time))
190 return 0;
192 set_time();
193 current_date = smalldate();
194 aconf = make_conf();
195 aconf->status = CONF_KILL;
196 DupString(aconf->host, host);
197 DupString(aconf->user, user);
198 aconf->port = 0;
200 /* Look for an oper reason */
201 if((oper_reason = strchr(reason, '|')) != NULL)
203 *oper_reason = '\0';
204 oper_reason++;
206 if(!EmptyString(oper_reason))
207 DupString(aconf->spasswd, oper_reason);
210 if(tkline_time > 0)
212 ircsnprintf(buffer, sizeof(buffer),
213 "Temporary K-line %d min. - %s (%s)",
214 (int) (tkline_time / 60), reason, current_date);
215 DupString(aconf->passwd, buffer);
216 apply_tkline(source_p, aconf, reason, oper_reason, current_date, tkline_time);
218 else
220 ircsnprintf(buffer, sizeof(buffer), "%s (%s)", reason, current_date);
221 DupString(aconf->passwd, buffer);
222 apply_kline(source_p, aconf, reason, oper_reason, current_date);
225 if(ConfigFileEntry.kline_delay)
227 if(kline_queued == 0)
229 eventAddOnce("check_klines", check_klines_event, NULL,
230 ConfigFileEntry.kline_delay);
231 kline_queued = 1;
234 else
235 check_klines();
237 return 0;
240 /* ms_kline()
242 * parv[1] - server targeted at
243 * parv[2] - tkline time (0 if perm)
244 * parv[3] - user
245 * parv[4] - host
246 * parv[5] - reason
248 static int
249 ms_kline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
251 int tkline_time = atoi(parv[2]);
253 /* 1.5-3 and earlier contains a bug that allows remote klines to be
254 * sent with an empty reason field. This is a protocol violation,
255 * but its not worth dropping the link over.. --anfl
257 if(parc < 6 || EmptyString(parv[5]))
258 return 0;
260 propagate_generic(source_p, "KLINE", parv[1], CAP_KLN,
261 "%d %s %s :%s",
262 tkline_time, parv[3], parv[4], parv[5]);
264 if(!match(parv[1], me.name))
265 return 0;
267 if(!IsPerson(source_p))
268 return 0;
270 handle_remote_kline(source_p, tkline_time, parv[3], parv[4], parv[5]);
271 return 0;
274 static int
275 me_kline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
277 /* <tkline_time> <user> <host> :<reason> */
278 if(!IsPerson(source_p))
279 return 0;
281 handle_remote_kline(source_p, atoi(parv[1]), parv[2], parv[3], parv[4]);
282 return 0;
285 static void
286 handle_remote_kline(struct Client *source_p, int tkline_time,
287 const char *user, const char *host, const char *kreason)
289 char buffer[BUFSIZE];
290 const char *current_date;
291 char *reason = LOCAL_COPY(kreason);
292 struct ConfItem *aconf = NULL;
293 char *oper_reason;
295 if(!find_shared_conf(source_p->username, source_p->host,
296 source_p->user->server,
297 (tkline_time > 0) ? SHARED_TKLINE : SHARED_PKLINE))
298 return;
300 if(!valid_user_host(source_p, user, host) ||
301 !valid_wild_card(source_p, user, host) ||
302 !valid_comment(source_p, reason))
303 return;
305 if(already_placed_kline(source_p, user, host, tkline_time))
306 return;
308 aconf = make_conf();
310 aconf->status = CONF_KILL;
311 DupString(aconf->user, user);
312 DupString(aconf->host, host);
314 /* Look for an oper reason */
315 if((oper_reason = strchr(reason, '|')) != NULL)
317 *oper_reason = '\0';
318 oper_reason++;
320 if(!EmptyString(oper_reason))
321 DupString(aconf->spasswd, oper_reason);
324 current_date = smalldate();
326 if(tkline_time > 0)
328 ircsnprintf(buffer, sizeof(buffer),
329 "Temporary K-line %d min. - %s (%s)",
330 (int) (tkline_time / 60), reason, current_date);
331 DupString(aconf->passwd, buffer);
332 apply_tkline(source_p, aconf, reason, oper_reason, current_date, tkline_time);
334 else
336 ircsnprintf(buffer, sizeof(buffer), "%s (%s)", reason, current_date);
337 DupString(aconf->passwd, buffer);
338 apply_kline(source_p, aconf, reason, oper_reason, current_date);
341 if(ConfigFileEntry.kline_delay)
343 if(kline_queued == 0)
345 eventAddOnce("check_klines", check_klines_event, NULL,
346 ConfigFileEntry.kline_delay);
347 kline_queued = 1;
350 else
351 check_klines();
353 return;
356 /* mo_unkline()
358 * parv[1] - kline to remove
359 * parv[2] - optional "ON"
360 * parv[3] - optional target server
362 static int
363 mo_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
365 const char *user;
366 char *host;
367 char splat[] = "*";
368 char *h = LOCAL_COPY(parv[1]);
370 if(!IsOperUnkline(source_p))
372 sendto_one(source_p, form_str(ERR_NOPRIVS),
373 me.name, source_p->name, "unkline");
374 return 0;
377 if((host = strchr(h, '@')) || *h == '*')
379 /* Explicit user@host mask given */
381 if(host) /* Found user@host */
383 *host++ = '\0';
385 /* check for @host */
386 if(*h)
387 user = h;
388 else
389 user = splat;
391 /* check for user@ */
392 if(!*host)
393 host = splat;
395 else
397 user = splat; /* no @ found, assume its *@somehost */
398 host = h;
401 else
403 sendto_one(source_p, ":%s NOTICE %s :Invalid parameters", me.name, source_p->name);
404 return 0;
407 /* possible remote kline.. */
408 if((parc > 3) && (irccmp(parv[2], "ON") == 0))
410 if (!IsOperRemoteBan(source_p))
412 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name,
413 "remoteban");
415 return 0;
418 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
419 "%s is removing K-Line for [%s@%s] on %s",
420 get_oper_name(source_p), user, host, parv[3]);
422 propagate_generic(source_p, "UNKLINE", parv[3], CAP_UNKLN,
423 "%s %s", user, host);
425 if(match(parv[3], me.name) == 0)
426 return 0;
428 else if(dlink_list_length(&cluster_conf_list) > 0)
429 cluster_generic(source_p, "UNKLINE", SHARED_UNKLINE, CAP_UNKLN,
430 "%s %s", user, host);
432 if(remove_temp_kline(user, host))
434 sendto_one(source_p,
435 ":%s NOTICE %s :Un-klined [%s@%s] from temporary k-lines",
436 me.name, parv[0], user, host);
437 sendto_realops_snomask(SNO_GENERAL, L_ALL,
438 "%s has removed the temporary K-Line for: [%s@%s]",
439 get_oper_name(source_p), user, host);
440 ilog(L_KLINE, "UK %s %s %s",
441 get_oper_name(source_p), user, host);
442 return 0;
445 remove_permkline_match(source_p, host, user);
447 return 0;
450 /* ms_unkline()
452 * parv[1] - target server
453 * parv[2] - user to unkline
454 * parv[3] - host to unkline
456 static int
457 ms_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
459 /* parv[0] parv[1] parv[2] parv[3]
460 * oper target server user host */
461 propagate_generic(source_p, "UNKLINE", parv[1], CAP_UNKLN,
462 "%s %s", parv[2], parv[3]);
464 if(!match(parv[1], me.name))
465 return 0;
467 if(!IsPerson(source_p))
468 return 0;
470 handle_remote_unkline(source_p, parv[2], parv[3]);
471 return 0;
474 static int
475 me_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
477 /* user host */
478 if(!IsPerson(source_p))
479 return 0;
481 handle_remote_unkline(source_p, parv[1], parv[2]);
482 return 0;
485 static void
486 handle_remote_unkline(struct Client *source_p, const char *user, const char *host)
488 if(!find_shared_conf(source_p->username, source_p->host,
489 source_p->user->server, SHARED_UNKLINE))
490 return;
492 if(remove_temp_kline(user, host))
494 sendto_one_notice(source_p,
495 ":Un-klined [%s@%s] from temporary k-lines",
496 user, host);
498 sendto_realops_snomask(SNO_GENERAL, L_ALL,
499 "%s has removed the temporary K-Line for: [%s@%s]",
500 get_oper_name(source_p), user, host);
502 ilog(L_KLINE, "UK %s %s %s",
503 get_oper_name(source_p), user, host);
504 return;
507 remove_permkline_match(source_p, host, user);
510 /* apply_kline()
512 * inputs -
513 * output - NONE
514 * side effects - kline as given, is added to the hashtable
515 * and conf file
517 static void
518 apply_kline(struct Client *source_p, struct ConfItem *aconf,
519 const char *reason, const char *oper_reason, const char *current_date)
521 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, aconf);
522 write_confitem(KLINE_TYPE, source_p, aconf->user, aconf->host,
523 reason, oper_reason, current_date, 0);
526 /* apply_tkline()
528 * inputs -
529 * output - NONE
530 * side effects - tkline as given is placed
532 static void
533 apply_tkline(struct Client *source_p, struct ConfItem *aconf,
534 const char *reason, const char *oper_reason, const char *current_date, int tkline_time)
536 aconf->hold = CurrentTime + tkline_time;
537 add_temp_kline(aconf);
539 /* no oper reason.. */
540 if(EmptyString(oper_reason))
542 sendto_realops_snomask(SNO_GENERAL, L_ALL,
543 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
544 get_oper_name(source_p), tkline_time / 60,
545 aconf->user, aconf->host, reason);
546 ilog(L_KLINE, "K %s %d %s %s %s",
547 get_oper_name(source_p), tkline_time / 60,
548 aconf->user, aconf->host, reason);
550 else
552 sendto_realops_snomask(SNO_GENERAL, L_ALL,
553 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
554 get_oper_name(source_p), tkline_time / 60,
555 aconf->user, aconf->host, reason, oper_reason);
556 ilog(L_KLINE, "K %s %d %s %s %s|%s",
557 get_oper_name(source_p), tkline_time / 60,
558 aconf->user, aconf->host, reason, oper_reason);
561 sendto_one_notice(source_p, ":Added temporary %d min. K-Line [%s@%s]",
562 tkline_time / 60, aconf->user, aconf->host);
565 /* find_user_host()
567 * inputs - client placing kline, user@host, user buffer, host buffer
568 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
569 * side effects -
571 static int
572 find_user_host(struct Client *source_p, const char *userhost, char *luser, char *lhost)
574 char *hostp;
576 hostp = strchr(userhost, '@');
578 if(hostp != NULL) /* I'm a little user@host */
580 *(hostp++) = '\0'; /* short and squat */
581 if(*userhost)
582 strlcpy(luser, userhost, USERLEN + 1); /* here is my user */
583 else
584 strcpy(luser, "*");
585 if(*hostp)
586 strlcpy(lhost, hostp, HOSTLEN + 1); /* here is my host */
587 else
588 strcpy(lhost, "*");
590 else
592 /* no '@', no '.', so its not a user@host or host, therefore
593 * its a nick, which support was removed for.
595 if(strchr(userhost, '.') == NULL)
596 return 0;
598 luser[0] = '*'; /* no @ found, assume its *@somehost */
599 luser[1] = '\0';
600 strlcpy(lhost, userhost, HOSTLEN + 1);
603 return 1;
606 /* valid_user_host()
608 * inputs - user buffer, host buffer
609 * output - 0 if invalid, 1 if valid
610 * side effects -
612 static int
613 valid_user_host(struct Client *source_p, const char *luser, const char *lhost)
615 /* # is invalid, as is '!' (n!u@h kline) */
616 if(strchr(lhost, '#') || strchr(luser, '#') || strchr(luser, '!'))
618 sendto_one_notice(source_p, ":Invalid K-Line");
619 return 0;
622 return 1;
625 /* valid_wild_card()
627 * input - user buffer, host buffer
628 * output - 0 if invalid, 1 if valid
629 * side effects -
631 static int
632 valid_wild_card(struct Client *source_p, const char *luser, const char *lhost)
634 const char *p;
635 char tmpch;
636 int nonwild = 0;
638 /* check there are enough non wildcard chars */
639 p = luser;
640 while ((tmpch = *p++))
642 if(!IsKWildChar(tmpch))
644 /* found enough chars, return */
645 if(++nonwild >= ConfigFileEntry.min_nonwildcard)
646 return 1;
650 /* try host, as user didnt contain enough */
651 p = lhost;
652 while ((tmpch = *p++))
654 if(!IsKWildChar(tmpch))
655 if(++nonwild >= ConfigFileEntry.min_nonwildcard)
656 return 1;
659 sendto_one_notice(source_p,
660 ":Please include at least %d non-wildcard "
661 "characters with the user@host",
662 ConfigFileEntry.min_nonwildcard);
663 return 0;
667 * valid_comment
668 * inputs - pointer to client
669 * - pointer to comment
670 * output - 0 if no valid comment, 1 if valid
671 * side effects - NONE
673 static int
674 valid_comment(struct Client *source_p, char *comment)
676 if(strchr(comment, '"'))
678 sendto_one_notice(source_p, ":Invalid character '\"' in comment");
679 return 0;
682 if(strlen(comment) > REASONLEN)
683 comment[REASONLEN] = '\0';
685 return 1;
688 /* already_placed_kline()
690 * inputs - source to notify, user@host to check, tkline time
691 * outputs - 1 if a perm kline or a tkline when a tkline is being
692 * set exists, else 0
693 * side effects - notifies source_p kline exists
695 /* Note: This currently works if the new K-line is a special case of an
696 * existing K-line, but not the other way round. To do that we would
697 * have to walk the hash and check every existing K-line. -A1kmm.
699 static int
700 already_placed_kline(struct Client *source_p, const char *luser, const char *lhost, int tkline)
702 const char *reason;
703 struct irc_sockaddr_storage iphost, *piphost;
704 struct ConfItem *aconf;
705 int t;
706 if(ConfigFileEntry.non_redundant_klines)
708 if((t = parse_netmask(lhost, (struct sockaddr *)&iphost, NULL)) != HM_HOST)
710 #ifdef IPV6
711 if(t == HM_IPV6)
712 t = AF_INET6;
713 else
714 #endif
715 t = AF_INET;
717 piphost = &iphost;
719 else
720 piphost = NULL;
722 if((aconf = find_conf_by_address(lhost, NULL, NULL, (struct sockaddr *)piphost, CONF_KILL, t, luser)))
724 /* setting a tkline, or existing one is perm */
725 if(tkline || ((aconf->flags & CONF_FLAGS_TEMPORARY) == 0))
727 reason = aconf->passwd ? aconf->passwd : "<No Reason>";
729 sendto_one_notice(source_p,
730 ":[%s@%s] already K-Lined by [%s@%s] - %s",
731 luser, lhost, aconf->user,
732 aconf->host, reason);
733 return 1;
738 return 0;
741 /* remove_permkline_match()
743 * hunts for a permanent kline, and removes it.
745 static void
746 remove_permkline_match(struct Client *source_p, const char *host, const char *user)
748 FILE *in, *out;
749 int pairme = 0;
750 int error_on_write = NO;
751 char buf[BUFSIZE];
752 char matchbuf[BUFSIZE];
753 char temppath[BUFSIZE];
754 const char *filename;
755 mode_t oldumask;
756 int matchlen;
758 ircsnprintf(temppath, sizeof(temppath),
759 "%s.tmp", ConfigFileEntry.klinefile);
761 filename = get_conf_name(KLINE_TYPE);
763 if((in = fopen(filename, "r")) == 0)
765 sendto_one_notice(source_p, ":Cannot open %s", filename);
766 return;
769 oldumask = umask(0);
770 if((out = fopen(temppath, "w")) == 0)
772 sendto_one_notice(source_p, ":Cannot open %s", temppath);
773 fclose(in);
774 umask(oldumask);
775 return;
778 umask(oldumask);
780 snprintf(matchbuf, sizeof(matchbuf), "\"%s\",\"%s\"", user, host);
781 matchlen = strlen(matchbuf);
783 while (fgets(buf, sizeof(buf), in))
785 if(error_on_write)
786 break;
788 if(!strncasecmp(buf, matchbuf, matchlen))
790 pairme++;
791 break;
793 else
794 error_on_write = flush_write(source_p, out, buf, temppath);
797 /* we dropped out of the loop early because we found a match,
798 * to drop into this somewhat faster loop as we presume we'll never
799 * have two matching klines --anfl
801 if(pairme && !error_on_write)
803 while(fgets(buf, sizeof(buf), in))
805 if(error_on_write)
806 break;
808 error_on_write = flush_write(source_p, out, buf, temppath);
812 fclose(in);
813 fclose(out);
815 /* The result of the rename should be checked too... oh well */
816 /* If there was an error on a write above, then its been reported
817 * and I am not going to trash the original kline /conf file
819 if(error_on_write)
821 sendto_one_notice(source_p, ":Couldn't write temp kline file, aborted");
822 return;
824 else if(!pairme)
826 sendto_one_notice(source_p, ":No K-Line for %s@%s",
827 user, host);
829 if(temppath != NULL)
830 (void) unlink(temppath);
832 return;
835 (void) rename(temppath, filename);
836 rehash_bans(0);
838 sendto_one_notice(source_p, ":K-Line for [%s@%s] is removed",
839 user, host);
841 sendto_realops_snomask(SNO_GENERAL, L_ALL,
842 "%s has removed the K-Line for: [%s@%s]",
843 get_oper_name(source_p), user, host);
845 ilog(L_KLINE, "UK %s %s %s",
846 get_oper_name(source_p), user, host);
847 return;
851 * flush_write()
853 * inputs - pointer to client structure of oper requesting unkline
854 * - out is the file descriptor
855 * - buf is the buffer to write
856 * - ntowrite is the expected number of character to be written
857 * - temppath is the temporary file name to be written
858 * output - YES for error on write
859 * - NO for success
860 * side effects - if successful, the buf is written to output file
861 * if a write failure happesn, and the file pointed to
862 * by temppath, if its non NULL, is removed.
864 * The idea here is, to be as robust as possible when writing to the
865 * kline file.
867 * -Dianora
870 static int
871 flush_write(struct Client *source_p, FILE * out, const char *buf, const char *temppath)
873 int error_on_write = (fputs(buf, out) < 0) ? YES : NO;
875 if(error_on_write)
877 sendto_one_notice(source_p, ":Unable to write to %s",
878 temppath);
879 if(temppath != NULL)
880 (void) unlink(temppath);
882 return (error_on_write);
885 /* remove_temp_kline()
887 * inputs - username, hostname to unkline
888 * outputs -
889 * side effects - tries to unkline anything that matches
891 static int
892 remove_temp_kline(const char *user, const char *host)
894 struct ConfItem *aconf;
895 dlink_node *ptr;
896 struct irc_sockaddr_storage addr, caddr;
897 int bits, cbits;
898 int mtype, ktype;
899 int i;
901 mtype = parse_netmask(host, (struct sockaddr *)&addr, &bits);
903 for (i = 0; i < LAST_TEMP_TYPE; i++)
905 DLINK_FOREACH(ptr, temp_klines[i].head)
907 aconf = ptr->data;
909 ktype = parse_netmask(aconf->host, (struct sockaddr *)&caddr, &cbits);
911 if(ktype != mtype || (user && irccmp(user, aconf->user)))
912 continue;
914 if(ktype == HM_HOST)
916 if(irccmp(aconf->host, host))
917 continue;
919 else if(bits != cbits ||
920 !comp_with_mask_sock((struct sockaddr *)&addr,
921 (struct sockaddr *)&caddr, bits))
922 continue;
924 dlinkDestroy(ptr, &temp_klines[i]);
925 delete_one_address_conf(aconf->host, aconf);
926 return YES;
930 return NO;