]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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 | |
212380e3 AC |
23 | */ |
24 | ||
25 | #include "stdinc.h" | |
212380e3 AC |
26 | #include "channel.h" |
27 | #include "class.h" | |
28 | #include "client.h" | |
4562c604 | 29 | #include "match.h" |
212380e3 AC |
30 | #include "ircd.h" |
31 | #include "hostmask.h" | |
32 | #include "numeric.h" | |
212380e3 AC |
33 | #include "s_conf.h" |
34 | #include "s_newconf.h" | |
4016731b | 35 | #include "logger.h" |
212380e3 AC |
36 | #include "send.h" |
37 | #include "hash.h" | |
38 | #include "s_serv.h" | |
39 | #include "msg.h" | |
40 | #include "parse.h" | |
41 | #include "modules.h" | |
35f6f850 | 42 | #include "reject.h" |
8bbeb278 | 43 | #include "bandbi.h" |
27f616dd | 44 | #include "operhash.h" |
212380e3 | 45 | |
eeabf33a EM |
46 | static const char kline_desc[] = "Provides the KLINE facility to ban users via hostmask"; |
47 | ||
3c7d6fcc EM |
48 | static void mo_kline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
49 | static void ms_kline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
50 | static void me_kline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
51 | static void mo_unkline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
52 | static void ms_unkline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
53 | static void me_unkline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
212380e3 AC |
54 | |
55 | struct Message kline_msgtab = { | |
7baa37a9 | 56 | "KLINE", 0, 0, 0, 0, |
212380e3 AC |
57 | {mg_unreg, mg_not_oper, {ms_kline, 5}, {ms_kline, 5}, {me_kline, 5}, {mo_kline, 3}} |
58 | }; | |
59 | ||
60 | struct Message unkline_msgtab = { | |
7baa37a9 | 61 | "UNKLINE", 0, 0, 0, 0, |
212380e3 AC |
62 | {mg_unreg, mg_not_oper, {ms_unkline, 4}, {ms_unkline, 4}, {me_unkline, 3}, {mo_unkline, 2}} |
63 | }; | |
64 | ||
65 | mapi_clist_av1 kline_clist[] = { &kline_msgtab, &unkline_msgtab, NULL }; | |
8bbeb278 | 66 | |
f1156bf0 | 67 | DECLARE_MODULE_AV2(kline, NULL, NULL, kline_clist, NULL, NULL, NULL, NULL, kline_desc); |
212380e3 AC |
68 | |
69 | /* Local function prototypes */ | |
3c7d6fcc EM |
70 | static bool find_user_host(struct Client *source_p, const char *userhost, char *user, char *host); |
71 | static bool valid_user_host(struct Client *source_p, const char *user, const char *host); | |
212380e3 AC |
72 | |
73 | static void handle_remote_kline(struct Client *source_p, int tkline_time, | |
8bbeb278 | 74 | const char *user, const char *host, const char *reason); |
212380e3 | 75 | static void apply_kline(struct Client *source_p, struct ConfItem *aconf, |
b52c2949 | 76 | const char *reason, const char *oper_reason); |
212380e3 | 77 | static void apply_tkline(struct Client *source_p, struct ConfItem *aconf, |
b52c2949 | 78 | const char *, const char *, int); |
431a1a27 JT |
79 | static void apply_prop_kline(struct Client *source_p, struct ConfItem *aconf, |
80 | const char *, const char *, int); | |
3c7d6fcc | 81 | static bool already_placed_kline(struct Client *, const char *, const char *, int); |
212380e3 | 82 | |
8bbeb278 | 83 | static void handle_remote_unkline(struct Client *source_p, const char *user, const char *host); |
40a1d446 | 84 | static void remove_permkline_match(struct Client *, struct ConfItem *); |
3c7d6fcc | 85 | static bool remove_temp_kline(struct Client *, struct ConfItem *); |
431a1a27 | 86 | static void remove_prop_kline(struct Client *, struct ConfItem *); |
212380e3 | 87 | |
b068a4b5 | 88 | |
212380e3 AC |
89 | /* mo_kline() |
90 | * | |
91 | * parv[1] - temp time or user@host | |
92 | * parv[2] - user@host, "ON", or reason | |
93 | * parv[3] - "ON", reason, or server to target | |
94 | * parv[4] - server to target, or reason | |
95 | * parv[5] - reason | |
96 | */ | |
3c7d6fcc | 97 | static void |
428ca87b | 98 | mo_kline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv) |
212380e3 | 99 | { |
8bbeb278 | 100 | char def[] = "No Reason"; |
212380e3 | 101 | char user[USERLEN + 2]; |
8e1e48e1 | 102 | char host_buf[HOSTLEN + 3], *host = host_buf + 1; |
8bbeb278 | 103 | char *reason = def; |
212380e3 | 104 | char *oper_reason; |
212380e3 AC |
105 | const char *target_server = NULL; |
106 | struct ConfItem *aconf; | |
107 | int tkline_time = 0; | |
108 | int loc = 1; | |
f66f0baa | 109 | bool propagated = ConfigFileEntry.use_propagated_bans; |
212380e3 AC |
110 | |
111 | if(!IsOperK(source_p)) | |
112 | { | |
8bbeb278 | 113 | sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "kline"); |
3c7d6fcc | 114 | return; |
212380e3 AC |
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) | |
3c7d6fcc | 124 | return; |
212380e3 | 125 | |
8e1e48e1 EK |
126 | if (*host == ':') |
127 | { | |
128 | host--; | |
129 | *host = '0'; | |
130 | } | |
131 | ||
212380e3 AC |
132 | loc++; |
133 | ||
8bbeb278 | 134 | if(parc >= loc + 2 && !irccmp(parv[loc], "ON")) |
212380e3 AC |
135 | { |
136 | if(!IsOperRemoteBan(source_p)) | |
137 | { | |
138 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
8bbeb278 | 139 | me.name, source_p->name, "remoteban"); |
3c7d6fcc | 140 | return; |
212380e3 AC |
141 | } |
142 | ||
8bbeb278 | 143 | target_server = parv[loc + 1]; |
212380e3 AC |
144 | loc += 2; |
145 | } | |
146 | ||
147 | if(parc <= loc || EmptyString(parv[loc])) | |
148 | { | |
149 | sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), | |
150 | me.name, source_p->name, "KLINE"); | |
3c7d6fcc | 151 | return; |
212380e3 AC |
152 | } |
153 | ||
154 | reason = LOCAL_COPY(parv[loc]); | |
155 | ||
7d9e8e9d EK |
156 | if(parse_netmask_strict(host, NULL, NULL) == HM_ERROR) |
157 | { | |
158 | sendto_one_notice(source_p, | |
159 | ":[%s@%s] looks like an ill-formed IP K-line, refusing to set it", | |
160 | user, host); | |
161 | return; | |
162 | } | |
163 | ||
212380e3 AC |
164 | if(target_server != NULL) |
165 | { | |
166 | propagate_generic(source_p, "KLINE", target_server, CAP_KLN, | |
8bbeb278 | 167 | "%d %s %s :%s", tkline_time, user, host, reason); |
212380e3 AC |
168 | |
169 | /* If we are sending it somewhere that doesnt include us, stop */ | |
170 | if(!match(target_server, me.name)) | |
3c7d6fcc | 171 | return; |
431a1a27 JT |
172 | |
173 | /* Set as local-only. */ | |
f66f0baa | 174 | propagated = false; |
212380e3 AC |
175 | } |
176 | /* if we have cluster servers, send it to them.. */ | |
431a1a27 | 177 | else if(!propagated && rb_dlink_list_length(&cluster_conf_list) > 0) |
8bbeb278 | 178 | cluster_generic(source_p, "KLINE", |
212380e3 | 179 | (tkline_time > 0) ? SHARED_TKLINE : SHARED_PKLINE, CAP_KLN, |
8bbeb278 | 180 | "%lu %s %s :%s", tkline_time, user, host, reason); |
212380e3 | 181 | |
9319a2e2 | 182 | if(!valid_user_host(source_p, user, host)) |
3c7d6fcc | 183 | return; |
212380e3 | 184 | |
5c2b9eaf JT |
185 | if(!valid_wild_card(user, host)) |
186 | { | |
187 | sendto_one_notice(source_p, | |
188 | ":Please include at least %d non-wildcard " | |
189 | "characters with the user@host", | |
190 | ConfigFileEntry.min_nonwildcard); | |
3c7d6fcc | 191 | return; |
5c2b9eaf JT |
192 | } |
193 | ||
431a1a27 JT |
194 | if(propagated && tkline_time == 0) |
195 | { | |
196 | sendto_one_notice(source_p, ":Cannot set a permanent global ban"); | |
3c7d6fcc | 197 | return; |
431a1a27 JT |
198 | } |
199 | ||
212380e3 | 200 | if(already_placed_kline(source_p, user, host, tkline_time)) |
3c7d6fcc | 201 | return; |
212380e3 | 202 | |
7df5fcfb | 203 | rb_set_time(); |
212380e3 AC |
204 | aconf = make_conf(); |
205 | aconf->status = CONF_KILL; | |
b52c2949 | 206 | aconf->created = rb_current_time(); |
47a03750 VY |
207 | aconf->host = rb_strdup(host); |
208 | aconf->user = rb_strdup(user); | |
212380e3 | 209 | aconf->port = 0; |
27f616dd | 210 | aconf->info.oper = operhash_add(get_oper_name(source_p)); |
212380e3 | 211 | |
9319a2e2 KB |
212 | if(strlen(reason) > BANREASONLEN) |
213 | reason[BANREASONLEN] = '\0'; | |
214 | ||
212380e3 AC |
215 | /* Look for an oper reason */ |
216 | if((oper_reason = strchr(reason, '|')) != NULL) | |
217 | { | |
218 | *oper_reason = '\0'; | |
219 | oper_reason++; | |
220 | ||
221 | if(!EmptyString(oper_reason)) | |
47a03750 | 222 | aconf->spasswd = rb_strdup(oper_reason); |
212380e3 | 223 | } |
c935336b | 224 | aconf->passwd = rb_strdup(reason); |
212380e3 | 225 | |
431a1a27 JT |
226 | if(propagated) |
227 | apply_prop_kline(source_p, aconf, reason, oper_reason, tkline_time); | |
228 | else if(tkline_time > 0) | |
b52c2949 | 229 | apply_tkline(source_p, aconf, reason, oper_reason, tkline_time); |
212380e3 | 230 | else |
b52c2949 | 231 | apply_kline(source_p, aconf, reason, oper_reason); |
212380e3 | 232 | |
b068a4b5 | 233 | check_one_kline(aconf); |
212380e3 AC |
234 | } |
235 | ||
236 | /* ms_kline() | |
237 | * | |
238 | * parv[1] - server targeted at | |
239 | * parv[2] - tkline time (0 if perm) | |
240 | * parv[3] - user | |
241 | * parv[4] - host | |
242 | * parv[5] - reason | |
243 | */ | |
3c7d6fcc | 244 | static void |
428ca87b | 245 | ms_kline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
246 | { |
247 | int tkline_time = atoi(parv[2]); | |
248 | ||
249 | /* 1.5-3 and earlier contains a bug that allows remote klines to be | |
250 | * sent with an empty reason field. This is a protocol violation, | |
251 | * but its not worth dropping the link over.. --anfl | |
252 | */ | |
253 | if(parc < 6 || EmptyString(parv[5])) | |
3c7d6fcc | 254 | return; |
212380e3 AC |
255 | |
256 | propagate_generic(source_p, "KLINE", parv[1], CAP_KLN, | |
8bbeb278 | 257 | "%d %s %s :%s", tkline_time, parv[3], parv[4], parv[5]); |
212380e3 AC |
258 | |
259 | if(!match(parv[1], me.name)) | |
3c7d6fcc | 260 | return; |
212380e3 AC |
261 | |
262 | if(!IsPerson(source_p)) | |
3c7d6fcc | 263 | return; |
212380e3 AC |
264 | |
265 | handle_remote_kline(source_p, tkline_time, parv[3], parv[4], parv[5]); | |
212380e3 AC |
266 | } |
267 | ||
3c7d6fcc | 268 | static void |
428ca87b | 269 | me_kline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
270 | { |
271 | /* <tkline_time> <user> <host> :<reason> */ | |
272 | if(!IsPerson(source_p)) | |
3c7d6fcc | 273 | return; |
212380e3 AC |
274 | |
275 | handle_remote_kline(source_p, atoi(parv[1]), parv[2], parv[3], parv[4]); | |
212380e3 AC |
276 | } |
277 | ||
278 | static void | |
279 | handle_remote_kline(struct Client *source_p, int tkline_time, | |
8bbeb278 | 280 | const char *user, const char *host, const char *kreason) |
212380e3 | 281 | { |
212380e3 AC |
282 | char *reason = LOCAL_COPY(kreason); |
283 | struct ConfItem *aconf = NULL; | |
284 | char *oper_reason; | |
285 | ||
286 | if(!find_shared_conf(source_p->username, source_p->host, | |
8bbeb278 AC |
287 | source_p->servptr->name, |
288 | (tkline_time > 0) ? SHARED_TKLINE : SHARED_PKLINE)) | |
212380e3 AC |
289 | return; |
290 | ||
9319a2e2 | 291 | if(!valid_user_host(source_p, user, host)) |
212380e3 AC |
292 | return; |
293 | ||
5c2b9eaf JT |
294 | if(!valid_wild_card(user, host)) |
295 | { | |
296 | sendto_one_notice(source_p, | |
297 | ":Please include at least %d non-wildcard " | |
298 | "characters with the user@host", | |
299 | ConfigFileEntry.min_nonwildcard); | |
803ce385 | 300 | return; |
5c2b9eaf JT |
301 | } |
302 | ||
212380e3 AC |
303 | if(already_placed_kline(source_p, user, host, tkline_time)) |
304 | return; | |
305 | ||
306 | aconf = make_conf(); | |
307 | ||
308 | aconf->status = CONF_KILL; | |
b52c2949 | 309 | aconf->created = rb_current_time(); |
47a03750 VY |
310 | aconf->user = rb_strdup(user); |
311 | aconf->host = rb_strdup(host); | |
27f616dd | 312 | aconf->info.oper = operhash_add(get_oper_name(source_p)); |
212380e3 | 313 | |
9319a2e2 KB |
314 | if(strlen(reason) > BANREASONLEN) |
315 | reason[BANREASONLEN] = '\0'; | |
316 | ||
212380e3 AC |
317 | /* Look for an oper reason */ |
318 | if((oper_reason = strchr(reason, '|')) != NULL) | |
319 | { | |
320 | *oper_reason = '\0'; | |
321 | oper_reason++; | |
322 | ||
323 | if(!EmptyString(oper_reason)) | |
47a03750 | 324 | aconf->spasswd = rb_strdup(oper_reason); |
212380e3 | 325 | } |
c935336b | 326 | aconf->passwd = rb_strdup(reason); |
212380e3 | 327 | |
212380e3 | 328 | if(tkline_time > 0) |
b52c2949 | 329 | apply_tkline(source_p, aconf, reason, oper_reason, tkline_time); |
212380e3 | 330 | else |
b52c2949 | 331 | apply_kline(source_p, aconf, reason, oper_reason); |
212380e3 | 332 | |
b068a4b5 | 333 | check_one_kline(aconf); |
212380e3 AC |
334 | } |
335 | ||
336 | /* mo_unkline() | |
337 | * | |
338 | * parv[1] - kline to remove | |
339 | * parv[2] - optional "ON" | |
340 | * parv[3] - optional target server | |
341 | */ | |
3c7d6fcc | 342 | static void |
428ca87b | 343 | mo_unkline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
344 | { |
345 | const char *user; | |
346 | char *host; | |
347 | char splat[] = "*"; | |
348 | char *h = LOCAL_COPY(parv[1]); | |
40a1d446 | 349 | struct ConfItem *aconf; |
f66f0baa | 350 | bool propagated = true; |
212380e3 AC |
351 | |
352 | if(!IsOperUnkline(source_p)) | |
353 | { | |
8bbeb278 | 354 | sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "unkline"); |
3c7d6fcc | 355 | return; |
212380e3 AC |
356 | } |
357 | ||
358 | if((host = strchr(h, '@')) || *h == '*' || strchr(h, '.') || strchr(h, ':')) | |
359 | { | |
360 | /* Explicit user@host mask given */ | |
361 | ||
362 | if(host) /* Found user@host */ | |
363 | { | |
364 | *host++ = '\0'; | |
365 | ||
366 | /* check for @host */ | |
367 | if(*h) | |
368 | user = h; | |
369 | else | |
370 | user = splat; | |
371 | ||
372 | /* check for user@ */ | |
373 | if(!*host) | |
374 | host = splat; | |
375 | } | |
376 | else | |
377 | { | |
378 | user = splat; /* no @ found, assume its *@somehost */ | |
379 | host = h; | |
380 | } | |
381 | } | |
382 | else | |
383 | { | |
5366977b | 384 | sendto_one_notice(source_p, ":Invalid parameters"); |
3c7d6fcc | 385 | return; |
212380e3 AC |
386 | } |
387 | ||
388 | /* possible remote kline.. */ | |
389 | if((parc > 3) && (irccmp(parv[2], "ON") == 0)) | |
390 | { | |
391 | if(!IsOperRemoteBan(source_p)) | |
392 | { | |
393 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
8bbeb278 | 394 | me.name, source_p->name, "remoteban"); |
3c7d6fcc | 395 | return; |
212380e3 AC |
396 | } |
397 | ||
8bbeb278 | 398 | propagate_generic(source_p, "UNKLINE", parv[3], CAP_UNKLN, "%s %s", user, host); |
212380e3 AC |
399 | |
400 | if(match(parv[3], me.name) == 0) | |
3c7d6fcc | 401 | return; |
431a1a27 | 402 | |
f66f0baa | 403 | propagated = false; |
212380e3 | 404 | } |
431a1a27 JT |
405 | |
406 | aconf = find_exact_conf_by_address(host, CONF_KILL, user); | |
407 | ||
408 | /* No clustering for removing a propagated kline */ | |
409 | if(propagated && (aconf == NULL || !aconf->lifetime) && | |
410 | rb_dlink_list_length(&cluster_conf_list) > 0) | |
212380e3 AC |
411 | cluster_generic(source_p, "UNKLINE", SHARED_UNKLINE, CAP_UNKLN, |
412 | "%s %s", user, host); | |
413 | ||
40a1d446 JT |
414 | if(aconf == NULL) |
415 | { | |
416 | sendto_one_notice(source_p, ":No K-Line for %s@%s", user, host); | |
3c7d6fcc | 417 | return; |
40a1d446 | 418 | } |
55abcbb2 | 419 | |
431a1a27 JT |
420 | if(aconf->lifetime) |
421 | { | |
422 | if(propagated) | |
423 | remove_prop_kline(source_p, aconf); | |
424 | else | |
425 | sendto_one_notice(source_p, ":Cannot remove global K-Line %s@%s on specific servers", user, host); | |
3c7d6fcc | 426 | return; |
431a1a27 | 427 | } |
40a1d446 | 428 | |
d922dded | 429 | if(remove_temp_kline(source_p, aconf)) |
3c7d6fcc | 430 | return; |
212380e3 | 431 | |
40a1d446 | 432 | remove_permkline_match(source_p, aconf); |
212380e3 AC |
433 | } |
434 | ||
435 | /* ms_unkline() | |
436 | * | |
437 | * parv[1] - target server | |
438 | * parv[2] - user to unkline | |
439 | * parv[3] - host to unkline | |
440 | */ | |
3c7d6fcc | 441 | static void |
428ca87b | 442 | ms_unkline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 | 443 | { |
8bbeb278 AC |
444 | /* parv[0] parv[1] parv[2] parv[3] |
445 | * oper target server user host */ | |
446 | propagate_generic(source_p, "UNKLINE", parv[1], CAP_UNKLN, "%s %s", parv[2], parv[3]); | |
212380e3 AC |
447 | |
448 | if(!match(parv[1], me.name)) | |
3c7d6fcc | 449 | return; |
212380e3 AC |
450 | |
451 | if(!IsPerson(source_p)) | |
3c7d6fcc | 452 | return; |
212380e3 AC |
453 | |
454 | handle_remote_unkline(source_p, parv[2], parv[3]); | |
212380e3 AC |
455 | } |
456 | ||
3c7d6fcc | 457 | static void |
428ca87b | 458 | me_unkline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
459 | { |
460 | /* user host */ | |
461 | if(!IsPerson(source_p)) | |
3c7d6fcc | 462 | return; |
212380e3 AC |
463 | |
464 | handle_remote_unkline(source_p, parv[1], parv[2]); | |
212380e3 AC |
465 | } |
466 | ||
467 | static void | |
468 | handle_remote_unkline(struct Client *source_p, const char *user, const char *host) | |
469 | { | |
40a1d446 JT |
470 | struct ConfItem *aconf; |
471 | ||
212380e3 | 472 | if(!find_shared_conf(source_p->username, source_p->host, |
8bbeb278 | 473 | source_p->servptr->name, SHARED_UNKLINE)) |
212380e3 AC |
474 | return; |
475 | ||
40a1d446 JT |
476 | aconf = find_exact_conf_by_address(host, CONF_KILL, user); |
477 | if(aconf == NULL) | |
478 | { | |
479 | sendto_one_notice(source_p, ":No K-Line for %s@%s", user, host); | |
480 | return; | |
481 | } | |
431a1a27 JT |
482 | if(aconf->lifetime) |
483 | { | |
484 | sendto_one_notice(source_p, ":Cannot remove global K-Line %s@%s on specific servers", user, host); | |
485 | return; | |
486 | } | |
40a1d446 | 487 | |
d922dded | 488 | if(remove_temp_kline(source_p, aconf)) |
212380e3 | 489 | return; |
212380e3 | 490 | |
40a1d446 | 491 | remove_permkline_match(source_p, aconf); |
212380e3 AC |
492 | } |
493 | ||
494 | /* apply_kline() | |
495 | * | |
55abcbb2 | 496 | * inputs - |
212380e3 AC |
497 | * output - NONE |
498 | * side effects - kline as given, is added to the hashtable | |
499 | * and conf file | |
500 | */ | |
501 | static void | |
502 | apply_kline(struct Client *source_p, struct ConfItem *aconf, | |
b52c2949 | 503 | const char *reason, const char *oper_reason) |
212380e3 | 504 | { |
40c1fd47 | 505 | add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf); |
8bbeb278 AC |
506 | bandb_add(BANDB_KLINE, source_p, aconf->user, aconf->host, |
507 | reason, EmptyString(oper_reason) ? NULL : oper_reason, 0); | |
9964e935 AC |
508 | |
509 | /* no oper reason.. */ | |
510 | if(EmptyString(oper_reason)) | |
511 | { | |
512 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
513 | "%s added K-Line for [%s@%s] [%s]", | |
514 | get_oper_name(source_p), aconf->user, aconf->host, reason); | |
515 | ilog(L_KLINE, "K %s 0 %s %s %s", | |
516 | get_oper_name(source_p), aconf->user, aconf->host, reason); | |
517 | } | |
518 | else | |
519 | { | |
520 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
521 | "%s added K-Line for [%s@%s] [%s|%s]", | |
522 | get_oper_name(source_p), aconf->user, aconf->host, | |
523 | reason, oper_reason); | |
524 | ilog(L_KLINE, "K %s 0 %s %s %s|%s", | |
525 | get_oper_name(source_p), aconf->user, aconf->host, reason, oper_reason); | |
526 | } | |
527 | ||
528 | sendto_one_notice(source_p, ":Added K-Line [%s@%s]", | |
529 | aconf->user, aconf->host); | |
212380e3 AC |
530 | } |
531 | ||
532 | /* apply_tkline() | |
533 | * | |
534 | * inputs - | |
535 | * output - NONE | |
536 | * side effects - tkline as given is placed | |
537 | */ | |
538 | static void | |
539 | apply_tkline(struct Client *source_p, struct ConfItem *aconf, | |
b52c2949 | 540 | const char *reason, const char *oper_reason, int tkline_time) |
212380e3 | 541 | { |
e3354945 | 542 | aconf->hold = rb_current_time() + tkline_time; |
212380e3 AC |
543 | add_temp_kline(aconf); |
544 | ||
545 | /* no oper reason.. */ | |
546 | if(EmptyString(oper_reason)) | |
547 | { | |
548 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
8bbeb278 AC |
549 | "%s added temporary %d min. K-Line for [%s@%s] [%s]", |
550 | get_oper_name(source_p), tkline_time / 60, | |
551 | aconf->user, aconf->host, reason); | |
212380e3 | 552 | ilog(L_KLINE, "K %s %d %s %s %s", |
8bbeb278 | 553 | get_oper_name(source_p), tkline_time / 60, aconf->user, aconf->host, reason); |
212380e3 AC |
554 | } |
555 | else | |
556 | { | |
557 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
8bbeb278 AC |
558 | "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]", |
559 | get_oper_name(source_p), tkline_time / 60, | |
560 | aconf->user, aconf->host, reason, oper_reason); | |
212380e3 | 561 | ilog(L_KLINE, "K %s %d %s %s %s|%s", |
8bbeb278 AC |
562 | get_oper_name(source_p), tkline_time / 60, |
563 | aconf->user, aconf->host, reason, oper_reason); | |
212380e3 AC |
564 | } |
565 | ||
566 | sendto_one_notice(source_p, ":Added temporary %d min. K-Line [%s@%s]", | |
567 | tkline_time / 60, aconf->user, aconf->host); | |
568 | } | |
569 | ||
431a1a27 JT |
570 | static void |
571 | apply_prop_kline(struct Client *source_p, struct ConfItem *aconf, | |
572 | const char *reason, const char *oper_reason, int tkline_time) | |
573 | { | |
431a1a27 JT |
574 | aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY; |
575 | aconf->hold = rb_current_time() + tkline_time; | |
576 | aconf->lifetime = aconf->hold; | |
577 | ||
3cbbfb25 | 578 | replace_old_ban(aconf); |
431a1a27 JT |
579 | |
580 | rb_dlinkAddAlloc(aconf, &prop_bans); | |
581 | add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf); | |
582 | ||
583 | /* no oper reason.. */ | |
584 | if(EmptyString(oper_reason)) | |
585 | { | |
586 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
587 | "%s added global %d min. K-Line for [%s@%s] [%s]", | |
588 | get_oper_name(source_p), tkline_time / 60, | |
589 | aconf->user, aconf->host, reason); | |
590 | ilog(L_KLINE, "K %s %d %s %s %s", | |
591 | get_oper_name(source_p), tkline_time / 60, aconf->user, aconf->host, reason); | |
592 | } | |
593 | else | |
594 | { | |
595 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
596 | "%s added global %d min. K-Line for [%s@%s] [%s|%s]", | |
597 | get_oper_name(source_p), tkline_time / 60, | |
598 | aconf->user, aconf->host, reason, oper_reason); | |
599 | ilog(L_KLINE, "K %s %d %s %s %s|%s", | |
600 | get_oper_name(source_p), tkline_time / 60, | |
601 | aconf->user, aconf->host, reason, oper_reason); | |
602 | } | |
603 | ||
604 | sendto_one_notice(source_p, ":Added global %d min. K-Line [%s@%s]", | |
605 | tkline_time / 60, aconf->user, aconf->host); | |
606 | ||
607 | sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS, | |
cedb7d05 | 608 | ":%s BAN K %s %s %lu %d %d * :%s%s%s", |
431a1a27 JT |
609 | source_p->id, aconf->user, aconf->host, |
610 | (unsigned long)aconf->created, | |
611 | (int)(aconf->hold - aconf->created), | |
612 | (int)(aconf->lifetime - aconf->created), | |
613 | reason, | |
614 | oper_reason ? "|" : "", | |
615 | oper_reason ? oper_reason : ""); | |
616 | } | |
617 | ||
212380e3 | 618 | /* find_user_host() |
55abcbb2 | 619 | * |
212380e3 | 620 | * inputs - client placing kline, user@host, user buffer, host buffer |
3c7d6fcc | 621 | * output - false if not ok to kline, true to kline i.e. if valid user host |
212380e3 AC |
622 | * side effects - |
623 | */ | |
3c7d6fcc | 624 | static bool |
212380e3 AC |
625 | find_user_host(struct Client *source_p, const char *userhost, char *luser, char *lhost) |
626 | { | |
627 | char *hostp; | |
628 | ||
629 | hostp = strchr(userhost, '@'); | |
8bbeb278 | 630 | |
212380e3 AC |
631 | if(hostp != NULL) /* I'm a little user@host */ |
632 | { | |
633 | *(hostp++) = '\0'; /* short and squat */ | |
634 | if(*userhost) | |
f427c8b0 | 635 | rb_strlcpy(luser, userhost, USERLEN + 1); /* here is my user */ |
212380e3 AC |
636 | else |
637 | strcpy(luser, "*"); | |
638 | if(*hostp) | |
f427c8b0 | 639 | rb_strlcpy(lhost, hostp, HOSTLEN + 1); /* here is my host */ |
212380e3 AC |
640 | else |
641 | strcpy(lhost, "*"); | |
8bbeb278 | 642 | } |
212380e3 AC |
643 | else |
644 | { | |
645 | /* no '@', no '.', so its not a user@host or host, therefore | |
646 | * its a nick, which support was removed for. | |
647 | */ | |
648 | if(strchr(userhost, '.') == NULL && strchr(userhost, ':') == NULL) | |
af0ba24b JT |
649 | { |
650 | sendto_one_notice(source_p, ":K-Line must be a user@host or host"); | |
3c7d6fcc | 651 | return false; |
af0ba24b | 652 | } |
212380e3 AC |
653 | |
654 | luser[0] = '*'; /* no @ found, assume its *@somehost */ | |
655 | luser[1] = '\0'; | |
f427c8b0 | 656 | rb_strlcpy(lhost, userhost, HOSTLEN + 1); |
212380e3 AC |
657 | } |
658 | ||
2b843a5b JT |
659 | /* would break the protocol */ |
660 | if (*luser == ':' || *lhost == ':') | |
661 | { | |
662 | sendto_one_notice(source_p, ":Invalid K-Line"); | |
3c7d6fcc | 663 | return false; |
2b843a5b JT |
664 | } |
665 | ||
3c7d6fcc | 666 | return true; |
212380e3 AC |
667 | } |
668 | ||
669 | /* valid_user_host() | |
670 | * | |
671 | * inputs - user buffer, host buffer | |
3c7d6fcc | 672 | * output - false if invalid, true if valid |
212380e3 AC |
673 | * side effects - |
674 | */ | |
3c7d6fcc | 675 | static bool |
212380e3 AC |
676 | valid_user_host(struct Client *source_p, const char *luser, const char *lhost) |
677 | { | |
8bbeb278 AC |
678 | /* # is invalid, as are '!' (n!u@h kline) and '@' (u@@h kline) */ |
679 | if(strchr(lhost, '#') || strchr(luser, '#') || strchr(luser, '!') || strchr(lhost, '@')) | |
212380e3 AC |
680 | { |
681 | sendto_one_notice(source_p, ":Invalid K-Line"); | |
3c7d6fcc | 682 | return false; |
212380e3 AC |
683 | } |
684 | ||
3c7d6fcc | 685 | return true; |
212380e3 AC |
686 | } |
687 | ||
212380e3 AC |
688 | /* already_placed_kline() |
689 | * | |
690 | * inputs - source to notify, user@host to check, tkline time | |
3c7d6fcc EM |
691 | * outputs - true if a perm kline or a tkline when a tkline is being |
692 | * set exists, else false | |
212380e3 AC |
693 | * side effects - notifies source_p kline exists |
694 | */ | |
695 | /* Note: This currently works if the new K-line is a special case of an | |
696 | * existing K-line, but not the other way round. To do that we would | |
697 | * have to walk the hash and check every existing K-line. -A1kmm. | |
698 | */ | |
3c7d6fcc | 699 | static bool |
212380e3 AC |
700 | already_placed_kline(struct Client *source_p, const char *luser, const char *lhost, int tkline) |
701 | { | |
7f27e316 | 702 | const char *reason, *p; |
e7046ee5 | 703 | struct rb_sockaddr_storage iphost, *piphost; |
212380e3 | 704 | struct ConfItem *aconf; |
8bbeb278 | 705 | int t, bits; |
7f27e316 JT |
706 | |
707 | aconf = find_exact_conf_by_address(lhost, CONF_KILL, luser); | |
8bbeb278 | 708 | if(aconf == NULL && ConfigFileEntry.non_redundant_klines) |
212380e3 | 709 | { |
7f27e316 | 710 | bits = 0; |
7d9e8e9d EK |
711 | t = parse_netmask_strict(lhost, &iphost, &bits); |
712 | piphost = &iphost; | |
713 | if (t == HM_IPV4) | |
714 | t = AF_INET; | |
715 | else if (t == HM_IPV6) | |
716 | t = AF_INET6; | |
212380e3 AC |
717 | else |
718 | piphost = NULL; | |
719 | ||
8bbeb278 AC |
720 | aconf = find_conf_by_address(lhost, NULL, NULL, (struct sockaddr *) piphost, |
721 | CONF_KILL, t, luser, NULL); | |
722 | if(aconf != NULL) | |
212380e3 | 723 | { |
7f27e316 JT |
724 | /* The above was really a lookup of a single IP, |
725 | * so check if the new kline is wider than the | |
726 | * existing one. | |
727 | * -- jilles | |
728 | */ | |
729 | p = strchr(aconf->host, '/'); | |
8bbeb278 | 730 | if(bits > 0 && (p == NULL || bits < atoi(p + 1))) |
7f27e316 JT |
731 | aconf = NULL; |
732 | } | |
733 | } | |
8bbeb278 | 734 | if(aconf != NULL) |
7f27e316 JT |
735 | { |
736 | /* setting a tkline, or existing one is perm */ | |
737 | if(tkline || ((aconf->flags & CONF_FLAGS_TEMPORARY) == 0)) | |
738 | { | |
739 | reason = aconf->passwd ? aconf->passwd : "<No Reason>"; | |
740 | ||
741 | sendto_one_notice(source_p, | |
742 | ":[%s@%s] already K-Lined by [%s@%s] - %s", | |
8bbeb278 | 743 | luser, lhost, aconf->user, aconf->host, reason); |
3c7d6fcc | 744 | return true; |
212380e3 AC |
745 | } |
746 | } | |
747 | ||
3c7d6fcc | 748 | return false; |
212380e3 AC |
749 | } |
750 | ||
751 | /* remove_permkline_match() | |
752 | * | |
753 | * hunts for a permanent kline, and removes it. | |
754 | */ | |
755 | static void | |
40a1d446 | 756 | remove_permkline_match(struct Client *source_p, struct ConfItem *aconf) |
212380e3 | 757 | { |
8bbeb278 | 758 | sendto_one_notice(source_p, ":K-Line for [%s@%s] is removed", aconf->user, aconf->host); |
212380e3 AC |
759 | |
760 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
8bbeb278 AC |
761 | "%s has removed the K-Line for: [%s@%s]", |
762 | get_oper_name(source_p), aconf->user, aconf->host); | |
212380e3 | 763 | |
8bbeb278 | 764 | ilog(L_KLINE, "UK %s %s %s", get_oper_name(source_p), aconf->user, aconf->host); |
40a1d446 | 765 | |
54ac8b60 | 766 | remove_reject_mask(aconf->user, aconf->host); |
8bbeb278 | 767 | bandb_del(BANDB_KLINE, aconf->user, aconf->host); |
40a1d446 | 768 | delete_one_address_conf(aconf->host, aconf); |
212380e3 AC |
769 | } |
770 | ||
212380e3 AC |
771 | /* remove_temp_kline() |
772 | * | |
773 | * inputs - username, hostname to unkline | |
774 | * outputs - | |
775 | * side effects - tries to unkline anything that matches | |
776 | */ | |
3c7d6fcc | 777 | static bool |
d922dded | 778 | remove_temp_kline(struct Client *source_p, struct ConfItem *aconf) |
212380e3 | 779 | { |
5b96d9a6 | 780 | rb_dlink_node *ptr; |
212380e3 AC |
781 | int i; |
782 | ||
8bbeb278 | 783 | for(i = 0; i < LAST_TEMP_TYPE; i++) |
212380e3 | 784 | { |
5b96d9a6 | 785 | RB_DLINK_FOREACH(ptr, temp_klines[i].head) |
212380e3 | 786 | { |
8bbeb278 | 787 | if(aconf == ptr->data) |
212380e3 | 788 | { |
d922dded | 789 | sendto_one_notice(source_p, |
8bbeb278 AC |
790 | ":Un-klined [%s@%s] from temporary k-lines", |
791 | aconf->user, aconf->host); | |
d922dded | 792 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
8bbeb278 AC |
793 | "%s has removed the temporary K-Line for: [%s@%s]", |
794 | get_oper_name(source_p), aconf->user, | |
795 | aconf->host); | |
d922dded JT |
796 | |
797 | ilog(L_KLINE, "UK %s %s %s", | |
8bbeb278 | 798 | get_oper_name(source_p), aconf->user, aconf->host); |
555ac41f | 799 | rb_dlinkDestroy(ptr, &temp_klines[i]); |
54ac8b60 | 800 | remove_reject_mask(aconf->user, aconf->host); |
40a1d446 | 801 | delete_one_address_conf(aconf->host, aconf); |
3c7d6fcc | 802 | return true; |
212380e3 | 803 | } |
212380e3 AC |
804 | } |
805 | } | |
806 | ||
3c7d6fcc | 807 | return false; |
212380e3 | 808 | } |
431a1a27 JT |
809 | |
810 | static void | |
811 | remove_prop_kline(struct Client *source_p, struct ConfItem *aconf) | |
812 | { | |
813 | rb_dlink_node *ptr; | |
483987a4 | 814 | time_t now; |
431a1a27 JT |
815 | |
816 | ptr = rb_dlinkFind(aconf, &prop_bans); | |
817 | if (!ptr) | |
818 | return; | |
819 | sendto_one_notice(source_p, | |
820 | ":Un-klined [%s@%s] from global k-lines", | |
821 | aconf->user, aconf->host); | |
822 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
823 | "%s has removed the global K-Line for: [%s@%s]", | |
824 | get_oper_name(source_p), aconf->user, | |
825 | aconf->host); | |
826 | ||
827 | ilog(L_KLINE, "UK %s %s %s", | |
828 | get_oper_name(source_p), aconf->user, aconf->host); | |
483987a4 JT |
829 | now = rb_current_time(); |
830 | if(aconf->created < now) | |
831 | aconf->created = now; | |
431a1a27 JT |
832 | else |
833 | aconf->created++; | |
cedb7d05 | 834 | aconf->hold = aconf->created; |
431a1a27 JT |
835 | operhash_delete(aconf->info.oper); |
836 | aconf->info.oper = operhash_add(get_oper_name(source_p)); | |
837 | aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY; | |
838 | sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS, | |
cedb7d05 | 839 | ":%s BAN K %s %s %lu %d %d * :*", |
431a1a27 JT |
840 | source_p->id, aconf->user, aconf->host, |
841 | (unsigned long)aconf->created, | |
842 | 0, | |
843 | (int)(aconf->lifetime - aconf->created)); | |
844 | remove_reject_mask(aconf->user, aconf->host); | |
483987a4 | 845 | deactivate_conf(aconf, ptr, now); |
431a1a27 | 846 | } |