2 * IRC - Internet Relay Chat, ircd/gline.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Finland
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)
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.
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.
21 * @brief Implementation of Gline manipulation functions.
29 #include "ircd_alloc.h"
30 #include "ircd_features.h"
32 #include "ircd_reply.h"
33 #include "ircd_snprintf.h"
34 #include "ircd_string.h"
49 /* #include <assert.h> -- Now using assert in ircd_log.h */
54 #define CHECK_APPROVED 0 /**< Mask is acceptable */
55 #define CHECK_OVERRIDABLE 1 /**< Mask is acceptable, but not by default */
56 #define CHECK_REJECTED 2 /**< Mask is totally unacceptable */
58 #define MASK_WILD_0 0x01 /**< Wildcards in the last position */
59 #define MASK_WILD_1 0x02 /**< Wildcards in the next-to-last position */
61 #define MASK_WILD_MASK 0x03 /**< Mask out the positional wildcards */
63 #define MASK_WILDS 0x10 /**< Mask contains wildcards */
64 #define MASK_IP 0x20 /**< Mask is an IP address */
65 #define MASK_HALT 0x40 /**< Finished processing mask */
67 /** List of user G-lines. */
68 struct Gline* GlobalGlineList = 0;
69 /** List of BadChan G-lines. */
70 struct Gline* BadChanGlineList = 0;
72 /** Iterate through \a list of G-lines. Use this like a for loop,
73 * i.e., follow it with braces and use whatever you passed as \a gl
74 * as a single G-line to be acted upon.
76 * @param[in] list List of G-lines to iterate over.
77 * @param[in] gl Name of a struct Gline pointer variable that will be made to point to the G-lines in sequence.
78 * @param[in] next Name of a scratch struct Gline pointer variable.
80 #define gliter(list, gl, next) \
81 /* Iterate through the G-lines in the list */ \
82 for ((gl) = (list); (gl); (gl) = (next)) \
83 /* Figure out the next pointer in list... */ \
84 if ((((next) = (gl)->gl_next) || 1) && \
85 /* Then see if it's expired */ \
86 (gl)->gl_lifetime <= CurrentTime) \
87 /* Record has expired, so free the G-line */ \
89 /* See if we need to expire the G-line */ \
90 else if (((gl)->gl_expire > CurrentTime || \
91 ((gl)->gl_flags &= ~GLINE_ACTIVE) || \
92 ((gl)->gl_state = GLOCAL_GLOBAL)) && 0) \
93 ; /* empty statement */ \
96 /** Find canonical user and host for a string.
97 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
98 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
99 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
101 * @param[in] userhost Input string from user.
102 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
103 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
104 * @param[in] def_user Default value for user part.
107 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
111 if (*userhost == '$') {
117 if (!(tmp = strchr(userhost, '@'))) {
127 /** Create a Gline structure.
128 * @param[in] user User part of mask.
129 * @param[in] host Host part of mask (NULL if not applicable).
130 * @param[in] reason Reason for G-line.
131 * @param[in] expire Expiration timestamp.
132 * @param[in] lastmod Last modification timestamp.
133 * @param[in] flags Bitwise combination of GLINE_* bits.
134 * @return Newly allocated G-line.
136 static struct Gline *
137 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
138 time_t lifetime, unsigned int flags)
140 struct Gline *gline, *after = 0;
142 /* Disable checking for overlapping G-lines. The problem is that we
143 * may have a wide-mask, long lifetime G-line that we've
144 * deactivated--maybe it was a mistake?--and someone comes along and
145 * wants to set a narrower overlapping G-line with a shorter
146 * lifetime. If we were to leave this logic enabled, there would be
147 * no way to set that narrower G-line.
149 /* if (!(flags & GLINE_BADCHAN)) { /\* search for overlapping glines first *\/ */
151 /* for (gline = GlobalGlineList; gline; gline = sgline) { */
152 /* sgline = gline->gl_next; */
154 /* if (gline->gl_expire <= CurrentTime) */
155 /* gline_free(gline); */
156 /* else if (((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL)) || */
157 /* (gline->gl_host && !host) || (!gline->gl_host && host)) */
159 /* else if (!mmatch(gline->gl_user, user) /\* gline contains new mask *\/ */
160 /* && (gline->gl_host == NULL || !mmatch(gline->gl_host, host))) { */
161 /* if (expire <= gline->gl_expire) /\* will expire before wider gline *\/ */
164 /* after = gline; /\* stick new gline after this one *\/ */
165 /* } else if (!mmatch(user, gline->gl_user) /\* new mask contains gline *\/ */
166 /* && (gline->gl_host==NULL || !mmatch(host, gline->gl_host)) */
167 /* && gline->gl_expire <= expire) /\* old expires before new *\/ */
168 /* gline_free(gline); /\* save some memory *\/ */
172 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
175 DupString(gline->gl_reason, reason); /* initialize gline... */
176 gline->gl_expire = expire;
177 gline->gl_lifetime = lifetime;
178 gline->gl_lastmod = lastmod;
179 gline->gl_flags = flags & GLINE_MASK;
180 gline->gl_state = GLOCAL_GLOBAL; /* not locally modified */
182 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
183 DupString(gline->gl_user, user); /* first, remember channel */
186 gline->gl_next = BadChanGlineList; /* then link it into list */
187 gline->gl_prev_p = &BadChanGlineList;
188 if (BadChanGlineList)
189 BadChanGlineList->gl_prev_p = &gline->gl_next;
190 BadChanGlineList = gline;
192 DupString(gline->gl_user, user); /* remember them... */
194 DupString(gline->gl_host, host);
196 gline->gl_host = NULL;
198 if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
199 gline->gl_flags |= GLINE_IPMASK;
202 gline->gl_next = after->gl_next;
203 gline->gl_prev_p = &after->gl_next;
205 after->gl_next->gl_prev_p = &gline->gl_next;
206 after->gl_next = gline;
208 gline->gl_next = GlobalGlineList; /* then link it into list */
209 gline->gl_prev_p = &GlobalGlineList;
211 GlobalGlineList->gl_prev_p = &gline->gl_next;
212 GlobalGlineList = gline;
219 /** Check local clients against a new G-line.
220 * If the G-line is inactive or a badchan, return immediately.
221 * Otherwise, if any users match it, disconnect them.
222 * @param[in] cptr Peer connect that sent the G-line.
223 * @param[in] sptr Client that originated the G-line.
224 * @param[in] gline New G-line to check.
225 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
228 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
230 struct Client *acptr;
231 int fd, retval = 0, tval;
233 if (feature_bool(FEAT_DISABLE_GLINES))
234 return 0; /* G-lines are disabled */
236 if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
238 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
241 for (fd = HighestFd; fd >= 0; --fd) {
245 if ((acptr = LocalClientArray[fd])) {
246 if (!cli_user(acptr))
249 if (GlineIsRealName(gline)) { /* Realname Gline */
250 Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
252 if (match(gline->gl_user+2, cli_info(acptr)) != 0)
254 Debug((DEBUG_DEBUG,"Matched!"));
255 } else { /* Host/IP gline */
256 if (cli_user(acptr)->username &&
257 match(gline->gl_user, (cli_user(acptr))->username) != 0)
260 if (GlineIsIpMask(gline)) {
261 if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
265 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
270 /* ok, here's one that got G-lined */
271 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
274 /* let the ops know about it */
275 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
276 get_client_name(acptr, SHOW_IP));
278 /* and get rid of him */
279 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
281 retval = tval; /* retain killed status */
288 * Implements the mask checking applied to local G-lines.
289 * Basically, host masks must have a minimum of two non-wild domain
290 * fields, and IP masks must have a minimum of 16 bits. If the mask
291 * has even one wild-card, OVERRIDABLE is returned, assuming the other
292 * check doesn't fail.
293 * @param[in] mask G-line mask to check.
294 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
297 gline_checkmask(char *mask)
299 unsigned int flags = MASK_IP;
300 unsigned int dots = 0;
301 unsigned int ipmask = 0;
303 for (; *mask; mask++) { /* go through given mask */
304 if (*mask == '.') { /* it's a separator; advance positional wilds */
305 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
308 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
309 ipmask += 8; /* It's an IP with no wilds, count bits */
310 } else if (*mask == '*' || *mask == '?')
311 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
312 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
314 ipmask = strtoul(mask, &mask, 10);
316 /* sanity-check to date */
317 if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
318 return CHECK_REJECTED;
321 return CHECK_REJECTED;
325 if (dots != 3 || ipmask > 32)
326 return CHECK_REJECTED;
331 flags |= MASK_HALT; /* Halt the ipmask calculation */
332 break; /* get out of the loop */
333 } else if (!IsIP6Char(*mask)) {
334 flags &= ~MASK_IP; /* not an IP anymore! */
339 /* Sanity-check quads */
340 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
345 /* update bit count if necessary */
346 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
349 /* Check to see that it's not too wide of a mask */
350 if (flags & MASK_WILDS &&
351 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
352 (flags & MASK_IP && ipmask < 16)))
353 return CHECK_REJECTED; /* to wide, reject */
355 /* Ok, it's approved; require override if it has wildcards, though */
356 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
359 /** Forward a G-line to other servers.
360 * @param[in] cptr Client that sent us the G-line.
361 * @param[in] sptr Client that originated the G-line.
362 * @param[in] gline G-line to forward.
366 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
368 if (GlineIsLocal(gline))
371 assert(gline->gl_lastmod);
373 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
374 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
375 gline->gl_host ? "@" : "",
376 gline->gl_host ? gline->gl_host : "",
377 gline->gl_expire - CurrentTime, gline->gl_lastmod,
378 gline->gl_lifetime, gline->gl_reason);
383 /** Create a new G-line and add it to global lists.
384 * \a userhost may be in one of four forms:
385 * \li A channel name, to add a BadChan.
386 * \li A string starting with $R and followed by a mask to match against their realname.
387 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
388 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
390 * @param[in] cptr Client that sent us the G-line.
391 * @param[in] sptr Client that originated the G-line.
392 * @param[in] userhost Text mask for the G-line.
393 * @param[in] reason Reason for G-line.
394 * @param[in] expire Expiration time of G-line.
395 * @param[in] lastmod Last modification time of G-line.
396 * @param[in] lifetime Lifetime of G-line.
397 * @param[in] flags Bitwise combination of GLINE_* flags.
398 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
401 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
402 char *reason, time_t expire, time_t lastmod, time_t lifetime,
405 struct Gline *agline;
406 char uhmask[USERLEN + HOSTLEN + 2];
410 assert(0 != userhost);
413 Debug((DEBUG_DEBUG, "gline_add(\"%s\", \"%s\", \"%s\", \"%s\", %Tu, %Tu "
414 "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), userhost, reason,
415 expire, lastmod, lifetime, flags));
417 if (*userhost == '#' || *userhost == '&') {
418 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
419 return send_reply(sptr, ERR_NOPRIVILEGES);
421 flags |= GLINE_BADCHAN;
424 } else if (*userhost == '$') {
425 switch (userhost[1]) {
426 case 'R': flags |= GLINE_REALNAME; break;
428 /* uh, what to do here? */
429 /* The answer, my dear Watson, is we throw a protocol_violation()
432 return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
434 sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
442 canon_userhost(userhost, &user, &host, "*");
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)
453 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
457 if ((tmp = count_users(uhmask)) >=
458 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
459 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
464 * You cannot set a negative (or zero) expire time, nor can you set an
465 * expiration time for greater than GLINE_MAX_EXPIRE.
467 if (!(flags & GLINE_FORCE) &&
468 (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
469 if (!IsServer(sptr) && MyConnect(sptr))
470 send_reply(sptr, ERR_BADEXPIRE, expire);
474 if (!lifetime) /* no lifetime set, use expiration time */
477 /* lifetime is already an absolute timestamp */
480 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
481 SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
483 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
485 cli_name((cli_user(sptr))->server),
486 (flags & GLINE_LOCAL) ? "local" : "global",
487 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
488 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
489 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
490 expire + TSoffset, reason);
493 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
494 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
495 flags & GLINE_LOCAL ? "local" : "global",
496 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
497 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
498 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
499 expire + TSoffset, reason);
502 agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
504 /* since we've disabled overlapped G-line checking, agline should
508 /* if (!agline) /\* if it overlapped, silently return *\/ */
511 gline_propagate(cptr, sptr, agline);
513 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
516 /** Activate a currently inactive G-line.
517 * @param[in] cptr Peer that told us to activate the G-line.
518 * @param[in] sptr Client that originally thought it was a good idea.
519 * @param[in] gline G-line to activate.
520 * @param[in] lastmod New value for last modification timestamp.
521 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
522 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
525 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
526 time_t lastmod, unsigned int flags)
528 unsigned int saveflags = 0;
532 saveflags = gline->gl_flags;
534 if (flags & GLINE_LOCAL)
535 gline->gl_flags &= ~GLINE_LDEACT;
537 gline->gl_flags |= GLINE_ACTIVE;
539 if (gline->gl_lastmod) {
540 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
543 gline->gl_lastmod = lastmod;
547 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
548 return 0; /* was active to begin with */
550 /* Inform ops and log it */
551 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
552 "expiring at %Tu: %s",
553 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
555 cli_name((cli_user(sptr))->server),
556 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
557 gline->gl_user, gline->gl_host ? "@" : "",
558 gline->gl_host ? gline->gl_host : "",
559 gline->gl_expire + TSoffset, gline->gl_reason);
561 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
562 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
563 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
564 gline->gl_host ? "@" : "",
565 gline->gl_host ? gline->gl_host : "",
566 gline->gl_expire + TSoffset, gline->gl_reason);
568 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
569 gline_propagate(cptr, sptr, gline);
571 return do_gline(cptr, sptr, gline);
574 /** Deactivate a G-line.
575 * @param[in] cptr Peer that gave us the message.
576 * @param[in] sptr Client that initiated the deactivation.
577 * @param[in] gline G-line to deactivate.
578 * @param[in] lastmod New value for G-line last modification timestamp.
579 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
583 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
584 time_t lastmod, unsigned int flags)
586 unsigned int saveflags = 0;
591 saveflags = gline->gl_flags;
593 if (GlineIsLocal(gline))
594 msg = "removing local";
595 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
596 msg = "removing global";
597 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
599 msg = "deactivating global";
601 if (flags & GLINE_LOCAL)
602 gline->gl_flags |= GLINE_LDEACT;
604 gline->gl_flags &= ~GLINE_ACTIVE;
606 if (gline->gl_lastmod) {
607 if (gline->gl_lastmod >= lastmod)
610 gline->gl_lastmod = lastmod;
614 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
615 return 0; /* was inactive to begin with */
618 /* Inform ops and log it */
619 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
621 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
623 cli_name((cli_user(sptr))->server),
624 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
625 gline->gl_user, gline->gl_host ? "@" : "",
626 gline->gl_host ? gline->gl_host : "",
627 gline->gl_expire + TSoffset, gline->gl_reason);
629 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
630 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
631 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
632 gline->gl_host ? "@" : "",
633 gline->gl_host ? gline->gl_host : "",
634 gline->gl_expire + TSoffset, gline->gl_reason);
636 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
637 gline_propagate(cptr, sptr, gline);
639 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
640 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
641 gline_free(gline); /* get rid of it */
646 /** Modify a global G-line.
647 * @param[in] cptr Client that sent us the G-line modification.
648 * @param[in] sptr Client that originated the G-line modification.
649 * @param[in] gline G-line being modified.
650 * @param[in] action Resultant status of the G-line.
651 * @param[in] reason Reason for G-line.
652 * @param[in] expire Expiration time of G-line.
653 * @param[in] lastmod Last modification time of G-line.
654 * @param[in] lifetime Lifetime of G-line.
655 * @param[in] flags Bitwise combination of GLINE_* flags.
656 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
659 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
660 enum GlineAction action, char *reason, time_t expire,
661 time_t lastmod, time_t lifetime, unsigned int flags)
663 char buf[BUFSIZE], *op = "";
667 assert(!GlineIsLocal(gline));
669 Debug((DEBUG_DEBUG, "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
670 "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
671 gline->gl_user, gline->gl_host ? "@" : "",
672 gline->gl_host ? gline->gl_host : "",
673 action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
674 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
675 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
676 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
677 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
678 reason, expire, lastmod, lifetime, flags));
680 /* First, let's check lastmod... */
681 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
682 if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
683 if (IsBurstOrBurstAck(cptr))
684 return 0; /* middle of a burst, it'll resync on its own */
685 return gline_resend(cptr, gline); /* resync the server */
686 } else if (GlineLastMod(gline) == lastmod)
687 return 0; /* we have that version of the G-line... */
690 /* All right, we know that there's a change of some sort. What is it? */
691 /* first, check out the expiration time... */
692 if ((flags & GLINE_EXPIRE) && expire) {
693 if (!(flags & GLINE_FORCE) &&
694 (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
695 if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
696 send_reply(sptr, ERR_BADEXPIRE, expire);
700 flags &= ~GLINE_EXPIRE;
702 /* Now check to see if there's any change... */
703 if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
704 flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
708 /* Next, check out lifetime--this one's a bit trickier... */
709 if (!(flags & GLINE_LIFETIME) || !lifetime)
710 lifetime = gline->gl_lifetime; /* use G-line lifetime */
712 lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
714 /* OK, let's see which is greater... */
715 if (lifetime > gline->gl_lifetime)
716 flags |= GLINE_LIFETIME; /* have to update lifetime */
718 flags &= ~GLINE_LIFETIME; /* no change to lifetime */
722 /* Finally, let's see if the reason needs to be updated */
723 if ((flags & GLINE_REASON) && reason &&
724 !ircd_strcmp(gline->gl_reason, reason))
725 flags &= ~GLINE_REASON; /* no changes to the reason */
727 /* OK, now let's take a look at the action... */
728 if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
729 (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
730 (action == GLINE_LOCAL_ACTIVATE &&
731 (gline->gl_state == GLOCAL_ACTIVATED)) ||
732 (action == GLINE_LOCAL_DEACTIVATE &&
733 (gline->gl_state == GLOCAL_DEACTIVATED)) ||
734 /* can't activate an expired G-line */
735 IRCD_MAX(gline->gl_expire, expire) <= CurrentTime)
736 action = GLINE_MODIFY; /* no activity state modifications */
738 Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
739 flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
740 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
741 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
742 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
743 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
745 /* If there are no changes to perform, do no changes */
746 if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
749 /* Now we know what needs to be changed, so let's process the changes... */
751 /* Start by updating lastmod, if indicated... */
752 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
753 gline->gl_lastmod = lastmod;
755 /* Then move on to activity status changes... */
757 case GLINE_ACTIVATE: /* Globally activating G-line */
758 gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
759 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
760 pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
761 op = "+"; /* operation for G-line propagation */
764 case GLINE_DEACTIVATE: /* Globally deactivating G-line */
765 gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
766 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
767 pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
768 op = "-"; /* operation for G-line propagation */
771 case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
772 gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
773 pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
776 case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
777 gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
778 pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
781 case GLINE_MODIFY: /* no change to activity status */
785 /* Handle expiration changes... */
786 if (flags & GLINE_EXPIRE) {
787 gline->gl_expire = expire; /* save new expiration time */
789 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
790 "%s%s changing expiration time to %Tu",
792 pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
793 " and" : "", expire);
796 /* Next, handle lifetime changes... */
797 if (flags & GLINE_LIFETIME) {
798 gline->gl_lifetime = lifetime; /* save new lifetime */
800 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
801 "%s%s extending record lifetime to %Tu",
802 pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
803 " and" : "", lifetime);
806 /* Now, handle reason changes... */
807 if (flags & GLINE_REASON) {
808 MyFree(gline->gl_reason); /* release old reason */
809 DupString(gline->gl_reason, reason); /* store new reason */
811 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
812 "%s%s changing reason to \"%s\"",
813 pos ? ";" : "", pos ? " and" : "", reason);
816 /* All right, inform ops... */
817 sendto_opmask_butone(0, SNO_GLINE, "%s modifying global %s for %s%s%s:%s",
818 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
819 cli_name(sptr) : cli_name((cli_user(sptr))->server),
820 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
821 gline->gl_user, gline->gl_host ? "@" : "",
822 gline->gl_host ? gline->gl_host : "", buf);
824 /* and log the change */
825 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
826 "%#C modifying global %s for %s%s%s:%s", sptr,
827 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
828 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
831 /* We'll be simple for this release, but we can update this to change
832 * the propagation syntax on future updates
834 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
835 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
836 "* %s%s%s%s%s %Tu %Tu %Tu :%s",
837 flags & GLINE_OPERFORCE ? "!" : "", op,
838 gline->gl_user, gline->gl_host ? "@" : "",
839 gline->gl_host ? gline->gl_host : "",
840 gline->gl_expire - CurrentTime, gline->gl_lastmod,
841 gline->gl_lifetime, gline->gl_reason);
843 /* OK, let's do the G-line... */
844 return do_gline(cptr, sptr, gline);
847 /** Destroy a local G-line.
848 * @param[in] cptr Peer that gave us the message.
849 * @param[in] sptr Client that initiated the destruction.
850 * @param[in] gline G-line to destroy.
854 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
857 assert(GlineIsLocal(gline));
859 /* Inform ops and log it */
860 sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
861 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
862 cli_name(sptr) : cli_name((cli_user(sptr))->server),
863 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
864 gline->gl_user, gline->gl_host ? "@" : "",
865 gline->gl_host ? gline->gl_host : "");
866 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
867 "%#C removing local %s for %s%s%s", sptr,
868 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
869 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
871 gline_free(gline); /* get rid of the G-line */
873 return 0; /* convenience return */
876 /** Find a G-line for a particular mask, guided by certain flags.
877 * Certain bits in \a flags are interpreted specially:
879 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
880 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
881 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
882 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
883 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
884 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
885 * <dt>anything else</dt><dd>Search user G-lines.</dd>
887 * @param[in] userhost Mask to search for.
888 * @param[in] flags Bitwise combination of GLINE_* flags.
889 * @return First matching G-line, or NULL if none are found.
892 gline_find(char *userhost, unsigned int flags)
894 struct Gline *gline = 0;
895 struct Gline *sgline;
896 char *user, *host, *t_uh;
898 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
899 gliter(BadChanGlineList, gline, sgline) {
900 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
901 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
902 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
904 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
905 match(gline->gl_user, userhost)) == 0)
910 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
911 *userhost == '#' || *userhost == '&')
914 DupString(t_uh, userhost);
915 canon_userhost(t_uh, &user, &host, "*");
917 gliter(GlobalGlineList, gline, sgline) {
918 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
919 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
920 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
922 else if (flags & GLINE_EXACT) {
923 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
924 || (!gline->gl_host && !host)) &&
925 (ircd_strcmp(gline->gl_user, user) == 0))
928 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
929 || (!gline->gl_host && !host)) &&
930 (match(gline->gl_user, user) == 0))
940 /** Find a matching G-line for a user.
941 * @param[in] cptr Client to compare against.
942 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
943 * GLINE_LASTMOD to limit matches.
944 * @return Matching G-line, or NULL if none are found.
947 gline_lookup(struct Client *cptr, unsigned int flags)
950 struct Gline *sgline;
952 gliter(GlobalGlineList, gline, sgline) {
953 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
954 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
957 if (GlineIsRealName(gline)) {
958 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
959 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
963 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
966 if (GlineIsIpMask(gline)) {
967 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
971 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
975 if (GlineIsActive(gline))
984 /** Delink and free a G-line.
985 * @param[in] gline G-line to free.
988 gline_free(struct Gline *gline)
992 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
994 gline->gl_next->gl_prev_p = gline->gl_prev_p;
996 MyFree(gline->gl_user); /* free up the memory */
998 MyFree(gline->gl_host);
999 MyFree(gline->gl_reason);
1003 /** Burst all known global G-lines to another server.
1004 * @param[in] cptr Destination of burst.
1007 gline_burst(struct Client *cptr)
1009 struct Gline *gline;
1010 struct Gline *sgline;
1012 gliter(GlobalGlineList, gline, sgline) {
1013 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1014 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1015 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1016 gline->gl_host ? "@" : "",
1017 gline->gl_host ? gline->gl_host : "",
1018 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1019 gline->gl_lifetime, gline->gl_reason);
1022 gliter(BadChanGlineList, gline, sgline) {
1023 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1024 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1025 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1026 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1027 gline->gl_lifetime, gline->gl_reason);
1031 /** Send a G-line to another server.
1032 * @param[in] cptr Who to inform of the G-line.
1033 * @param[in] gline G-line to send.
1037 gline_resend(struct Client *cptr, struct Gline *gline)
1039 if (GlineIsLocal(gline) || !gline->gl_lastmod)
1042 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1043 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1044 gline->gl_host ? "@" : "",
1045 gline->gl_host ? gline->gl_host : "",
1046 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1047 gline->gl_lifetime, gline->gl_reason);
1052 /** Display one or all G-lines to a user.
1053 * If \a userhost is not NULL, only send the first matching G-line.
1054 * Otherwise send the whole list.
1055 * @param[in] sptr User asking for G-line list.
1056 * @param[in] userhost G-line mask to search for (or NULL).
1060 gline_list(struct Client *sptr, char *userhost)
1062 struct Gline *gline;
1063 struct Gline *sgline;
1066 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1067 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1069 /* send gline information along */
1070 send_reply(sptr, RPL_GLIST, gline->gl_user,
1071 gline->gl_host ? "@" : "",
1072 gline->gl_host ? gline->gl_host : "",
1073 gline->gl_expire + TSoffset, gline->gl_lastmod,
1074 gline->gl_lifetime + TSoffset,
1075 GlineIsLocal(gline) ? cli_name(&me) : "*",
1076 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1077 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1078 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1080 gliter(GlobalGlineList, gline, sgline) {
1081 send_reply(sptr, RPL_GLIST, gline->gl_user,
1082 gline->gl_host ? "@" : "",
1083 gline->gl_host ? gline->gl_host : "",
1084 gline->gl_expire + TSoffset, gline->gl_lastmod,
1085 gline->gl_lifetime + TSoffset,
1086 GlineIsLocal(gline) ? cli_name(&me) : "*",
1087 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1088 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1089 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1092 gliter(BadChanGlineList, gline, sgline) {
1093 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1094 gline->gl_expire + TSoffset, gline->gl_lastmod,
1095 gline->gl_lifetime + TSoffset,
1096 GlineIsLocal(gline) ? cli_name(&me) : "*",
1097 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1098 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1099 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1103 /* end of gline information */
1104 return send_reply(sptr, RPL_ENDOFGLIST);
1107 /** Statistics callback to list G-lines.
1108 * @param[in] sptr Client requesting statistics.
1109 * @param[in] sd Stats descriptor for request (ignored).
1110 * @param[in] param Extra parameter from user (ignored).
1113 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1116 struct Gline *gline;
1117 struct Gline *sgline;
1119 gliter(GlobalGlineList, gline, sgline) {
1120 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1121 gline->gl_host ? "@" : "",
1122 gline->gl_host ? gline->gl_host : "",
1123 gline->gl_expire + TSoffset, gline->gl_lastmod,
1124 gline->gl_lifetime + TSoffset,
1125 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1126 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1127 GlineIsRemActive(gline) ? '+' : '-',
1132 /** Calculate memory used by G-lines.
1133 * @param[out] gl_size Number of bytes used by G-lines.
1134 * @return Number of G-lines in use.
1137 gline_memory_count(size_t *gl_size)
1139 struct Gline *gline;
1140 unsigned int gl = 0;
1142 for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1144 *gl_size += sizeof(struct Gline);
1145 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1146 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1147 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1150 for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1152 *gl_size += sizeof(struct Gline);
1153 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1154 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1155 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;