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"
26 #include "ircd_reply.h"
27 #include "ircd_string.h"
39 #include "sys.h" /* FALSE bleah */
44 #include <arpa/inet.h> /* for inet_ntoa */
46 struct Gline* GlobalGlineList = 0;
47 struct Gline* BadChanGlineList = 0;
50 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
54 if (!(tmp = strchr(userhost, '@'))) {
65 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
68 struct Gline *gline, *sgline, *after = 0;
71 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
72 canon_userhost(userhost, &user, &host, "*"); /* find user and host */
74 for (gline = GlobalGlineList; gline; gline = sgline) {
75 sgline = gline->gl_next;
77 if (gline->gl_expire <= CurrentTime)
79 else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
81 else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
82 !mmatch(gline->gl_host, host)) {
83 if (expire <= gline->gl_expire) /* will expire before wider gline */
86 after = gline; /* stick new gline after this one */
87 } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
88 !mmatch(host, gline->gl_host) &&
89 gline->gl_expire <= expire) /* gline expires before new one */
90 gline_free(gline); /* save some memory */
94 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
97 DupString(gline->gl_reason, reason); /* initialize gline... */
98 gline->gl_expire = expire;
99 gline->gl_lastmod = lastmod;
100 gline->gl_flags = flags & GLINE_MASK;
102 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
103 DupString(gline->gl_user, userhost); /* first, remember channel */
106 gline->gl_next = BadChanGlineList; /* then link it into list */
107 gline->gl_prev_p = &BadChanGlineList;
108 if (BadChanGlineList)
109 BadChanGlineList->gl_prev_p = &gline->gl_next;
110 BadChanGlineList = gline;
112 DupString(gline->gl_user, user); /* remember them... */
113 DupString(gline->gl_host, host);
115 if (check_if_ipmask(host)) { /* mark if it's an IP mask */
121 class = sscanf(host,"%d.%d.%d.%d/%d",
122 &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
129 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
130 gline->ipnum.s_addr = inet_addr(ipname);
131 Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
132 gline->gl_flags |= GLINE_IPMASK;
136 gline->gl_next = after->gl_next;
137 gline->gl_prev_p = &after->gl_next;
139 after->gl_next->gl_prev_p = &gline->gl_next;
140 after->gl_next = gline;
142 gline->gl_next = GlobalGlineList; /* then link it into list */
143 gline->gl_prev_p = &GlobalGlineList;
145 GlobalGlineList->gl_prev_p = &gline->gl_next;
146 GlobalGlineList = gline;
154 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
156 struct Client *acptr;
157 int fd, retval = 0, tval;
159 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
162 for (fd = HighestFd; fd >= 0; --fd) {
166 if ((acptr = LocalClientArray[fd])) {
170 if (acptr->user->username &&
171 match (gline->gl_user, acptr->user->username) != 0)
174 if (GlineIsIpMask(gline)) {
175 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
176 if ((acptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
180 if (match(gline->gl_host, acptr->sockhost) != 0)
184 /* ok, here's one that got G-lined */
185 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
188 /* let the ops know about it */
189 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
190 get_client_name(acptr, FALSE));
192 /* and get rid of him */
193 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
195 retval = tval; /* retain killed status */
202 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
204 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
207 if (gline->gl_lastmod)
208 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
209 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
210 GlineIsBadChan(gline) ? "" : "@",
211 GlineIsBadChan(gline) ? "" : gline->gl_host,
212 gline->gl_expire - CurrentTime, gline->gl_lastmod,
215 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu :%s",
216 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
217 GlineIsBadChan(gline) ? "" : "@",
218 GlineIsBadChan(gline) ? "" : gline->gl_host,
219 gline->gl_expire - CurrentTime, gline->gl_reason);
223 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
224 char *reason, time_t expire, time_t lastmod, unsigned int flags)
226 struct Gline *agline;
228 assert(0 != userhost);
232 * You cannot set a negative (or zero) expire time, nor can you set an
233 * expiration time for greater than GLINE_MAX_EXPIRE.
235 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
236 if (!IsServer(sptr) && MyConnect(sptr))
237 send_reply(sptr, ERR_BADEXPIRE, expire);
241 expire += CurrentTime; /* convert from lifetime to timestamp */
243 /* NO_OLD_GLINE allows *@#channel to work correctly */
244 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
245 # ifndef NO_OLD_GLINE
246 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
247 # endif /* OLD_GLINE */
249 # ifndef LOCAL_BADCHAN
250 if (flags & GLINE_LOCAL)
253 flags |= GLINE_BADCHAN;
257 sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
259 IsServer(sptr) ? sptr->name : sptr->user->server->name,
260 flags & GLINE_LOCAL ? "local" : "global",
261 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
262 expire + TSoffset, reason);
266 write_log(GPATH, "# %Tu %C adding %s %s for %s, expiring at %Tu: %s\n",
267 TStime(), sptr, flags & GLINE_LOCAL ? "local" : "global",
268 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
269 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))
284 /* this can be inserted into the conf */
285 write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
286 GlineHost(agline), GlineReason(agline), GlineUser(agline));
289 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
293 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
294 time_t lastmod, unsigned int flags)
296 unsigned int saveflags = 0;
300 saveflags = gline->gl_flags;
302 if (flags & GLINE_LOCAL)
303 gline->gl_flags &= ~GLINE_LDEACT;
305 gline->gl_flags |= GLINE_ACTIVE;
307 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
310 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",
319 IsServer(sptr) ? sptr->name : sptr->user->server->name,
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);
326 write_log(GPATH, "# %Tu %C activating global %s for %s%s%s, expiring at "
327 "%Tu: %s\n", TStime(), sptr,
328 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
329 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
330 GlineIsBadChan(gline) ? "" : gline->gl_host,
331 gline->gl_expire + TSoffset, gline->gl_reason);
334 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
335 propagate_gline(cptr, sptr, gline);
337 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
341 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
342 time_t lastmod, unsigned int flags)
344 unsigned int saveflags = 0;
349 saveflags = gline->gl_flags;
351 if (GlineIsLocal(gline))
352 msg = "removing local";
353 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL))
354 msg = "removing global";
356 msg = "deactivating global";
358 if (flags & GLINE_LOCAL)
359 gline->gl_flags |= GLINE_LDEACT;
361 gline->gl_flags &= ~GLINE_ACTIVE;
363 if (gline->gl_lastmod >= lastmod)
366 gline->gl_lastmod = lastmod;
369 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
370 return 0; /* was inactive to begin with */
373 /* Inform ops and log it */
374 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
376 IsServer(sptr) ? sptr->name : sptr->user->server->name,
377 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
378 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
379 GlineIsBadChan(gline) ? "" : gline->gl_host,
380 gline->gl_expire + TSoffset, gline->gl_reason);
383 write_log(GPATH, "# %Tu %C %s %s for %s%s%s, expiring at %Tu: %s\n",
384 TStime(), sptr, msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
385 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
386 GlineIsBadChan(gline) ? "" : gline->gl_host,
387 gline->gl_expire + TSoffset, gline->gl_reason);
390 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
392 else if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
393 propagate_gline(cptr, sptr, gline);
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, cptr->user->username) != 0)
478 if (GlineIsIpMask(gline)) {
479 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
480 if ((cptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
484 if (match(gline->gl_host, cptr->user->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) ? me.name : "*",
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) ? me.name : "*",
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) ? me.name : "*",
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);