]> jfr.im git - irc/quakenet/snircd.git/blob - ircd/gline.c
forward port of asuka-badchan.patch to .12
[irc/quakenet/snircd.git] / ircd / gline.c
1 /*
2 * IRC - Internet Relay Chat, ircd/gline.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Finland
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, or (at your option)
9 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /** @file
21 * @brief Implementation of Gline manipulation functions.
22 * @version $Id: gline.c,v 1.61 2005/09/22 20:42:21 entrope Exp $
23 */
24 #include "config.h"
25
26 #include "gline.h"
27 #include "channel.h"
28 #include "client.h"
29 #include "ircd.h"
30 #include "ircd_alloc.h"
31 #include "ircd_features.h"
32 #include "ircd_log.h"
33 #include "ircd_reply.h"
34 #include "ircd_snprintf.h"
35 #include "ircd_string.h"
36 #include "match.h"
37 #include "numeric.h"
38 #include "s_bsd.h"
39 #include "s_debug.h"
40 #include "s_misc.h"
41 #include "s_stats.h"
42 #include "send.h"
43 #include "struct.h"
44 #include "msg.h"
45 #include "numnicks.h"
46 #include "numeric.h"
47 #include "whocmds.h"
48 #include "hash.h"
49
50 /* #include <assert.h> -- Now using assert in ircd_log.h */
51 #include <string.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54
55 #define CHECK_APPROVED 0 /**< Mask is acceptable */
56 #define CHECK_OVERRIDABLE 1 /**< Mask is acceptable, but not by default */
57 #define CHECK_REJECTED 2 /**< Mask is totally unacceptable */
58
59 #define MASK_WILD_0 0x01 /**< Wildcards in the last position */
60 #define MASK_WILD_1 0x02 /**< Wildcards in the next-to-last position */
61
62 #define MASK_WILD_MASK 0x03 /**< Mask out the positional wildcards */
63
64 #define MASK_WILDS 0x10 /**< Mask contains wildcards */
65 #define MASK_IP 0x20 /**< Mask is an IP address */
66 #define MASK_HALT 0x40 /**< Finished processing mask */
67
68 /** List of user G-lines. */
69 struct Gline* GlobalGlineList = 0;
70 /** List of BadChan G-lines. */
71 struct Gline* BadChanGlineList = 0;
72
73 /** Find canonical user and host for a string.
74 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
75 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
76 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
77 *
78 * @param[in] userhost Input string from user.
79 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
80 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
81 * @param[in] def_user Default value for user part.
82 */
83 static void
84 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
85 {
86 char *tmp;
87
88 if (*userhost == '$') {
89 *user_p = userhost;
90 *host_p = NULL;
91 return;
92 }
93
94 if (!(tmp = strchr(userhost, '@'))) {
95 *user_p = def_user;
96 *host_p = userhost;
97 } else {
98 *user_p = userhost;
99 *(tmp++) = '\0';
100 *host_p = tmp;
101 }
102 }
103
104 /** Create a Gline structure.
105 * @param[in] user User part of mask.
106 * @param[in] host Host part of mask (NULL if not applicable).
107 * @param[in] reason Reason for G-line.
108 * @param[in] expire Expiration timestamp.
109 * @param[in] lastmod Last modification timestamp.
110 * @param[in] flags Bitwise combination of GLINE_* bits.
111 * @return Newly allocated G-line.
112 */
113 static struct Gline *
114 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
115 unsigned int flags)
116 {
117 struct Gline *gline, *sgline, *after = 0;
118
119 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
120
121 for (gline = GlobalGlineList; gline; gline = sgline) {
122 sgline = gline->gl_next;
123
124 if (gline->gl_expire <= CurrentTime)
125 gline_free(gline);
126 else if (((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL)) ||
127 (gline->gl_host && !host) || (!gline->gl_host && host))
128 continue;
129 else if (!mmatch(gline->gl_user, user) /* gline contains new mask */
130 && (gline->gl_host == NULL || !mmatch(gline->gl_host, host))) {
131 if (expire <= gline->gl_expire) /* will expire before wider gline */
132 return 0;
133 else
134 after = gline; /* stick new gline after this one */
135 } else if (!mmatch(user, gline->gl_user) /* new mask contains gline */
136 && (gline->gl_host==NULL || !mmatch(host, gline->gl_host))
137 && gline->gl_expire <= expire) /* old expires before new */
138 gline_free(gline); /* save some memory */
139 }
140 }
141
142 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
143 assert(0 != gline);
144
145 DupString(gline->gl_reason, reason); /* initialize gline... */
146 gline->gl_expire = expire;
147 gline->gl_lastmod = lastmod;
148 gline->gl_flags = flags & GLINE_MASK;
149
150 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
151 DupString(gline->gl_user, user); /* first, remember channel */
152 gline->gl_host = 0;
153
154 gline->gl_next = BadChanGlineList; /* then link it into list */
155 gline->gl_prev_p = &BadChanGlineList;
156 if (BadChanGlineList)
157 BadChanGlineList->gl_prev_p = &gline->gl_next;
158 BadChanGlineList = gline;
159 } else {
160 DupString(gline->gl_user, user); /* remember them... */
161 if (*user != '$')
162 DupString(gline->gl_host, host);
163 else
164 gline->gl_host = NULL;
165
166 if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
167 gline->gl_flags |= GLINE_IPMASK;
168
169 if (after) {
170 gline->gl_next = after->gl_next;
171 gline->gl_prev_p = &after->gl_next;
172 if (after->gl_next)
173 after->gl_next->gl_prev_p = &gline->gl_next;
174 after->gl_next = gline;
175 } else {
176 gline->gl_next = GlobalGlineList; /* then link it into list */
177 gline->gl_prev_p = &GlobalGlineList;
178 if (GlobalGlineList)
179 GlobalGlineList->gl_prev_p = &gline->gl_next;
180 GlobalGlineList = gline;
181 }
182 }
183
184 return gline;
185 }
186
187 /** Check local clients against a new G-line.
188 * If the G-line is inactive, return immediately.
189 * Otherwise, if any users match it, disconnect them or kick them if the G-line is a BADCHAN.
190 * @param[in] cptr Peer connect that sent the G-line.
191 * @param[in] sptr Client that originated the G-line.
192 * @param[in] gline New G-line to check.
193 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
194 */
195 static int
196 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
197 {
198 struct Client *acptr;
199 int fd, retval = 0, tval;
200
201 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
202 return 0;
203
204 if (GlineIsBadChan(gline)) {
205 /* Handle BADCHAN gline */
206 struct Channel *chptr,*nchptr;
207 struct Membership *member,*nmember;
208
209 if (string_has_wildcards(gline->gl_user)) {
210 for(chptr=GlobalChannelList;chptr;chptr=nchptr) {
211 nchptr=chptr->next;
212 if (match(gline->gl_user, chptr->chname))
213 continue;
214 for (member=chptr->members;member;member=nmember) {
215 nmember=member->next_member;
216 if (!MyUser(member->user) || IsZombie(member) || IsAnOper(member->user))
217 continue;
218 sendcmdto_serv_butone(&me, CMD_KICK, NULL, "%H %C :Badchanneled (%s)", chptr, member->user, gline->gl_reason);
219 sendcmdto_channel_butserv_butone(&me, CMD_KICK, chptr, NULL, 0, "%H %C :Badchanneled (%s)", chptr, member->user, gline->gl_reason);
220 make_zombie(member, member->user, &me, &me, chptr);
221 retval=1;
222 }
223 }
224 } else {
225 if ((chptr=FindChannel(gline->gl_user))) {
226 for (member=chptr->members;member;member=nmember) {
227 nmember=member->next_member;
228 if (!MyUser(member->user) || IsZombie(member) || IsAnOper(member->user))
229 continue;
230 sendcmdto_serv_butone(&me, CMD_KICK, NULL, "%H %C :Badchanneled (%s)", chptr, member->user, gline->gl_reason);
231 sendcmdto_channel_butserv_butone(&me, CMD_KICK, chptr, NULL, 0, "%H %C :Badchanneled (%s)", chptr, member->user, gline->gl_reason);
232 make_zombie(member, member->user, &me, &me, chptr);
233 retval=1;
234 }
235 }
236 }
237 } else {
238 for (fd = HighestFd; fd >= 0; --fd) {
239 /*
240 * get the users!
241 */
242 if ((acptr = LocalClientArray[fd])) {
243 if (!cli_user(acptr))
244 continue;
245
246 if (GlineIsRealName(gline)) { /* Realname Gline */
247 Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
248 gline->gl_user+2));
249 if (match(gline->gl_user+2, cli_info(acptr)) != 0)
250 continue;
251 Debug((DEBUG_DEBUG,"Matched!"));
252 } else { /* Host/IP gline */
253 if (cli_user(acptr)->username &&
254 match(gline->gl_user, (cli_user(acptr))->realusername) != 0)
255 continue;
256
257 if (GlineIsIpMask(gline)) {
258 if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
259 continue;
260 }
261 else {
262 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
263 continue;
264 }
265 }
266
267 /* ok, here's one that got G-lined */
268 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
269 gline->gl_reason);
270
271 /* let the ops know about it */
272 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
273 get_client_name(acptr, SHOW_IP));
274
275 /* and get rid of him */
276 /* Asuka - Reimplement HEAD_IN_SAND_GLINE from Lain */
277 if ((tval = exit_client_msg(cptr, acptr, &me,
278 feature_bool(FEAT_HIS_GLINE) ? "G-lined" : "G-lined (%s)", gline->gl_reason)))
279 retval = tval; /* retain killed status */
280 }
281 }
282 }
283 return retval;
284 }
285
286 /**
287 * Implements the mask checking applied to local G-lines.
288 * Basically, host masks must have a minimum of two non-wild domain
289 * fields, and IP masks must have a minimum of 16 bits. If the mask
290 * has even one wild-card, OVERRIDABLE is returned, assuming the other
291 * check doesn't fail.
292 * @param[in] mask G-line mask to check.
293 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
294 */
295 static int
296 gline_checkmask(char *mask)
297 {
298 unsigned int flags = MASK_IP;
299 unsigned int dots = 0;
300 unsigned int ipmask = 0;
301
302 for (; *mask; mask++) { /* go through given mask */
303 if (*mask == '.') { /* it's a separator; advance positional wilds */
304 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
305 dots++;
306
307 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
308 ipmask += 8; /* It's an IP with no wilds, count bits */
309 } else if (*mask == '*' || *mask == '?')
310 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
311 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
312 ++mask;
313 ipmask = strtoul(mask, &mask, 10);
314
315 /* sanity-check to date */
316 if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
317 return CHECK_REJECTED;
318 if (!dots) {
319 if (ipmask > 128)
320 return CHECK_REJECTED;
321 if (ipmask < 128)
322 flags |= MASK_WILDS;
323 } else {
324 if (dots != 3 || ipmask > 32)
325 return CHECK_REJECTED;
326 if (ipmask < 32)
327 flags |= MASK_WILDS;
328 }
329
330 flags |= MASK_HALT; /* Halt the ipmask calculation */
331 break; /* get out of the loop */
332 } else if (!IsIP6Char(*mask)) {
333 flags &= ~MASK_IP; /* not an IP anymore! */
334 ipmask = 0;
335 }
336 }
337
338 /* Sanity-check quads */
339 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
340 flags &= ~MASK_IP;
341 ipmask = 0;
342 }
343
344 /* update bit count if necessary */
345 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
346 ipmask += 8;
347
348 /* Check to see that it's not too wide of a mask */
349 if (flags & MASK_WILDS &&
350 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
351 (flags & MASK_IP && ipmask < 16)))
352 return CHECK_REJECTED; /* to wide, reject */
353
354 /* Ok, it's approved; require override if it has wildcards, though */
355 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
356 }
357
358 /** Forward a G-line to other servers.
359 * @param[in] cptr Client that sent us the G-line.
360 * @param[in] sptr Client that originated the G-line.
361 * @param[in] gline G-line to forward.
362 * @return Zero.
363 */
364 int
365 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
366 {
367 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
368 return 0;
369
370 if (gline->gl_lastmod)
371 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
372 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
373 gline->gl_host ? "@" : "",
374 gline->gl_host ? gline->gl_host : "",
375 gline->gl_expire - CurrentTime, gline->gl_lastmod,
376 gline->gl_reason);
377 else
378 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
379 (GlineIsRemActive(gline) ?
380 "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
381 gline->gl_user,
382 gline->gl_host ? "@" : "",
383 gline->gl_host ? gline->gl_host : "",
384 gline->gl_expire - CurrentTime, gline->gl_reason);
385
386 return 0;
387 }
388
389 /** Create a new G-line and add it to global lists.
390 * \a userhost may be in one of four forms:
391 * \li A channel name, to add a BadChan.
392 * \li A string starting with $R and followed by a mask to match against their realname.
393 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
394 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
395 *
396 * @param[in] cptr Client that sent us the G-line.
397 * @param[in] sptr Client that originated the G-line.
398 * @param[in] userhost Text mask for the G-line.
399 * @param[in] reason Reason for G-line.
400 * @param[in] expire Duration of G-line in seconds.
401 * @param[in] lastmod Last modification time of G-line.
402 * @param[in] flags Bitwise combination of GLINE_* flags.
403 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
404 */
405 int
406 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
407 char *reason, time_t expire, time_t lastmod, unsigned int flags)
408 {
409 struct Gline *agline;
410 char uhmask[USERLEN + HOSTLEN + 2];
411 char *user, *host;
412 int tmp;
413
414 assert(0 != userhost);
415 assert(0 != reason);
416
417 if (*userhost == '#' || *userhost == '&') {
418 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
419 return send_reply(sptr, ERR_NOPRIVILEGES);
420
421 flags |= GLINE_BADCHAN;
422 user = userhost;
423 host = 0;
424 } else if (*userhost == '$') {
425 switch (*userhost == '$' ? userhost[1] : userhost[3]) {
426 case 'R': flags |= GLINE_REALNAME; break;
427 default:
428 /* uh, what to do here? */
429 /* The answer, my dear Watson, is we throw a protocol_violation()
430 -- hikari */
431 if (IsServer(cptr))
432 return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
433 else {
434 sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
435 return 0;
436 }
437 break;
438 }
439 user = (*userhost =='$' ? userhost : userhost+2);
440 host = 0;
441 } else {
442 canon_userhost(userhost, &user, &host, "*");
443 if (sizeof(uhmask) <
444 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
445 return send_reply(sptr, ERR_LONGMASK);
446 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
447 switch (gline_checkmask(host)) {
448 case CHECK_OVERRIDABLE: /* oper overrided restriction */
449 if (flags & GLINE_OPERFORCE)
450 break;
451 /*FALLTHROUGH*/
452 case CHECK_REJECTED:
453 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
454 break;
455 }
456
457 if ((tmp = count_users(uhmask)) >=
458 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
459 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
460 }
461 }
462
463 /*
464 * You cannot set a negative (or zero) expire time, nor can you set an
465 * expiration time for greater than GLINE_MAX_EXPIRE.
466 */
467 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
468 if (!IsServer(sptr) && MyConnect(sptr))
469 send_reply(sptr, ERR_BADEXPIRE, expire);
470 return 0;
471 }
472
473 expire += CurrentTime; /* convert from lifetime to timestamp */
474
475 /* Inform ops... */
476 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
477 SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
478 "%Tu: %s",
479 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
480 cli_name(sptr) :
481 cli_name((cli_user(sptr))->server),
482 (flags & GLINE_LOCAL) ? "local" : "global",
483 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
484 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
485 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
486 expire + TSoffset, reason);
487
488 /* and log it */
489 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
490 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
491 flags & GLINE_LOCAL ? "local" : "global",
492 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
493 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
494 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
495 expire + TSoffset, reason);
496
497 /* make the gline */
498 agline = make_gline(user, host, reason, expire, lastmod, flags);
499
500 if (!agline) /* if it overlapped, silently return */
501 return 0;
502
503 gline_propagate(cptr, sptr, agline);
504
505 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
506 }
507
508 /** Activate a currently inactive G-line.
509 * @param[in] cptr Peer that told us to activate the G-line.
510 * @param[in] sptr Client that originally thought it was a good idea.
511 * @param[in] gline G-line to activate.
512 * @param[in] lastmod New value for last modification timestamp.
513 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
514 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
515 */
516 int
517 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
518 time_t lastmod, unsigned int flags)
519 {
520 unsigned int saveflags = 0;
521
522 assert(0 != gline);
523
524 saveflags = gline->gl_flags;
525
526 if (flags & GLINE_LOCAL)
527 gline->gl_flags &= ~GLINE_LDEACT;
528 else {
529 gline->gl_flags |= GLINE_ACTIVE;
530
531 if (gline->gl_lastmod) {
532 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
533 gline->gl_lastmod++;
534 else
535 gline->gl_lastmod = lastmod;
536 }
537 }
538
539 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
540 return 0; /* was active to begin with */
541
542 /* Inform ops and log it */
543 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
544 "expiring at %Tu: %s",
545 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
546 cli_name(sptr) :
547 cli_name((cli_user(sptr))->server),
548 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
549 gline->gl_user, gline->gl_host ? "@" : "",
550 gline->gl_host ? gline->gl_host : "",
551 gline->gl_expire + TSoffset, gline->gl_reason);
552
553 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
554 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
555 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
556 gline->gl_host ? "@" : "",
557 gline->gl_host ? gline->gl_host : "",
558 gline->gl_expire + TSoffset, gline->gl_reason);
559
560 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
561 gline_propagate(cptr, sptr, gline);
562
563 return do_gline(cptr, sptr, gline);
564 }
565
566 /** Deactivate a G-line.
567 * @param[in] cptr Peer that gave us the message.
568 * @param[in] sptr Client that initiated the deactivation.
569 * @param[in] gline G-line to deactivate.
570 * @param[in] lastmod New value for G-line last modification timestamp.
571 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
572 * @return Zero.
573 */
574 int
575 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
576 time_t lastmod, unsigned int flags)
577 {
578 unsigned int saveflags = 0;
579 char *msg;
580
581 assert(0 != gline);
582
583 saveflags = gline->gl_flags;
584
585 if (GlineIsLocal(gline))
586 msg = "removing local";
587 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
588 msg = "removing global";
589 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
590 } else {
591 msg = "deactivating global";
592
593 if (flags & GLINE_LOCAL)
594 gline->gl_flags |= GLINE_LDEACT;
595 else {
596 gline->gl_flags &= ~GLINE_ACTIVE;
597
598 if (gline->gl_lastmod) {
599 if (gline->gl_lastmod >= lastmod)
600 gline->gl_lastmod++;
601 else
602 gline->gl_lastmod = lastmod;
603 }
604 }
605
606 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
607 return 0; /* was inactive to begin with */
608 }
609
610 /* Inform ops and log it */
611 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
612 "%s",
613 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
614 cli_name(sptr) :
615 cli_name((cli_user(sptr))->server),
616 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
617 gline->gl_user, gline->gl_host ? "@" : "",
618 gline->gl_host ? gline->gl_host : "",
619 gline->gl_expire + TSoffset, gline->gl_reason);
620
621 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
622 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
623 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
624 gline->gl_host ? "@" : "",
625 gline->gl_host ? gline->gl_host : "",
626 gline->gl_expire + TSoffset, gline->gl_reason);
627
628 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
629 gline_propagate(cptr, sptr, gline);
630
631 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
632 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
633 gline_free(gline); /* get rid of it */
634
635 return 0;
636 }
637
638 /** Find a G-line for a particular mask, guided by certain flags.
639 * Certain bits in \a flags are interpreted specially:
640 * <dl>
641 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
642 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
643 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
644 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
645 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
646 * <dt>anything else</dt><dd>Search user G-lines.</dd>
647 * </dl>
648 * @param[in] userhost Mask to search for.
649 * @param[in] flags Bitwise combination of GLINE_* flags.
650 * @return First matching G-line, or NULL if none are found.
651 */
652 struct Gline *
653 gline_find(char *userhost, unsigned int flags)
654 {
655 struct Gline *gline;
656 struct Gline *sgline;
657 char *user, *host, *t_uh;
658
659 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
660 for (gline = BadChanGlineList; gline; gline = sgline) {
661 sgline = gline->gl_next;
662
663 if (gline->gl_expire <= CurrentTime)
664 gline_free(gline);
665 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
666 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
667 continue;
668 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
669 match(gline->gl_user, userhost)) == 0)
670 return gline;
671 }
672 }
673
674 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
675 *userhost == '#' || *userhost == '&')
676 return 0;
677
678 DupString(t_uh, userhost);
679 canon_userhost(t_uh, &user, &host, "*");
680
681 for (gline = GlobalGlineList; gline; gline = sgline) {
682 sgline = gline->gl_next;
683
684 if (gline->gl_expire <= CurrentTime)
685 gline_free(gline);
686 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
687 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
688 continue;
689 else if (flags & GLINE_EXACT) {
690 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
691 || (!gline->gl_host && !host)) &&
692 (ircd_strcmp(gline->gl_user, user) == 0))
693 break;
694 } else {
695 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
696 || (!gline->gl_host && !host)) &&
697 (match(gline->gl_user, user) == 0))
698 break;
699 }
700 }
701
702 MyFree(t_uh);
703
704 return gline;
705 }
706
707 /** Find a matching G-line for a user.
708 * @param[in] cptr Client to compare against.
709 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
710 * GLINE_LASTMOD to limit matches.
711 * @return Matching G-line, or NULL if none are found.
712 */
713 struct Gline *
714 gline_lookup(struct Client *cptr, unsigned int flags)
715 {
716 struct Gline *gline;
717 struct Gline *sgline;
718
719 for (gline = GlobalGlineList; gline; gline = sgline) {
720 sgline = gline->gl_next;
721
722 if (gline->gl_expire <= CurrentTime) {
723 gline_free(gline);
724 continue;
725 }
726
727 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
728 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
729 continue;
730
731 if (GlineIsRealName(gline)) {
732 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
733 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
734 continue;
735 }
736 else {
737 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
738 continue;
739
740 if (GlineIsIpMask(gline)) {
741 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
742 continue;
743 }
744 else {
745 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
746 continue;
747 }
748 }
749 if (GlineIsActive(gline))
750 return gline;
751 }
752 /*
753 * No Glines matched
754 */
755 return 0;
756 }
757
758 /** Delink and free a G-line.
759 * @param[in] gline G-line to free.
760 */
761 void
762 gline_free(struct Gline *gline)
763 {
764 assert(0 != gline);
765
766 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
767 if (gline->gl_next)
768 gline->gl_next->gl_prev_p = gline->gl_prev_p;
769
770 MyFree(gline->gl_user); /* free up the memory */
771 if (gline->gl_host)
772 MyFree(gline->gl_host);
773 MyFree(gline->gl_reason);
774 MyFree(gline);
775 }
776
777 /** Burst all known global G-lines to another server.
778 * @param[in] cptr Destination of burst.
779 */
780 void
781 gline_burst(struct Client *cptr)
782 {
783 struct Gline *gline;
784 struct Gline *sgline;
785
786 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
787 sgline = gline->gl_next;
788
789 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
790 gline_free(gline);
791 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
792 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
793 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
794 gline->gl_host ? "@" : "",
795 gline->gl_host ? gline->gl_host : "",
796 gline->gl_expire - CurrentTime, gline->gl_lastmod,
797 gline->gl_reason);
798 }
799
800 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
801 sgline = gline->gl_next;
802
803 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
804 gline_free(gline);
805 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
806 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
807 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
808 gline->gl_expire - CurrentTime, gline->gl_lastmod,
809 gline->gl_reason);
810 }
811 }
812
813 /** Send a G-line to another server.
814 * @param[in] cptr Who to inform of the G-line.
815 * @param[in] gline G-line to send.
816 * @return Zero.
817 */
818 int
819 gline_resend(struct Client *cptr, struct Gline *gline)
820 {
821 if (GlineIsLocal(gline) || !gline->gl_lastmod)
822 return 0;
823
824 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
825 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
826 gline->gl_host ? "@" : "",
827 gline->gl_host ? gline->gl_host : "",
828 gline->gl_expire - CurrentTime, gline->gl_lastmod,
829 gline->gl_reason);
830
831 return 0;
832 }
833
834 /** Display one or all G-lines to a user.
835 * If \a userhost is not NULL, only send the first matching G-line.
836 * Otherwise send the whole list.
837 * @param[in] sptr User asking for G-line list.
838 * @param[in] userhost G-line mask to search for (or NULL).
839 * @return Zero.
840 */
841 int
842 gline_list(struct Client *sptr, char *userhost)
843 {
844 struct Gline *gline;
845 struct Gline *sgline;
846
847 if (userhost) {
848 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
849 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
850
851 /* send gline information along */
852 send_reply(sptr, RPL_GLIST, gline->gl_user,
853 gline->gl_host ? "@" : "",
854 gline->gl_host ? gline->gl_host : "",
855 gline->gl_expire + TSoffset,
856 GlineIsLocal(gline) ? cli_name(&me) : "*",
857 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
858 } else {
859 for (gline = GlobalGlineList; gline; gline = sgline) {
860 sgline = gline->gl_next;
861
862 if (gline->gl_expire <= CurrentTime)
863 gline_free(gline);
864 else
865 send_reply(sptr, RPL_GLIST, gline->gl_user,
866 gline->gl_host ? "@" : "",
867 gline->gl_host ? gline->gl_host : "",
868 gline->gl_expire + TSoffset,
869 GlineIsLocal(gline) ? cli_name(&me) : "*",
870 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
871 }
872
873 for (gline = BadChanGlineList; gline; gline = sgline) {
874 sgline = gline->gl_next;
875
876 if (gline->gl_expire <= CurrentTime)
877 gline_free(gline);
878 else
879 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
880 gline->gl_expire + TSoffset,
881 GlineIsLocal(gline) ? cli_name(&me) : "*",
882 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
883 }
884 }
885
886 /* end of gline information */
887 return send_reply(sptr, RPL_ENDOFGLIST);
888 }
889
890 /** Statistics callback to list G-lines.
891 * @param[in] sptr Client requesting statistics.
892 * @param[in] sd Stats descriptor for request (ignored).
893 * @param[in] param Extra parameter from user (ignored).
894 */
895 void
896 gline_stats(struct Client *sptr, const struct StatDesc *sd,
897 char *param)
898 {
899 struct Gline *gline;
900 struct Gline *sgline;
901
902 for (gline = GlobalGlineList; gline; gline = sgline) {
903 sgline = gline->gl_next;
904
905 if (gline->gl_expire <= CurrentTime)
906 gline_free(gline);
907 else
908 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
909 gline->gl_host ? "@" : "",
910 gline->gl_host ? gline->gl_host : "",
911 gline->gl_expire + TSoffset, gline->gl_reason);
912 }
913 }
914
915 /** Calculate memory used by G-lines.
916 * @param[out] gl_size Number of bytes used by G-lines.
917 * @return Number of G-lines in use.
918 */
919 int
920 gline_memory_count(size_t *gl_size)
921 {
922 struct Gline *gline;
923 unsigned int gl = 0;
924
925 for (gline = GlobalGlineList; gline; gline = gline->gl_next)
926 {
927 gl++;
928 *gl_size += sizeof(struct Gline);
929 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
930 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
931 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
932 }
933 return gl;
934 }