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_reply.h"
30 #include "ircd_string.h"
42 #include "sys.h" /* FALSE bleah */
47 #include <arpa/inet.h> /* for inet_ntoa */
49 struct Gline* GlobalGlineList = 0;
50 struct Gline* BadChanGlineList = 0;
53 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
57 if (!(tmp = strchr(userhost, '@'))) {
68 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
71 struct Gline *gline, *sgline, *after = 0;
74 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
75 canon_userhost(userhost, &user, &host, "*"); /* find user and host */
77 for (gline = GlobalGlineList; gline; gline = sgline) {
78 sgline = gline->gl_next;
80 if (gline->gl_expire <= CurrentTime)
82 else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
84 else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
85 !mmatch(gline->gl_host, host)) {
86 if (expire <= gline->gl_expire) /* will expire before wider gline */
89 after = gline; /* stick new gline after this one */
90 } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
91 !mmatch(host, gline->gl_host) &&
92 gline->gl_expire <= expire) /* gline expires before new one */
93 gline_free(gline); /* save some memory */
97 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
100 DupString(gline->gl_reason, reason); /* initialize gline... */
101 gline->gl_expire = expire;
102 gline->gl_lastmod = lastmod;
103 gline->gl_flags = flags & GLINE_MASK;
105 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
106 DupString(gline->gl_user, userhost); /* first, remember channel */
109 gline->gl_next = BadChanGlineList; /* then link it into list */
110 gline->gl_prev_p = &BadChanGlineList;
111 if (BadChanGlineList)
112 BadChanGlineList->gl_prev_p = &gline->gl_next;
113 BadChanGlineList = gline;
115 DupString(gline->gl_user, user); /* remember them... */
116 DupString(gline->gl_host, host);
118 if (check_if_ipmask(host)) { /* mark if it's an IP mask */
124 class = sscanf(host,"%d.%d.%d.%d/%d",
125 &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
132 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
133 gline->ipnum.s_addr = inet_addr(ipname);
134 Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
135 gline->gl_flags |= GLINE_IPMASK;
139 gline->gl_next = after->gl_next;
140 gline->gl_prev_p = &after->gl_next;
142 after->gl_next->gl_prev_p = &gline->gl_next;
143 after->gl_next = gline;
145 gline->gl_next = GlobalGlineList; /* then link it into list */
146 gline->gl_prev_p = &GlobalGlineList;
148 GlobalGlineList->gl_prev_p = &gline->gl_next;
149 GlobalGlineList = gline;
157 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
159 struct Client *acptr;
160 int fd, retval = 0, tval;
162 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
165 for (fd = HighestFd; fd >= 0; --fd) {
169 if ((acptr = LocalClientArray[fd])) {
170 if (!cli_user(acptr))
173 if (cli_user(acptr)->username &&
174 match (gline->gl_user, (cli_user(acptr))->username) != 0)
177 if (GlineIsIpMask(gline)) {
178 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
179 if (((cli_ip(acptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
183 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
187 /* ok, here's one that got G-lined */
188 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
191 /* let the ops know about it */
192 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
193 get_client_name(acptr, TRUE));
195 /* and get rid of him */
196 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
198 retval = tval; /* retain killed status */
205 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
207 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
210 if (gline->gl_lastmod)
211 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
212 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
213 GlineIsBadChan(gline) ? "" : "@",
214 GlineIsBadChan(gline) ? "" : gline->gl_host,
215 gline->gl_expire - CurrentTime, gline->gl_lastmod,
218 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
219 (GlineIsRemActive(gline) ?
220 "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
221 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
222 GlineIsBadChan(gline) ? "" : gline->gl_host,
223 gline->gl_expire - CurrentTime, gline->gl_reason);
229 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
230 char *reason, time_t expire, time_t lastmod, unsigned int flags)
232 struct Gline *agline;
234 assert(0 != userhost);
238 * You cannot set a negative (or zero) expire time, nor can you set an
239 * expiration time for greater than GLINE_MAX_EXPIRE.
241 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
242 if (!IsServer(sptr) && MyConnect(sptr))
243 send_reply(sptr, ERR_BADEXPIRE, expire);
247 expire += CurrentTime; /* convert from lifetime to timestamp */
249 /* NO_OLD_GLINE allows *@#channel to work correctly */
250 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
251 # ifndef NO_OLD_GLINE
252 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
253 # endif /* OLD_GLINE */
255 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
256 return send_reply(sptr, ERR_NOPRIVILEGES);
258 flags |= GLINE_BADCHAN;
262 sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
263 "%Tu: %s", IsServer(sptr) ? cli_name(sptr) :
264 cli_name((cli_user(sptr))->server),
265 flags & GLINE_LOCAL ? "local" : "global",
266 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
267 expire + TSoffset, reason);
270 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
271 "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
272 flags & GLINE_LOCAL ? "local" : "global",
273 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
274 expire + TSoffset, reason);
277 agline = make_gline(userhost, reason, expire, lastmod, flags);
279 if (!agline) /* if it overlapped, silently return */
282 gline_propagate(cptr, sptr, agline);
284 if (GlineIsBadChan(agline))
287 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
291 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
292 time_t lastmod, unsigned int flags)
294 unsigned int saveflags = 0;
298 saveflags = gline->gl_flags;
300 if (flags & GLINE_LOCAL)
301 gline->gl_flags &= ~GLINE_LDEACT;
303 gline->gl_flags |= GLINE_ACTIVE;
305 if (gline->gl_lastmod) {
306 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
309 gline->gl_lastmod = lastmod;
313 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
314 return 0; /* was active to begin with */
316 /* Inform ops and log it */
317 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
318 "expiring at %Tu: %s", IsServer(sptr) ? cli_name(sptr) :
319 cli_name((cli_user(sptr))->server),
320 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
321 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
322 GlineIsBadChan(gline) ? "" : gline->gl_host,
323 gline->gl_expire + TSoffset, gline->gl_reason);
325 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
326 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
327 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
328 GlineIsBadChan(gline) ? "" : "@",
329 GlineIsBadChan(gline) ? "" : gline->gl_host,
330 gline->gl_expire + TSoffset, gline->gl_reason);
332 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
333 gline_propagate(cptr, sptr, gline);
335 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
339 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
340 time_t lastmod, unsigned int flags)
342 unsigned int saveflags = 0;
347 saveflags = gline->gl_flags;
349 if (GlineIsLocal(gline))
350 msg = "removing local";
351 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
352 msg = "removing global";
353 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
355 msg = "deactivating global";
357 if (flags & GLINE_LOCAL)
358 gline->gl_flags |= GLINE_LDEACT;
360 gline->gl_flags &= ~GLINE_ACTIVE;
362 if (gline->gl_lastmod) {
363 if (gline->gl_lastmod >= lastmod)
366 gline->gl_lastmod = lastmod;
370 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
371 return 0; /* was inactive to begin with */
374 /* Inform ops and log it */
375 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
376 "%s", IsServer(sptr) ? cli_name(sptr) :
377 cli_name((cli_user(sptr))->server),
378 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
379 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
380 GlineIsBadChan(gline) ? "" : gline->gl_host,
381 gline->gl_expire + TSoffset, gline->gl_reason);
383 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
384 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
385 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
386 GlineIsBadChan(gline) ? "" : "@",
387 GlineIsBadChan(gline) ? "" : gline->gl_host,
388 gline->gl_expire + TSoffset, gline->gl_reason);
390 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
391 gline_propagate(cptr, sptr, gline);
393 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
394 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
395 gline_free(gline); /* get rid of it */
401 gline_find(char *userhost, unsigned int flags)
404 struct Gline *sgline;
405 char *user, *host, *t_uh;
407 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
408 for (gline = BadChanGlineList; gline; gline = sgline) {
409 sgline = gline->gl_next;
411 if (gline->gl_expire <= CurrentTime)
413 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
414 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
416 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
417 match(gline->gl_user, userhost)) == 0)
422 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
423 *userhost == '#' || *userhost == '&' || *userhost == '+'
425 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
426 #endif /* NO_OLD_GLINE */
430 DupString(t_uh, userhost);
431 canon_userhost(t_uh, &user, &host, 0);
433 for (gline = GlobalGlineList; gline; gline = sgline) {
434 sgline = gline->gl_next;
436 if (gline->gl_expire <= CurrentTime)
438 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
439 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
441 else if (flags & GLINE_EXACT) {
442 if (ircd_strcmp(gline->gl_host, host) == 0 &&
443 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
444 ircd_strcmp(gline->gl_user, user) == 0))
447 if (match(gline->gl_host, host) == 0 &&
448 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
449 match(gline->gl_user, user) == 0))
460 gline_lookup(struct Client *cptr, unsigned int flags)
463 struct Gline *sgline;
465 for (gline = GlobalGlineList; gline; gline = sgline) {
466 sgline = gline->gl_next;
468 if (gline->gl_expire <= CurrentTime) {
473 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
474 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
477 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
480 if (GlineIsIpMask(gline)) {
481 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
482 if (((cli_ip(cptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
486 if (match(gline->gl_host, (cli_user(cptr))->host) != 0)
498 gline_free(struct Gline *gline)
502 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
504 gline->gl_next->gl_prev_p = gline->gl_prev_p;
506 MyFree(gline->gl_user); /* free up the memory */
508 MyFree(gline->gl_host);
509 MyFree(gline->gl_reason);
514 gline_burst(struct Client *cptr)
517 struct Gline *sgline;
519 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
520 sgline = gline->gl_next;
522 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
524 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
525 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
526 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
527 gline->gl_host, gline->gl_expire - CurrentTime,
528 gline->gl_lastmod, gline->gl_reason);
531 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
532 sgline = gline->gl_next;
534 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
536 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
537 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
538 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
539 gline->gl_expire - CurrentTime, gline->gl_lastmod,
545 gline_resend(struct Client *cptr, struct Gline *gline)
547 if (GlineIsLocal(gline) || !gline->gl_lastmod)
550 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
551 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
552 GlineIsBadChan(gline) ? "" : "@",
553 GlineIsBadChan(gline) ? "" : gline->gl_host,
554 gline->gl_expire - CurrentTime, gline->gl_lastmod,
561 gline_list(struct Client *sptr, char *userhost)
564 struct Gline *sgline;
567 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
568 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
570 /* send gline information along */
571 send_reply(sptr, RPL_GLIST, gline->gl_user,
572 GlineIsBadChan(gline) ? "" : "@",
573 GlineIsBadChan(gline) ? "" : gline->gl_host,
574 gline->gl_expire + TSoffset,
575 GlineIsLocal(gline) ? cli_name(&me) : "*",
576 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
578 for (gline = GlobalGlineList; gline; gline = sgline) {
579 sgline = gline->gl_next;
581 if (gline->gl_expire <= CurrentTime)
584 send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
585 gline->gl_expire + TSoffset,
586 GlineIsLocal(gline) ? cli_name(&me) : "*",
587 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
590 for (gline = BadChanGlineList; gline; gline = sgline) {
591 sgline = gline->gl_next;
593 if (gline->gl_expire <= CurrentTime)
596 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
597 gline->gl_expire + TSoffset,
598 GlineIsLocal(gline) ? cli_name(&me) : "*",
599 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
603 /* end of gline information */
604 return send_reply(sptr, RPL_ENDOFGLIST);
608 gline_stats(struct Client *sptr)
611 struct Gline *sgline;
613 for (gline = GlobalGlineList; gline; gline = sgline) {
614 sgline = gline->gl_next;
616 if (gline->gl_expire <= CurrentTime)
619 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
620 gline->gl_expire + TSoffset, gline->gl_reason);