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])) {
171 if (acptr->user->username &&
172 match (gline->gl_user, acptr->user->username) != 0)
175 if (GlineIsIpMask(gline)) {
176 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
177 if ((acptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
181 if (match(gline->gl_host, acptr->sockhost) != 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 propagate_gline(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, "* %c%s%s%s %Tu :%s",
217 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
218 GlineIsBadChan(gline) ? "" : "@",
219 GlineIsBadChan(gline) ? "" : gline->gl_host,
220 gline->gl_expire - CurrentTime, gline->gl_reason);
224 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
225 char *reason, time_t expire, time_t lastmod, unsigned int flags)
227 struct Gline *agline;
229 assert(0 != userhost);
233 * You cannot set a negative (or zero) expire time, nor can you set an
234 * expiration time for greater than GLINE_MAX_EXPIRE.
236 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
237 if (!IsServer(sptr) && MyConnect(sptr))
238 send_reply(sptr, ERR_BADEXPIRE, expire);
242 expire += CurrentTime; /* convert from lifetime to timestamp */
244 /* NO_OLD_GLINE allows *@#channel to work correctly */
245 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
246 # ifndef NO_OLD_GLINE
247 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
248 # endif /* OLD_GLINE */
250 # ifndef LOCAL_BADCHAN
251 if (flags & GLINE_LOCAL)
254 flags |= GLINE_BADCHAN;
258 sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
260 IsServer(sptr) ? sptr->name : sptr->user->server->name,
261 flags & GLINE_LOCAL ? "local" : "global",
262 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
263 expire + TSoffset, reason);
266 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
267 "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
268 flags & GLINE_LOCAL ? "local" : "global",
269 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
270 expire + TSoffset, reason);
273 agline = make_gline(userhost, reason, expire, lastmod, flags);
275 if (!agline) /* if it overlapped, silently return */
278 propagate_gline(cptr, sptr, agline);
280 if (GlineIsBadChan(agline))
283 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
287 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
288 time_t lastmod, unsigned int flags)
290 unsigned int saveflags = 0;
294 saveflags = gline->gl_flags;
296 if (flags & GLINE_LOCAL)
297 gline->gl_flags &= ~GLINE_LDEACT;
299 gline->gl_flags |= GLINE_ACTIVE;
301 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
304 gline->gl_lastmod = lastmod;
307 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
308 return 0; /* was active to begin with */
310 /* Inform ops and log it */
311 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
312 "expiring at %Tu: %s",
313 IsServer(sptr) ? sptr->name : sptr->user->server->name,
314 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
315 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
316 GlineIsBadChan(gline) ? "" : gline->gl_host,
317 gline->gl_expire + TSoffset, gline->gl_reason);
319 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
320 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
321 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
322 GlineIsBadChan(gline) ? "" : "@",
323 GlineIsBadChan(gline) ? "" : gline->gl_host,
324 gline->gl_expire + TSoffset, gline->gl_reason);
326 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
327 propagate_gline(cptr, sptr, gline);
329 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
333 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
334 time_t lastmod, unsigned int flags)
336 unsigned int saveflags = 0;
341 saveflags = gline->gl_flags;
343 if (GlineIsLocal(gline))
344 msg = "removing local";
345 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL))
346 msg = "removing global";
348 msg = "deactivating global";
350 if (flags & GLINE_LOCAL)
351 gline->gl_flags |= GLINE_LDEACT;
353 gline->gl_flags &= ~GLINE_ACTIVE;
355 if (gline->gl_lastmod >= lastmod)
358 gline->gl_lastmod = lastmod;
361 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
362 return 0; /* was inactive to begin with */
365 /* Inform ops and log it */
366 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
368 IsServer(sptr) ? sptr->name : sptr->user->server->name,
369 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
370 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
371 GlineIsBadChan(gline) ? "" : gline->gl_host,
372 gline->gl_expire + TSoffset, gline->gl_reason);
374 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
375 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
376 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
377 GlineIsBadChan(gline) ? "" : "@",
378 GlineIsBadChan(gline) ? "" : gline->gl_host,
379 gline->gl_expire + TSoffset, gline->gl_reason);
381 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
383 else if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
384 propagate_gline(cptr, sptr, gline);
390 gline_find(char *userhost, unsigned int flags)
393 struct Gline *sgline;
394 char *user, *host, *t_uh;
396 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
397 for (gline = BadChanGlineList; gline; gline = sgline) {
398 sgline = gline->gl_next;
400 if (gline->gl_expire <= CurrentTime)
402 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
403 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
405 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
406 match(gline->gl_user, userhost)) == 0)
411 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
412 *userhost == '#' || *userhost == '&' || *userhost == '+'
414 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
415 #endif /* NO_OLD_GLINE */
419 DupString(t_uh, userhost);
420 canon_userhost(t_uh, &user, &host, 0);
422 for (gline = GlobalGlineList; gline; gline = sgline) {
423 sgline = gline->gl_next;
425 if (gline->gl_expire <= CurrentTime)
427 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
428 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
430 else if (flags & GLINE_EXACT) {
431 if (ircd_strcmp(gline->gl_host, host) == 0 &&
432 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
433 ircd_strcmp(gline->gl_user, user) == 0))
436 if (match(gline->gl_host, host) == 0 &&
437 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
438 match(gline->gl_user, user) == 0))
449 gline_lookup(struct Client *cptr, unsigned int flags)
452 struct Gline *sgline;
454 for (gline = GlobalGlineList; gline; gline = sgline) {
455 sgline = gline->gl_next;
457 if (gline->gl_expire <= CurrentTime) {
462 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
463 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
466 if (match(gline->gl_user, cptr->user->username) != 0)
469 if (GlineIsIpMask(gline)) {
470 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
471 if ((cptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
475 if (match(gline->gl_host, cptr->user->host) != 0)
487 gline_free(struct Gline *gline)
491 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
493 gline->gl_next->gl_prev_p = gline->gl_prev_p;
495 MyFree(gline->gl_user); /* free up the memory */
497 MyFree(gline->gl_host);
498 MyFree(gline->gl_reason);
503 gline_burst(struct Client *cptr)
506 struct Gline *sgline;
508 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
509 sgline = gline->gl_next;
511 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
513 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
514 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
515 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
516 gline->gl_host, gline->gl_expire - CurrentTime,
517 gline->gl_lastmod, gline->gl_reason);
520 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
521 sgline = gline->gl_next;
523 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
525 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
526 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
527 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
528 gline->gl_expire - CurrentTime, gline->gl_lastmod,
534 gline_resend(struct Client *cptr, struct Gline *gline)
536 if (GlineIsLocal(gline) || !gline->gl_lastmod)
539 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
540 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
541 GlineIsBadChan(gline) ? "" : "@",
542 GlineIsBadChan(gline) ? "" : gline->gl_host,
543 gline->gl_expire - CurrentTime, gline->gl_lastmod,
550 gline_list(struct Client *sptr, char *userhost)
553 struct Gline *sgline;
556 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
557 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
559 /* send gline information along */
560 send_reply(sptr, RPL_GLIST, gline->gl_user,
561 GlineIsBadChan(gline) ? "" : "@",
562 GlineIsBadChan(gline) ? "" : gline->gl_host,
563 gline->gl_expire + TSoffset,
564 GlineIsLocal(gline) ? me.name : "*",
565 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
567 for (gline = GlobalGlineList; gline; gline = sgline) {
568 sgline = gline->gl_next;
570 if (gline->gl_expire <= CurrentTime)
573 send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
574 gline->gl_expire + TSoffset,
575 GlineIsLocal(gline) ? me.name : "*",
576 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
579 for (gline = BadChanGlineList; gline; gline = sgline) {
580 sgline = gline->gl_next;
582 if (gline->gl_expire <= CurrentTime)
585 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
586 gline->gl_expire + TSoffset,
587 GlineIsLocal(gline) ? me.name : "*",
588 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
592 /* end of gline information */
593 return send_reply(sptr, RPL_ENDOFGLIST);
597 gline_stats(struct Client *sptr)
600 struct Gline *sgline;
602 for (gline = GlobalGlineList; gline; gline = sgline) {
603 sgline = gline->gl_next;
605 if (gline->gl_expire <= CurrentTime)
608 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
609 gline->gl_expire + TSoffset, gline->gl_reason);