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.
22 * @version $Id: gline.c 1904 2009-02-09 00:03:34Z entrope $
31 #include "ircd_alloc.h"
32 #include "ircd_features.h"
34 #include "ircd_reply.h"
35 #include "ircd_snprintf.h"
36 #include "ircd_string.h"
50 /* #include <assert.h> -- Now using assert in ircd_log.h */
55 #define CHECK_APPROVED 0 /**< Mask is acceptable */
56 #define CHECK_OVERRIDABLE 1 /**< Mask is acceptable, but not by default */
57 #define CHECK_REJECTED 2 /**< Mask is totally unacceptable */
59 #define MASK_WILD_0 0x01 /**< Wildcards in the last position */
60 #define MASK_WILD_1 0x02 /**< Wildcards in the next-to-last position */
62 #define MASK_WILD_MASK 0x03 /**< Mask out the positional wildcards */
64 #define MASK_WILDS 0x10 /**< Mask contains wildcards */
65 #define MASK_IP 0x20 /**< Mask is an IP address */
66 #define MASK_HALT 0x40 /**< Finished processing mask */
68 /** List of user G-lines. */
69 struct Gline* GlobalGlineList = 0;
70 /** List of BadChan G-lines. */
71 struct Gline* BadChanGlineList = 0;
73 /** Iterate through \a list of G-lines. Use this like a for loop,
74 * i.e., follow it with braces and use whatever you passed as \a gl
75 * as a single G-line to be acted upon.
77 * @param[in] list List of G-lines to iterate over.
78 * @param[in] gl Name of a struct Gline pointer variable that will be made to point to the G-lines in sequence.
79 * @param[in] next Name of a scratch struct Gline pointer variable.
81 /* There is some subtlety here with the boolean operators:
82 * (x || 1) is used to continue in a logical-and series even when !x.
83 * (x && 0) is used to continue in a logical-or series even when x.
85 #define GL_EXPIRED(gl) ((gl)->gl_lifetime <= CurrentTime || (gl)->gl_expire <= CurrentTime)
86 #define gliter(list, gl, next) \
87 /* Iterate through the G-lines in the list */ \
88 for ((gl) = (list); (gl); (gl) = (next)) \
89 /* Figure out the next pointer in list... */ \
90 if ((((next) = (gl)->gl_next) || 1) && \
91 /* Then see if it's expired */ \
93 /* Record has expired, so free the G-line */ \
95 /* See if we need to expire the G-line */ \
96 else if ((((gl)->gl_expire > CurrentTime) || \
97 (((gl)->gl_flags &= ~GLINE_ACTIVE) && 0) || \
98 ((gl)->gl_state = GLOCAL_GLOBAL)) && 0) \
99 ; /* empty statement */ \
102 /** Find canonical user and host for a string.
103 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
104 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
105 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
107 * @param[in] userhost Input string from user.
108 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
109 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
110 * @param[in] def_user Default value for user part.
113 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
117 if (*userhost == '$') {
123 if (!(tmp = strchr(userhost, '@'))) {
133 /** Create a Gline structure.
134 * @param[in] user User part of mask.
135 * @param[in] host Host part of mask (NULL if not applicable).
136 * @param[in] reason Reason for G-line.
137 * @param[in] expire Expiration timestamp.
138 * @param[in] lastmod Last modification timestamp.
139 * @param[in] flags Bitwise combination of GLINE_* bits.
140 * @return Newly allocated G-line.
142 static struct Gline *
143 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
144 time_t lifetime, unsigned int flags)
150 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
153 DupString(gline->gl_reason, reason); /* initialize gline... */
154 gline->gl_expire = expire;
155 gline->gl_lifetime = lifetime;
156 gline->gl_lastmod = lastmod;
157 gline->gl_flags = flags & GLINE_MASK;
158 gline->gl_state = GLOCAL_GLOBAL; /* not locally modified */
160 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
161 DupString(gline->gl_user, user); /* first, remember channel */
162 gline->gl_host = NULL;
164 gline->gl_next = BadChanGlineList; /* then link it into list */
165 gline->gl_prev_p = &BadChanGlineList;
166 if (BadChanGlineList)
167 BadChanGlineList->gl_prev_p = &gline->gl_next;
168 BadChanGlineList = gline;
170 DupString(gline->gl_user, user); /* remember them... */
172 DupString(gline->gl_host, host);
174 gline->gl_host = NULL;
176 if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
177 gline->gl_flags |= GLINE_IPMASK;
179 gline->gl_next = GlobalGlineList; /* then link it into list */
180 gline->gl_prev_p = &GlobalGlineList;
182 GlobalGlineList->gl_prev_p = &gline->gl_next;
183 GlobalGlineList = gline;
189 /** Check local clients against a new G-line.
190 * If the G-line is inactive, return immediately.
191 * Otherwise, if any users match it, disconnect them.
192 * @param[in] cptr Peer connect that sent the G-line.
193 * @param[in] sptr Client that originated the G-line.
194 * @param[in] gline New G-line to check.
195 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
198 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
200 struct Client *acptr;
201 int fd, retval = 0, tval;
202 struct Membership *member, *nmember;
203 struct Channel *chptr, *nchptr;
205 if (feature_bool(FEAT_DISABLE_GLINES))
206 return 0; /* G-lines are disabled */
208 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
211 /* Kick all users out of BADCHANs. */
212 if(GlineIsBadChan(gline)) {
213 for(chptr = GlobalChannelList; chptr; chptr = nchptr) {
214 nchptr = chptr->next; /* save nchptr, we may delete him */
215 if(0 == match(GlineUser(gline), chptr->chname)) {
216 for(member = chptr->members; member; member = nmember) {
217 nmember = member->next_member; /* save nmember, we may delete him */
218 /* Do not kick opers. */
219 if(!MyUser(member->user) || IsZombie(member) || IsAnOper(member->user)) continue;
220 sendcmdto_serv_butone(&me, CMD_KICK, NULL, "%H %C :Badchanneled: %s", chptr, member->user, GlineReason(gline));
221 sendcmdto_channel_butserv_butone(&me, CMD_KICK, chptr, NULL, 0, "%H %C :Badchanneled: %s", chptr, member->user, GlineReason(gline));
222 make_zombie(member, member->user, &me, &me, chptr);
224 if(!string_has_wildcards(GlineUser(gline))) break;
230 for (fd = HighestFd; fd >= 0; --fd) {
234 if ((acptr = LocalClientArray[fd])) {
235 if (!cli_user(acptr))
238 if (GlineIsRealName(gline)) { /* Realname Gline */
239 Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
241 if (match(gline->gl_user+2, cli_info(acptr)) != 0)
243 Debug((DEBUG_DEBUG,"Matched!"));
244 } else { /* Host/IP gline */
245 if (cli_user(acptr)->username &&
246 match(gline->gl_user, (cli_user(acptr))->username) != 0)
249 if (GlineIsIpMask(gline)) {
250 if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
254 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
259 /* ok, here's one that got G-lined */
260 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
263 /* let the ops know about it */
264 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
265 get_client_name(acptr, SHOW_IP));
267 /* and get rid of him */
268 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
270 retval = tval; /* retain killed status */
277 * Implements the mask checking applied to local G-lines.
278 * Basically, host masks must have a minimum of two non-wild domain
279 * fields, and IP masks must have a minimum of 16 bits. If the mask
280 * has even one wild-card, OVERRIDABLE is returned, assuming the other
281 * check doesn't fail.
282 * @param[in] mask G-line mask to check.
283 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
286 gline_checkmask(char *mask)
288 unsigned int flags = MASK_IP;
289 unsigned int dots = 0;
290 unsigned int ipmask = 0;
292 for (; *mask; mask++) { /* go through given mask */
293 if (*mask == '.') { /* it's a separator; advance positional wilds */
294 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
297 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
298 ipmask += 8; /* It's an IP with no wilds, count bits */
299 } else if (*mask == '*' || *mask == '?')
300 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
301 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
303 ipmask = strtoul(mask, &mask, 10);
305 /* sanity-check to date */
306 if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
307 return CHECK_REJECTED;
310 return CHECK_REJECTED;
314 if (dots != 3 || ipmask > 32)
315 return CHECK_REJECTED;
320 flags |= MASK_HALT; /* Halt the ipmask calculation */
321 break; /* get out of the loop */
322 } else if (!IsIP6Char(*mask)) {
323 flags &= ~MASK_IP; /* not an IP anymore! */
328 /* Sanity-check quads */
329 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
334 /* update bit count if necessary */
335 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
338 /* Check to see that it's not too wide of a mask */
339 if (flags & MASK_WILDS &&
340 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
341 (flags & MASK_IP && ipmask < 16)))
342 return CHECK_REJECTED; /* to wide, reject */
344 /* Ok, it's approved; require override if it has wildcards, though */
345 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
348 /** Forward a G-line to other servers.
349 * @param[in] cptr Client that sent us the G-line.
350 * @param[in] sptr Client that originated the G-line.
351 * @param[in] gline G-line to forward.
355 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
357 if (GlineIsLocal(gline))
360 assert(gline->gl_lastmod);
362 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
363 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
364 gline->gl_host ? "@" : "",
365 gline->gl_host ? gline->gl_host : "",
366 gline->gl_expire - CurrentTime, gline->gl_lastmod,
367 gline->gl_lifetime, gline->gl_reason);
372 /** Count number of users who match \a mask.
373 * @param[in] mask user\@host or user\@ip mask to check.
374 * @param[in] flags Bitmask possibly containing the value GLINE_LOCAL, to limit searches to this server.
375 * @return Count of matching users.
378 count_users(char *mask, int flags)
380 struct irc_in_addr ipmask;
381 struct Client *acptr;
384 char namebuf[USERLEN + HOSTLEN + 2];
385 char ipbuf[USERLEN + SOCKIPLEN + 2];
386 unsigned char ipmask_len;
388 ipmask_valid = ipmask_parse(mask, &ipmask, &ipmask_len);
389 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
392 if ((flags & GLINE_LOCAL) && !MyConnect(acptr))
395 ircd_snprintf(0, namebuf, sizeof(namebuf), "%s@%s",
396 cli_user(acptr)->username, cli_user(acptr)->host);
397 ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%s@%s", cli_user(acptr)->username,
398 ircd_ntoa(&cli_ip(acptr)));
400 if (!match(mask, namebuf)
401 || !match(mask, ipbuf)
402 || (ipmask_valid && ipmask_check(&cli_ip(acptr), &ipmask, ipmask_len)))
409 /** Count number of users with a realname matching \a mask.
410 * @param[in] mask Wildcard mask to match against realnames.
411 * @return Count of matching users.
414 count_realnames(const char *mask)
416 struct Client *acptr;
422 matchcomp(cmask, &minlen, NULL, mask);
423 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
426 if (strlen(cli_info(acptr)) < minlen)
428 if (!matchexec(cli_info(acptr), cmask, minlen))
434 /** Create a new G-line and add it to global lists.
435 * \a userhost may be in one of four forms:
436 * \li A channel name, to add a BadChan.
437 * \li A string starting with $R and followed by a mask to match against their realname.
438 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
439 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
441 * @param[in] cptr Client that sent us the G-line.
442 * @param[in] sptr Client that originated the G-line.
443 * @param[in] userhost Text mask for the G-line.
444 * @param[in] reason Reason for G-line.
445 * @param[in] expire Expiration time of G-line.
446 * @param[in] lastmod Last modification time of G-line.
447 * @param[in] lifetime Lifetime of G-line.
448 * @param[in] flags Bitwise combination of GLINE_* flags.
449 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
452 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
453 char *reason, time_t expire, time_t lastmod, time_t lifetime,
456 struct Gline *agline;
457 char uhmask[USERLEN + HOSTLEN + 2];
461 assert(0 != userhost);
463 assert(((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_GLOBAL) ||
464 ((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_LOCAL));
466 Debug((DEBUG_DEBUG, "gline_add(\"%s\", \"%s\", \"%s\", \"%s\", %Tu, %Tu "
467 "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), userhost, reason,
468 expire, lastmod, lifetime, flags));
470 if (*userhost == '#' || *userhost == '&') {
471 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
472 return send_reply(sptr, ERR_NOPRIVILEGES);
474 flags |= GLINE_BADCHAN;
477 } else if (*userhost == '$') {
478 switch (userhost[1]) {
479 case 'R': flags |= GLINE_REALNAME; break;
481 /* uh, what to do here? */
482 /* The answer, my dear Watson, is we throw a protocol_violation()
485 return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
486 sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
491 if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
492 tmp = count_realnames(userhost + 2);
493 if ((tmp >= feature_int(FEAT_GLINEMAXUSERCOUNT))
494 && !(flags & GLINE_OPERFORCE))
495 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
498 canon_userhost(userhost, &user, &host, "*");
500 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
501 return send_reply(sptr, ERR_LONGMASK);
502 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
503 switch (gline_checkmask(host)) {
504 case CHECK_OVERRIDABLE: /* oper overrided restriction */
505 if (flags & GLINE_OPERFORCE)
509 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
513 if ((tmp = count_users(uhmask, flags)) >=
514 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
515 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
520 * You cannot set a negative (or zero) expire time, nor can you set an
521 * expiration time for greater than GLINE_MAX_EXPIRE, it will be
522 * set to GLINE_MAX_EXPIRE
524 if(expire <= CurrentTime) {
525 if(!IsServer(sptr) && MyConnect(sptr))
526 send_reply(sptr, ERR_BADEXPIRE, expire);
529 else if(expire > CurrentTime + GLINE_MAX_EXPIRE) {
530 if(!IsServer(sptr) && MyConnect(sptr))
531 send_reply(sptr, ERR_CHANGEDEXPIRE, expire, CurrentTime + GLINE_MAX_EXPIRE);
532 expire = CurrentTime + GLINE_MAX_EXPIRE;
535 if (!lifetime) /* no lifetime set, use expiration time */
538 /* lifetime is already an absolute timestamp */
541 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
542 SNO_AUTO, "%s adding %s%s %s for %s%s%s, expiring at "
544 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
546 cli_name((cli_user(sptr))->server),
547 (flags & GLINE_ACTIVE) ? "" : "deactivated ",
548 (flags & GLINE_LOCAL) ? "local" : "global",
549 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
550 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
551 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
552 expire + TSoffset, reason);
555 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
556 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
557 flags & GLINE_LOCAL ? "local" : "global",
558 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
559 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
560 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
561 expire + TSoffset, reason);
564 agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
566 /* since we've disabled overlapped G-line checking, agline should
571 gline_propagate(cptr, sptr, agline);
573 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
576 /** Activate a currently inactive G-line.
577 * @param[in] cptr Peer that told us to activate the G-line.
578 * @param[in] sptr Client that originally thought it was a good idea.
579 * @param[in] gline G-line to activate.
580 * @param[in] lastmod New value for last modification timestamp.
581 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
582 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
585 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
586 time_t lastmod, unsigned int flags)
588 unsigned int saveflags = 0;
592 saveflags = gline->gl_flags;
594 if (flags & GLINE_LOCAL)
595 gline->gl_flags &= ~GLINE_LDEACT;
597 gline->gl_flags |= GLINE_ACTIVE;
599 if (gline->gl_lastmod) {
600 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
603 gline->gl_lastmod = lastmod;
607 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
608 return 0; /* was active to begin with */
610 /* Inform ops and log it */
611 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
612 "expiring at %Tu: %s",
613 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
615 cli_name((cli_user(sptr))->server),
616 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
617 gline->gl_user, gline->gl_host ? "@" : "",
618 gline->gl_host ? gline->gl_host : "",
619 gline->gl_expire + TSoffset, gline->gl_reason);
621 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
622 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
623 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
624 gline->gl_host ? "@" : "",
625 gline->gl_host ? gline->gl_host : "",
626 gline->gl_expire + TSoffset, gline->gl_reason);
628 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
629 gline_propagate(cptr, sptr, gline);
631 return do_gline(cptr, sptr, gline);
634 /** Deactivate a G-line.
635 * @param[in] cptr Peer that gave us the message.
636 * @param[in] sptr Client that initiated the deactivation.
637 * @param[in] gline G-line to deactivate.
638 * @param[in] lastmod New value for G-line last modification timestamp.
639 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
643 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
644 time_t lastmod, unsigned int flags)
646 unsigned int saveflags = 0;
651 saveflags = gline->gl_flags;
653 if (GlineIsLocal(gline))
654 msg = "removing local";
655 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
656 msg = "removing global";
657 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
659 msg = "deactivating global";
661 if (flags & GLINE_LOCAL)
662 gline->gl_flags |= GLINE_LDEACT;
664 gline->gl_flags &= ~GLINE_ACTIVE;
666 if (gline->gl_lastmod) {
667 if (gline->gl_lastmod >= lastmod)
670 gline->gl_lastmod = lastmod;
674 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
675 return 0; /* was inactive to begin with */
678 /* Inform ops and log it */
679 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
681 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
683 cli_name((cli_user(sptr))->server),
684 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
685 gline->gl_user, gline->gl_host ? "@" : "",
686 gline->gl_host ? gline->gl_host : "",
687 gline->gl_expire + TSoffset, gline->gl_reason);
689 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
690 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
691 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
692 gline->gl_host ? "@" : "",
693 gline->gl_host ? gline->gl_host : "",
694 gline->gl_expire + TSoffset, gline->gl_reason);
696 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
697 gline_propagate(cptr, sptr, gline);
699 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
700 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
701 gline_free(gline); /* get rid of it */
706 /** Modify a global G-line.
707 * @param[in] cptr Client that sent us the G-line modification.
708 * @param[in] sptr Client that originated the G-line modification.
709 * @param[in] gline G-line being modified.
710 * @param[in] action Resultant status of the G-line.
711 * @param[in] reason Reason for G-line.
712 * @param[in] expire Expiration time of G-line.
713 * @param[in] lastmod Last modification time of G-line.
714 * @param[in] lifetime Lifetime of G-line.
715 * @param[in] flags Bitwise combination of GLINE_* flags.
716 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
719 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
720 enum GlineAction action, char *reason, time_t expire,
721 time_t lastmod, time_t lifetime, unsigned int flags)
723 char buf[BUFSIZE], *op = "";
724 int pos = 0, disable_gl = 0;
727 assert(!GlineIsLocal(gline));
729 Debug((DEBUG_DEBUG, "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
730 "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
731 gline->gl_user, gline->gl_host ? "@" : "",
732 gline->gl_host ? gline->gl_host : "",
733 action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
734 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
735 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
736 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
737 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
738 reason, expire, lastmod, lifetime, flags));
740 /* First, let's check lastmod... */
741 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
742 if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
743 if (IsBurstOrBurstAck(cptr))
744 return 0; /* middle of a burst, it'll resync on its own */
745 return gline_resend(cptr, gline); /* resync the server */
746 } else if (GlineLastMod(gline) == lastmod)
747 return 0; /* we have that version of the G-line... */
750 /* All right, we know that there's a change of some sort. What is it? */
751 /* first, check out the expiration time... */
752 if (flags & GLINE_EXPIRE) {
753 if (!(flags & GLINE_FORCE)) {
754 /* Allow to set times smaller than CurrentTime to allow the
755 * explicit deletion/deactivation of a gline.
757 if(expire > CurrentTime + GLINE_MAX_EXPIRE) {
758 if(!IsServer(sptr) && MyConnect(sptr))
759 send_reply(sptr, ERR_CHANGEDEXPIRE, expire, CurrentTime + GLINE_MAX_EXPIRE);
760 expire = CurrentTime + GLINE_MAX_EXPIRE;
763 if(expire <= CurrentTime) disable_gl = 1;
765 flags &= ~GLINE_EXPIRE;
769 /* Now check to see if there's any change... */
770 if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
771 flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
775 /* Next, check out lifetime--this one's a bit trickier... */
776 if (!(flags & GLINE_LIFETIME))
777 lifetime = gline->gl_lifetime; /* use G-line lifetime */
779 lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
781 /* OK, let's see which is greater... */
782 if (lifetime > gline->gl_lifetime)
783 flags |= GLINE_LIFETIME; /* have to update lifetime */
785 flags &= ~GLINE_LIFETIME; /* no change to lifetime */
789 /* Finally, let's see if the reason needs to be updated */
790 if ((flags & GLINE_REASON) && reason &&
791 !ircd_strcmp(gline->gl_reason, reason))
792 flags &= ~GLINE_REASON; /* no changes to the reason */
794 /* OK, now let's take a look at the action... */
795 if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
796 (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
797 (action == GLINE_LOCAL_ACTIVATE &&
798 (gline->gl_state == GLOCAL_ACTIVATED)) ||
799 (action == GLINE_LOCAL_DEACTIVATE &&
800 (gline->gl_state == GLOCAL_DEACTIVATED)) ||
801 /* can't activate an expired G-line */
802 IRCD_MAX(gline->gl_expire, expire) <= CurrentTime)
803 action = GLINE_MODIFY; /* no activity state modifications */
805 Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
806 flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
807 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
808 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
809 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
810 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
812 /* If there are no changes to perform, do no changes */
813 if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
816 /* Now we know what needs to be changed, so let's process the changes... */
818 /* Start by updating lastmod, if indicated... */
819 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
820 gline->gl_lastmod = lastmod;
822 /* Then move on to activity status changes... */
824 case GLINE_ACTIVATE: /* Globally activating G-line */
825 gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
826 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
827 pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
828 op = "+"; /* operation for G-line propagation */
831 case GLINE_DEACTIVATE: /* Globally deactivating G-line */
832 gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
833 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
834 pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
835 op = "-"; /* operation for G-line propagation */
838 case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
839 gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
840 pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
843 case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
844 gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
845 pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
848 case GLINE_MODIFY: /* no change to activity status */
852 /* Handle expiration changes... */
853 if (flags & GLINE_EXPIRE) {
854 gline->gl_expire = expire; /* save new expiration time */
856 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
857 "%s%s changing expiration time to %Tu",
859 pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
860 " and" : "", expire);
863 /* Next, handle lifetime changes... */
864 if (flags & GLINE_LIFETIME) {
865 gline->gl_lifetime = lifetime; /* save new lifetime */
867 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
868 "%s%s extending record lifetime to %Tu",
869 pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
870 " and" : "", lifetime);
873 /* Now, handle reason changes... */
874 if (flags & GLINE_REASON) {
875 MyFree(gline->gl_reason); /* release old reason */
876 DupString(gline->gl_reason, reason); /* store new reason */
878 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
879 "%s%s changing reason to \"%s\"",
880 pos ? ";" : "", pos ? " and" : "", reason);
883 /* All right, inform ops... */
885 /* We send a special message if the expiration time has been set below CurrentTime.
886 * However, we additionally save the exact message in the log, so we have both information
887 * in there. But the ops don't need to be bothered twice.
889 sendto_opmask_butone(0, SNO_GLINE, "%s deleting global %s for %s%s%s",
890 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
891 cli_name(sptr) : cli_name((cli_user(sptr))->server),
892 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
893 gline->gl_user, gline->gl_host ? "@" : "",
894 gline->gl_host ? gline->gl_host : "");
895 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
896 "%#C deleting global %s for %s%s%s", sptr,
897 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
898 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
901 sendto_opmask_butone(0, SNO_GLINE, "%s modifying global %s for %s%s%s:%s",
902 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
903 cli_name(sptr) : cli_name((cli_user(sptr))->server),
904 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
905 gline->gl_user, gline->gl_host ? "@" : "",
906 gline->gl_host ? gline->gl_host : "", buf);
909 /* and log the change */
910 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
911 "%#C modifying global %s for %s%s%s:%s", sptr,
912 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
913 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
916 /* We'll be simple for this release, but we can update this to change
917 * the propagation syntax on future updates
919 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
920 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
921 "* %s%s%s%s%s %Tu %Tu %Tu :%s",
922 flags & GLINE_OPERFORCE ? "!" : "", op,
923 gline->gl_user, gline->gl_host ? "@" : "",
924 gline->gl_host ? gline->gl_host : "",
925 gline->gl_expire, gline->gl_lastmod,
926 gline->gl_lifetime, gline->gl_reason);
928 /* OK, let's do the G-line... */
929 if(disable_gl) return 0;
930 else return do_gline(cptr, sptr, gline);
933 /** Destroy a local G-line.
934 * @param[in] cptr Peer that gave us the message.
935 * @param[in] sptr Client that initiated the destruction.
936 * @param[in] gline G-line to destroy.
940 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
943 assert(GlineIsLocal(gline));
945 /* Inform ops and log it */
946 sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
947 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
948 cli_name(sptr) : cli_name((cli_user(sptr))->server),
949 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
950 gline->gl_user, gline->gl_host ? "@" : "",
951 gline->gl_host ? gline->gl_host : "");
952 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
953 "%#C removing local %s for %s%s%s", sptr,
954 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
955 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
957 gline_free(gline); /* get rid of the G-line */
959 return 0; /* convenience return */
962 /** Find a G-line for a particular mask, guided by certain flags.
963 * Certain bits in \a flags are interpreted specially:
965 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
966 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
967 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
968 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
969 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
970 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
971 * <dt>anything else</dt><dd>Search user G-lines.</dd>
973 * @param[in] userhost Mask to search for.
974 * @param[in] flags Bitwise combination of GLINE_* flags.
975 * @return First matching G-line, or NULL if none are found.
978 gline_find(char *userhost, unsigned int flags)
980 struct Gline *gline = 0;
981 struct Gline *sgline;
982 char *user, *host, *t_uh;
984 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
985 gliter(BadChanGlineList, gline, sgline) {
986 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
987 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
989 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
990 match(gline->gl_user, userhost)) == 0)
995 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
996 *userhost == '#' || *userhost == '&')
999 DupString(t_uh, userhost);
1000 canon_userhost(t_uh, &user, &host, "*");
1002 gliter(GlobalGlineList, gline, sgline) {
1003 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
1004 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
1006 else if (flags & GLINE_EXACT) {
1007 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
1008 || (!gline->gl_host && !host)) &&
1009 (ircd_strcmp(gline->gl_user, user) == 0))
1012 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
1013 || (!gline->gl_host && !host)) &&
1014 (match(gline->gl_user, user) == 0))
1024 /** Find a matching G-line for a user.
1025 * @param[in] cptr Client to compare against.
1026 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
1027 * GLINE_LASTMOD to limit matches.
1028 * @return Matching G-line, or NULL if none are found.
1031 gline_lookup(struct Client *cptr, unsigned int flags)
1033 struct Gline *gline;
1034 struct Gline *sgline;
1036 gliter(GlobalGlineList, gline, sgline) {
1037 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
1038 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
1041 if (GlineIsRealName(gline)) {
1042 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
1043 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
1047 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
1050 if (GlineIsIpMask(gline)) {
1051 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
1055 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
1059 if (GlineIsActive(gline))
1068 /** Delink and free a G-line.
1069 * @param[in] gline G-line to free.
1072 gline_free(struct Gline *gline)
1076 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
1078 gline->gl_next->gl_prev_p = gline->gl_prev_p;
1080 MyFree(gline->gl_user); /* free up the memory */
1082 MyFree(gline->gl_host);
1083 MyFree(gline->gl_reason);
1087 /** Burst all known global G-lines to another server.
1088 * @param[in] cptr Destination of burst.
1091 gline_burst(struct Client *cptr)
1093 struct Gline *gline;
1094 struct Gline *sgline;
1096 gliter(GlobalGlineList, gline, sgline) {
1097 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1098 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1099 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1100 gline->gl_host ? "@" : "",
1101 gline->gl_host ? gline->gl_host : "",
1102 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1103 gline->gl_lifetime, gline->gl_reason);
1106 gliter(BadChanGlineList, gline, sgline) {
1107 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1108 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1109 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1110 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1111 gline->gl_lifetime, gline->gl_reason);
1115 /** Send a G-line to another server.
1116 * @param[in] cptr Who to inform of the G-line.
1117 * @param[in] gline G-line to send.
1121 gline_resend(struct Client *cptr, struct Gline *gline)
1123 if (GlineIsLocal(gline) || !gline->gl_lastmod)
1126 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1127 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1128 gline->gl_host ? "@" : "",
1129 gline->gl_host ? gline->gl_host : "",
1130 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1131 gline->gl_lifetime, gline->gl_reason);
1136 /** Display one or all G-lines to a user.
1137 * If \a userhost is not NULL, only send the first matching G-line.
1138 * Otherwise send the whole list.
1139 * @param[in] sptr User asking for G-line list.
1140 * @param[in] userhost G-line mask to search for (or NULL).
1144 gline_list(struct Client *sptr, char *userhost)
1146 struct Gline *gline;
1147 struct Gline *sgline;
1150 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1151 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1153 /* send gline information along */
1154 send_reply(sptr, RPL_GLIST, gline->gl_user,
1155 gline->gl_host ? "@" : "",
1156 gline->gl_host ? gline->gl_host : "",
1157 gline->gl_expire + TSoffset, gline->gl_lastmod,
1158 gline->gl_lifetime + TSoffset,
1159 GlineIsLocal(gline) ? cli_name(&me) : "*",
1160 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1161 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1162 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1164 gliter(GlobalGlineList, gline, sgline) {
1165 send_reply(sptr, RPL_GLIST, gline->gl_user,
1166 gline->gl_host ? "@" : "",
1167 gline->gl_host ? gline->gl_host : "",
1168 gline->gl_expire + TSoffset, gline->gl_lastmod,
1169 gline->gl_lifetime + TSoffset,
1170 GlineIsLocal(gline) ? cli_name(&me) : "*",
1171 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1172 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1173 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1176 gliter(BadChanGlineList, gline, sgline) {
1177 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1178 gline->gl_expire + TSoffset, gline->gl_lastmod,
1179 gline->gl_lifetime + TSoffset,
1180 GlineIsLocal(gline) ? cli_name(&me) : "*",
1181 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1182 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1183 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1187 /* end of gline information */
1188 return send_reply(sptr, RPL_ENDOFGLIST);
1191 /** Statistics callback to list G-lines.
1192 * @param[in] sptr Client requesting statistics.
1193 * @param[in] sd Stats descriptor for request (ignored).
1194 * @param[in] param Extra parameter from user (ignored).
1197 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1200 struct Gline *gline;
1201 struct Gline *sgline;
1203 gliter(GlobalGlineList, gline, sgline) {
1204 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1205 gline->gl_host ? "@" : "",
1206 gline->gl_host ? gline->gl_host : "",
1207 gline->gl_expire + TSoffset, gline->gl_lastmod,
1208 gline->gl_lifetime + TSoffset,
1209 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1210 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1211 GlineIsRemActive(gline) ? '+' : '-',
1216 /** Calculate memory used by G-lines.
1217 * @param[out] gl_size Number of bytes used by G-lines.
1218 * @return Number of G-lines in use.
1221 gline_memory_count(size_t *gl_size)
1223 struct Gline *gline;
1224 unsigned int gl = 0;
1226 for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1228 *gl_size += sizeof(struct Gline);
1229 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1230 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1231 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1234 for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1236 *gl_size += sizeof(struct Gline);
1237 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1238 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1239 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;