ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_kline.c
Revision: 3208
Committed: Tue Mar 25 15:31:30 2014 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 13576 byte(s)
Log Message:
- Minor cleanup to m_dline.c, m_gline.c, m_kline.c

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 */
21
22 /*! \file m_kline.c
23 * \brief Includes required functions for processing the KLINE/UNKLINE command.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "client.h"
30 #include "irc_string.h"
31 #include "ircd.h"
32 #include "conf.h"
33 #include "hostmask.h"
34 #include "numeric.h"
35 #include "log.h"
36 #include "s_misc.h"
37 #include "send.h"
38 #include "hash.h"
39 #include "s_serv.h"
40 #include "s_gline.h"
41 #include "parse.h"
42 #include "modules.h"
43 #include "conf_db.h"
44 #include "memory.h"
45
46
47 static void
48 check_kline(struct AddressRec *arec)
49 {
50 dlink_node *ptr = NULL, *ptr_next = NULL;
51
52 DLINK_FOREACH_SAFE(ptr, ptr_next, local_client_list.head)
53 {
54 struct Client *client_p = ptr->data;
55
56 if (IsDead(client_p))
57 continue;
58
59 if (match(arec->username, client_p->username))
60 continue;
61
62 switch (arec->masktype)
63 {
64 case HM_IPV4:
65 if (client_p->localClient->aftype == AF_INET)
66 if (match_ipv4(&client_p->localClient->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits))
67 conf_try_ban(client_p, arec->conf);
68 break;
69 case HM_IPV6:
70 if (client_p->localClient->aftype == AF_INET6)
71 if (match_ipv6(&client_p->localClient->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits))
72 conf_try_ban(client_p, arec->conf);
73 break;
74 default: /* HM_HOST */
75 if (!match(arec->Mask.hostname, client_p->host))
76 conf_try_ban(client_p, arec->conf);
77 break;
78 }
79 }
80 }
81
82 /* apply_tkline()
83 *
84 * inputs -
85 * output - NONE
86 * side effects - tkline as given is placed
87 */
88 static void
89 m_kline_add_kline(struct Client *source_p, struct MaskItem *conf,
90 time_t tkline_time)
91 {
92 if (tkline_time)
93 {
94 conf->until = CurrentTime + tkline_time;
95 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
96 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
97 get_oper_name(source_p), tkline_time/60,
98 conf->user, conf->host,
99 conf->reason);
100 sendto_one_notice(source_p, &me, ":Added temporary %d min. K-Line [%s@%s]",
101 tkline_time/60, conf->user, conf->host);
102 ilog(LOG_TYPE_KLINE, "%s added temporary %d min. K-Line for [%s@%s] [%s]",
103 get_oper_name(source_p), tkline_time/60,
104 conf->user, conf->host, conf->reason);
105 }
106 else
107 {
108 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
109 "%s added K-Line for [%s@%s] [%s]",
110 get_oper_name(source_p),
111 conf->user, conf->host, conf->reason);
112 sendto_one_notice(source_p, &me, ":Added K-Line [%s@%s]",
113 conf->user, conf->host);
114 ilog(LOG_TYPE_KLINE, "%s added K-Line for [%s@%s] [%s]",
115 get_oper_name(source_p), conf->user, conf->host, conf->reason);
116 }
117
118 conf->setat = CurrentTime;
119 SetConfDatabase(conf);
120
121 check_kline(add_conf_by_address(CONF_KLINE, conf));
122 }
123
124 /* static int remove_tkline_match(const char *host, const char *user)
125 * Input: A hostname, a username to unkline.
126 * Output: returns YES on success, NO if no tkline removed.
127 * Side effects: Any matching tklines are removed.
128 */
129 static int
130 remove_kline_match(const char *host, const char *user)
131 {
132 struct irc_ssaddr iphost, *piphost;
133 struct MaskItem *conf;
134 int t = 0;
135 int aftype = 0;
136
137 if ((t = parse_netmask(host, &iphost, NULL)) != HM_HOST)
138 {
139 #ifdef IPV6
140 if (t == HM_IPV6)
141 aftype = AF_INET6;
142 else
143 #endif
144 aftype = AF_INET;
145 piphost = &iphost;
146 }
147 else
148 piphost = NULL;
149
150 if ((conf = find_conf_by_address(host, piphost, CONF_KLINE, aftype, user, NULL, 0)))
151 {
152 if (IsConfDatabase(conf))
153 {
154 delete_one_address_conf(host, conf);
155 return 1;
156 }
157 }
158
159 return 0;
160 }
161
162 /* already_placed_kline()
163 * inputs - user to complain to, username & host to check for
164 * outputs - returns 1 on existing K-line, 0 if doesn't exist
165 * side effects - notifies source_p if the K-line already exists
166 */
167 /*
168 * Note: This currently works if the new K-line is a special case of an
169 * existing K-line, but not the other way round. To do that we would
170 * have to walk the hash and check every existing K-line. -A1kmm.
171 */
172 static int
173 already_placed_kline(struct Client *source_p, const char *luser, const char *lhost, int warn)
174 {
175 const char *reason;
176 struct irc_ssaddr iphost, *piphost;
177 struct MaskItem *conf = NULL;
178 int t = 0;
179 int aftype = 0;
180
181 if ((t = parse_netmask(lhost, &iphost, NULL)) != HM_HOST)
182 {
183 #ifdef IPV6
184 if (t == HM_IPV6)
185 aftype = AF_INET6;
186 else
187 #endif
188 aftype = AF_INET;
189 piphost = &iphost;
190 }
191 else
192 piphost = NULL;
193
194 if ((conf = find_conf_by_address(lhost, piphost, CONF_KLINE, aftype, luser, NULL, 0)))
195 {
196 if (warn)
197 {
198 reason = conf->reason ? conf->reason : CONF_NOREASON;
199 sendto_one_notice(source_p, &me, ":[%s@%s] already K-Lined by [%s@%s] - %s",
200 luser, lhost, conf->user, conf->host, reason);
201 }
202
203 return 1;
204 }
205
206 return 0;
207 }
208
209 /* mo_kline()
210 *
211 * inputs - pointer to server
212 * - pointer to client
213 * - parameter count
214 * - parameter list
215 * output -
216 * side effects - k line is added
217 */
218 static int
219 mo_kline(struct Client *source_p, int parc, char *parv[])
220 {
221 char buffer[IRCD_BUFSIZE];
222 char *reason = NULL;
223 char *user = NULL;
224 char *host = NULL;
225 const char *current_date;
226 char *target_server = NULL;
227 struct MaskItem *conf;
228 time_t tkline_time = 0;
229
230 if (!HasOFlag(source_p, OPER_FLAG_K))
231 {
232 sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "kline");
233 return 0;
234 }
235
236 if (parse_aline("KLINE", source_p, parc, parv, AWILD, &user, &host,
237 &tkline_time, &target_server, &reason) < 0)
238 return 0;
239
240 if (target_server != NULL)
241 {
242 sendto_match_servs(source_p, target_server, CAP_KLN, "KLINE %s %lu %s %s :%s",
243 target_server, (unsigned long)tkline_time,
244 user, host, reason);
245
246 /* Allow ON to apply local kline as well if it matches */
247 if (match(target_server, me.name))
248 return 0;
249 }
250 else
251 cluster_a_line(source_p, "KLINE", CAP_KLN, SHARED_KLINE,
252 "%d %s %s :%s", tkline_time, user, host, reason);
253
254 if (already_placed_kline(source_p, user, host, 1))
255 return 0;
256
257 current_date = smalldate(0);
258 conf = conf_make(CONF_KLINE);
259
260 conf->host = xstrdup(host);
261 conf->user = xstrdup(user);
262
263 if (tkline_time != 0)
264 snprintf(buffer, sizeof(buffer), "Temporary K-line %d min. - %s (%s)",
265 (int)(tkline_time/60), reason, current_date);
266 else
267 snprintf(buffer, sizeof(buffer), "%s (%s)", reason, current_date);
268
269 conf->reason = xstrdup(buffer);
270 m_kline_add_kline(source_p, conf, tkline_time);
271 return 0;
272 }
273
274 /* me_kline - handle remote kline. no propagation */
275 static int
276 me_kline(struct Client *source_p, int parc, char *parv[])
277 {
278 char buffer[IRCD_BUFSIZE];
279 struct MaskItem *conf = NULL;
280 int tkline_time = 0;
281 const char *current_date;
282 char *kuser, *khost, *kreason;
283
284 if (parc != 6 || EmptyString(parv[5]))
285 return 0;
286
287 if (match(parv[1], me.name))
288 return 0;
289
290 tkline_time = valid_tkline(parv[2], TK_SECONDS);
291 kuser = parv[3];
292 khost = parv[4];
293 kreason = parv[5];
294
295 current_date = smalldate(0);
296
297 if (HasFlag(source_p, FLAGS_SERVICE) ||
298 find_matching_name_conf(CONF_ULINE, source_p->servptr->name,
299 source_p->username, source_p->host,
300 SHARED_KLINE))
301 {
302 if (!IsClient(source_p) ||
303 already_placed_kline(source_p, kuser, khost, 1))
304 return 0;
305
306 conf = conf_make(CONF_KLINE);
307 conf->host = xstrdup(khost);
308 conf->user = xstrdup(kuser);
309
310 if (tkline_time != 0)
311 snprintf(buffer, sizeof(buffer), "Temporary K-line %d min. - %s (%s)",
312 (int)(tkline_time/60), kreason, current_date);
313 else
314 snprintf(buffer, sizeof(buffer), "%s (%s)", kreason, current_date);
315
316 conf->reason = xstrdup(buffer);
317 m_kline_add_kline(source_p, conf, tkline_time);
318 }
319
320 return 0;
321 }
322
323 static int
324 ms_kline(struct Client *source_p, int parc, char *parv[])
325 {
326 if (parc != 6 || EmptyString(parv[5]))
327 return 0;
328
329 /* parv[0] parv[1] parv[2] parv[3] parv[4] parv[5] */
330 /* oper target_server tkline_time user host reason */
331 sendto_match_servs(source_p, parv[1], CAP_KLN, "KLINE %s %s %s %s :%s",
332 parv[1], parv[2], parv[3], parv[4], parv[5]);
333
334 return me_kline(source_p, parc, parv);
335 }
336
337 /*
338 ** mo_unkline
339 ** Added Aug 31, 1997
340 ** common (Keith Fralick) fralick@gate.net
341 **
342 ** parv[0] = command
343 ** parv[1] = address to remove
344 *
345 *
346 */
347 static int
348 mo_unkline(struct Client *source_p, int parc, char *parv[])
349 {
350 char *target_server = NULL;
351 char *user, *host;
352
353 if (!HasOFlag(source_p, OPER_FLAG_UNKLINE))
354 {
355 sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "unkline");
356 return 0;
357 }
358
359 if (EmptyString(parv[1]))
360 {
361 sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "UNKLINE");
362 return 0;
363 }
364
365 if (parse_aline("UNKLINE", source_p, parc, parv, 0, &user,
366 &host, NULL, &target_server, NULL) < 0)
367 return 0;
368
369 if (target_server != NULL)
370 {
371 sendto_match_servs(source_p, target_server, CAP_UNKLN,
372 "UNKLINE %s %s %s",
373 target_server, user, host);
374
375 /* Allow ON to apply local unkline as well if it matches */
376 if (match(target_server, me.name))
377 return 0;
378 }
379 else
380 cluster_a_line(source_p, "UNKLINE", CAP_UNKLN, SHARED_UNKLINE,
381 "%s %s", user, host);
382
383 if (remove_kline_match(host, user))
384 {
385 sendto_one_notice(source_p, &me, ":K-Line for [%s@%s] is removed",
386 user, host);
387 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
388 "%s has removed the K-Line for: [%s@%s]",
389 get_oper_name(source_p), user, host);
390 ilog(LOG_TYPE_KLINE, "%s removed K-Line for [%s@%s]",
391 get_oper_name(source_p), user, host);
392 }
393 else
394 sendto_one_notice(source_p, &me, ":No K-Line for [%s@%s] found",
395 user, host);
396 return 0;
397 }
398
399 /* me_unkline()
400 *
401 * inputs - server
402 * - client
403 * - parc
404 * - parv
405 * outputs - none
406 * side effects - if server is authorized, kline is removed
407 * does not propagate message
408 */
409 static int
410 me_unkline(struct Client *source_p, int parc, char *parv[])
411 {
412 const char *kuser, *khost;
413
414 if (parc != 4)
415 return 0;
416
417 kuser = parv[2];
418 khost = parv[3];
419
420 if (!IsClient(source_p) || match(parv[1], me.name))
421 return 0;
422
423 if (HasFlag(source_p, FLAGS_SERVICE) ||
424 find_matching_name_conf(CONF_ULINE, source_p->servptr->name,
425 source_p->username, source_p->host,
426 SHARED_UNKLINE))
427 {
428 if (remove_kline_match(khost, kuser))
429 {
430 sendto_one_notice(source_p, &me, ":K-Line for [%s@%s] is removed",
431 kuser, khost);
432 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
433 "%s has removed the K-Line for: [%s@%s]",
434 get_oper_name(source_p), kuser, khost);
435 ilog(LOG_TYPE_KLINE, "%s removed K-Line for [%s@%s]",
436 get_oper_name(source_p), kuser, khost);
437 }
438 else
439 sendto_one_notice(source_p, &me, ":No K-Line for [%s@%s] found",
440 kuser, khost);
441 }
442
443 return 0;
444 }
445
446 /* ms_unkline - propagates and handles a remote unkline message */
447 static int
448 ms_unkline(struct Client *source_p, int parc, char *parv[])
449 {
450 if (parc != 4)
451 return 0;
452
453 sendto_match_servs(source_p, parv[1], CAP_UNKLN,
454 "UNKLINE %s %s %s",
455 parv[1], parv[2], parv[3]);
456
457 return me_unkline(source_p, parc, parv);
458 }
459
460 static struct Message kline_msgtab =
461 {
462 "KLINE", 0, 0, 2, MAXPARA, MFLG_SLOW, 0,
463 { m_unregistered, m_not_oper, ms_kline, me_kline, mo_kline, m_ignore }
464 };
465
466 static struct Message unkline_msgtab =
467 {
468 "UNKLINE", 0, 0, 2, MAXPARA, MFLG_SLOW, 0,
469 { m_unregistered, m_not_oper, ms_unkline, me_unkline, mo_unkline, m_ignore }
470 };
471
472 static void
473 module_init(void)
474 {
475 mod_add_cmd(&kline_msgtab);
476 mod_add_cmd(&unkline_msgtab);
477 add_capability("KLN", CAP_KLN, 1);
478 add_capability("UNKLN", CAP_UNKLN, 1);
479 }
480
481 static void
482 module_exit(void)
483 {
484 mod_del_cmd(&kline_msgtab);
485 mod_del_cmd(&unkline_msgtab);
486 delete_capability("UNKLN");
487 delete_capability("KLN");
488 }
489
490 struct module module_entry =
491 {
492 .node = { NULL, NULL, NULL },
493 .name = NULL,
494 .version = "$Revision$",
495 .handle = NULL,
496 .modinit = module_init,
497 .modexit = module_exit,
498 .flags = 0
499 };

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision