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 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 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
252 return send_reply(sptr, ERR_NOPRIVILEGES);
254 flags |= GLINE_BADCHAN;
258 sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
259 "%Tu: %s", IsServer(sptr) ? cli_name(sptr) :
260 cli_name((cli_user(sptr))->server),
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", IsServer(sptr) ? cli_name(sptr) :
313 cli_name((cli_user(sptr))->server),
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";
347 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
349 msg = "deactivating global";
351 if (flags & GLINE_LOCAL)
352 gline->gl_flags |= GLINE_LDEACT;
354 gline->gl_flags &= ~GLINE_ACTIVE;
356 if (gline->gl_lastmod >= lastmod)
359 gline->gl_lastmod = lastmod;
362 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
363 return 0; /* was inactive to begin with */
366 /* Inform ops and log it */
367 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
368 "%s", IsServer(sptr) ? cli_name(sptr) :
369 cli_name((cli_user(sptr))->server),
370 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
371 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
372 GlineIsBadChan(gline) ? "" : gline->gl_host,
373 gline->gl_expire + TSoffset, gline->gl_reason);
375 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
376 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
377 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
378 GlineIsBadChan(gline) ? "" : "@",
379 GlineIsBadChan(gline) ? "" : gline->gl_host,
380 gline->gl_expire + TSoffset, gline->gl_reason);
382 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
383 propagate_gline(cptr, sptr, gline);
385 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
386 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
387 gline_free(gline); /* get rid of it */
393 gline_find(char *userhost, unsigned int flags)
396 struct Gline *sgline;
397 char *user, *host, *t_uh;
399 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
400 for (gline = BadChanGlineList; gline; gline = sgline) {
401 sgline = gline->gl_next;
403 if (gline->gl_expire <= CurrentTime)
405 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
406 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
408 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
409 match(gline->gl_user, userhost)) == 0)
414 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
415 *userhost == '#' || *userhost == '&' || *userhost == '+'
417 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
418 #endif /* NO_OLD_GLINE */
422 DupString(t_uh, userhost);
423 canon_userhost(t_uh, &user, &host, 0);
425 for (gline = GlobalGlineList; gline; gline = sgline) {
426 sgline = gline->gl_next;
428 if (gline->gl_expire <= CurrentTime)
430 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
431 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
433 else if (flags & GLINE_EXACT) {
434 if (ircd_strcmp(gline->gl_host, host) == 0 &&
435 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
436 ircd_strcmp(gline->gl_user, user) == 0))
439 if (match(gline->gl_host, host) == 0 &&
440 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
441 match(gline->gl_user, user) == 0))
452 gline_lookup(struct Client *cptr, unsigned int flags)
455 struct Gline *sgline;
457 for (gline = GlobalGlineList; gline; gline = sgline) {
458 sgline = gline->gl_next;
460 if (gline->gl_expire <= CurrentTime) {
465 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
466 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
469 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
472 if (GlineIsIpMask(gline)) {
473 Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
474 if (((cli_ip(cptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
478 if (match(gline->gl_host, (cli_user(cptr))->host) != 0)
490 gline_free(struct Gline *gline)
494 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
496 gline->gl_next->gl_prev_p = gline->gl_prev_p;
498 MyFree(gline->gl_user); /* free up the memory */
500 MyFree(gline->gl_host);
501 MyFree(gline->gl_reason);
506 gline_burst(struct Client *cptr)
509 struct Gline *sgline;
511 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
512 sgline = gline->gl_next;
514 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
516 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
517 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
518 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
519 gline->gl_host, gline->gl_expire - CurrentTime,
520 gline->gl_lastmod, gline->gl_reason);
523 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
524 sgline = gline->gl_next;
526 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
528 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
529 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
530 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
531 gline->gl_expire - CurrentTime, gline->gl_lastmod,
537 gline_resend(struct Client *cptr, struct Gline *gline)
539 if (GlineIsLocal(gline) || !gline->gl_lastmod)
542 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
543 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
544 GlineIsBadChan(gline) ? "" : "@",
545 GlineIsBadChan(gline) ? "" : gline->gl_host,
546 gline->gl_expire - CurrentTime, gline->gl_lastmod,
553 gline_list(struct Client *sptr, char *userhost)
556 struct Gline *sgline;
559 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
560 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
562 /* send gline information along */
563 send_reply(sptr, RPL_GLIST, gline->gl_user,
564 GlineIsBadChan(gline) ? "" : "@",
565 GlineIsBadChan(gline) ? "" : gline->gl_host,
566 gline->gl_expire + TSoffset,
567 GlineIsLocal(gline) ? cli_name(&me) : "*",
568 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
570 for (gline = GlobalGlineList; gline; gline = sgline) {
571 sgline = gline->gl_next;
573 if (gline->gl_expire <= CurrentTime)
576 send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
577 gline->gl_expire + TSoffset,
578 GlineIsLocal(gline) ? cli_name(&me) : "*",
579 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
582 for (gline = BadChanGlineList; gline; gline = sgline) {
583 sgline = gline->gl_next;
585 if (gline->gl_expire <= CurrentTime)
588 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
589 gline->gl_expire + TSoffset,
590 GlineIsLocal(gline) ? cli_name(&me) : "*",
591 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
595 /* end of gline information */
596 return send_reply(sptr, RPL_ENDOFGLIST);
600 gline_stats(struct Client *sptr)
603 struct Gline *sgline;
605 for (gline = GlobalGlineList; gline; gline = sgline) {
606 sgline = gline->gl_next;
608 if (gline->gl_expire <= CurrentTime)
611 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
612 gline->gl_expire + TSoffset, gline->gl_reason);