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 sendto_one(acptr, ":%s %d %s :*** %s.", me.name, ERR_YOUREBANNEDCREEP,
156 acptr->name, gline->gl_reason);
158 /* let the ops know about it */
159 sendto_op_mask(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(cptr, CMD_GLINE, sptr, "* %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(cptr, CMD_GLINE, sptr, "* %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_error_to_client(sptr, ERR_BADEXPIRE, expire);
213 expire += CurrentTime; /* convert from lifetime to timestamp */
215 /* NO_OLD_GLINE allows *@#channel to work correctly */
217 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
218 # ifndef NO_OLD_GLINE
219 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
220 # endif /* OLD_GLINE */
222 # ifndef LOCAL_BADCHAN
223 if (flags & GLINE_LOCAL)
226 flags |= GLINE_BADCHAN;
231 sendto_op_mask(SNO_GLINE, "%s adding %s %s for %s, expiring at "
233 IsServer(sptr) ? sptr->name : sptr->user->server->name,
234 flags & GLINE_LOCAL ? "local" : "global",
235 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
236 expire + TSoffset, reason);
241 write_log(GPATH, "# " TIME_T_FMT " %s adding %s %s for %s, expiring at "
242 TIME_T_FMT ": %s\n", TStime(), sptr->name,
243 flags & GLINE_LOCAL ? "local" : "global",
244 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
245 expire + TSoffset, reason);
247 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s adding %s %s for %s, "
248 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
249 sptr->user->username, sptr->user->host,
250 flags & GLINE_LOCAL ? "local" : "global",
251 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
252 expire + TSoffset, reason);
256 agline = make_gline(userhost, reason, expire, lastmod, flags);
258 if (!agline) /* if it overlapped, silently return */
261 propagate_gline(cptr, sptr, agline);
263 if (GlineIsBadChan(agline))
267 /* this can be inserted into the conf */
268 write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
269 GlineHost(agline), GlineReason(agline), GlineUser(agline));
272 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
276 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
277 time_t lastmod, unsigned int flags)
279 unsigned int saveflags = 0;
282 assert(!GlineIsLocal(gline));
284 saveflags = gline->gl_flags;
286 if (flags & GLINE_LOCAL)
287 gline->gl_flags &= ~GLINE_LDEACT;
289 gline->gl_flags |= GLINE_ACTIVE;
291 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
294 gline->gl_lastmod = lastmod;
297 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
298 return 0; /* was active to begin with */
300 /* Inform ops and log it */
301 sendto_op_mask(SNO_GLINE, "%s activating global %s for %s%s%s, expiring at "
303 IsServer(sptr) ? sptr->name : sptr->user->server->name,
304 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
305 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
306 GlineIsBadChan(gline) ? "" : gline->gl_host,
307 gline->gl_expire + TSoffset, gline->gl_reason);
311 write_log(GPATH, "# " TIME_T_FMT " %s activating global %s for %s%s%s, "
312 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
313 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
314 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
315 GlineIsBadChan(gline) ? "" : gline->gl_host,
316 gline->gl_expire + TSoffset, gline->gl_reason);
318 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s activating %s for "
319 "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
320 sptr->user->username, sptr->user->host,
321 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
322 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
323 GlineIsBadChan(gline) ? "" : gline->gl_host,
324 gline->gl_expire + TSoffset, gline->gl_reason);
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;
340 saveflags = gline->gl_flags;
342 if (!GlineIsLocal(gline)) {
343 if (flags & GLINE_LOCAL)
344 gline->gl_flags |= GLINE_LDEACT;
346 gline->gl_flags &= ~GLINE_ACTIVE;
348 if (gline->gl_lastmod >= lastmod)
351 gline->gl_lastmod = lastmod;
354 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
355 return 0; /* was inactive to begin with */
358 /* Inform ops and log it */
359 sendto_op_mask(SNO_GLINE, "%s %s %s for %s%s%s, expiring at "
361 IsServer(sptr) ? sptr->name : sptr->user->server->name,
362 GlineIsLocal(gline) ? "removing local" :
363 "deactivating global",
364 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
365 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
366 GlineIsBadChan(gline) ? "" : gline->gl_host,
367 gline->gl_expire + TSoffset, gline->gl_reason);
371 write_log(GPATH, "# " TIME_T_FMT " %s %s %s for %s%s%s, "
372 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
373 GlineIsLocal(gline) ? "removing local" : "deactivating global",
374 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
375 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
376 GlineIsBadChan(gline) ? "" : gline->gl_host,
377 gline->gl_expire + TSoffset, gline->gl_reason);
379 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s %s %s for "
380 "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
381 sptr->user->username, sptr->user->host,
382 GlineIsLocal(gline) ? "removing local" : "deactivating global",
383 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
384 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
385 GlineIsBadChan(gline) ? "" : gline->gl_host,
386 gline->gl_expire + TSoffset, gline->gl_reason);
389 if (GlineIsLocal(gline))
392 propagate_gline(cptr, sptr, gline);
398 gline_find(char *userhost, unsigned int flags)
401 struct Gline *sgline;
402 char *user, *host, *t_uh;
404 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
405 for (gline = BadChanGlineList; gline; gline = sgline) {
406 sgline = gline->gl_next;
408 if (gline->gl_expire <= CurrentTime)
410 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
411 match(gline->gl_user, userhost)) == 0)
416 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
417 *userhost == '#' || *userhost == '&' || *userhost == '+'
419 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
420 #endif /* NO_OLD_GLINE */
424 DupString(t_uh, userhost);
425 canon_userhost(t_uh, &user, &host, 0);
427 for (gline = GlobalGlineList; gline; gline = sgline) {
428 sgline = gline->gl_next;
430 if (gline->gl_expire <= CurrentTime)
432 else if (flags & GLINE_EXACT) {
433 if (ircd_strcmp(gline->gl_host, host) == 0 &&
434 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
435 ircd_strcmp(gline->gl_user, user) == 0))
438 if (match(gline->gl_host, host) == 0 &&
439 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
440 match(gline->gl_user, user) == 0))
451 gline_lookup(struct Client *cptr)
454 struct Gline *sgline;
456 for (gline = GlobalGlineList; gline; gline = sgline) {
457 sgline = gline->gl_next;
459 if (gline->gl_expire <= CurrentTime)
461 else if ((GlineIsIpMask(gline) ?
462 match(gline->gl_host, ircd_ntoa((const char *)&cptr->ip)) :
463 match(gline->gl_host, cptr->user->host)) == 0 &&
464 match(gline->gl_user, cptr->user->username) == 0)
472 gline_free(struct Gline *gline)
476 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
478 gline->gl_next->gl_prev_p = gline->gl_prev_p;
480 MyFree(gline->gl_user); /* free up the memory */
482 MyFree(gline->gl_host);
483 MyFree(gline->gl_reason);
488 gline_burst(struct Client *cptr)
491 struct Gline *sgline;
493 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
494 sgline = gline->gl_next;
496 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
498 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
499 sendcmdto_one(cptr, CMD_GLINE, &me, "* %c%s@%s %Tu %Tu :%s",
500 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
501 gline->gl_host, gline->gl_expire - CurrentTime,
502 gline->gl_lastmod, gline->gl_reason);
505 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
506 sgline = gline->gl_next;
508 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
510 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
511 sendcmdto_one(cptr, CMD_GLINE, &me, "* %c%s %Tu %Tu :%s",
512 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
513 gline->gl_expire - CurrentTime, gline->gl_lastmod,
519 gline_resend(struct Client *cptr, struct Gline *gline)
521 if (GlineIsLocal(gline) || !gline->gl_lastmod)
524 sendcmdto_one(cptr, CMD_GLINE, &me, "* %c%s%s%s %Tu %Tu :%s",
525 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
526 GlineIsBadChan(gline) ? "" : "@",
527 GlineIsBadChan(gline) ? "" : gline->gl_host,
528 gline->gl_expire - CurrentTime, gline->gl_lastmod,
535 gline_list(struct Client *sptr, char *userhost)
538 struct Gline *sgline;
541 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
542 return send_error_to_client(sptr, ERR_NOSUCHGLINE, userhost);
544 /* send gline information along */
545 sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name, gline->gl_user,
546 GlineIsBadChan(gline) ? "" : "@",
547 GlineIsBadChan(gline) ? "" : gline->gl_host,
548 gline->gl_expire + TSoffset,
549 GlineIsLocal(gline) ? me.name : "*",
550 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
552 for (gline = GlobalGlineList; gline; gline = sgline) {
553 sgline = gline->gl_next;
555 if (gline->gl_expire <= CurrentTime)
558 sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name,
559 gline->gl_user, "@", gline->gl_host,
560 gline->gl_expire + TSoffset,
561 GlineIsLocal(gline) ? me.name : "*",
562 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
565 for (gline = BadChanGlineList; gline; gline = sgline) {
566 sgline = gline->gl_next;
568 if (gline->gl_expire <= CurrentTime)
571 sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name,
572 gline->gl_user, "", "", gline->gl_expire + TSoffset,
573 GlineIsLocal(gline) ? me.name : "*",
574 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
578 /* end of gline information */
579 sendto_one(sptr, rpl_str(RPL_ENDOFGLIST), me.name, sptr->name);
584 gline_stats(struct Client *sptr)
587 struct Gline *sgline;
589 for (gline = GlobalGlineList; gline; gline = sgline) {
590 sgline = gline->gl_next;
592 if (gline->gl_expire <= CurrentTime)
595 sendto_one(sptr, rpl_str(RPL_STATSGLINE), me.name, sptr->name, 'G',
596 gline->gl_user, gline->gl_host, gline->gl_expire + TSoffset,