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