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_string.h"
33 #include "sys.h" /* FALSE bleah */
37 struct Gline* GlobalGlineList = 0;
38 struct Gline* BadChanGlineList = 0;
41 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
45 if (!(tmp = strchr(userhost, '@'))) {
56 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
62 agline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
65 gline->gl_expire = expire; /* initialize gline... */
66 gline->gl_lastmod = lastmod;
67 gline->gl_flags = flags & GLINE_MASK;
69 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
70 DupString(gline->gl_user, userhost); /* first, remember channel */
73 gline->gl_next = BadChanGlineList; /* then link it into list */
74 gline->gl_prev_p = &BadChanGlineList;
76 BadChanGlineList->gl_prev_p = &agline->gl_next;
77 BadChanGlineList = agline;
79 canon_userhost(userhost, &user, &host, "*"); /* find user and host */
81 DupString(gline->gl_user, user); /* remember them... */
82 DupString(gline->gl_host, host);
84 if (check_if_ipmask(host)) /* mark if it's an IP mask */
85 gline->gl_flags |= GLINE_IPMASK;
87 gline->gl_next = GlobalGlineList; /* then link it into list */
88 gline->gl_prev_p = &GlobalGlineList;
90 GlobalGlineList->gl_prev_p = &agline->gl_next;
91 GlobalGlineList = agline;
98 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
100 struct Client *acptr;
101 int fd, retval = 0, tval;
103 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
106 for (fd = HighestFd; fd >= 0; --fd) {
110 if ((acptr = LocalClientArray[fd])) {
114 if ((GlineIsIpMask(gline) ? match(gline->host, acptr->sock_ip) :
115 match(gline->host, acptr->sockhost)) == 0 &&
116 (!acptr->user->username ||
117 match(gline->name, acptr->user->username) == 0)) {
118 /* ok, here's one that got G-lined */
119 sendto_one(acptr, ":%s %d %s :*** %s.", me.name, ERR_YOUREBANNEDCREEP,
120 acptr->name, gline->reason);
122 /* let the ops know about it */
123 sendto_op_mask(SNO_GLINE, "G-line active for %s",
124 get_client_name(acptr, FALSE));
126 /* and get rid of him */
127 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
129 retval = tval; /* retain killed status */
138 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
140 if (GlineIsLocal(gline)) /* don't propagate local glines */
143 if (IsUser(sptr)) { /* select appropriate source */
144 assert(0 != gline->gl_lastmod);
145 sendto_serv_butone(cptr, "%s%s " TOK_GLINE " * %c%s%s%s " TIME_T_FMT " "
146 TIME_T_FMT " :%s", NumNick(sptr),
147 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
148 GlineIsBadChan(gline) ? "" : "@",
149 GlineIsBadChan(gline) ? "" : gline->gl_host,
150 gline->gl_expire - TStime(), gline->gl_lastmod,
153 if (gline->gl_lastmod)
154 sendto_serv_butone(cptr, "%s " TOK_GLINE " * %c%s%s%s " TIME_T_FMT " "
155 TIME_T_FMT " :%s", NumServ(sptr),
156 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
157 GlineIsBadChan(gline) ? "" : "@",
158 GlineIsBadChan(gline) ? "" : gline->gl_host,
159 gline->gl_expire - TStime(), gline->gl_lastmod,
162 sendto_serv_butone(cptr, "%s " TOK_GLINE " * %c%s%s%s " TIME_T_FMT
163 " :%s", NumServ(sptr),
164 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
165 GlineIsBadChan(gline) ? "" : "@",
166 GlineIsBadChan(gline) ? "" : gline->gl_host,
167 gline->gl_expire - TStime(), gline->gl_reason);
172 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
173 char *reason, time_t expire, time_t lastmod, unsigned int flags)
175 struct Gline *agline;
177 assert(0 != userhost);
181 * You cannot set a negative (or zero) expire time, nor can you set an
182 * expiration time for greater than GLINE_MAX_EXPIRE.
184 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
185 if (!IsServer(sptr) && MyConnect(sptr))
186 send_error_to_client(sptr, ERR_BADEXPIRE, expire);
190 expire += TStime(); /* convert from lifetime to timestamp */
192 /* NO_OLD_GLINE allows *@#channel to work correctly */
194 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
195 # ifndef NO_OLD_GLINE
196 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
197 # endif /* OLD_GLINE */
199 # ifndef LOCAL_BADCHAN
200 if (flags & GLINE_LOCAL)
203 flags |= GLINE_BADCHAN;
208 sendto_op_mask(SNO_GLINE, "%s adding %s %s for %s, expiring at "
210 IsServer(sptr) ? sptr->name : sptr->user->server->name,
211 flags & GLINE_LOCAL ? "local" : "global",
212 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
218 write_log(GPATH, "# " TIME_T_FMT " %s adding %s %s for %s, expiring at "
219 TIME_T_FMT ": %s\n", TStime(), sptr->name,
220 flags & GLINE_LOCAL ? "local" : "global",
221 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost, expire,
224 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s adding %s %s for %s, "
225 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
226 sptr->user->username, sptr->user->host,
227 flags & GLINE_LOCAL ? "local" : "global",
228 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost, expire,
233 agline = make_gline(userhost, reason, expire, lastmod, flags);
235 propagate_gline(cptr, sptr, agline);
237 if (GlineIsBadChan(agline))
241 /* this can be inserted into the conf */
242 write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
243 GlineHost(agline), GlineReason(agline), GlineUser(agline));
246 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
250 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
255 gline->gl_flags |= GLINE_ACTIVE;
256 gline->gl_lastmod = lastmod;
258 /* Inform ops and log it */
259 sendto_op_mask(SNO_GLINE, "%s activating %s %s for %s%s%s, expiring at "
261 IsServer(sptr) ? sptr->name : sptr->user->server->name,
262 GlineIsLocal(gline) ? "local" : "global",
263 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
264 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
265 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
270 write_log(GPATH, "# " TIME_T_FMT " %s activating %s %s for %s%s%s, "
271 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
272 GlineIsLocal(gline) ? "local" : "global",
273 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
274 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
275 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
278 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s activating %s %s for "
279 "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
280 sptr->user->username, sptr->user->host,
281 GlineIsLocal(gline) ? "local" : "global",
282 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
283 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
284 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
288 propagate_gline(cptr, sptr, gline);
290 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
294 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
299 gline->gl_flags &= ~GLINE_ACTIVE;
300 gline->gl_lastmod = lastmod;
302 /* Inform ops and log it */
303 sendto_op_mask(SNO_GLINE, "%s deactivating %s %s for %s%s%s, expiring at "
305 IsServer(sptr) ? sptr->name : sptr->user->server->name,
306 GlineIsLocal(gline) ? "local" : "global",
307 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
308 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
309 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
314 write_log(GPATH, "# " TIME_T_FMT " %s deactivating %s %s for %s%s%s, "
315 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
316 GlineIsLocal(gline) ? "local" : "global",
317 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
318 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
319 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
322 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s deactivating %s %s for "
323 "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
324 sptr->user->username, sptr->user->host,
325 GlineIsLocal(gline) ? "local" : "global",
326 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
327 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
328 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
332 propagate_gline(cptr, sptr, gline);
338 gline_find(char *userhost, unsigned int flags)
341 struct Gline *sgline;
342 char *user, *host, *t_uh;
344 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
345 for (gline = BadChanGlineList; gline; gline = sgline) {
346 sgline = gline->gl_next;
348 if (gline->gl_expire <= TStime())
350 else if (match(gline->gl_user, userhost) == 0)
355 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN)
358 DupString(t_uh, userhost);
359 canon_userhost(t_uh, &user, &host, 0);
361 for (gline = GlobalGlineList; gline; gline = sgline) {
362 sgline = gline->gl_next;
364 if (gline->gl_expire <= TStime())
366 else if (match(gline->host, host) == 0 &&
367 ((!user && ircd_strcmp(gline->user, "*") == 0) ||
368 match(gline->user, user) == 0))
378 gline_lookup(struct Client *cptr)
381 struct Gline *sgline;
383 for (gline = GlobalGlineList; gline; gline = sgline) {
384 sgline = gline->gl_next;
386 if (gline->gl_expire <= TStime())
388 else if ((GlineIsIpMask(gline) ?
389 match(gline->gl_host, cptr->sock_ip) :
390 match(gline->gl_host, cptr->sockhost)) == 0 &&
391 match(gline->gl_user, cptr->user->username) == 0)
399 gline_free(struct Gline *gline)
403 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
405 gline->gl_next->gl_prev_p = gline->gl_prev_p;
407 MyFree(gline->gl_user); /* free up the memory */
409 MyFree(gline->gl_host);
410 MyFree(gline->gl_reason);
415 gline_burst(struct Client *cptr)
418 struct Gline *sgline;
420 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
421 sgline = gline->gl_next;
423 if (gline->gl_expire <= TStime()) /* expire any that need expiring */
425 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
426 sendto_one(cptr, "%s " TOK_GLINE " * %c%s@%s " TIME_T_FMT " " TIME_T_FMT
427 " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
428 gline->gl_user, gline->gl_host, gline->gl_expire - TStime(),
429 gline->gl_lastmod, gline->gl_reason);
432 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
433 sgline = gline->gl_next;
435 if (gline->gl_expire <= TStime()) /* expire any that need expiring */
437 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
438 sendto_one(cptr, "%s " TOK_GLINE " * %c%s " TIME_T_FMT " " TIME_T_FMT
439 " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
440 gline->gl_user, gline->gl_expire - TStime(),
441 gline->gl_lastmod, gline->gl_reason);
446 gline_resend(struct Client *cptr, struct Gline *gline)
448 if (GlineIsLocal(gline) || !gline->gl_lastmod)
451 sendto_one(cptr, "%s " TOK_GLINE " * %c%s%s%s " TIME_T_FMT " " TIME_T_FMT
452 " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
453 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
454 GlineIsBadChan(gline) ? "" : gline->gl_host,
455 gline->gl_expire - TStime(), gline->gl_lastmod, gline->gl_reason);
461 gline_list(struct Client *sptr, char *userhost)
464 struct Gline *sgline;
467 if (!(gline = gline_find(userhost, GLINE_ANY))) { /* no such gline */
468 send_error_to_client(sptr, ERR_NOSUCHGLINE, userhost);
472 /* send gline information along */
473 sendto_one(sptr, rpl_str(GLIST), me.name, sptr->name, gline->gl_user,
474 GlineIsBadChan(gline) ? "" : "@",
475 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
476 GlineIsLocal(gline) ? me.name : "*",
477 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
479 for (gline = GlobalGlineList; gline; gline = sgline) {
480 sgline = gline->gl_next;
482 if (gline->gl_expire <= TStime())
485 sendto_one(sptr, rpl_str(GLIST), me.name, sptr->name, gline->gl_user,
486 "@", gline->gl_host, gline->gl_expire,
487 GlineIsLocal(gline) ? me.name : "*",
488 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
491 for (gline = BadChanGlineList; gline; gline = sgline) {
492 sgline = gline->gl_next;
494 if (gline->gl_expire <= TStime())
497 sendto_one(sptr, rpl_str(GLIST), me.name, sptr->name, gline->gl_user,
498 "", "", gline->gl_expire,
499 GlineIsLocal(gline) ? me.name : "*",
500 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
504 /* end of gline information */
505 sendto_one(cptr, rpl_str(RPL_ENDOFGLIST), me.name, sptr->name);
510 gline_stats(struct Client *sptr)
513 struct Gline *sgline;
515 for (gline = GlobalGlineList; gline; gline = sgline) {
516 sgline = gline->gl_next;
518 if (gline->gl_expire <= TStime())
521 sendto_one(sptr, rpl_str(RPL_STATSGLINE), me.name, sptr->name, 'G',
522 gline->gl_user, gline->gl_host, gline->gl_expire,
528 #if 0 /* forget about it! */
529 struct Gline *make_gline(int is_ipmask, char *host, char *reason,
530 char *name, time_t expire)
532 struct Gline *agline;
536 if ('#' == *host || '&' == *host || '+' == *host)
537 gtype = 1; /* BAD CHANNEL GLINE */
540 agline = (struct Gline*) MyMalloc(sizeof(struct Gline)); /* alloc memory */
542 DupString(agline->host, host); /* copy vital information */
543 DupString(agline->reason, reason);
544 DupString(agline->name, name);
545 agline->expire = expire;
546 agline->gflags = GLINE_ACTIVE; /* gline is active */
548 SetGlineIsIpMask(agline);
552 agline->next = BadChanGlineList; /* link it into the list */
553 return (BadChanGlineList = agline);
556 agline->next = GlobalGlineList; /* link it into the list */
557 return (GlobalGlineList = agline);
560 struct Gline *find_gline(struct Client *cptr, struct Gline **pgline)
562 struct Gline* gline = GlobalGlineList;
563 struct Gline* prev = 0;
567 * look through all glines
569 if (gline->expire <= TStime()) {
571 * handle expired glines
573 free_gline(gline, prev);
574 gline = prev ? prev->next : GlobalGlineList;
576 break; /* gline == NULL means gline == NULL */
580 /* Does gline match? */
581 /* Added a check against the user's IP address as well -Kev */
582 if ((GlineIsIpMask(gline) ?
583 match(gline->host, ircd_ntoa((const char*) &cptr->ip)) :
584 match(gline->host, cptr->sockhost)) == 0 &&
585 match(gline->name, cptr->user->username) == 0) {
587 *pgline = prev; /* If they need it, give them the previous gline
588 entry (probably for free_gline, below) */
596 return 0; /* found no glines */
599 void free_gline(struct Gline* gline, struct Gline* prev)
603 prev->next = gline->next; /* squeeze agline out */
606 assert(0 != gline->host);
607 if ('#' == *gline->host ||
608 '&' == *gline->host ||
609 '+' == *gline->host) {
610 BadChanGlineList = gline->next;
614 GlobalGlineList = gline->next;
617 MyFree(gline->host); /* and free up the memory */
618 MyFree(gline->reason);
623 void gline_remove_expired(time_t now)
626 struct Gline* prev = 0;
628 for (gline = GlobalGlineList; gline; gline = gline->next) {
629 if (gline->expire < now) {
630 free_gline(gline, prev);
631 gline = (prev) ? prev : GlobalGlineList;
641 int bad_channel(const char* name)
643 struct Gline* agline = BadChanGlineList;
647 if ((agline->gflags & GLINE_ACTIVE) && (agline->expire > TStime()) &&
648 !mmatch(agline->host, name)) {
651 agline = agline->next;
656 void bad_channel_remove_expired(time_t now)
659 struct Gline* prev = 0;
661 for (gline = BadChanGlineList; gline; gline = gline->next) {
662 if (gline->expire < now) {
663 free_gline(gline, prev);
664 gline = (prev) ? prev : BadChanGlineList;
676 void add_gline(struct Client *sptr, int ip_mask, char *host, char *comment,
677 char *user, time_t expire, int local)
679 struct Client *acptr;
680 struct Gline *agline;
686 if ('#' == *host || '&' == *host || '+' == *host)
687 gtype = 1; /* BAD CHANNEL */
691 sendto_op_mask(SNO_GLINE,
692 "%s adding %s%s for %s@%s, expiring at " TIME_T_FMT ": %s", sptr->name,
693 local ? "local " : "",
694 gtype ? "BADCHAN" : "GLINE", user, host, expire, comment);
698 "# " TIME_T_FMT " %s adding %s %s for %s@%s, expiring at " TIME_T_FMT
699 ": %s\n", TStime(), sptr->name, local ? "local" : "global",
700 gtype ? "BADCHAN" : "GLINE", user, host, expire, comment);
702 /* this can be inserted into the conf */
704 write_log(GPATH, "%c:%s:%s:%s\n", ip_mask ? 'k' : 'K', host, comment,
708 agline = make_gline(ip_mask, host, comment, user, expire);
710 SetGlineIsLocal(agline);
717 for (fd = HighestFd; fd >= 0; --fd) {
721 if ((acptr = LocalClientArray[fd])) {
728 if (!acptr->user || strlen(acptr->sockhost) > HOSTLEN ||
729 (acptr->user->username ? strlen(acptr->user->username) : 0) > HOSTLEN)
730 continue; /* these tests right out of
731 find_kill for safety's sake */
734 if ((GlineIsIpMask(agline) ? match(agline->host, acptr->sock_ip) :
735 match(agline->host, acptr->sockhost)) == 0 &&
736 (!acptr->user->username ||
737 match(agline->name, acptr->user->username) == 0))
740 /* ok, he was the one that got G-lined */
741 sendto_one(acptr, ":%s %d %s :*** %s.", me.name,
742 ERR_YOUREBANNEDCREEP, acptr->name, agline->reason);
744 /* let the ops know about my first kill */
745 sendto_op_mask(SNO_GLINE, "G-line active for %s",
746 get_client_name(acptr, FALSE));
748 /* and get rid of him */
750 exit_client_msg(sptr->from, acptr, &me, "G-lined (%s)", agline->reason);