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