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 (acptr->user->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, 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,
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);
225 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
226 char *reason, time_t expire, time_t lastmod, unsigned int flags)
228 struct Gline *agline;
230 assert(0 != userhost);
234 * You cannot set a negative (or zero) expire time, nor can you set an
235 * expiration time for greater than GLINE_MAX_EXPIRE.
237 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
238 if (!IsServer(sptr) && MyConnect(sptr))
239 send_reply(sptr, ERR_BADEXPIRE, expire);
243 expire += CurrentTime; /* convert from lifetime to timestamp */
245 /* NO_OLD_GLINE allows *@#channel to work correctly */
246 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
247 # ifndef NO_OLD_GLINE
248 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
249 # endif /* OLD_GLINE */
251 # ifndef LOCAL_BADCHAN
252 if (flags & GLINE_LOCAL)
255 flags |= GLINE_BADCHAN;
259 sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
260 "%Tu: %s", IsServer(sptr) ? cli_name(sptr) :
261 cli_name((cli_user(sptr))->server),
262 flags & GLINE_LOCAL ? "local" : "global",
263 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
264 expire + TSoffset, reason);
267 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
268 "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
269 flags & GLINE_LOCAL ? "local" : "global",
270 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
271 expire + TSoffset, reason);
274 agline = make_gline(userhost, reason, expire, lastmod, flags);
276 if (!agline) /* if it overlapped, silently return */
279 propagate_gline(cptr, sptr, agline);
281 if (GlineIsBadChan(agline))
284 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
288 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
289 time_t lastmod, unsigned int flags)
291 unsigned int saveflags = 0;
295 saveflags = gline->gl_flags;
297 if (flags & GLINE_LOCAL)
298 gline->gl_flags &= ~GLINE_LDEACT;
300 gline->gl_flags |= GLINE_ACTIVE;
302 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
305 gline->gl_lastmod = lastmod;
308 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
309 return 0; /* was active to begin with */
311 /* Inform ops and log it */
312 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
313 "expiring at %Tu: %s", IsServer(sptr) ? cli_name(sptr) :
314 cli_name((cli_user(sptr))->server),
315 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
316 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
317 GlineIsBadChan(gline) ? "" : gline->gl_host,
318 gline->gl_expire + TSoffset, gline->gl_reason);
320 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
321 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
322 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
323 GlineIsBadChan(gline) ? "" : "@",
324 GlineIsBadChan(gline) ? "" : gline->gl_host,
325 gline->gl_expire + TSoffset, gline->gl_reason);
327 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
328 propagate_gline(cptr, sptr, gline);
330 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
334 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
335 time_t lastmod, unsigned int flags)
337 unsigned int saveflags = 0;
342 saveflags = gline->gl_flags;
344 if (GlineIsLocal(gline))
345 msg = "removing local";
346 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
347 msg = "removing global";
348 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
350 msg = "deactivating global";
352 if (flags & GLINE_LOCAL)
353 gline->gl_flags |= GLINE_LDEACT;
355 gline->gl_flags &= ~GLINE_ACTIVE;
357 if (gline->gl_lastmod >= lastmod)
360 gline->gl_lastmod = lastmod;
363 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
364 return 0; /* was inactive to begin with */
367 /* Inform ops and log it */
368 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
369 "%s", IsServer(sptr) ? cli_name(sptr) :
370 cli_name((cli_user(sptr))->server),
371 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
372 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
373 GlineIsBadChan(gline) ? "" : gline->gl_host,
374 gline->gl_expire + TSoffset, gline->gl_reason);
376 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
377 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
378 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
379 GlineIsBadChan(gline) ? "" : "@",
380 GlineIsBadChan(gline) ? "" : gline->gl_host,
381 gline->gl_expire + TSoffset, gline->gl_reason);
383 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
384 propagate_gline(cptr, sptr, gline);
386 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
387 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
388 gline_free(gline); /* get rid of it */
394 gline_find(char *userhost, unsigned int flags)
397 struct Gline *sgline;
398 char *user, *host, *t_uh;
400 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
401 for (gline = BadChanGlineList; gline; gline = sgline) {
402 sgline = gline->gl_next;
404 if (gline->gl_expire <= CurrentTime)
406 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
407 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
409 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
410 match(gline->gl_user, userhost)) == 0)
415 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
416 *userhost == '#' || *userhost == '&' || *userhost == '+'
418 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
419 #endif /* NO_OLD_GLINE */
423 DupString(t_uh, userhost);
424 canon_userhost(t_uh, &user, &host, 0);
426 for (gline = GlobalGlineList; gline; gline = sgline) {
427 sgline = gline->gl_next;
429 if (gline->gl_expire <= CurrentTime)
431 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
432 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
434 else if (flags & GLINE_EXACT) {
435 if (ircd_strcmp(gline->gl_host, host) == 0 &&
436 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
437 ircd_strcmp(gline->gl_user, user) == 0))
440 if (match(gline->gl_host, host) == 0 &&
441 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
442 match(gline->gl_user, user) == 0))
453 gline_lookup(struct Client *cptr, unsigned int flags)
456 struct Gline *sgline;
458 for (gline = GlobalGlineList; gline; gline = sgline) {
459 sgline = gline->gl_next;
461 if (gline->gl_expire <= CurrentTime) {
466 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
467 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
470 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
473 if (GlineIsIpMask(gline)) {
474 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
475 if (((cli_ip(cptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
479 if (match(gline->gl_host, (cli_user(cptr))->host) != 0)
491 gline_free(struct Gline *gline)
495 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
497 gline->gl_next->gl_prev_p = gline->gl_prev_p;
499 MyFree(gline->gl_user); /* free up the memory */
501 MyFree(gline->gl_host);
502 MyFree(gline->gl_reason);
507 gline_burst(struct Client *cptr)
510 struct Gline *sgline;
512 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
513 sgline = gline->gl_next;
515 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
517 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
518 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
519 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
520 gline->gl_host, gline->gl_expire - CurrentTime,
521 gline->gl_lastmod, gline->gl_reason);
524 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
525 sgline = gline->gl_next;
527 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
529 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
530 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
531 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
532 gline->gl_expire - CurrentTime, gline->gl_lastmod,
538 gline_resend(struct Client *cptr, struct Gline *gline)
540 if (GlineIsLocal(gline) || !gline->gl_lastmod)
543 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
544 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
545 GlineIsBadChan(gline) ? "" : "@",
546 GlineIsBadChan(gline) ? "" : gline->gl_host,
547 gline->gl_expire - CurrentTime, gline->gl_lastmod,
554 gline_list(struct Client *sptr, char *userhost)
557 struct Gline *sgline;
560 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
561 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
563 /* send gline information along */
564 send_reply(sptr, RPL_GLIST, gline->gl_user,
565 GlineIsBadChan(gline) ? "" : "@",
566 GlineIsBadChan(gline) ? "" : gline->gl_host,
567 gline->gl_expire + TSoffset,
568 GlineIsLocal(gline) ? cli_name(&me) : "*",
569 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
571 for (gline = GlobalGlineList; gline; gline = sgline) {
572 sgline = gline->gl_next;
574 if (gline->gl_expire <= CurrentTime)
577 send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
578 gline->gl_expire + TSoffset,
579 GlineIsLocal(gline) ? cli_name(&me) : "*",
580 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
583 for (gline = BadChanGlineList; gline; gline = sgline) {
584 sgline = gline->gl_next;
586 if (gline->gl_expire <= CurrentTime)
589 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
590 gline->gl_expire + TSoffset,
591 GlineIsLocal(gline) ? cli_name(&me) : "*",
592 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
596 /* end of gline information */
597 return send_reply(sptr, RPL_ENDOFGLIST);
601 gline_stats(struct Client *sptr)
604 struct Gline *sgline;
606 for (gline = GlobalGlineList; gline; gline = sgline) {
607 sgline = gline->gl_next;
609 if (gline->gl_expire <= CurrentTime)
612 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
613 gline->gl_expire + TSoffset, gline->gl_reason);