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 - CurrentTime, 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 Duration of G-line in seconds.
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) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
468 if (!IsServer(sptr) && MyConnect(sptr))
469 send_reply(sptr, ERR_BADEXPIRE, expire);
473 expire += CurrentTime; /* convert from lifetime to timestamp */
475 if (!lifetime) /* no lifetime set, use expiration time */
477 else /* convert lifetime into timestamp */
478 lifetime += CurrentTime;
481 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
482 SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
484 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
486 cli_name((cli_user(sptr))->server),
487 (flags & GLINE_LOCAL) ? "local" : "global",
488 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
489 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
490 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
491 expire + TSoffset, reason);
494 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
495 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
496 flags & GLINE_LOCAL ? "local" : "global",
497 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
498 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
499 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
500 expire + TSoffset, reason);
503 agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
505 /* since we've disabled overlapped G-line checking, agline should
509 /* if (!agline) /\* if it overlapped, silently return *\/ */
512 gline_propagate(cptr, sptr, agline);
514 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
517 /** Activate a currently inactive G-line.
518 * @param[in] cptr Peer that told us to activate the G-line.
519 * @param[in] sptr Client that originally thought it was a good idea.
520 * @param[in] gline G-line to activate.
521 * @param[in] lastmod New value for last modification timestamp.
522 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
523 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
526 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
527 time_t lastmod, unsigned int flags)
529 unsigned int saveflags = 0;
533 saveflags = gline->gl_flags;
535 if (flags & GLINE_LOCAL)
536 gline->gl_flags &= ~GLINE_LDEACT;
538 gline->gl_flags |= GLINE_ACTIVE;
540 if (gline->gl_lastmod) {
541 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
544 gline->gl_lastmod = lastmod;
548 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
549 return 0; /* was active to begin with */
551 /* Inform ops and log it */
552 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
553 "expiring at %Tu: %s",
554 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
556 cli_name((cli_user(sptr))->server),
557 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
558 gline->gl_user, gline->gl_host ? "@" : "",
559 gline->gl_host ? gline->gl_host : "",
560 gline->gl_expire + TSoffset, gline->gl_reason);
562 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
563 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
564 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
565 gline->gl_host ? "@" : "",
566 gline->gl_host ? gline->gl_host : "",
567 gline->gl_expire + TSoffset, gline->gl_reason);
569 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
570 gline_propagate(cptr, sptr, gline);
572 return do_gline(cptr, sptr, gline);
575 /** Deactivate a G-line.
576 * @param[in] cptr Peer that gave us the message.
577 * @param[in] sptr Client that initiated the deactivation.
578 * @param[in] gline G-line to deactivate.
579 * @param[in] lastmod New value for G-line last modification timestamp.
580 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
584 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
585 time_t lastmod, unsigned int flags)
587 unsigned int saveflags = 0;
592 saveflags = gline->gl_flags;
594 if (GlineIsLocal(gline))
595 msg = "removing local";
596 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
597 msg = "removing global";
598 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
600 msg = "deactivating global";
602 if (flags & GLINE_LOCAL)
603 gline->gl_flags |= GLINE_LDEACT;
605 gline->gl_flags &= ~GLINE_ACTIVE;
607 if (gline->gl_lastmod) {
608 if (gline->gl_lastmod >= lastmod)
611 gline->gl_lastmod = lastmod;
615 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
616 return 0; /* was inactive to begin with */
619 /* Inform ops and log it */
620 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
622 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
624 cli_name((cli_user(sptr))->server),
625 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
626 gline->gl_user, gline->gl_host ? "@" : "",
627 gline->gl_host ? gline->gl_host : "",
628 gline->gl_expire + TSoffset, gline->gl_reason);
630 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
631 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
632 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
633 gline->gl_host ? "@" : "",
634 gline->gl_host ? gline->gl_host : "",
635 gline->gl_expire + TSoffset, gline->gl_reason);
637 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
638 gline_propagate(cptr, sptr, gline);
640 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
641 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
642 gline_free(gline); /* get rid of it */
647 /** Modify a global G-line.
648 * @param[in] cptr Client that sent us the G-line modification.
649 * @param[in] sptr Client that originated the G-line modification.
650 * @param[in] gline G-line being modified.
651 * @param[in] action Resultant status of the G-line.
652 * @param[in] reason Reason for G-line.
653 * @param[in] expire Duration of G-line in seconds.
654 * @param[in] lastmod Last modification time of G-line.
655 * @param[in] lifetime Lifetime of G-line.
656 * @param[in] flags Bitwise combination of GLINE_* flags.
657 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
660 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
661 enum GlineAction action, char *reason, time_t expire,
662 time_t lastmod, time_t lifetime, unsigned int flags)
664 char buf[BUFSIZE], *op = "";
668 assert(!GlineIsLocal(gline));
670 Debug((DEBUG_DEBUG, "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
671 "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
672 gline->gl_user, gline->gl_host ? "@" : "",
673 gline->gl_host ? gline->gl_host : "",
674 action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
675 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
676 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
677 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
678 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
679 reason, expire, lastmod, lifetime, flags));
681 /* First, let's check lastmod... */
682 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
683 if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
684 if (IsBurstOrBurstAck(cptr))
685 return 0; /* middle of a burst, it'll resync on its own */
686 return gline_resend(cptr, gline); /* resync the server */
687 } else if (GlineLastMod(gline) == lastmod)
688 return 0; /* we have that version of the G-line... */
691 /* All right, we know that there's a change of some sort. What is it? */
692 /* first, check out the expiration time... */
693 if ((flags & GLINE_EXPIRE) && expire) {
694 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
695 if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
696 send_reply(sptr, ERR_BADEXPIRE, expire);
700 /* convert to a timestamp... */
701 expire += CurrentTime;
703 flags &= ~GLINE_EXPIRE;
705 /* Now check to see if there's any change... */
706 if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
707 flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
711 /* Next, check out lifetime--this one's a bit trickier... */
712 if ((flags & GLINE_LIFETIME) && lifetime)
713 lifetime += CurrentTime; /* convert to a timestamp */
715 lifetime = gline->gl_lifetime; /* use G-line lifetime */
717 lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
719 /* OK, let's see which is greater... */
720 if (lifetime > gline->gl_lifetime)
721 flags |= GLINE_LIFETIME; /* have to update lifetime */
723 flags &= ~GLINE_LIFETIME; /* no change to lifetime */
727 /* Finally, let's see if the reason needs to be updated */
728 if ((flags & GLINE_REASON) && reason &&
729 !ircd_strcmp(gline->gl_reason, reason))
730 flags &= ~GLINE_REASON; /* no changes to the reason */
732 /* OK, now let's take a look at the action... */
733 if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
734 (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
735 (action == GLINE_LOCAL_ACTIVATE &&
736 (gline->gl_state == GLOCAL_ACTIVATED)) ||
737 (action == GLINE_LOCAL_DEACTIVATE &&
738 (gline->gl_state == GLOCAL_DEACTIVATED)) ||
739 /* can't activate an expired G-line */
740 IRCD_MAX(gline->gl_expire, expire) <= CurrentTime)
741 action = GLINE_MODIFY; /* no activity state modifications */
743 Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
744 flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
745 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
746 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
747 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
748 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
750 /* If there are no changes to perform, do no changes */
751 if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
754 /* Now we know what needs to be changed, so let's process the changes... */
756 /* Start by updating lastmod, if indicated... */
757 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
758 gline->gl_lastmod = lastmod;
760 /* Then move on to activity status changes... */
762 case GLINE_ACTIVATE: /* Globally activating G-line */
763 gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
764 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
765 pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
766 op = "+"; /* operation for G-line propagation */
769 case GLINE_DEACTIVATE: /* Globally deactivating G-line */
770 gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
771 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
772 pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
773 op = "-"; /* operation for G-line propagation */
776 case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
777 gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
778 pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
781 case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
782 gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
783 pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
786 case GLINE_MODIFY: /* no change to activity status */
790 /* Handle expiration changes... */
791 if (flags & GLINE_EXPIRE) {
792 gline->gl_expire = expire; /* save new expiration time */
794 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
795 "%s%s changing expiration time to %Tu",
797 pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
798 " and" : "", expire);
801 /* Next, handle lifetime changes... */
802 if (flags & GLINE_LIFETIME) {
803 gline->gl_lifetime = lifetime; /* save new lifetime */
805 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
806 "%s%s extending record lifetime to %Tu",
807 pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
808 " and" : "", lifetime);
811 /* Now, handle reason changes... */
812 if (flags & GLINE_REASON) {
813 MyFree(gline->gl_reason); /* release old reason */
814 DupString(gline->gl_reason, reason); /* store new reason */
816 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
817 "%s%s changing reason to \"%s\"",
818 pos ? ";" : "", pos ? " and" : "", reason);
821 /* All right, inform ops... */
822 sendto_opmask_butone(0, SNO_GLINE, "%s modifying global %s for %s%s%s:%s",
823 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
824 cli_name(sptr) : cli_name((cli_user(sptr))->server),
825 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
826 gline->gl_user, gline->gl_host ? "@" : "",
827 gline->gl_host ? gline->gl_host : "", buf);
829 /* and log the change */
830 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
831 "%#C modifying global %s for %s%s%s:%s", sptr,
832 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
833 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
836 /* We'll be simple for this release, but we can update this to change
837 * the propagation syntax on future updates
839 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
840 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
841 "* %s%s%s%s%s %Tu %Tu %Tu :%s",
842 flags & GLINE_OPERFORCE ? "!" : "", op,
843 gline->gl_user, gline->gl_host ? "@" : "",
844 gline->gl_host ? gline->gl_host : "",
845 gline->gl_expire - CurrentTime, gline->gl_lastmod,
846 gline->gl_lifetime - CurrentTime, gline->gl_reason);
848 /* OK, let's do the G-line... */
849 return do_gline(cptr, sptr, gline);
852 /** Destroy a local G-line.
853 * @param[in] cptr Peer that gave us the message.
854 * @param[in] sptr Client that initiated the destruction.
855 * @param[in] gline G-line to destroy.
859 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
862 assert(GlineIsLocal(gline));
864 /* Inform ops and log it */
865 sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
866 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
867 cli_name(sptr) : cli_name((cli_user(sptr))->server),
868 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
869 gline->gl_user, gline->gl_host ? "@" : "",
870 gline->gl_host ? gline->gl_host : "");
871 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
872 "%#C removing local %s for %s%s%s", sptr,
873 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
874 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
876 gline_free(gline); /* get rid of the G-line */
878 return 0; /* convenience return */
881 /** Find a G-line for a particular mask, guided by certain flags.
882 * Certain bits in \a flags are interpreted specially:
884 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
885 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
886 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
887 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
888 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
889 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
890 * <dt>anything else</dt><dd>Search user G-lines.</dd>
892 * @param[in] userhost Mask to search for.
893 * @param[in] flags Bitwise combination of GLINE_* flags.
894 * @return First matching G-line, or NULL if none are found.
897 gline_find(char *userhost, unsigned int flags)
899 struct Gline *gline = 0;
900 struct Gline *sgline;
901 char *user, *host, *t_uh;
903 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
904 gliter(BadChanGlineList, gline, sgline) {
905 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
906 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
907 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
909 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
910 match(gline->gl_user, userhost)) == 0)
915 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
916 *userhost == '#' || *userhost == '&')
919 DupString(t_uh, userhost);
920 canon_userhost(t_uh, &user, &host, "*");
922 gliter(GlobalGlineList, gline, sgline) {
923 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
924 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
925 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
927 else if (flags & GLINE_EXACT) {
928 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
929 || (!gline->gl_host && !host)) &&
930 (ircd_strcmp(gline->gl_user, user) == 0))
933 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
934 || (!gline->gl_host && !host)) &&
935 (match(gline->gl_user, user) == 0))
945 /** Find a matching G-line for a user.
946 * @param[in] cptr Client to compare against.
947 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
948 * GLINE_LASTMOD to limit matches.
949 * @return Matching G-line, or NULL if none are found.
952 gline_lookup(struct Client *cptr, unsigned int flags)
955 struct Gline *sgline;
957 gliter(GlobalGlineList, gline, sgline) {
958 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
959 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
962 if (GlineIsRealName(gline)) {
963 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
964 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
968 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
971 if (GlineIsIpMask(gline)) {
972 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
976 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
980 if (GlineIsActive(gline))
989 /** Delink and free a G-line.
990 * @param[in] gline G-line to free.
993 gline_free(struct Gline *gline)
997 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
999 gline->gl_next->gl_prev_p = gline->gl_prev_p;
1001 MyFree(gline->gl_user); /* free up the memory */
1003 MyFree(gline->gl_host);
1004 MyFree(gline->gl_reason);
1008 /** Burst all known global G-lines to another server.
1009 * @param[in] cptr Destination of burst.
1012 gline_burst(struct Client *cptr)
1014 struct Gline *gline;
1015 struct Gline *sgline;
1017 gliter(GlobalGlineList, gline, sgline) {
1018 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1019 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1020 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1021 gline->gl_host ? "@" : "",
1022 gline->gl_host ? gline->gl_host : "",
1023 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1024 gline->gl_lifetime - CurrentTime, gline->gl_reason);
1027 gliter(BadChanGlineList, gline, sgline) {
1028 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1029 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1030 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1031 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1032 gline->gl_lifetime - CurrentTime, gline->gl_reason);
1036 /** Send a G-line to another server.
1037 * @param[in] cptr Who to inform of the G-line.
1038 * @param[in] gline G-line to send.
1042 gline_resend(struct Client *cptr, struct Gline *gline)
1044 if (GlineIsLocal(gline) || !gline->gl_lastmod)
1047 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1048 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1049 gline->gl_host ? "@" : "",
1050 gline->gl_host ? gline->gl_host : "",
1051 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1052 gline->gl_lifetime - CurrentTime, gline->gl_reason);
1057 /** Display one or all G-lines to a user.
1058 * If \a userhost is not NULL, only send the first matching G-line.
1059 * Otherwise send the whole list.
1060 * @param[in] sptr User asking for G-line list.
1061 * @param[in] userhost G-line mask to search for (or NULL).
1065 gline_list(struct Client *sptr, char *userhost)
1067 struct Gline *gline;
1068 struct Gline *sgline;
1071 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1072 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1074 /* send gline information along */
1075 send_reply(sptr, RPL_GLIST, gline->gl_user,
1076 gline->gl_host ? "@" : "",
1077 gline->gl_host ? gline->gl_host : "",
1078 gline->gl_expire + TSoffset, gline->gl_lastmod,
1079 gline->gl_lifetime + TSoffset,
1080 GlineIsLocal(gline) ? cli_name(&me) : "*",
1081 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1082 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1083 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1085 gliter(GlobalGlineList, gline, sgline) {
1086 send_reply(sptr, RPL_GLIST, gline->gl_user,
1087 gline->gl_host ? "@" : "",
1088 gline->gl_host ? gline->gl_host : "",
1089 gline->gl_expire + TSoffset, gline->gl_lastmod,
1090 gline->gl_lifetime + TSoffset,
1091 GlineIsLocal(gline) ? cli_name(&me) : "*",
1092 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1093 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1094 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1097 gliter(BadChanGlineList, gline, sgline) {
1098 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1099 gline->gl_expire + TSoffset, gline->gl_lastmod,
1100 gline->gl_lifetime + TSoffset,
1101 GlineIsLocal(gline) ? cli_name(&me) : "*",
1102 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1103 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1104 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1108 /* end of gline information */
1109 return send_reply(sptr, RPL_ENDOFGLIST);
1112 /** Statistics callback to list G-lines.
1113 * @param[in] sptr Client requesting statistics.
1114 * @param[in] sd Stats descriptor for request (ignored).
1115 * @param[in] param Extra parameter from user (ignored).
1118 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1121 struct Gline *gline;
1122 struct Gline *sgline;
1124 gliter(GlobalGlineList, gline, sgline) {
1125 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1126 gline->gl_host ? "@" : "",
1127 gline->gl_host ? gline->gl_host : "",
1128 gline->gl_expire + TSoffset, gline->gl_lastmod,
1129 gline->gl_lifetime + TSoffset,
1130 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1131 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1132 GlineIsRemActive(gline) ? '+' : '-',
1137 /** Calculate memory used by G-lines.
1138 * @param[out] gl_size Number of bytes used by G-lines.
1139 * @return Number of G-lines in use.
1142 gline_memory_count(size_t *gl_size)
1144 struct Gline *gline;
1145 unsigned int gl = 0;
1147 for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1149 *gl_size += sizeof(struct Gline);
1150 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1151 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1152 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1155 for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1157 *gl_size += sizeof(struct Gline);
1158 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1159 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1160 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;