]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_kline.c
[svn] Exempt klines with a fixed user@ (no */?) from min_nonwildcard checks.
[irc/rqf/shadowircd.git] / modules / m_kline.c
CommitLineData
212380e3 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 *
598ebb42 24 * $Id: m_kline.c 3464 2007-05-19 22:21:10Z jilles $
212380e3 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
50static int mo_kline(struct Client *, struct Client *, int, const char **);
51static int ms_kline(struct Client *, struct Client *, int, const char **);
52static int me_kline(struct Client *, struct Client *, int, const char **);
53static int mo_unkline(struct Client *, struct Client *, int, const char **);
54static int ms_unkline(struct Client *, struct Client *, int, const char **);
55static int me_unkline(struct Client *, struct Client *, int, const char **);
56
57struct 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
62struct 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
67mapi_clist_av1 kline_clist[] = { &kline_msgtab, &unkline_msgtab, NULL };
598ebb42 68DECLARE_MODULE_AV1(kline, NULL, NULL, kline_clist, NULL, NULL, "$Revision: 3464 $");
212380e3 69
70/* Local function prototypes */
71static int find_user_host(struct Client *source_p, const char *userhost, char *user, char *host);
72static int valid_comment(struct Client *source_p, char *comment);
73static int valid_user_host(struct Client *source_p, const char *user, const char *host);
74static int valid_wild_card(struct Client *source_p, const char *user, const char *host);
75
76static void handle_remote_kline(struct Client *source_p, int tkline_time,
77 const char *user, const char *host, const char *reason);
78static void apply_kline(struct Client *source_p, struct ConfItem *aconf,
79 const char *reason, const char *oper_reason, const char *current_date);
80static void apply_tkline(struct Client *source_p, struct ConfItem *aconf,
81 const char *, const char *, const char *, int);
82static int already_placed_kline(struct Client *, const char *, const char *, int);
83
84static void handle_remote_unkline(struct Client *source_p,
85 const char *user, const char *host);
86static void remove_permkline_match(struct Client *, const char *, const char *);
87static int flush_write(struct Client *, FILE *, const char *, const char *);
88static int remove_temp_kline(const char *, const char *);
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 */
98static int
99mo_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 */
235static int
236ms_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
261static int
262me_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
272static void
273handle_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->user->server,
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 */
349static int
350mo_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
357 if(!IsOperUnkline(source_p))
358 {
359 sendto_one(source_p, form_str(ERR_NOPRIVS),
360 me.name, source_p->name, "unkline");
361 return 0;
362 }
363
364 if((host = strchr(h, '@')) || *h == '*' || strchr(h, '.') || strchr(h, ':'))
365 {
366 /* Explicit user@host mask given */
367
368 if(host) /* Found user@host */
369 {
370 *host++ = '\0';
371
372 /* check for @host */
373 if(*h)
374 user = h;
375 else
376 user = splat;
377
378 /* check for user@ */
379 if(!*host)
380 host = splat;
381 }
382 else
383 {
384 user = splat; /* no @ found, assume its *@somehost */
385 host = h;
386 }
387 }
388 else
389 {
5366977b 390 sendto_one_notice(source_p, ":Invalid parameters");
212380e3 391 return 0;
392 }
393
394 /* possible remote kline.. */
395 if((parc > 3) && (irccmp(parv[2], "ON") == 0))
396 {
397 if(!IsOperRemoteBan(source_p))
398 {
399 sendto_one(source_p, form_str(ERR_NOPRIVS),
400 me.name, source_p->name, "remoteban");
401 return 0;
402 }
403
404 propagate_generic(source_p, "UNKLINE", parv[3], CAP_UNKLN,
405 "%s %s", user, host);
406
407 if(match(parv[3], me.name) == 0)
408 return 0;
409 }
410 else if(dlink_list_length(&cluster_conf_list) > 0)
411 cluster_generic(source_p, "UNKLINE", SHARED_UNKLINE, CAP_UNKLN,
412 "%s %s", user, host);
413
414 if(remove_temp_kline(user, host))
415 {
5366977b 416 sendto_one_notice(source_p, ":Un-klined [%s@%s] from temporary k-lines", user, host);
212380e3 417 sendto_realops_snomask(SNO_GENERAL, L_ALL,
418 "%s has removed the temporary K-Line for: [%s@%s]",
419 get_oper_name(source_p), user, host);
420 ilog(L_KLINE, "UK %s %s %s",
421 get_oper_name(source_p), user, host);
422 return 0;
423 }
424
425 remove_permkline_match(source_p, host, user);
426
427 return 0;
428}
429
430/* ms_unkline()
431 *
432 * parv[1] - target server
433 * parv[2] - user to unkline
434 * parv[3] - host to unkline
435 */
436static int
437ms_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
438{
439 /* parv[0] parv[1] parv[2] parv[3]
440 * oper target server user host */
441 propagate_generic(source_p, "UNKLINE", parv[1], CAP_UNKLN,
442 "%s %s", parv[2], parv[3]);
443
444 if(!match(parv[1], me.name))
445 return 0;
446
447 if(!IsPerson(source_p))
448 return 0;
449
450 handle_remote_unkline(source_p, parv[2], parv[3]);
451 return 0;
452}
453
454static int
455me_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
456{
457 /* user host */
458 if(!IsPerson(source_p))
459 return 0;
460
461 handle_remote_unkline(source_p, parv[1], parv[2]);
462 return 0;
463}
464
465static void
466handle_remote_unkline(struct Client *source_p, const char *user, const char *host)
467{
468 if(!find_shared_conf(source_p->username, source_p->host,
469 source_p->user->server, SHARED_UNKLINE))
470 return;
471
472 if(remove_temp_kline(user, host))
473 {
474 sendto_one_notice(source_p,
475 ":Un-klined [%s@%s] from temporary k-lines",
476 user, host);
477
478 sendto_realops_snomask(SNO_GENERAL, L_ALL,
479 "%s has removed the temporary K-Line for: [%s@%s]",
480 get_oper_name(source_p), user, host);
481
482 ilog(L_KLINE, "UK %s %s %s",
483 get_oper_name(source_p), user, host);
484 return;
485 }
486
487 remove_permkline_match(source_p, host, user);
488}
489
490/* apply_kline()
491 *
492 * inputs -
493 * output - NONE
494 * side effects - kline as given, is added to the hashtable
495 * and conf file
496 */
497static void
498apply_kline(struct Client *source_p, struct ConfItem *aconf,
499 const char *reason, const char *oper_reason, const char *current_date)
500{
501 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, aconf);
502 write_confitem(KLINE_TYPE, source_p, aconf->user, aconf->host,
503 reason, oper_reason, current_date, 0);
504}
505
506/* apply_tkline()
507 *
508 * inputs -
509 * output - NONE
510 * side effects - tkline as given is placed
511 */
512static void
513apply_tkline(struct Client *source_p, struct ConfItem *aconf,
514 const char *reason, const char *oper_reason, const char *current_date, int tkline_time)
515{
516 aconf->hold = CurrentTime + tkline_time;
517 add_temp_kline(aconf);
518
519 /* no oper reason.. */
520 if(EmptyString(oper_reason))
521 {
522 sendto_realops_snomask(SNO_GENERAL, L_ALL,
523 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
524 get_oper_name(source_p), tkline_time / 60,
525 aconf->user, aconf->host, reason);
526 ilog(L_KLINE, "K %s %d %s %s %s",
527 get_oper_name(source_p), tkline_time / 60,
528 aconf->user, aconf->host, reason);
529 }
530 else
531 {
532 sendto_realops_snomask(SNO_GENERAL, L_ALL,
533 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
534 get_oper_name(source_p), tkline_time / 60,
535 aconf->user, aconf->host, reason, oper_reason);
536 ilog(L_KLINE, "K %s %d %s %s %s|%s",
537 get_oper_name(source_p), tkline_time / 60,
538 aconf->user, aconf->host, reason, oper_reason);
539 }
540
541 sendto_one_notice(source_p, ":Added temporary %d min. K-Line [%s@%s]",
542 tkline_time / 60, aconf->user, aconf->host);
543}
544
545/* find_user_host()
546 *
547 * inputs - client placing kline, user@host, user buffer, host buffer
548 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
549 * side effects -
550 */
551static int
552find_user_host(struct Client *source_p, const char *userhost, char *luser, char *lhost)
553{
554 char *hostp;
555
556 hostp = strchr(userhost, '@');
557
558 if(hostp != NULL) /* I'm a little user@host */
559 {
560 *(hostp++) = '\0'; /* short and squat */
561 if(*userhost)
562 strlcpy(luser, userhost, USERLEN + 1); /* here is my user */
563 else
564 strcpy(luser, "*");
565 if(*hostp)
566 strlcpy(lhost, hostp, HOSTLEN + 1); /* here is my host */
567 else
568 strcpy(lhost, "*");
569 }
570 else
571 {
572 /* no '@', no '.', so its not a user@host or host, therefore
573 * its a nick, which support was removed for.
574 */
575 if(strchr(userhost, '.') == NULL && strchr(userhost, ':') == NULL)
576 return 0;
577
578 luser[0] = '*'; /* no @ found, assume its *@somehost */
579 luser[1] = '\0';
580 strlcpy(lhost, userhost, HOSTLEN + 1);
581 }
582
583 return 1;
584}
585
586/* valid_user_host()
587 *
588 * inputs - user buffer, host buffer
589 * output - 0 if invalid, 1 if valid
590 * side effects -
591 */
592static int
593valid_user_host(struct Client *source_p, const char *luser, const char *lhost)
594{
595 /* # is invalid, as is '!' (n!u@h kline) */
596 if(strchr(lhost, '#') || strchr(luser, '#') || strchr(luser, '!'))
597 {
598 sendto_one_notice(source_p, ":Invalid K-Line");
599 return 0;
600 }
601
602 return 1;
603}
604
605/* valid_wild_card()
606 *
607 * input - user buffer, host buffer
608 * output - 0 if invalid, 1 if valid
609 * side effects -
610 */
611static int
612valid_wild_card(struct Client *source_p, const char *luser, const char *lhost)
613{
614 const char *p;
615 char tmpch;
616 int nonwild = 0;
617
598ebb42 618 /* user has no wildcards, always accept -- jilles */
619 if (!strchr(luser, '?') && !strchr(luser, '*'))
620 return 1;
621
212380e3 622 /* check there are enough non wildcard chars */
623 p = luser;
624 while ((tmpch = *p++))
625 {
626 if(!IsKWildChar(tmpch))
627 {
628 /* found enough chars, return */
629 if(++nonwild >= ConfigFileEntry.min_nonwildcard)
630 return 1;
631 }
632 }
633
634 /* try host, as user didnt contain enough */
635 p = lhost;
636 while ((tmpch = *p++))
637 {
638 if(!IsKWildChar(tmpch))
639 if(++nonwild >= ConfigFileEntry.min_nonwildcard)
640 return 1;
641 }
642
643 sendto_one_notice(source_p,
644 ":Please include at least %d non-wildcard "
645 "characters with the user@host",
646 ConfigFileEntry.min_nonwildcard);
647 return 0;
648}
649
650/*
651 * valid_comment
652 * inputs - pointer to client
653 * - pointer to comment
654 * output - 0 if no valid comment, 1 if valid
655 * side effects - NONE
656 */
657static int
658valid_comment(struct Client *source_p, char *comment)
659{
660 if(strchr(comment, '"'))
661 {
662 sendto_one_notice(source_p, ":Invalid character '\"' in comment");
663 return 0;
664 }
665
61569b65 666 if(strlen(comment) > BANREASONLEN)
667 comment[BANREASONLEN] = '\0';
212380e3 668
669 return 1;
670}
671
672/* already_placed_kline()
673 *
674 * inputs - source to notify, user@host to check, tkline time
675 * outputs - 1 if a perm kline or a tkline when a tkline is being
676 * set exists, else 0
677 * side effects - notifies source_p kline exists
678 */
679/* Note: This currently works if the new K-line is a special case of an
680 * existing K-line, but not the other way round. To do that we would
681 * have to walk the hash and check every existing K-line. -A1kmm.
682 */
683static int
684already_placed_kline(struct Client *source_p, const char *luser, const char *lhost, int tkline)
685{
686 const char *reason;
687 struct irc_sockaddr_storage iphost, *piphost;
688 struct ConfItem *aconf;
689 int t;
690 if(ConfigFileEntry.non_redundant_klines)
691 {
692 if((t = parse_netmask(lhost, (struct sockaddr *)&iphost, NULL)) != HM_HOST)
693 {
694#ifdef IPV6
695 if(t == HM_IPV6)
696 t = AF_INET6;
697 else
698#endif
699 t = AF_INET;
700
701 piphost = &iphost;
702 }
703 else
704 piphost = NULL;
705
706 if((aconf = find_conf_by_address(lhost, NULL, NULL, (struct sockaddr *)piphost, CONF_KILL, t, luser)))
707 {
708 /* setting a tkline, or existing one is perm */
709 if(tkline || ((aconf->flags & CONF_FLAGS_TEMPORARY) == 0))
710 {
711 reason = aconf->passwd ? aconf->passwd : "<No Reason>";
712
713 sendto_one_notice(source_p,
714 ":[%s@%s] already K-Lined by [%s@%s] - %s",
715 luser, lhost, aconf->user,
716 aconf->host, reason);
717 return 1;
718 }
719 }
720 }
721
722 return 0;
723}
724
725/* remove_permkline_match()
726 *
727 * hunts for a permanent kline, and removes it.
728 */
729static void
730remove_permkline_match(struct Client *source_p, const char *host, const char *user)
731{
732 FILE *in, *out;
733 int pairme = 0;
734 int error_on_write = NO;
735 char buf[BUFSIZE];
736 char matchbuf[BUFSIZE];
737 char temppath[BUFSIZE];
738 const char *filename;
739 mode_t oldumask;
740 int matchlen;
741
742 ircsnprintf(temppath, sizeof(temppath),
743 "%s.tmp", ConfigFileEntry.klinefile);
744
745 filename = get_conf_name(KLINE_TYPE);
746
747 if((in = fopen(filename, "r")) == 0)
748 {
749 sendto_one_notice(source_p, ":Cannot open %s", filename);
750 return;
751 }
752
753 oldumask = umask(0);
754 if((out = fopen(temppath, "w")) == 0)
755 {
756 sendto_one_notice(source_p, ":Cannot open %s", temppath);
757 fclose(in);
758 umask(oldumask);
759 return;
760 }
761
762 umask(oldumask);
763
764 snprintf(matchbuf, sizeof(matchbuf), "\"%s\",\"%s\"", user, host);
765 matchlen = strlen(matchbuf);
766
767 while (fgets(buf, sizeof(buf), in))
768 {
769 if(error_on_write)
770 break;
771
772 if(!strncasecmp(buf, matchbuf, matchlen))
773 {
774 pairme++;
775 break;
776 }
777 else
778 error_on_write = flush_write(source_p, out, buf, temppath);
779 }
780
781 /* we dropped out of the loop early because we found a match,
782 * to drop into this somewhat faster loop as we presume we'll never
783 * have two matching klines --anfl
784 */
785 if(pairme && !error_on_write)
786 {
787 while(fgets(buf, sizeof(buf), in))
788 {
789 if(error_on_write)
790 break;
791
792 error_on_write = flush_write(source_p, out, buf, temppath);
793 }
794 }
795
796 fclose(in);
797 if (fclose(out))
798 error_on_write = YES;
799
800 /* The result of the rename should be checked too... oh well */
801 /* If there was an error on a write above, then its been reported
802 * and I am not going to trash the original kline /conf file
803 */
804 if(error_on_write)
805 {
806 sendto_one_notice(source_p, ":Couldn't write temp kline file, aborted");
807 return;
808 }
809 else if(!pairme)
810 {
811 sendto_one_notice(source_p, ":No K-Line for %s@%s",
812 user, host);
813
814 if(temppath != NULL)
815 (void) unlink(temppath);
816
817 return;
818 }
819
820 if (rename(temppath, filename))
821 {
822 sendto_one_notice(source_p, ":Couldn't rename temp file, aborted");
823 return;
824 }
825 rehash_bans(0);
826
827 sendto_one_notice(source_p, ":K-Line for [%s@%s] is removed",
828 user, host);
829
830 sendto_realops_snomask(SNO_GENERAL, L_ALL,
831 "%s has removed the K-Line for: [%s@%s]",
832 get_oper_name(source_p), user, host);
833
834 ilog(L_KLINE, "UK %s %s %s",
835 get_oper_name(source_p), user, host);
836 return;
837}
838
839/*
840 * flush_write()
841 *
842 * inputs - pointer to client structure of oper requesting unkline
843 * - out is the file descriptor
844 * - buf is the buffer to write
845 * - ntowrite is the expected number of character to be written
846 * - temppath is the temporary file name to be written
847 * output - YES for error on write
848 * - NO for success
849 * side effects - if successful, the buf is written to output file
850 * if a write failure happesn, and the file pointed to
851 * by temppath, if its non NULL, is removed.
852 *
853 * The idea here is, to be as robust as possible when writing to the
854 * kline file.
855 *
856 * -Dianora
857 */
858
859static int
860flush_write(struct Client *source_p, FILE * out, const char *buf, const char *temppath)
861{
862 int error_on_write = (fputs(buf, out) < 0) ? YES : NO;
863
864 if(error_on_write)
865 {
866 sendto_one_notice(source_p, ":Unable to write to %s",
867 temppath);
868 if(temppath != NULL)
869 (void) unlink(temppath);
870 }
871 return (error_on_write);
872}
873
874/* remove_temp_kline()
875 *
876 * inputs - username, hostname to unkline
877 * outputs -
878 * side effects - tries to unkline anything that matches
879 */
880static int
881remove_temp_kline(const char *user, const char *host)
882{
883 struct ConfItem *aconf;
884 dlink_node *ptr;
885 struct irc_sockaddr_storage addr, caddr;
886 int bits, cbits;
887 int mtype, ktype;
888 int i;
889
890 mtype = parse_netmask(host, (struct sockaddr *)&addr, &bits);
891
892 for (i = 0; i < LAST_TEMP_TYPE; i++)
893 {
894 DLINK_FOREACH(ptr, temp_klines[i].head)
895 {
896 aconf = ptr->data;
897
898 ktype = parse_netmask(aconf->host, (struct sockaddr *)&caddr, &cbits);
899
900 if(ktype != mtype || (user && irccmp(user, aconf->user)))
901 continue;
902
903 if(ktype == HM_HOST)
904 {
905 if(irccmp(aconf->host, host))
906 continue;
907 }
908 else if(bits != cbits ||
909 !comp_with_mask_sock((struct sockaddr *)&addr,
910 (struct sockaddr *)&caddr, bits))
911 continue;
912
913 dlinkDestroy(ptr, &temp_klines[i]);
914 delete_one_address_conf(aconf->host, aconf);
915 return YES;
916 }
917 }
918
919 return NO;
920}