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 if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
457 tmp = count_realnames(userhost + 2);
458 if ((tmp >= feature_int(FEAT_GLINEMAXUSERCOUNT))
459 && !(flags & GLINE_OPERFORCE))
460 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
463 canon_userhost(userhost, &user, &host, "*");
465 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
466 return send_reply(sptr, ERR_LONGMASK);
467 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
468 switch (gline_checkmask(host)) {
469 case CHECK_OVERRIDABLE: /* oper overrided restriction */
470 if (flags & GLINE_OPERFORCE)
474 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
478 if ((tmp = count_users(uhmask)) >=
479 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
480 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
485 * You cannot set a negative (or zero) expire time, nor can you set an
486 * expiration time for greater than GLINE_MAX_EXPIRE.
488 if (!(flags & GLINE_FORCE) &&
489 (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
490 if (!IsServer(sptr) && MyConnect(sptr))
491 send_reply(sptr, ERR_BADEXPIRE, expire);
495 if (!lifetime) /* no lifetime set, use expiration time */
498 /* lifetime is already an absolute timestamp */
501 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
502 SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
504 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
506 cli_name((cli_user(sptr))->server),
507 (flags & GLINE_LOCAL) ? "local" : "global",
508 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
509 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
510 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
511 expire + TSoffset, reason);
514 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
515 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
516 flags & GLINE_LOCAL ? "local" : "global",
517 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
518 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
519 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
520 expire + TSoffset, reason);
523 agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
525 /* since we've disabled overlapped G-line checking, agline should
530 gline_propagate(cptr, sptr, agline);
532 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
535 /** Activate a currently inactive G-line.
536 * @param[in] cptr Peer that told us to activate the G-line.
537 * @param[in] sptr Client that originally thought it was a good idea.
538 * @param[in] gline G-line to activate.
539 * @param[in] lastmod New value for last modification timestamp.
540 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
541 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
544 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
545 time_t lastmod, unsigned int flags)
547 unsigned int saveflags = 0;
551 saveflags = gline->gl_flags;
553 if (flags & GLINE_LOCAL)
554 gline->gl_flags &= ~GLINE_LDEACT;
556 gline->gl_flags |= GLINE_ACTIVE;
558 if (gline->gl_lastmod) {
559 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
562 gline->gl_lastmod = lastmod;
566 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
567 return 0; /* was active to begin with */
569 /* Inform ops and log it */
570 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
571 "expiring at %Tu: %s",
572 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
574 cli_name((cli_user(sptr))->server),
575 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
576 gline->gl_user, gline->gl_host ? "@" : "",
577 gline->gl_host ? gline->gl_host : "",
578 gline->gl_expire + TSoffset, gline->gl_reason);
580 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
581 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
582 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
583 gline->gl_host ? "@" : "",
584 gline->gl_host ? gline->gl_host : "",
585 gline->gl_expire + TSoffset, gline->gl_reason);
587 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
588 gline_propagate(cptr, sptr, gline);
590 return do_gline(cptr, sptr, gline);
593 /** Deactivate a G-line.
594 * @param[in] cptr Peer that gave us the message.
595 * @param[in] sptr Client that initiated the deactivation.
596 * @param[in] gline G-line to deactivate.
597 * @param[in] lastmod New value for G-line last modification timestamp.
598 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
602 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
603 time_t lastmod, unsigned int flags)
605 unsigned int saveflags = 0;
610 saveflags = gline->gl_flags;
612 if (GlineIsLocal(gline))
613 msg = "removing local";
614 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
615 msg = "removing global";
616 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
618 msg = "deactivating global";
620 if (flags & GLINE_LOCAL)
621 gline->gl_flags |= GLINE_LDEACT;
623 gline->gl_flags &= ~GLINE_ACTIVE;
625 if (gline->gl_lastmod) {
626 if (gline->gl_lastmod >= lastmod)
629 gline->gl_lastmod = lastmod;
633 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
634 return 0; /* was inactive to begin with */
637 /* Inform ops and log it */
638 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
640 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
642 cli_name((cli_user(sptr))->server),
643 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
644 gline->gl_user, gline->gl_host ? "@" : "",
645 gline->gl_host ? gline->gl_host : "",
646 gline->gl_expire + TSoffset, gline->gl_reason);
648 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
649 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
650 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
651 gline->gl_host ? "@" : "",
652 gline->gl_host ? gline->gl_host : "",
653 gline->gl_expire + TSoffset, gline->gl_reason);
655 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
656 gline_propagate(cptr, sptr, gline);
658 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
659 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
660 gline_free(gline); /* get rid of it */
665 /** Modify a global G-line.
666 * @param[in] cptr Client that sent us the G-line modification.
667 * @param[in] sptr Client that originated the G-line modification.
668 * @param[in] gline G-line being modified.
669 * @param[in] action Resultant status of the G-line.
670 * @param[in] reason Reason for G-line.
671 * @param[in] expire Expiration time of G-line.
672 * @param[in] lastmod Last modification time of G-line.
673 * @param[in] lifetime Lifetime of G-line.
674 * @param[in] flags Bitwise combination of GLINE_* flags.
675 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
678 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
679 enum GlineAction action, char *reason, time_t expire,
680 time_t lastmod, time_t lifetime, unsigned int flags)
682 char buf[BUFSIZE], *op = "";
686 assert(!GlineIsLocal(gline));
688 Debug((DEBUG_DEBUG, "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
689 "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
690 gline->gl_user, gline->gl_host ? "@" : "",
691 gline->gl_host ? gline->gl_host : "",
692 action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
693 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
694 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
695 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
696 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
697 reason, expire, lastmod, lifetime, flags));
699 /* First, let's check lastmod... */
700 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
701 if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
702 if (IsBurstOrBurstAck(cptr))
703 return 0; /* middle of a burst, it'll resync on its own */
704 return gline_resend(cptr, gline); /* resync the server */
705 } else if (GlineLastMod(gline) == lastmod)
706 return 0; /* we have that version of the G-line... */
709 /* All right, we know that there's a change of some sort. What is it? */
710 /* first, check out the expiration time... */
711 if ((flags & GLINE_EXPIRE) && expire) {
712 if (!(flags & GLINE_FORCE) &&
713 (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
714 if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
715 send_reply(sptr, ERR_BADEXPIRE, expire);
719 flags &= ~GLINE_EXPIRE;
721 /* Now check to see if there's any change... */
722 if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
723 flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
727 /* Next, check out lifetime--this one's a bit trickier... */
728 if (!(flags & GLINE_LIFETIME) || !lifetime)
729 lifetime = gline->gl_lifetime; /* use G-line lifetime */
731 lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
733 /* OK, let's see which is greater... */
734 if (lifetime > gline->gl_lifetime)
735 flags |= GLINE_LIFETIME; /* have to update lifetime */
737 flags &= ~GLINE_LIFETIME; /* no change to lifetime */
741 /* Finally, let's see if the reason needs to be updated */
742 if ((flags & GLINE_REASON) && reason &&
743 !ircd_strcmp(gline->gl_reason, reason))
744 flags &= ~GLINE_REASON; /* no changes to the reason */
746 /* OK, now let's take a look at the action... */
747 if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
748 (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
749 (action == GLINE_LOCAL_ACTIVATE &&
750 (gline->gl_state == GLOCAL_ACTIVATED)) ||
751 (action == GLINE_LOCAL_DEACTIVATE &&
752 (gline->gl_state == GLOCAL_DEACTIVATED)) ||
753 /* can't activate an expired G-line */
754 IRCD_MAX(gline->gl_expire, expire) <= CurrentTime)
755 action = GLINE_MODIFY; /* no activity state modifications */
757 Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
758 flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
759 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
760 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
761 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
762 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
764 /* If there are no changes to perform, do no changes */
765 if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
768 /* Now we know what needs to be changed, so let's process the changes... */
770 /* Start by updating lastmod, if indicated... */
771 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
772 gline->gl_lastmod = lastmod;
774 /* Then move on to activity status changes... */
776 case GLINE_ACTIVATE: /* Globally activating G-line */
777 gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
778 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
779 pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
780 op = "+"; /* operation for G-line propagation */
783 case GLINE_DEACTIVATE: /* Globally deactivating G-line */
784 gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
785 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
786 pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
787 op = "-"; /* operation for G-line propagation */
790 case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
791 gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
792 pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
795 case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
796 gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
797 pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
800 case GLINE_MODIFY: /* no change to activity status */
804 /* Handle expiration changes... */
805 if (flags & GLINE_EXPIRE) {
806 gline->gl_expire = expire; /* save new expiration time */
808 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
809 "%s%s changing expiration time to %Tu",
811 pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
812 " and" : "", expire);
815 /* Next, handle lifetime changes... */
816 if (flags & GLINE_LIFETIME) {
817 gline->gl_lifetime = lifetime; /* save new lifetime */
819 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
820 "%s%s extending record lifetime to %Tu",
821 pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
822 " and" : "", lifetime);
825 /* Now, handle reason changes... */
826 if (flags & GLINE_REASON) {
827 MyFree(gline->gl_reason); /* release old reason */
828 DupString(gline->gl_reason, reason); /* store new reason */
830 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
831 "%s%s changing reason to \"%s\"",
832 pos ? ";" : "", pos ? " and" : "", reason);
835 /* All right, inform ops... */
836 sendto_opmask_butone(0, SNO_GLINE, "%s modifying global %s for %s%s%s:%s",
837 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
838 cli_name(sptr) : cli_name((cli_user(sptr))->server),
839 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
840 gline->gl_user, gline->gl_host ? "@" : "",
841 gline->gl_host ? gline->gl_host : "", buf);
843 /* and log the change */
844 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
845 "%#C modifying global %s for %s%s%s:%s", sptr,
846 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
847 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
850 /* We'll be simple for this release, but we can update this to change
851 * the propagation syntax on future updates
853 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
854 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
855 "* %s%s%s%s%s %Tu %Tu %Tu :%s",
856 flags & GLINE_OPERFORCE ? "!" : "", op,
857 gline->gl_user, gline->gl_host ? "@" : "",
858 gline->gl_host ? gline->gl_host : "",
859 gline->gl_expire - CurrentTime, gline->gl_lastmod,
860 gline->gl_lifetime, gline->gl_reason);
862 /* OK, let's do the G-line... */
863 return do_gline(cptr, sptr, gline);
866 /** Destroy a local G-line.
867 * @param[in] cptr Peer that gave us the message.
868 * @param[in] sptr Client that initiated the destruction.
869 * @param[in] gline G-line to destroy.
873 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
876 assert(GlineIsLocal(gline));
878 /* Inform ops and log it */
879 sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
880 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
881 cli_name(sptr) : cli_name((cli_user(sptr))->server),
882 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
883 gline->gl_user, gline->gl_host ? "@" : "",
884 gline->gl_host ? gline->gl_host : "");
885 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
886 "%#C removing local %s for %s%s%s", sptr,
887 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
888 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
890 gline_free(gline); /* get rid of the G-line */
892 return 0; /* convenience return */
895 /** Find a G-line for a particular mask, guided by certain flags.
896 * Certain bits in \a flags are interpreted specially:
898 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
899 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
900 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
901 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
902 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
903 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
904 * <dt>anything else</dt><dd>Search user G-lines.</dd>
906 * @param[in] userhost Mask to search for.
907 * @param[in] flags Bitwise combination of GLINE_* flags.
908 * @return First matching G-line, or NULL if none are found.
911 gline_find(char *userhost, unsigned int flags)
913 struct Gline *gline = 0;
914 struct Gline *sgline;
915 char *user, *host, *t_uh;
917 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
918 gliter(BadChanGlineList, gline, sgline) {
919 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
920 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
921 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
923 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
924 match(gline->gl_user, userhost)) == 0)
929 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
930 *userhost == '#' || *userhost == '&')
933 DupString(t_uh, userhost);
934 canon_userhost(t_uh, &user, &host, "*");
936 gliter(GlobalGlineList, gline, sgline) {
937 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
938 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
939 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
941 else if (flags & GLINE_EXACT) {
942 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
943 || (!gline->gl_host && !host)) &&
944 (ircd_strcmp(gline->gl_user, user) == 0))
947 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
948 || (!gline->gl_host && !host)) &&
949 (match(gline->gl_user, user) == 0))
959 /** Find a matching G-line for a user.
960 * @param[in] cptr Client to compare against.
961 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
962 * GLINE_LASTMOD to limit matches.
963 * @return Matching G-line, or NULL if none are found.
966 gline_lookup(struct Client *cptr, unsigned int flags)
969 struct Gline *sgline;
971 gliter(GlobalGlineList, gline, sgline) {
972 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
973 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
976 if (GlineIsRealName(gline)) {
977 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
978 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
982 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
985 if (GlineIsIpMask(gline)) {
986 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
990 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
994 if (GlineIsActive(gline))
1003 /** Delink and free a G-line.
1004 * @param[in] gline G-line to free.
1007 gline_free(struct Gline *gline)
1011 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
1013 gline->gl_next->gl_prev_p = gline->gl_prev_p;
1015 MyFree(gline->gl_user); /* free up the memory */
1017 MyFree(gline->gl_host);
1018 MyFree(gline->gl_reason);
1022 /** Burst all known global G-lines to another server.
1023 * @param[in] cptr Destination of burst.
1026 gline_burst(struct Client *cptr)
1028 struct Gline *gline;
1029 struct Gline *sgline;
1031 gliter(GlobalGlineList, gline, sgline) {
1032 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1033 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1034 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1035 gline->gl_host ? "@" : "",
1036 gline->gl_host ? gline->gl_host : "",
1037 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1038 gline->gl_lifetime, gline->gl_reason);
1041 gliter(BadChanGlineList, gline, sgline) {
1042 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1043 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1044 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1045 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1046 gline->gl_lifetime, gline->gl_reason);
1050 /** Send a G-line to another server.
1051 * @param[in] cptr Who to inform of the G-line.
1052 * @param[in] gline G-line to send.
1056 gline_resend(struct Client *cptr, struct Gline *gline)
1058 if (GlineIsLocal(gline) || !gline->gl_lastmod)
1061 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1062 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1063 gline->gl_host ? "@" : "",
1064 gline->gl_host ? gline->gl_host : "",
1065 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1066 gline->gl_lifetime, gline->gl_reason);
1071 /** Display one or all G-lines to a user.
1072 * If \a userhost is not NULL, only send the first matching G-line.
1073 * Otherwise send the whole list.
1074 * @param[in] sptr User asking for G-line list.
1075 * @param[in] userhost G-line mask to search for (or NULL).
1079 gline_list(struct Client *sptr, char *userhost)
1081 struct Gline *gline;
1082 struct Gline *sgline;
1085 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1086 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1088 /* send gline information along */
1089 send_reply(sptr, RPL_GLIST, gline->gl_user,
1090 gline->gl_host ? "@" : "",
1091 gline->gl_host ? gline->gl_host : "",
1092 gline->gl_expire + TSoffset, gline->gl_lastmod,
1093 gline->gl_lifetime + TSoffset,
1094 GlineIsLocal(gline) ? cli_name(&me) : "*",
1095 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1096 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1097 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1099 gliter(GlobalGlineList, gline, sgline) {
1100 send_reply(sptr, RPL_GLIST, gline->gl_user,
1101 gline->gl_host ? "@" : "",
1102 gline->gl_host ? gline->gl_host : "",
1103 gline->gl_expire + TSoffset, gline->gl_lastmod,
1104 gline->gl_lifetime + TSoffset,
1105 GlineIsLocal(gline) ? cli_name(&me) : "*",
1106 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1107 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1108 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1111 gliter(BadChanGlineList, gline, sgline) {
1112 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1113 gline->gl_expire + TSoffset, gline->gl_lastmod,
1114 gline->gl_lifetime + TSoffset,
1115 GlineIsLocal(gline) ? cli_name(&me) : "*",
1116 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1117 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1118 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1122 /* end of gline information */
1123 return send_reply(sptr, RPL_ENDOFGLIST);
1126 /** Statistics callback to list G-lines.
1127 * @param[in] sptr Client requesting statistics.
1128 * @param[in] sd Stats descriptor for request (ignored).
1129 * @param[in] param Extra parameter from user (ignored).
1132 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1135 struct Gline *gline;
1136 struct Gline *sgline;
1138 gliter(GlobalGlineList, gline, sgline) {
1139 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1140 gline->gl_host ? "@" : "",
1141 gline->gl_host ? gline->gl_host : "",
1142 gline->gl_expire + TSoffset, gline->gl_lastmod,
1143 gline->gl_lifetime + TSoffset,
1144 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1145 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1146 GlineIsRemActive(gline) ? '+' : '-',
1151 /** Calculate memory used by G-lines.
1152 * @param[out] gl_size Number of bytes used by G-lines.
1153 * @return Number of G-lines in use.
1156 gline_memory_count(size_t *gl_size)
1158 struct Gline *gline;
1159 unsigned int gl = 0;
1161 for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1163 *gl_size += sizeof(struct Gline);
1164 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1165 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1166 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1169 for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1171 *gl_size += sizeof(struct Gline);
1172 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1173 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1174 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;