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.
25 #include "ircd_alloc.h"
27 #include "ircd_reply.h"
28 #include "ircd_string.h"
40 #include "sys.h" /* FALSE bleah */
45 #include <arpa/inet.h> /* for inet_ntoa */
47 struct Gline* GlobalGlineList = 0;
48 struct Gline* BadChanGlineList = 0;
51 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
55 if (!(tmp = strchr(userhost, '@'))) {
66 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
69 struct Gline *gline, *sgline, *after = 0;
72 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
73 canon_userhost(userhost, &user, &host, "*"); /* find user and host */
75 for (gline = GlobalGlineList; gline; gline = sgline) {
76 sgline = gline->gl_next;
78 if (gline->gl_expire <= CurrentTime)
80 else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
82 else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
83 !mmatch(gline->gl_host, host)) {
84 if (expire <= gline->gl_expire) /* will expire before wider gline */
87 after = gline; /* stick new gline after this one */
88 } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
89 !mmatch(host, gline->gl_host) &&
90 gline->gl_expire <= expire) /* gline expires before new one */
91 gline_free(gline); /* save some memory */
95 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
98 DupString(gline->gl_reason, reason); /* initialize gline... */
99 gline->gl_expire = expire;
100 gline->gl_lastmod = lastmod;
101 gline->gl_flags = flags & GLINE_MASK;
103 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
104 DupString(gline->gl_user, userhost); /* first, remember channel */
107 gline->gl_next = BadChanGlineList; /* then link it into list */
108 gline->gl_prev_p = &BadChanGlineList;
109 if (BadChanGlineList)
110 BadChanGlineList->gl_prev_p = &gline->gl_next;
111 BadChanGlineList = gline;
113 DupString(gline->gl_user, user); /* remember them... */
114 DupString(gline->gl_host, host);
116 if (check_if_ipmask(host)) { /* mark if it's an IP mask */
122 class = sscanf(host,"%d.%d.%d.%d/%d",
123 &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
130 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
131 gline->ipnum.s_addr = inet_addr(ipname);
132 Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
133 gline->gl_flags |= GLINE_IPMASK;
137 gline->gl_next = after->gl_next;
138 gline->gl_prev_p = &after->gl_next;
140 after->gl_next->gl_prev_p = &gline->gl_next;
141 after->gl_next = gline;
143 gline->gl_next = GlobalGlineList; /* then link it into list */
144 gline->gl_prev_p = &GlobalGlineList;
146 GlobalGlineList->gl_prev_p = &gline->gl_next;
147 GlobalGlineList = gline;
155 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
157 struct Client *acptr;
158 int fd, retval = 0, tval;
160 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
163 for (fd = HighestFd; fd >= 0; --fd) {
167 if ((acptr = LocalClientArray[fd])) {
168 if (!cli_user(acptr))
171 if (cli_user(acptr)->username &&
172 match (gline->gl_user, (cli_user(acptr))->username) != 0)
175 if (GlineIsIpMask(gline)) {
176 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
177 if (((cli_ip(acptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
181 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
185 /* ok, here's one that got G-lined */
186 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
189 /* let the ops know about it */
190 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
191 get_client_name(acptr, FALSE));
193 /* and get rid of him */
194 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
196 retval = tval; /* retain killed status */
203 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
205 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
208 if (gline->gl_lastmod)
209 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
210 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
211 GlineIsBadChan(gline) ? "" : "@",
212 GlineIsBadChan(gline) ? "" : gline->gl_host,
213 gline->gl_expire - CurrentTime, gline->gl_lastmod,
216 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
217 (GlineIsRemActive(gline) ?
218 "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
219 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
220 GlineIsBadChan(gline) ? "" : gline->gl_host,
221 gline->gl_expire - CurrentTime, gline->gl_reason);
227 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
228 char *reason, time_t expire, time_t lastmod, unsigned int flags)
230 struct Gline *agline;
232 assert(0 != userhost);
236 * You cannot set a negative (or zero) expire time, nor can you set an
237 * expiration time for greater than GLINE_MAX_EXPIRE.
239 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
240 if (!IsServer(sptr) && MyConnect(sptr))
241 send_reply(sptr, ERR_BADEXPIRE, expire);
245 expire += CurrentTime; /* convert from lifetime to timestamp */
247 /* NO_OLD_GLINE allows *@#channel to work correctly */
248 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
249 # ifndef NO_OLD_GLINE
250 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
251 # endif /* OLD_GLINE */
253 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
254 return send_reply(sptr, ERR_NOPRIVILEGES);
256 flags |= GLINE_BADCHAN;
260 sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
261 "%Tu: %s", IsServer(sptr) ? cli_name(sptr) :
262 cli_name((cli_user(sptr))->server),
263 flags & GLINE_LOCAL ? "local" : "global",
264 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
265 expire + TSoffset, reason);
268 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
269 "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
270 flags & GLINE_LOCAL ? "local" : "global",
271 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
272 expire + TSoffset, reason);
275 agline = make_gline(userhost, reason, expire, lastmod, flags);
277 if (!agline) /* if it overlapped, silently return */
280 gline_propagate(cptr, sptr, agline);
282 if (GlineIsBadChan(agline))
285 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
289 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
290 time_t lastmod, unsigned int flags)
292 unsigned int saveflags = 0;
296 saveflags = gline->gl_flags;
298 if (flags & GLINE_LOCAL)
299 gline->gl_flags &= ~GLINE_LDEACT;
301 gline->gl_flags |= GLINE_ACTIVE;
303 if (gline->gl_lastmod) {
304 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
307 gline->gl_lastmod = lastmod;
311 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
312 return 0; /* was active to begin with */
314 /* Inform ops and log it */
315 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
316 "expiring at %Tu: %s", IsServer(sptr) ? cli_name(sptr) :
317 cli_name((cli_user(sptr))->server),
318 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
319 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
320 GlineIsBadChan(gline) ? "" : gline->gl_host,
321 gline->gl_expire + TSoffset, gline->gl_reason);
323 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
324 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
325 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
326 GlineIsBadChan(gline) ? "" : "@",
327 GlineIsBadChan(gline) ? "" : gline->gl_host,
328 gline->gl_expire + TSoffset, gline->gl_reason);
330 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
331 gline_propagate(cptr, sptr, gline);
333 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
337 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
338 time_t lastmod, unsigned int flags)
340 unsigned int saveflags = 0;
345 saveflags = gline->gl_flags;
347 if (GlineIsLocal(gline))
348 msg = "removing local";
349 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
350 msg = "removing global";
351 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
353 msg = "deactivating global";
355 if (flags & GLINE_LOCAL)
356 gline->gl_flags |= GLINE_LDEACT;
358 gline->gl_flags &= ~GLINE_ACTIVE;
360 if (gline->gl_lastmod) {
361 if (gline->gl_lastmod >= lastmod)
364 gline->gl_lastmod = lastmod;
368 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
369 return 0; /* was inactive to begin with */
372 /* Inform ops and log it */
373 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
374 "%s", IsServer(sptr) ? cli_name(sptr) :
375 cli_name((cli_user(sptr))->server),
376 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
377 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
378 GlineIsBadChan(gline) ? "" : gline->gl_host,
379 gline->gl_expire + TSoffset, gline->gl_reason);
381 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
382 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
383 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
384 GlineIsBadChan(gline) ? "" : "@",
385 GlineIsBadChan(gline) ? "" : gline->gl_host,
386 gline->gl_expire + TSoffset, gline->gl_reason);
388 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
389 gline_propagate(cptr, sptr, gline);
391 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
392 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
393 gline_free(gline); /* get rid of it */
399 gline_find(char *userhost, unsigned int flags)
402 struct Gline *sgline;
403 char *user, *host, *t_uh;
405 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
406 for (gline = BadChanGlineList; gline; gline = sgline) {
407 sgline = gline->gl_next;
409 if (gline->gl_expire <= CurrentTime)
411 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
412 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
414 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
415 match(gline->gl_user, userhost)) == 0)
420 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
421 *userhost == '#' || *userhost == '&' || *userhost == '+'
423 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
424 #endif /* NO_OLD_GLINE */
428 DupString(t_uh, userhost);
429 canon_userhost(t_uh, &user, &host, 0);
431 for (gline = GlobalGlineList; gline; gline = sgline) {
432 sgline = gline->gl_next;
434 if (gline->gl_expire <= CurrentTime)
436 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
437 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
439 else if (flags & GLINE_EXACT) {
440 if (ircd_strcmp(gline->gl_host, host) == 0 &&
441 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
442 ircd_strcmp(gline->gl_user, user) == 0))
445 if (match(gline->gl_host, host) == 0 &&
446 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
447 match(gline->gl_user, user) == 0))
458 gline_lookup(struct Client *cptr, unsigned int flags)
461 struct Gline *sgline;
463 for (gline = GlobalGlineList; gline; gline = sgline) {
464 sgline = gline->gl_next;
466 if (gline->gl_expire <= CurrentTime) {
471 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
472 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
475 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
478 if (GlineIsIpMask(gline)) {
479 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
480 if (((cli_ip(cptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
484 if (match(gline->gl_host, (cli_user(cptr))->host) != 0)
496 gline_free(struct Gline *gline)
500 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
502 gline->gl_next->gl_prev_p = gline->gl_prev_p;
504 MyFree(gline->gl_user); /* free up the memory */
506 MyFree(gline->gl_host);
507 MyFree(gline->gl_reason);
512 gline_burst(struct Client *cptr)
515 struct Gline *sgline;
517 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
518 sgline = gline->gl_next;
520 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
522 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
523 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
524 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
525 gline->gl_host, gline->gl_expire - CurrentTime,
526 gline->gl_lastmod, gline->gl_reason);
529 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
530 sgline = gline->gl_next;
532 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
534 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
535 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
536 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
537 gline->gl_expire - CurrentTime, gline->gl_lastmod,
543 gline_resend(struct Client *cptr, struct Gline *gline)
545 if (GlineIsLocal(gline) || !gline->gl_lastmod)
548 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
549 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
550 GlineIsBadChan(gline) ? "" : "@",
551 GlineIsBadChan(gline) ? "" : gline->gl_host,
552 gline->gl_expire - CurrentTime, gline->gl_lastmod,
559 gline_list(struct Client *sptr, char *userhost)
562 struct Gline *sgline;
565 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
566 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
568 /* send gline information along */
569 send_reply(sptr, RPL_GLIST, gline->gl_user,
570 GlineIsBadChan(gline) ? "" : "@",
571 GlineIsBadChan(gline) ? "" : gline->gl_host,
572 gline->gl_expire + TSoffset,
573 GlineIsLocal(gline) ? cli_name(&me) : "*",
574 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
576 for (gline = GlobalGlineList; gline; gline = sgline) {
577 sgline = gline->gl_next;
579 if (gline->gl_expire <= CurrentTime)
582 send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
583 gline->gl_expire + TSoffset,
584 GlineIsLocal(gline) ? cli_name(&me) : "*",
585 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
588 for (gline = BadChanGlineList; gline; gline = sgline) {
589 sgline = gline->gl_next;
591 if (gline->gl_expire <= CurrentTime)
594 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
595 gline->gl_expire + TSoffset,
596 GlineIsLocal(gline) ? cli_name(&me) : "*",
597 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
601 /* end of gline information */
602 return send_reply(sptr, RPL_ENDOFGLIST);
606 gline_stats(struct Client *sptr)
609 struct Gline *sgline;
611 for (gline = GlobalGlineList; gline; gline = sgline) {
612 sgline = gline->gl_next;
614 if (gline->gl_expire <= CurrentTime)
617 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
618 gline->gl_expire + TSoffset, gline->gl_reason);