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