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"
48 /* #include <assert.h> -- Now using assert in ircd_log.h */
53 #define CHECK_APPROVED 0 /**< Mask is acceptable */
54 #define CHECK_OVERRIDABLE 1 /**< Mask is acceptable, but not by default */
55 #define CHECK_REJECTED 2 /**< Mask is totally unacceptable */
57 #define MASK_WILD_0 0x01 /**< Wildcards in the last position */
58 #define MASK_WILD_1 0x02 /**< Wildcards in the next-to-last position */
60 #define MASK_WILD_MASK 0x03 /**< Mask out the positional wildcards */
62 #define MASK_WILDS 0x10 /**< Mask contains wildcards */
63 #define MASK_IP 0x20 /**< Mask is an IP address */
64 #define MASK_HALT 0x40 /**< Finished processing mask */
66 /** List of user G-lines. */
67 struct Gline* GlobalGlineList = 0;
68 /** List of BadChan G-lines. */
69 struct Gline* BadChanGlineList = 0;
71 /** Iterate through \a list of G-lines. Use this like a for loop,
72 * i.e., follow it with braces and use whatever you passed as \a gl
73 * as a single G-line to be acted upon.
75 * @param[in] list List of G-lines to iterate over.
76 * @param[in] gl Name of a struct Gline pointer variable that will be made to point to the G-lines in sequence.
77 * @param[in] next Name of a scratch struct Gline pointer variable.
79 /* There is some subtlety here with the boolean operators:
80 * (x || 1) is used to continue in a logical-and series even when !x.
81 * (x && 0) is used to continue in a logical-or series even when x.
83 #define gliter(list, gl, next) \
84 /* Iterate through the G-lines in the list */ \
85 for ((gl) = (list); (gl); (gl) = (next)) \
86 /* Figure out the next pointer in list... */ \
87 if ((((next) = (gl)->gl_next) || 1) && \
88 /* Then see if it's expired */ \
89 (gl)->gl_lifetime <= CurrentTime) \
90 /* Record has expired, so free the G-line */ \
92 /* See if we need to expire the G-line */ \
93 else if ((((gl)->gl_expire > CurrentTime) || \
94 (((gl)->gl_flags &= ~GLINE_ACTIVE) && 0) || \
95 ((gl)->gl_state = GLOCAL_GLOBAL)) && 0) \
96 ; /* empty statement */ \
99 /** Find canonical user and host for a string.
100 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
101 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
102 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
104 * @param[in] userhost Input string from user.
105 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
106 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
107 * @param[in] def_user Default value for user part.
110 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
114 if (*userhost == '$') {
120 if (!(tmp = strchr(userhost, '@'))) {
130 /** Create a Gline structure.
131 * @param[in] user User part of mask.
132 * @param[in] host Host part of mask (NULL if not applicable).
133 * @param[in] reason Reason for G-line.
134 * @param[in] expire Expiration timestamp.
135 * @param[in] lastmod Last modification timestamp.
136 * @param[in] flags Bitwise combination of GLINE_* bits.
137 * @return Newly allocated G-line.
139 static struct Gline *
140 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
141 time_t lifetime, unsigned int flags)
145 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
148 DupString(gline->gl_reason, reason); /* initialize gline... */
149 gline->gl_expire = expire;
150 gline->gl_lifetime = lifetime;
151 gline->gl_lastmod = lastmod;
152 gline->gl_flags = flags & GLINE_MASK;
153 gline->gl_state = GLOCAL_GLOBAL; /* not locally modified */
155 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
156 DupString(gline->gl_user, user); /* first, remember channel */
157 gline->gl_host = NULL;
159 gline->gl_next = BadChanGlineList; /* then link it into list */
160 gline->gl_prev_p = &BadChanGlineList;
161 if (BadChanGlineList)
162 BadChanGlineList->gl_prev_p = &gline->gl_next;
163 BadChanGlineList = gline;
165 DupString(gline->gl_user, user); /* remember them... */
167 DupString(gline->gl_host, host);
169 gline->gl_host = NULL;
171 if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
172 gline->gl_flags |= GLINE_IPMASK;
174 gline->gl_next = GlobalGlineList; /* then link it into list */
175 gline->gl_prev_p = &GlobalGlineList;
177 GlobalGlineList->gl_prev_p = &gline->gl_next;
178 GlobalGlineList = gline;
184 /** Check local clients against a new G-line.
185 * If the G-line is inactive or a badchan, return immediately.
186 * Otherwise, if any users match it, disconnect them.
187 * @param[in] cptr Peer connect that sent the G-line.
188 * @param[in] sptr Client that originated the G-line.
189 * @param[in] gline New G-line to check.
190 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
193 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
195 struct Client *acptr;
196 int fd, retval = 0, tval;
198 if (feature_bool(FEAT_DISABLE_GLINES))
199 return 0; /* G-lines are disabled */
201 if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
203 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
206 for (fd = HighestFd; fd >= 0; --fd) {
210 if ((acptr = LocalClientArray[fd])) {
211 if (!cli_user(acptr))
214 if (GlineIsRealName(gline)) { /* Realname Gline */
215 Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
217 if (match(gline->gl_user+2, cli_info(acptr)) != 0)
219 Debug((DEBUG_DEBUG,"Matched!"));
220 } else { /* Host/IP gline */
221 if (cli_user(acptr)->username &&
222 match(gline->gl_user, (cli_user(acptr))->username) != 0)
225 if (GlineIsIpMask(gline)) {
226 if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
230 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
235 /* ok, here's one that got G-lined */
236 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
239 /* let the ops know about it */
240 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
241 get_client_name(acptr, SHOW_IP));
243 /* and get rid of him */
244 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
246 retval = tval; /* retain killed status */
253 * Implements the mask checking applied to local G-lines.
254 * Basically, host masks must have a minimum of two non-wild domain
255 * fields, and IP masks must have a minimum of 16 bits. If the mask
256 * has even one wild-card, OVERRIDABLE is returned, assuming the other
257 * check doesn't fail.
258 * @param[in] mask G-line mask to check.
259 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
262 gline_checkmask(char *mask)
264 unsigned int flags = MASK_IP;
265 unsigned int dots = 0;
266 unsigned int ipmask = 0;
268 for (; *mask; mask++) { /* go through given mask */
269 if (*mask == '.') { /* it's a separator; advance positional wilds */
270 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
273 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
274 ipmask += 8; /* It's an IP with no wilds, count bits */
275 } else if (*mask == '*' || *mask == '?')
276 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
277 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
279 ipmask = strtoul(mask, &mask, 10);
281 /* sanity-check to date */
282 if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
283 return CHECK_REJECTED;
286 return CHECK_REJECTED;
290 if (dots != 3 || ipmask > 32)
291 return CHECK_REJECTED;
296 flags |= MASK_HALT; /* Halt the ipmask calculation */
297 break; /* get out of the loop */
298 } else if (!IsIP6Char(*mask)) {
299 flags &= ~MASK_IP; /* not an IP anymore! */
304 /* Sanity-check quads */
305 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
310 /* update bit count if necessary */
311 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
314 /* Check to see that it's not too wide of a mask */
315 if (flags & MASK_WILDS &&
316 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
317 (flags & MASK_IP && ipmask < 16)))
318 return CHECK_REJECTED; /* to wide, reject */
320 /* Ok, it's approved; require override if it has wildcards, though */
321 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
324 /** Forward a G-line to other servers.
325 * @param[in] cptr Client that sent us the G-line.
326 * @param[in] sptr Client that originated the G-line.
327 * @param[in] gline G-line to forward.
331 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
333 if (GlineIsLocal(gline))
336 assert(gline->gl_lastmod);
338 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
339 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
340 gline->gl_host ? "@" : "",
341 gline->gl_host ? gline->gl_host : "",
342 gline->gl_expire - CurrentTime, gline->gl_lastmod,
343 gline->gl_lifetime, gline->gl_reason);
348 /** Count number of users who match \a mask.
349 * @param[in] mask user\@host or user\@ip mask to check.
350 * @return Count of matching users.
353 count_users(char *mask)
355 struct Client *acptr;
357 char namebuf[USERLEN + HOSTLEN + 2];
358 char ipbuf[USERLEN + SOCKIPLEN + 2];
360 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
364 ircd_snprintf(0, namebuf, sizeof(namebuf), "%s@%s",
365 cli_user(acptr)->username, cli_user(acptr)->host);
366 ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%s@%s", cli_user(acptr)->username,
367 ircd_ntoa(&cli_ip(acptr)));
369 if (!match(mask, namebuf) || !match(mask, ipbuf))
376 /** Count number of users with a realname matching \a mask.
377 * @param[in] mask Wildcard mask to match against realnames.
378 * @return Count of matching users.
381 count_realnames(const char *mask)
383 struct Client *acptr;
389 matchcomp(cmask, &minlen, NULL, mask);
390 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
393 if (strlen(cli_info(acptr)) < minlen)
395 if (!matchexec(cli_info(acptr), cmask, minlen))
401 /** Create a new G-line and add it to global lists.
402 * \a userhost may be in one of four forms:
403 * \li A channel name, to add a BadChan.
404 * \li A string starting with $R and followed by a mask to match against their realname.
405 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
406 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
408 * @param[in] cptr Client that sent us the G-line.
409 * @param[in] sptr Client that originated the G-line.
410 * @param[in] userhost Text mask for the G-line.
411 * @param[in] reason Reason for G-line.
412 * @param[in] expire Expiration time of G-line.
413 * @param[in] lastmod Last modification time of G-line.
414 * @param[in] lifetime Lifetime of G-line.
415 * @param[in] flags Bitwise combination of GLINE_* flags.
416 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
419 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
420 char *reason, time_t expire, time_t lastmod, time_t lifetime,
423 struct Gline *agline;
424 char uhmask[USERLEN + HOSTLEN + 2];
428 assert(0 != userhost);
431 Debug((DEBUG_DEBUG, "gline_add(\"%s\", \"%s\", \"%s\", \"%s\", %Tu, %Tu "
432 "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), userhost, reason,
433 expire, lastmod, lifetime, flags));
435 if (*userhost == '#' || *userhost == '&') {
436 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
437 return send_reply(sptr, ERR_NOPRIVILEGES);
439 flags |= GLINE_BADCHAN;
442 } else if (*userhost == '$') {
443 switch (userhost[1]) {
444 case 'R': flags |= GLINE_REALNAME; break;
446 /* uh, what to do here? */
447 /* The answer, my dear Watson, is we throw a protocol_violation()
450 return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
451 sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
456 tmp = count_realnames(userhost + 2);
457 if ((tmp >= feature_int(FEAT_GLINEMAXUSERCOUNT))
458 && !(flags & GLINE_OPERFORCE))
459 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
461 canon_userhost(userhost, &user, &host, "*");
463 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
464 return send_reply(sptr, ERR_LONGMASK);
465 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
466 switch (gline_checkmask(host)) {
467 case CHECK_OVERRIDABLE: /* oper overrided restriction */
468 if (flags & GLINE_OPERFORCE)
472 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
476 if ((tmp = count_users(uhmask)) >=
477 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
478 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
483 * You cannot set a negative (or zero) expire time, nor can you set an
484 * expiration time for greater than GLINE_MAX_EXPIRE.
486 if (!(flags & GLINE_FORCE) &&
487 (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
488 if (!IsServer(sptr) && MyConnect(sptr))
489 send_reply(sptr, ERR_BADEXPIRE, expire);
493 if (!lifetime) /* no lifetime set, use expiration time */
496 /* lifetime is already an absolute timestamp */
499 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
500 SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
502 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
504 cli_name((cli_user(sptr))->server),
505 (flags & GLINE_LOCAL) ? "local" : "global",
506 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
507 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
508 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
509 expire + TSoffset, reason);
512 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
513 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
514 flags & GLINE_LOCAL ? "local" : "global",
515 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
516 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
517 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
518 expire + TSoffset, reason);
521 agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
523 /* since we've disabled overlapped G-line checking, agline should
528 gline_propagate(cptr, sptr, agline);
530 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
533 /** Activate a currently inactive G-line.
534 * @param[in] cptr Peer that told us to activate the G-line.
535 * @param[in] sptr Client that originally thought it was a good idea.
536 * @param[in] gline G-line to activate.
537 * @param[in] lastmod New value for last modification timestamp.
538 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
539 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
542 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
543 time_t lastmod, unsigned int flags)
545 unsigned int saveflags = 0;
549 saveflags = gline->gl_flags;
551 if (flags & GLINE_LOCAL)
552 gline->gl_flags &= ~GLINE_LDEACT;
554 gline->gl_flags |= GLINE_ACTIVE;
556 if (gline->gl_lastmod) {
557 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
560 gline->gl_lastmod = lastmod;
564 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
565 return 0; /* was active to begin with */
567 /* Inform ops and log it */
568 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
569 "expiring at %Tu: %s",
570 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
572 cli_name((cli_user(sptr))->server),
573 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
574 gline->gl_user, gline->gl_host ? "@" : "",
575 gline->gl_host ? gline->gl_host : "",
576 gline->gl_expire + TSoffset, gline->gl_reason);
578 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
579 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
580 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
581 gline->gl_host ? "@" : "",
582 gline->gl_host ? gline->gl_host : "",
583 gline->gl_expire + TSoffset, gline->gl_reason);
585 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
586 gline_propagate(cptr, sptr, gline);
588 return do_gline(cptr, sptr, gline);
591 /** Deactivate a G-line.
592 * @param[in] cptr Peer that gave us the message.
593 * @param[in] sptr Client that initiated the deactivation.
594 * @param[in] gline G-line to deactivate.
595 * @param[in] lastmod New value for G-line last modification timestamp.
596 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
600 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
601 time_t lastmod, unsigned int flags)
603 unsigned int saveflags = 0;
608 saveflags = gline->gl_flags;
610 if (GlineIsLocal(gline))
611 msg = "removing local";
612 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
613 msg = "removing global";
614 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
616 msg = "deactivating global";
618 if (flags & GLINE_LOCAL)
619 gline->gl_flags |= GLINE_LDEACT;
621 gline->gl_flags &= ~GLINE_ACTIVE;
623 if (gline->gl_lastmod) {
624 if (gline->gl_lastmod >= lastmod)
627 gline->gl_lastmod = lastmod;
631 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
632 return 0; /* was inactive to begin with */
635 /* Inform ops and log it */
636 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
638 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
640 cli_name((cli_user(sptr))->server),
641 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
642 gline->gl_user, gline->gl_host ? "@" : "",
643 gline->gl_host ? gline->gl_host : "",
644 gline->gl_expire + TSoffset, gline->gl_reason);
646 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
647 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
648 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
649 gline->gl_host ? "@" : "",
650 gline->gl_host ? gline->gl_host : "",
651 gline->gl_expire + TSoffset, gline->gl_reason);
653 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
654 gline_propagate(cptr, sptr, gline);
656 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
657 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
658 gline_free(gline); /* get rid of it */
663 /** Modify a global G-line.
664 * @param[in] cptr Client that sent us the G-line modification.
665 * @param[in] sptr Client that originated the G-line modification.
666 * @param[in] gline G-line being modified.
667 * @param[in] action Resultant status of the G-line.
668 * @param[in] reason Reason for G-line.
669 * @param[in] expire Expiration time of G-line.
670 * @param[in] lastmod Last modification time of G-line.
671 * @param[in] lifetime Lifetime of G-line.
672 * @param[in] flags Bitwise combination of GLINE_* flags.
673 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
676 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
677 enum GlineAction action, char *reason, time_t expire,
678 time_t lastmod, time_t lifetime, unsigned int flags)
680 char buf[BUFSIZE], *op = "";
684 assert(!GlineIsLocal(gline));
686 Debug((DEBUG_DEBUG, "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
687 "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
688 gline->gl_user, gline->gl_host ? "@" : "",
689 gline->gl_host ? gline->gl_host : "",
690 action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
691 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
692 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
693 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
694 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
695 reason, expire, lastmod, lifetime, flags));
697 /* First, let's check lastmod... */
698 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
699 if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
700 if (IsBurstOrBurstAck(cptr))
701 return 0; /* middle of a burst, it'll resync on its own */
702 return gline_resend(cptr, gline); /* resync the server */
703 } else if (GlineLastMod(gline) == lastmod)
704 return 0; /* we have that version of the G-line... */
707 /* All right, we know that there's a change of some sort. What is it? */
708 /* first, check out the expiration time... */
709 if ((flags & GLINE_EXPIRE) && expire) {
710 if (!(flags & GLINE_FORCE) &&
711 (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
712 if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
713 send_reply(sptr, ERR_BADEXPIRE, expire);
717 flags &= ~GLINE_EXPIRE;
719 /* Now check to see if there's any change... */
720 if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
721 flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
725 /* Next, check out lifetime--this one's a bit trickier... */
726 if (!(flags & GLINE_LIFETIME) || !lifetime)
727 lifetime = gline->gl_lifetime; /* use G-line lifetime */
729 lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
731 /* OK, let's see which is greater... */
732 if (lifetime > gline->gl_lifetime)
733 flags |= GLINE_LIFETIME; /* have to update lifetime */
735 flags &= ~GLINE_LIFETIME; /* no change to lifetime */
739 /* Finally, let's see if the reason needs to be updated */
740 if ((flags & GLINE_REASON) && reason &&
741 !ircd_strcmp(gline->gl_reason, reason))
742 flags &= ~GLINE_REASON; /* no changes to the reason */
744 /* OK, now let's take a look at the action... */
745 if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
746 (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
747 (action == GLINE_LOCAL_ACTIVATE &&
748 (gline->gl_state == GLOCAL_ACTIVATED)) ||
749 (action == GLINE_LOCAL_DEACTIVATE &&
750 (gline->gl_state == GLOCAL_DEACTIVATED)) ||
751 /* can't activate an expired G-line */
752 IRCD_MAX(gline->gl_expire, expire) <= CurrentTime)
753 action = GLINE_MODIFY; /* no activity state modifications */
755 Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
756 flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
757 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
758 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
759 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
760 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
762 /* If there are no changes to perform, do no changes */
763 if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
766 /* Now we know what needs to be changed, so let's process the changes... */
768 /* Start by updating lastmod, if indicated... */
769 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
770 gline->gl_lastmod = lastmod;
772 /* Then move on to activity status changes... */
774 case GLINE_ACTIVATE: /* Globally activating G-line */
775 gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
776 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
777 pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
778 op = "+"; /* operation for G-line propagation */
781 case GLINE_DEACTIVATE: /* Globally deactivating G-line */
782 gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
783 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
784 pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
785 op = "-"; /* operation for G-line propagation */
788 case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
789 gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
790 pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
793 case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
794 gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
795 pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
798 case GLINE_MODIFY: /* no change to activity status */
802 /* Handle expiration changes... */
803 if (flags & GLINE_EXPIRE) {
804 gline->gl_expire = expire; /* save new expiration time */
806 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
807 "%s%s changing expiration time to %Tu",
809 pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
810 " and" : "", expire);
813 /* Next, handle lifetime changes... */
814 if (flags & GLINE_LIFETIME) {
815 gline->gl_lifetime = lifetime; /* save new lifetime */
817 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
818 "%s%s extending record lifetime to %Tu",
819 pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
820 " and" : "", lifetime);
823 /* Now, handle reason changes... */
824 if (flags & GLINE_REASON) {
825 MyFree(gline->gl_reason); /* release old reason */
826 DupString(gline->gl_reason, reason); /* store new reason */
828 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
829 "%s%s changing reason to \"%s\"",
830 pos ? ";" : "", pos ? " and" : "", reason);
833 /* All right, inform ops... */
834 sendto_opmask_butone(0, SNO_GLINE, "%s modifying global %s for %s%s%s:%s",
835 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
836 cli_name(sptr) : cli_name((cli_user(sptr))->server),
837 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
838 gline->gl_user, gline->gl_host ? "@" : "",
839 gline->gl_host ? gline->gl_host : "", buf);
841 /* and log the change */
842 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
843 "%#C modifying global %s for %s%s%s:%s", sptr,
844 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
845 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
848 /* We'll be simple for this release, but we can update this to change
849 * the propagation syntax on future updates
851 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
852 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
853 "* %s%s%s%s%s %Tu %Tu %Tu :%s",
854 flags & GLINE_OPERFORCE ? "!" : "", op,
855 gline->gl_user, gline->gl_host ? "@" : "",
856 gline->gl_host ? gline->gl_host : "",
857 gline->gl_expire - CurrentTime, gline->gl_lastmod,
858 gline->gl_lifetime, gline->gl_reason);
860 /* OK, let's do the G-line... */
861 return do_gline(cptr, sptr, gline);
864 /** Destroy a local G-line.
865 * @param[in] cptr Peer that gave us the message.
866 * @param[in] sptr Client that initiated the destruction.
867 * @param[in] gline G-line to destroy.
871 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
874 assert(GlineIsLocal(gline));
876 /* Inform ops and log it */
877 sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
878 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
879 cli_name(sptr) : cli_name((cli_user(sptr))->server),
880 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
881 gline->gl_user, gline->gl_host ? "@" : "",
882 gline->gl_host ? gline->gl_host : "");
883 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
884 "%#C removing local %s for %s%s%s", sptr,
885 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
886 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
888 gline_free(gline); /* get rid of the G-line */
890 return 0; /* convenience return */
893 /** Find a G-line for a particular mask, guided by certain flags.
894 * Certain bits in \a flags are interpreted specially:
896 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
897 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
898 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
899 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
900 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
901 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
902 * <dt>anything else</dt><dd>Search user G-lines.</dd>
904 * @param[in] userhost Mask to search for.
905 * @param[in] flags Bitwise combination of GLINE_* flags.
906 * @return First matching G-line, or NULL if none are found.
909 gline_find(char *userhost, unsigned int flags)
911 struct Gline *gline = 0;
912 struct Gline *sgline;
913 char *user, *host, *t_uh;
915 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
916 gliter(BadChanGlineList, gline, sgline) {
917 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
918 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
919 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
921 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
922 match(gline->gl_user, userhost)) == 0)
927 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
928 *userhost == '#' || *userhost == '&')
931 DupString(t_uh, userhost);
932 canon_userhost(t_uh, &user, &host, "*");
934 gliter(GlobalGlineList, gline, sgline) {
935 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
936 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
937 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
939 else if (flags & GLINE_EXACT) {
940 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
941 || (!gline->gl_host && !host)) &&
942 (ircd_strcmp(gline->gl_user, user) == 0))
945 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
946 || (!gline->gl_host && !host)) &&
947 (match(gline->gl_user, user) == 0))
957 /** Find a matching G-line for a user.
958 * @param[in] cptr Client to compare against.
959 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
960 * GLINE_LASTMOD to limit matches.
961 * @return Matching G-line, or NULL if none are found.
964 gline_lookup(struct Client *cptr, unsigned int flags)
967 struct Gline *sgline;
969 gliter(GlobalGlineList, gline, sgline) {
970 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
971 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
974 if (GlineIsRealName(gline)) {
975 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
976 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
980 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
983 if (GlineIsIpMask(gline)) {
984 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
988 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
992 if (GlineIsActive(gline))
1001 /** Delink and free a G-line.
1002 * @param[in] gline G-line to free.
1005 gline_free(struct Gline *gline)
1009 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
1011 gline->gl_next->gl_prev_p = gline->gl_prev_p;
1013 MyFree(gline->gl_user); /* free up the memory */
1015 MyFree(gline->gl_host);
1016 MyFree(gline->gl_reason);
1020 /** Burst all known global G-lines to another server.
1021 * @param[in] cptr Destination of burst.
1024 gline_burst(struct Client *cptr)
1026 struct Gline *gline;
1027 struct Gline *sgline;
1029 gliter(GlobalGlineList, gline, sgline) {
1030 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1031 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1032 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1033 gline->gl_host ? "@" : "",
1034 gline->gl_host ? gline->gl_host : "",
1035 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1036 gline->gl_lifetime, gline->gl_reason);
1039 gliter(BadChanGlineList, gline, sgline) {
1040 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1041 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1042 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1043 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1044 gline->gl_lifetime, gline->gl_reason);
1048 /** Send a G-line to another server.
1049 * @param[in] cptr Who to inform of the G-line.
1050 * @param[in] gline G-line to send.
1054 gline_resend(struct Client *cptr, struct Gline *gline)
1056 if (GlineIsLocal(gline) || !gline->gl_lastmod)
1059 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1060 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1061 gline->gl_host ? "@" : "",
1062 gline->gl_host ? gline->gl_host : "",
1063 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1064 gline->gl_lifetime, gline->gl_reason);
1069 /** Display one or all G-lines to a user.
1070 * If \a userhost is not NULL, only send the first matching G-line.
1071 * Otherwise send the whole list.
1072 * @param[in] sptr User asking for G-line list.
1073 * @param[in] userhost G-line mask to search for (or NULL).
1077 gline_list(struct Client *sptr, char *userhost)
1079 struct Gline *gline;
1080 struct Gline *sgline;
1083 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1084 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1086 /* send gline information along */
1087 send_reply(sptr, RPL_GLIST, gline->gl_user,
1088 gline->gl_host ? "@" : "",
1089 gline->gl_host ? gline->gl_host : "",
1090 gline->gl_expire + TSoffset, gline->gl_lastmod,
1091 gline->gl_lifetime + TSoffset,
1092 GlineIsLocal(gline) ? cli_name(&me) : "*",
1093 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1094 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1095 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1097 gliter(GlobalGlineList, gline, sgline) {
1098 send_reply(sptr, RPL_GLIST, gline->gl_user,
1099 gline->gl_host ? "@" : "",
1100 gline->gl_host ? gline->gl_host : "",
1101 gline->gl_expire + TSoffset, gline->gl_lastmod,
1102 gline->gl_lifetime + TSoffset,
1103 GlineIsLocal(gline) ? cli_name(&me) : "*",
1104 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1105 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1106 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1109 gliter(BadChanGlineList, gline, sgline) {
1110 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1111 gline->gl_expire + TSoffset, gline->gl_lastmod,
1112 gline->gl_lifetime + TSoffset,
1113 GlineIsLocal(gline) ? cli_name(&me) : "*",
1114 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1115 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1116 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1120 /* end of gline information */
1121 return send_reply(sptr, RPL_ENDOFGLIST);
1124 /** Statistics callback to list G-lines.
1125 * @param[in] sptr Client requesting statistics.
1126 * @param[in] sd Stats descriptor for request (ignored).
1127 * @param[in] param Extra parameter from user (ignored).
1130 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1133 struct Gline *gline;
1134 struct Gline *sgline;
1136 gliter(GlobalGlineList, gline, sgline) {
1137 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1138 gline->gl_host ? "@" : "",
1139 gline->gl_host ? gline->gl_host : "",
1140 gline->gl_expire + TSoffset, gline->gl_lastmod,
1141 gline->gl_lifetime + TSoffset,
1142 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1143 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1144 GlineIsRemActive(gline) ? '+' : '-',
1149 /** Calculate memory used by G-lines.
1150 * @param[out] gl_size Number of bytes used by G-lines.
1151 * @return Number of G-lines in use.
1154 gline_memory_count(size_t *gl_size)
1156 struct Gline *gline;
1157 unsigned int gl = 0;
1159 for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1161 *gl_size += sizeof(struct Gline);
1162 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1163 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1164 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1167 for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1169 *gl_size += sizeof(struct Gline);
1170 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1171 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1172 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;