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"
29 #include "ircd_policy.h"
30 #include "ircd_reply.h"
31 #include "ircd_string.h"
43 #include "sys.h" /* FALSE bleah */
48 #include <arpa/inet.h> /* for inet_ntoa */
50 struct Gline* GlobalGlineList = 0;
51 struct Gline* BadChanGlineList = 0;
54 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
58 if (!(tmp = strchr(userhost, '@'))) {
69 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
72 struct Gline *gline, *sgline, *after = 0;
75 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
76 canon_userhost(userhost, &user, &host, "*"); /* find user and host */
78 for (gline = GlobalGlineList; gline; gline = sgline) {
79 sgline = gline->gl_next;
81 if (gline->gl_expire <= CurrentTime)
83 else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
85 else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
86 !mmatch(gline->gl_host, host)) {
87 if (expire <= gline->gl_expire) /* will expire before wider gline */
90 after = gline; /* stick new gline after this one */
91 } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
92 !mmatch(host, gline->gl_host) &&
93 gline->gl_expire <= expire) /* gline expires before new one */
94 gline_free(gline); /* save some memory */
98 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
101 DupString(gline->gl_reason, reason); /* initialize gline... */
102 gline->gl_expire = expire;
103 gline->gl_lastmod = lastmod;
104 gline->gl_flags = flags & GLINE_MASK;
106 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
107 DupString(gline->gl_user, userhost); /* first, remember channel */
110 gline->gl_next = BadChanGlineList; /* then link it into list */
111 gline->gl_prev_p = &BadChanGlineList;
112 if (BadChanGlineList)
113 BadChanGlineList->gl_prev_p = &gline->gl_next;
114 BadChanGlineList = gline;
116 DupString(gline->gl_user, user); /* remember them... */
117 DupString(gline->gl_host, host);
119 if (check_if_ipmask(host)) { /* mark if it's an IP mask */
125 class = sscanf(host,"%d.%d.%d.%d/%d",
126 &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
133 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
134 gline->ipnum.s_addr = inet_addr(ipname);
135 Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
136 gline->gl_flags |= GLINE_IPMASK;
140 gline->gl_next = after->gl_next;
141 gline->gl_prev_p = &after->gl_next;
143 after->gl_next->gl_prev_p = &gline->gl_next;
144 after->gl_next = gline;
146 gline->gl_next = GlobalGlineList; /* then link it into list */
147 gline->gl_prev_p = &GlobalGlineList;
149 GlobalGlineList->gl_prev_p = &gline->gl_next;
150 GlobalGlineList = gline;
158 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
160 struct Client *acptr;
161 int fd, retval = 0, tval;
163 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
166 for (fd = HighestFd; fd >= 0; --fd) {
170 if ((acptr = LocalClientArray[fd])) {
171 if (!cli_user(acptr))
174 if (cli_user(acptr)->username &&
175 match (gline->gl_user, (cli_user(acptr))->username) != 0)
178 if (GlineIsIpMask(gline)) {
179 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
180 if (((cli_ip(acptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
184 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
188 /* ok, here's one that got G-lined */
189 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
192 /* let the ops know about it */
193 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
194 get_client_name(acptr, TRUE));
196 /* and get rid of him */
197 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
199 retval = tval; /* retain killed status */
206 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
208 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
211 if (gline->gl_lastmod)
212 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
213 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
214 GlineIsBadChan(gline) ? "" : "@",
215 GlineIsBadChan(gline) ? "" : gline->gl_host,
216 gline->gl_expire - CurrentTime, gline->gl_lastmod,
219 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
220 (GlineIsRemActive(gline) ?
221 "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
222 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
223 GlineIsBadChan(gline) ? "" : gline->gl_host,
224 gline->gl_expire - CurrentTime, gline->gl_reason);
230 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
231 char *reason, time_t expire, time_t lastmod, unsigned int flags)
233 struct Gline *agline;
235 assert(0 != userhost);
239 * You cannot set a negative (or zero) expire time, nor can you set an
240 * expiration time for greater than GLINE_MAX_EXPIRE.
242 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
243 if (!IsServer(sptr) && MyConnect(sptr))
244 send_reply(sptr, ERR_BADEXPIRE, expire);
248 expire += CurrentTime; /* convert from lifetime to timestamp */
250 /* NO_OLD_GLINE allows *@#channel to work correctly */
251 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
252 # ifndef NO_OLD_GLINE
253 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
254 # endif /* OLD_GLINE */
256 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
257 return send_reply(sptr, ERR_NOPRIVILEGES);
259 flags |= GLINE_BADCHAN;
263 sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
265 #ifdef HEAD_IN_SAND_SNOTICES
268 IsServer(sptr) ? cli_name(sptr) :
269 cli_name((cli_user(sptr))->server),
271 flags & GLINE_LOCAL ? "local" : "global",
272 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
273 expire + TSoffset, reason);
276 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
277 "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
278 flags & GLINE_LOCAL ? "local" : "global",
279 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
280 expire + TSoffset, reason);
283 agline = make_gline(userhost, reason, expire, lastmod, flags);
285 if (!agline) /* if it overlapped, silently return */
288 gline_propagate(cptr, sptr, agline);
290 if (GlineIsBadChan(agline))
293 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
297 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
298 time_t lastmod, unsigned int flags)
300 unsigned int saveflags = 0;
304 saveflags = gline->gl_flags;
306 if (flags & GLINE_LOCAL)
307 gline->gl_flags &= ~GLINE_LDEACT;
309 gline->gl_flags |= GLINE_ACTIVE;
311 if (gline->gl_lastmod) {
312 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
315 gline->gl_lastmod = lastmod;
319 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
320 return 0; /* was active to begin with */
322 /* Inform ops and log it */
323 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
324 "expiring at %Tu: %s",
325 #ifdef HEAD_IN_SAND_SNOTICES
328 IsServer(sptr) ? cli_name(sptr) :
329 cli_name((cli_user(sptr))->server),
331 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
332 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
333 GlineIsBadChan(gline) ? "" : gline->gl_host,
334 gline->gl_expire + TSoffset, gline->gl_reason);
336 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
337 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
338 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
339 GlineIsBadChan(gline) ? "" : "@",
340 GlineIsBadChan(gline) ? "" : gline->gl_host,
341 gline->gl_expire + TSoffset, gline->gl_reason);
343 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
344 gline_propagate(cptr, sptr, gline);
346 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
350 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
351 time_t lastmod, unsigned int flags)
353 unsigned int saveflags = 0;
358 saveflags = gline->gl_flags;
360 if (GlineIsLocal(gline))
361 msg = "removing local";
362 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
363 msg = "removing global";
364 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
366 msg = "deactivating global";
368 if (flags & GLINE_LOCAL)
369 gline->gl_flags |= GLINE_LDEACT;
371 gline->gl_flags &= ~GLINE_ACTIVE;
373 if (gline->gl_lastmod) {
374 if (gline->gl_lastmod >= lastmod)
377 gline->gl_lastmod = lastmod;
381 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
382 return 0; /* was inactive to begin with */
385 /* Inform ops and log it */
386 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
388 #ifdef HEAD_IN_SAND_SNOTICES
391 IsServer(sptr) ? cli_name(sptr) :
392 cli_name((cli_user(sptr))->server),
394 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
395 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
396 GlineIsBadChan(gline) ? "" : gline->gl_host,
397 gline->gl_expire + TSoffset, gline->gl_reason);
399 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
400 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
401 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
402 GlineIsBadChan(gline) ? "" : "@",
403 GlineIsBadChan(gline) ? "" : gline->gl_host,
404 gline->gl_expire + TSoffset, gline->gl_reason);
406 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
407 gline_propagate(cptr, sptr, gline);
409 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
410 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
411 gline_free(gline); /* get rid of it */
417 gline_find(char *userhost, unsigned int flags)
420 struct Gline *sgline;
421 char *user, *host, *t_uh;
423 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
424 for (gline = BadChanGlineList; gline; gline = sgline) {
425 sgline = gline->gl_next;
427 if (gline->gl_expire <= CurrentTime)
429 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
430 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
432 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
433 match(gline->gl_user, userhost)) == 0)
438 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
439 *userhost == '#' || *userhost == '&' || *userhost == '+'
441 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
442 #endif /* NO_OLD_GLINE */
446 DupString(t_uh, userhost);
447 canon_userhost(t_uh, &user, &host, 0);
449 for (gline = GlobalGlineList; gline; gline = sgline) {
450 sgline = gline->gl_next;
452 if (gline->gl_expire <= CurrentTime)
454 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
455 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
457 else if (flags & GLINE_EXACT) {
458 if (ircd_strcmp(gline->gl_host, host) == 0 &&
459 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
460 ircd_strcmp(gline->gl_user, user) == 0))
463 if (match(gline->gl_host, host) == 0 &&
464 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
465 match(gline->gl_user, user) == 0))
476 gline_lookup(struct Client *cptr, unsigned int flags)
479 struct Gline *sgline;
481 for (gline = GlobalGlineList; gline; gline = sgline) {
482 sgline = gline->gl_next;
484 if (gline->gl_expire <= CurrentTime) {
489 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
490 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
493 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
496 if (GlineIsIpMask(gline)) {
497 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
498 if (((cli_ip(cptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
502 if (match(gline->gl_host, (cli_user(cptr))->host) != 0)
514 gline_free(struct Gline *gline)
518 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
520 gline->gl_next->gl_prev_p = gline->gl_prev_p;
522 MyFree(gline->gl_user); /* free up the memory */
524 MyFree(gline->gl_host);
525 MyFree(gline->gl_reason);
530 gline_burst(struct Client *cptr)
533 struct Gline *sgline;
535 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
536 sgline = gline->gl_next;
538 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
540 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
541 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
542 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
543 gline->gl_host, gline->gl_expire - CurrentTime,
544 gline->gl_lastmod, gline->gl_reason);
547 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
548 sgline = gline->gl_next;
550 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
552 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
553 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
554 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
555 gline->gl_expire - CurrentTime, gline->gl_lastmod,
561 gline_resend(struct Client *cptr, struct Gline *gline)
563 if (GlineIsLocal(gline) || !gline->gl_lastmod)
566 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
567 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
568 GlineIsBadChan(gline) ? "" : "@",
569 GlineIsBadChan(gline) ? "" : gline->gl_host,
570 gline->gl_expire - CurrentTime, gline->gl_lastmod,
577 gline_list(struct Client *sptr, char *userhost)
580 struct Gline *sgline;
583 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
584 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
586 /* send gline information along */
587 send_reply(sptr, RPL_GLIST, gline->gl_user,
588 GlineIsBadChan(gline) ? "" : "@",
589 GlineIsBadChan(gline) ? "" : gline->gl_host,
590 gline->gl_expire + TSoffset,
591 GlineIsLocal(gline) ? cli_name(&me) : "*",
592 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
594 for (gline = GlobalGlineList; gline; gline = sgline) {
595 sgline = gline->gl_next;
597 if (gline->gl_expire <= CurrentTime)
600 send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
601 gline->gl_expire + TSoffset,
602 GlineIsLocal(gline) ? cli_name(&me) : "*",
603 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
606 for (gline = BadChanGlineList; gline; gline = sgline) {
607 sgline = gline->gl_next;
609 if (gline->gl_expire <= CurrentTime)
612 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
613 gline->gl_expire + TSoffset,
614 GlineIsLocal(gline) ? cli_name(&me) : "*",
615 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
619 /* end of gline information */
620 return send_reply(sptr, RPL_ENDOFGLIST);
624 gline_stats(struct Client *sptr)
627 struct Gline *sgline;
629 for (gline = GlobalGlineList; gline; gline = sgline) {
630 sgline = gline->gl_next;
632 if (gline->gl_expire <= CurrentTime)
635 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
636 gline->gl_expire + TSoffset, gline->gl_reason);