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"
38 #include "sys.h" /* FALSE bleah */
43 struct Gline* GlobalGlineList = 0;
44 struct Gline* BadChanGlineList = 0;
47 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
51 if (!(tmp = strchr(userhost, '@'))) {
62 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
65 struct Gline *gline, *sgline, *after = 0;
68 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
69 canon_userhost(userhost, &user, &host, "*"); /* find user and host */
71 for (gline = GlobalGlineList; gline; gline = sgline) {
72 sgline = gline->gl_next;
74 if (gline->gl_expire <= CurrentTime)
76 else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
78 else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
79 !mmatch(gline->gl_host, host)) {
80 if (expire <= gline->gl_expire) /* will expire before wider gline */
83 after = gline; /* stick new gline after this one */
84 } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
85 !mmatch(host, gline->gl_host) &&
86 gline->gl_expire <= expire) /* gline expires before new one */
87 gline_free(gline); /* save some memory */
91 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
94 DupString(gline->gl_reason, reason); /* initialize gline... */
95 gline->gl_expire = expire;
96 gline->gl_lastmod = lastmod;
97 gline->gl_flags = flags & GLINE_MASK;
99 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
100 DupString(gline->gl_user, userhost); /* first, remember channel */
103 gline->gl_next = BadChanGlineList; /* then link it into list */
104 gline->gl_prev_p = &BadChanGlineList;
105 if (BadChanGlineList)
106 BadChanGlineList->gl_prev_p = &gline->gl_next;
107 BadChanGlineList = gline;
109 DupString(gline->gl_user, user); /* remember them... */
110 DupString(gline->gl_host, host);
112 if (check_if_ipmask(host)) /* mark if it's an IP mask */
113 gline->gl_flags |= GLINE_IPMASK;
116 gline->gl_next = after->gl_next;
117 gline->gl_prev_p = &after->gl_next;
119 after->gl_next->gl_prev_p = &gline->gl_next;
120 after->gl_next = gline;
122 gline->gl_next = GlobalGlineList; /* then link it into list */
123 gline->gl_prev_p = &GlobalGlineList;
125 GlobalGlineList->gl_prev_p = &gline->gl_next;
126 GlobalGlineList = gline;
134 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
136 struct Client *acptr;
137 int fd, retval = 0, tval;
139 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
142 for (fd = HighestFd; fd >= 0; --fd) {
146 if ((acptr = LocalClientArray[fd])) {
150 if ((GlineIsIpMask(gline) ? match(gline->gl_host, acptr->sock_ip) :
151 match(gline->gl_host, acptr->sockhost)) == 0 &&
152 (!acptr->user->username ||
153 match(gline->gl_user, acptr->user->username) == 0)) {
154 /* ok, here's one that got G-lined */
155 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
158 /* let the ops know about it */
159 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
160 get_client_name(acptr, FALSE));
162 /* and get rid of him */
163 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
165 retval = tval; /* retain killed status */
174 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
176 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
179 if (gline->gl_lastmod)
180 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
181 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
182 GlineIsBadChan(gline) ? "" : "@",
183 GlineIsBadChan(gline) ? "" : gline->gl_host,
184 gline->gl_expire - CurrentTime, gline->gl_lastmod,
187 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu :%s",
188 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
189 GlineIsBadChan(gline) ? "" : "@",
190 GlineIsBadChan(gline) ? "" : gline->gl_host,
191 gline->gl_expire - CurrentTime, gline->gl_reason);
195 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
196 char *reason, time_t expire, time_t lastmod, unsigned int flags)
198 struct Gline *agline;
200 assert(0 != userhost);
204 * You cannot set a negative (or zero) expire time, nor can you set an
205 * expiration time for greater than GLINE_MAX_EXPIRE.
207 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
208 if (!IsServer(sptr) && MyConnect(sptr))
209 send_reply(sptr, ERR_BADEXPIRE, expire);
213 expire += CurrentTime; /* convert from lifetime to timestamp */
215 /* NO_OLD_GLINE allows *@#channel to work correctly */
216 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
217 # ifndef NO_OLD_GLINE
218 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
219 # endif /* OLD_GLINE */
221 # ifndef LOCAL_BADCHAN
222 if (flags & GLINE_LOCAL)
225 flags |= GLINE_BADCHAN;
229 sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
231 IsServer(sptr) ? sptr->name : sptr->user->server->name,
232 flags & GLINE_LOCAL ? "local" : "global",
233 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
234 expire + TSoffset, reason);
238 write_log(GPATH, "# %Tu %C adding %s %s for %s, expiring at %Tu: %s\n",
239 TStime(), sptr, flags & GLINE_LOCAL ? "local" : "global",
240 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
241 expire + TSoffset, reason);
245 agline = make_gline(userhost, reason, expire, lastmod, flags);
247 if (!agline) /* if it overlapped, silently return */
250 propagate_gline(cptr, sptr, agline);
252 if (GlineIsBadChan(agline))
256 /* this can be inserted into the conf */
257 write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
258 GlineHost(agline), GlineReason(agline), GlineUser(agline));
261 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
265 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
266 time_t lastmod, unsigned int flags)
268 unsigned int saveflags = 0;
271 assert(!GlineIsLocal(gline));
273 saveflags = gline->gl_flags;
275 if (flags & GLINE_LOCAL)
276 gline->gl_flags &= ~GLINE_LDEACT;
278 gline->gl_flags |= GLINE_ACTIVE;
280 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
283 gline->gl_lastmod = lastmod;
286 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
287 return 0; /* was active to begin with */
289 /* Inform ops and log it */
290 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
291 "expiring at %Tu: %s",
292 IsServer(sptr) ? sptr->name : sptr->user->server->name,
293 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
294 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
295 GlineIsBadChan(gline) ? "" : gline->gl_host,
296 gline->gl_expire + TSoffset, gline->gl_reason);
299 write_log(GPATH, "# %Tu %C activating global %s for %s%s%s, expiring at "
300 "%Tu: %s\n", TStime(), sptr,
301 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
302 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
303 GlineIsBadChan(gline) ? "" : gline->gl_host,
304 gline->gl_expire + TSoffset, gline->gl_reason);
307 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
308 propagate_gline(cptr, sptr, gline);
310 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
314 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
315 time_t lastmod, unsigned int flags)
317 unsigned int saveflags = 0;
322 saveflags = gline->gl_flags;
324 if (GlineIsLocal(gline))
325 msg = "removing local";
326 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL))
327 msg = "removing global";
329 msg = "deactivating global";
331 if (flags & GLINE_LOCAL)
332 gline->gl_flags |= GLINE_LDEACT;
334 gline->gl_flags &= ~GLINE_ACTIVE;
336 if (gline->gl_lastmod >= lastmod)
339 gline->gl_lastmod = lastmod;
342 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
343 return 0; /* was inactive to begin with */
346 /* Inform ops and log it */
347 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
349 IsServer(sptr) ? sptr->name : sptr->user->server->name,
350 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
351 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
352 GlineIsBadChan(gline) ? "" : gline->gl_host,
353 gline->gl_expire + TSoffset, gline->gl_reason);
356 write_log(GPATH, "# %Tu %C %s %s for %s%s%s, expiring at %Tu: %s\n",
357 TStime(), sptr, msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
358 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
359 GlineIsBadChan(gline) ? "" : gline->gl_host,
360 gline->gl_expire + TSoffset, gline->gl_reason);
363 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
365 else if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
366 propagate_gline(cptr, sptr, gline);
372 gline_find(char *userhost, unsigned int flags)
375 struct Gline *sgline;
376 char *user, *host, *t_uh;
378 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
379 for (gline = BadChanGlineList; gline; gline = sgline) {
380 sgline = gline->gl_next;
382 if (gline->gl_expire <= CurrentTime)
384 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
385 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
387 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
388 match(gline->gl_user, userhost)) == 0)
393 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
394 *userhost == '#' || *userhost == '&' || *userhost == '+'
396 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
397 #endif /* NO_OLD_GLINE */
401 DupString(t_uh, userhost);
402 canon_userhost(t_uh, &user, &host, 0);
404 for (gline = GlobalGlineList; gline; gline = sgline) {
405 sgline = gline->gl_next;
407 if (gline->gl_expire <= CurrentTime)
409 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
410 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
412 else if (flags & GLINE_EXACT) {
413 if (ircd_strcmp(gline->gl_host, host) == 0 &&
414 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
415 ircd_strcmp(gline->gl_user, user) == 0))
418 if (match(gline->gl_host, host) == 0 &&
419 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
420 match(gline->gl_user, user) == 0))
431 gline_lookup(struct Client *cptr, unsigned int flags)
434 struct Gline *sgline;
436 for (gline = GlobalGlineList; gline; gline = sgline) {
437 sgline = gline->gl_next;
439 if (gline->gl_expire <= CurrentTime)
441 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
442 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
444 else if ((GlineIsIpMask(gline) ?
445 match(gline->gl_host, ircd_ntoa((const char *)&cptr->ip)) :
446 match(gline->gl_host, cptr->user->host)) == 0 &&
447 match(gline->gl_user, cptr->user->username) == 0)
455 gline_free(struct Gline *gline)
459 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
461 gline->gl_next->gl_prev_p = gline->gl_prev_p;
463 MyFree(gline->gl_user); /* free up the memory */
465 MyFree(gline->gl_host);
466 MyFree(gline->gl_reason);
471 gline_burst(struct Client *cptr)
474 struct Gline *sgline;
476 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
477 sgline = gline->gl_next;
479 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
481 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
482 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
483 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
484 gline->gl_host, gline->gl_expire - CurrentTime,
485 gline->gl_lastmod, gline->gl_reason);
488 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
489 sgline = gline->gl_next;
491 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
493 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
494 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
495 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
496 gline->gl_expire - CurrentTime, gline->gl_lastmod,
502 gline_resend(struct Client *cptr, struct Gline *gline)
504 if (GlineIsLocal(gline) || !gline->gl_lastmod)
507 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
508 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
509 GlineIsBadChan(gline) ? "" : "@",
510 GlineIsBadChan(gline) ? "" : gline->gl_host,
511 gline->gl_expire - CurrentTime, gline->gl_lastmod,
518 gline_list(struct Client *sptr, char *userhost)
521 struct Gline *sgline;
524 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
525 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
527 /* send gline information along */
528 send_reply(sptr, RPL_GLIST, gline->gl_user,
529 GlineIsBadChan(gline) ? "" : "@",
530 GlineIsBadChan(gline) ? "" : gline->gl_host,
531 gline->gl_expire + TSoffset,
532 GlineIsLocal(gline) ? me.name : "*",
533 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
535 for (gline = GlobalGlineList; gline; gline = sgline) {
536 sgline = gline->gl_next;
538 if (gline->gl_expire <= CurrentTime)
541 send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
542 gline->gl_expire + TSoffset,
543 GlineIsLocal(gline) ? me.name : "*",
544 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
547 for (gline = BadChanGlineList; gline; gline = sgline) {
548 sgline = gline->gl_next;
550 if (gline->gl_expire <= CurrentTime)
553 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
554 gline->gl_expire + TSoffset,
555 GlineIsLocal(gline) ? me.name : "*",
556 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
560 /* end of gline information */
561 return send_reply(sptr, RPL_ENDOFGLIST);
565 gline_stats(struct Client *sptr)
568 struct Gline *sgline;
570 for (gline = GlobalGlineList; gline; gline = sgline) {
571 sgline = gline->gl_next;
573 if (gline->gl_expire <= CurrentTime)
576 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
577 gline->gl_expire + TSoffset, gline->gl_reason);