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 1, 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.
27 #include "ircd_alloc.h"
28 #include "ircd_features.h"
30 #include "ircd_policy.h"
31 #include "ircd_reply.h"
32 #include "ircd_snprintf.h"
33 #include "ircd_string.h"
45 #include "sys.h" /* FALSE bleah */
52 #include <arpa/inet.h> /* for inet_ntoa */
54 #define CHECK_APPROVED 0 /* Mask is acceptable */
55 #define CHECK_OVERRIDABLE 1 /* Mask is acceptable, but not by default */
56 #define CHECK_REJECTED 2 /* Mask is totally unacceptable */
58 #define MASK_WILD_0 0x01 /* Wildcards in the last position */
59 #define MASK_WILD_1 0x02 /* Wildcards in the next-to-last position */
61 #define MASK_WILD_MASK 0x03 /* Mask out the positional wildcards */
63 #define MASK_WILDS 0x10 /* Mask contains wildcards */
64 #define MASK_IP 0x20 /* Mask is an IP address */
65 #define MASK_HALT 0x40 /* Finished processing mask */
67 struct Gline* GlobalGlineList = 0;
68 struct Gline* BadChanGlineList = 0;
71 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
75 if (!(tmp = strchr(userhost, '@'))) {
86 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
89 struct Gline *gline, *sgline, *after = 0;
91 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
93 for (gline = GlobalGlineList; gline; gline = sgline) {
94 sgline = gline->gl_next;
96 if (gline->gl_expire <= CurrentTime)
98 else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
100 else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
101 !mmatch(gline->gl_host, host)) {
102 if (expire <= gline->gl_expire) /* will expire before wider gline */
105 after = gline; /* stick new gline after this one */
106 } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
107 !mmatch(host, gline->gl_host) &&
108 gline->gl_expire <= expire) /* gline expires before new one */
109 gline_free(gline); /* save some memory */
113 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
116 DupString(gline->gl_reason, reason); /* initialize gline... */
117 gline->gl_expire = expire;
118 gline->gl_lastmod = lastmod;
119 gline->gl_flags = flags & GLINE_MASK;
121 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
122 DupString(gline->gl_user, user); /* first, remember channel */
125 gline->gl_next = BadChanGlineList; /* then link it into list */
126 gline->gl_prev_p = &BadChanGlineList;
127 if (BadChanGlineList)
128 BadChanGlineList->gl_prev_p = &gline->gl_next;
129 BadChanGlineList = gline;
131 DupString(gline->gl_user, user); /* remember them... */
132 DupString(gline->gl_host, host);
134 if (check_if_ipmask(host)) { /* mark if it's an IP mask */
140 class = sscanf(host,"%d.%d.%d.%d/%d",
141 &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
148 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
149 gline->ipnum.s_addr = inet_addr(ipname);
150 Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
151 gline->gl_flags |= GLINE_IPMASK;
155 gline->gl_next = after->gl_next;
156 gline->gl_prev_p = &after->gl_next;
158 after->gl_next->gl_prev_p = &gline->gl_next;
159 after->gl_next = gline;
161 gline->gl_next = GlobalGlineList; /* then link it into list */
162 gline->gl_prev_p = &GlobalGlineList;
164 GlobalGlineList->gl_prev_p = &gline->gl_next;
165 GlobalGlineList = gline;
173 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
175 struct Client *acptr;
176 int fd, retval = 0, tval;
178 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
181 for (fd = HighestFd; fd >= 0; --fd) {
185 if ((acptr = LocalClientArray[fd])) {
186 if (!cli_user(acptr))
189 if (cli_user(acptr)->username &&
190 match (gline->gl_user, (cli_user(acptr))->username) != 0)
193 if (GlineIsIpMask(gline)) {
194 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
195 if (((cli_ip(acptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
199 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
203 /* ok, here's one that got G-lined */
204 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
207 /* let the ops know about it */
208 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
209 get_client_name(acptr, TRUE));
211 /* and get rid of him */
212 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
214 retval = tval; /* retain killed status */
221 * This routine implements the mask checking applied to local
222 * G-lines. Basically, host masks must have a minimum of two non-wild
223 * domain fields, and IP masks must have a minimum of 16 bits. If the
224 * mask has even one wild-card, OVERRIDABLE is returned, assuming the
225 * other check doesn't fail.
228 gline_checkmask(char *mask)
230 unsigned int flags = MASK_IP;
231 unsigned int dots = 0;
232 unsigned int ipmask = 0;
234 for (; *mask; mask++) { /* go through given mask */
235 if (*mask == '.') { /* it's a separator; advance positional wilds */
236 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
239 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
240 ipmask += 8; /* It's an IP with no wilds, count bits */
241 } else if (*mask == '*' || *mask == '?')
242 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
243 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
244 ipmask = strtoul(++mask, &mask, 10);
246 if (*mask || dots != 3 || ipmask > 32 || /* sanity-check to date */
247 (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
248 return CHECK_REJECTED; /* how strange... */
250 if (ipmask < 32) /* it's a masked address; mark wilds */
253 flags |= MASK_HALT; /* Halt the ipmask calculation */
255 break; /* get out of the loop */
256 } else if (!IsDigit(*mask)) {
257 flags &= ~MASK_IP; /* not an IP anymore! */
262 /* Sanity-check quads */
263 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
268 /* update bit count if necessary */
269 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
272 /* Check to see that it's not too wide of a mask */
273 if (flags & MASK_WILDS &&
274 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
275 (flags & MASK_IP && ipmask < 16)))
276 return CHECK_REJECTED; /* to wide, reject */
278 /* Ok, it's approved; require override if it has wildcards, though */
279 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
283 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
285 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
288 if (gline->gl_lastmod)
289 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
290 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
291 GlineIsBadChan(gline) ? "" : "@",
292 GlineIsBadChan(gline) ? "" : gline->gl_host,
293 gline->gl_expire - CurrentTime, gline->gl_lastmod,
296 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
297 (GlineIsRemActive(gline) ?
298 "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
299 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
300 GlineIsBadChan(gline) ? "" : gline->gl_host,
301 gline->gl_expire - CurrentTime, gline->gl_reason);
307 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
308 char *reason, time_t expire, time_t lastmod, unsigned int flags)
310 struct Gline *agline;
311 char uhmask[USERLEN + HOSTLEN + 2];
315 assert(0 != userhost);
318 /* NO_OLD_GLINE allows *@#channel to work correctly */
319 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
320 # ifndef NO_OLD_GLINE
321 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
322 # endif /* OLD_GLINE */
324 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
325 return send_reply(sptr, ERR_NOPRIVILEGES);
327 flags |= GLINE_BADCHAN;
328 # ifndef NO_OLD_GLINE
329 if (userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+')
332 # endif /* OLD_GLINE */
336 canon_userhost(userhost, &user, &host, "*");
338 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
339 return send_reply(sptr, ERR_LONGMASK);
340 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
341 switch (gline_checkmask(host)) {
342 case CHECK_OVERRIDABLE: /* oper overrided restriction */
343 if (flags & GLINE_OPERFORCE)
347 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
351 if ((tmp = count_users(uhmask)) >=
352 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
353 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
358 * You cannot set a negative (or zero) expire time, nor can you set an
359 * expiration time for greater than GLINE_MAX_EXPIRE.
361 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
362 if (!IsServer(sptr) && MyConnect(sptr))
363 send_reply(sptr, ERR_BADEXPIRE, expire);
367 expire += CurrentTime; /* convert from lifetime to timestamp */
370 sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s%s%s, expiring at "
372 #ifdef HEAD_IN_SAND_SNOTICES
375 IsServer(sptr) ? cli_name(sptr) :
376 cli_name((cli_user(sptr))->server),
378 flags & GLINE_LOCAL ? "local" : "global",
379 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
380 flags & GLINE_BADCHAN ? "" : "@",
381 flags & GLINE_BADCHAN ? "" : host,
382 expire + TSoffset, reason);
385 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
386 "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
387 flags & GLINE_LOCAL ? "local" : "global",
388 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
389 expire + TSoffset, reason);
392 agline = make_gline(user, host, reason, expire, lastmod, flags);
394 if (!agline) /* if it overlapped, silently return */
397 gline_propagate(cptr, sptr, agline);
399 if (GlineIsBadChan(agline))
402 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
406 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
407 time_t lastmod, unsigned int flags)
409 unsigned int saveflags = 0;
413 saveflags = gline->gl_flags;
415 if (flags & GLINE_LOCAL)
416 gline->gl_flags &= ~GLINE_LDEACT;
418 gline->gl_flags |= GLINE_ACTIVE;
420 if (gline->gl_lastmod) {
421 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
424 gline->gl_lastmod = lastmod;
428 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
429 return 0; /* was active to begin with */
431 /* Inform ops and log it */
432 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
433 "expiring at %Tu: %s",
434 #ifdef HEAD_IN_SAND_SNOTICES
437 IsServer(sptr) ? cli_name(sptr) :
438 cli_name((cli_user(sptr))->server),
440 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
441 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
442 GlineIsBadChan(gline) ? "" : gline->gl_host,
443 gline->gl_expire + TSoffset, gline->gl_reason);
445 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
446 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
447 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
448 GlineIsBadChan(gline) ? "" : "@",
449 GlineIsBadChan(gline) ? "" : gline->gl_host,
450 gline->gl_expire + TSoffset, gline->gl_reason);
452 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
453 gline_propagate(cptr, sptr, gline);
455 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
459 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
460 time_t lastmod, unsigned int flags)
462 unsigned int saveflags = 0;
467 saveflags = gline->gl_flags;
469 if (GlineIsLocal(gline))
470 msg = "removing local";
471 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
472 msg = "removing global";
473 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
475 msg = "deactivating global";
477 if (flags & GLINE_LOCAL)
478 gline->gl_flags |= GLINE_LDEACT;
480 gline->gl_flags &= ~GLINE_ACTIVE;
482 if (gline->gl_lastmod) {
483 if (gline->gl_lastmod >= lastmod)
486 gline->gl_lastmod = lastmod;
490 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
491 return 0; /* was inactive to begin with */
494 /* Inform ops and log it */
495 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
497 #ifdef HEAD_IN_SAND_SNOTICES
500 IsServer(sptr) ? cli_name(sptr) :
501 cli_name((cli_user(sptr))->server),
503 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
504 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
505 GlineIsBadChan(gline) ? "" : gline->gl_host,
506 gline->gl_expire + TSoffset, gline->gl_reason);
508 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
509 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
510 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
511 GlineIsBadChan(gline) ? "" : "@",
512 GlineIsBadChan(gline) ? "" : gline->gl_host,
513 gline->gl_expire + TSoffset, gline->gl_reason);
515 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
516 gline_propagate(cptr, sptr, gline);
518 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
519 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
520 gline_free(gline); /* get rid of it */
526 gline_find(char *userhost, unsigned int flags)
529 struct Gline *sgline;
530 char *user, *host, *t_uh;
532 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
533 for (gline = BadChanGlineList; gline; gline = sgline) {
534 sgline = gline->gl_next;
536 if (gline->gl_expire <= CurrentTime)
538 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
539 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
541 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
542 match(gline->gl_user, userhost)) == 0)
547 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
548 *userhost == '#' || *userhost == '&' || *userhost == '+'
550 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
551 #endif /* NO_OLD_GLINE */
555 DupString(t_uh, userhost);
556 canon_userhost(t_uh, &user, &host, 0);
558 for (gline = GlobalGlineList; gline; gline = sgline) {
559 sgline = gline->gl_next;
561 if (gline->gl_expire <= CurrentTime)
563 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
564 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
566 else if (flags & GLINE_EXACT) {
567 if (ircd_strcmp(gline->gl_host, host) == 0 &&
568 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
569 ircd_strcmp(gline->gl_user, user) == 0))
572 if (match(gline->gl_host, host) == 0 &&
573 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
574 match(gline->gl_user, user) == 0))
585 gline_lookup(struct Client *cptr, unsigned int flags)
588 struct Gline *sgline;
590 for (gline = GlobalGlineList; gline; gline = sgline) {
591 sgline = gline->gl_next;
593 if (gline->gl_expire <= CurrentTime) {
598 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
599 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
602 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
605 if (GlineIsIpMask(gline)) {
606 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
607 if (((cli_ip(cptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
611 if (match(gline->gl_host, (cli_user(cptr))->host) != 0)
623 gline_free(struct Gline *gline)
627 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
629 gline->gl_next->gl_prev_p = gline->gl_prev_p;
631 MyFree(gline->gl_user); /* free up the memory */
633 MyFree(gline->gl_host);
634 MyFree(gline->gl_reason);
639 gline_burst(struct Client *cptr)
642 struct Gline *sgline;
644 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
645 sgline = gline->gl_next;
647 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
649 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
650 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
651 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
652 gline->gl_host, gline->gl_expire - CurrentTime,
653 gline->gl_lastmod, gline->gl_reason);
656 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
657 sgline = gline->gl_next;
659 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
661 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
662 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
663 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
664 gline->gl_expire - CurrentTime, gline->gl_lastmod,
670 gline_resend(struct Client *cptr, struct Gline *gline)
672 if (GlineIsLocal(gline) || !gline->gl_lastmod)
675 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
676 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
677 GlineIsBadChan(gline) ? "" : "@",
678 GlineIsBadChan(gline) ? "" : gline->gl_host,
679 gline->gl_expire - CurrentTime, gline->gl_lastmod,
686 gline_list(struct Client *sptr, char *userhost)
689 struct Gline *sgline;
692 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
693 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
695 /* send gline information along */
696 send_reply(sptr, RPL_GLIST, gline->gl_user,
697 GlineIsBadChan(gline) ? "" : "@",
698 GlineIsBadChan(gline) ? "" : gline->gl_host,
699 gline->gl_expire + TSoffset,
700 GlineIsLocal(gline) ? cli_name(&me) : "*",
701 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
703 for (gline = GlobalGlineList; gline; gline = sgline) {
704 sgline = gline->gl_next;
706 if (gline->gl_expire <= CurrentTime)
709 send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
710 gline->gl_expire + TSoffset,
711 GlineIsLocal(gline) ? cli_name(&me) : "*",
712 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
715 for (gline = BadChanGlineList; gline; gline = sgline) {
716 sgline = gline->gl_next;
718 if (gline->gl_expire <= CurrentTime)
721 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
722 gline->gl_expire + TSoffset,
723 GlineIsLocal(gline) ? cli_name(&me) : "*",
724 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
728 /* end of gline information */
729 return send_reply(sptr, RPL_ENDOFGLIST);
733 gline_stats(struct Client *sptr)
736 struct Gline *sgline;
738 for (gline = GlobalGlineList; gline; gline = sgline) {
739 sgline = gline->gl_next;
741 if (gline->gl_expire <= CurrentTime)
744 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
745 gline->gl_expire + TSoffset, gline->gl_reason);