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, gline->gl_lastmod, gline->gl_reason);
152 if (gline->gl_lastmod)
153 sendto_serv_butone(cptr, "%s " TOK_GLINE " * %c%s%s%s " TIME_T_FMT " "
154 TIME_T_FMT " :%s", NumServ(sptr),
155 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
156 GlineIsBadChan(gline) ? "" : "@",
157 GlineIsBadChan(gline) ? "" : gline->gl_host,
158 gline->gl_expire, gline->gl_lastmod,
161 sendto_serv_butone(cptr, "%s " TOK_GLINE " * %c%s%s%s " TIME_T_FMT
162 " :%s", NumServ(sptr),
163 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
164 GlineIsBadChan(gline) ? "" : "@",
165 GlineIsBadChan(gline) ? "" : gline->gl_host,
166 gline->gl_expire, gline->gl_reason);
171 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
172 char *reason, time_t expire, time_t lastmod, unsigned int flags)
174 struct Gline *agline;
176 assert(0 != userhost);
180 * You cannot set a negative (or zero) expire time, nor can you set an
181 * expiration time for greater than GLINE_MAX_EXPIRE.
183 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
184 if (!IsServer(sptr) && MyConnect(sptr))
185 send_error_to_client(sptr, ERR_BADEXPIRE, expire);
189 expire += TStime(); /* convert from lifetime to timestamp */
192 if (*userhost == '#' || *userhost == '&' || *userhost == '+')
193 flags |= GLINE_BADCHAN;
197 sendto_op_mask(SNO_GLINE, "%s adding %s %s for %s, expiring at "
199 IsServer(sptr) ? sptr->name : sptr->user->server->name,
200 flags & GLINE_LOCAL ? "local" : "global",
201 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
207 write_log(GPATH, "# " TIME_T_FMT " %s adding %s %s for %s, expiring at "
208 TIME_T_FMT ": %s\n", TStime(), sptr->name,
209 flags & GLINE_LOCAL ? "local" : "global",
210 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost, expire,
213 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s adding %s %s for %s, "
214 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
215 sptr->user->username, sptr->user->host,
216 flags & GLINE_LOCAL ? "local" : "global",
217 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost, expire,
222 agline = make_gline(userhost, reason, expire, lastmod, flags);
224 propagate_gline(cptr, sptr, agline);
226 if (GlineIsBadChan(agline))
230 /* this can be inserted into the conf */
231 write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
232 GlineHost(agline), GlineReason(agline), GlineUser(agline));
235 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
239 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
244 gline->gl_flags |= GLINE_ACTIVE;
245 gline->gl_lastmod = lastmod;
247 /* Inform ops and log it */
248 sendto_op_mask(SNO_GLINE, "%s activating %s %s for %s%s%s, expiring at "
250 IsServer(sptr) ? sptr->name : sptr->user->server->name,
251 GlineIsLocal(gline) ? "local" : "global",
252 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
253 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
254 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
259 write_log(GPATH, "# " TIME_T_FMT " %s activating %s %s for %s%s%s, "
260 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
261 GlineIsLocal(gline) ? "local" : "global",
262 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
263 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
264 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
267 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s activating %s %s for "
268 "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
269 sptr->user->username, sptr->user->host,
270 GlineIsLocal(gline) ? "local" : "global",
271 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
272 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
273 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
277 propagate_gline(cptr, sptr, gline);
279 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
283 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
288 gline->gl_flags &= ~GLINE_ACTIVE;
289 gline->gl_lastmod = lastmod;
291 /* Inform ops and log it */
292 sendto_op_mask(SNO_GLINE, "%s deactivating %s %s for %s%s%s, expiring at "
294 IsServer(sptr) ? sptr->name : sptr->user->server->name,
295 GlineIsLocal(gline) ? "local" : "global",
296 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
297 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
298 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
303 write_log(GPATH, "# " TIME_T_FMT " %s deactivating %s %s for %s%s%s, "
304 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
305 GlineIsLocal(gline) ? "local" : "global",
306 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
307 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
308 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
311 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s deactivating %s %s for "
312 "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
313 sptr->user->username, sptr->user->host,
314 GlineIsLocal(gline) ? "local" : "global",
315 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
316 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
317 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
321 propagate_gline(cptr, sptr, gline);
327 gline_find(char *userhost, unsigned int flags)
330 struct Gline *sgline;
331 char *user, *host, *t_uh;
333 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
334 for (gline = BadChanGlineList; gline; gline = sgline) {
335 sgline = gline->gl_next;
337 if (gline->gl_expire <= TStime())
339 else if (match(gline->gl_user, userhost) == 0)
344 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN)
347 DupString(t_uh, userhost);
348 canon_userhost(t_uh, &user, &host, 0);
350 for (gline = GlobalGlineList; gline; gline = sgline) {
351 sgline = gline->gl_next;
353 if (gline->gl_expire <= TStime())
355 else if (match(gline->host, host) == 0 &&
356 ((!user && ircd_strcmp(gline->user, "*") == 0) ||
357 match(gline->user, user) == 0))
367 gline_lookup(struct Client *cptr)
370 struct Gline *sgline;
372 for (gline = GlobalGlineList; gline; gline = sgline) {
373 sgline = gline->gl_next;
375 if (gline->gl_expire <= TStime())
377 else if ((GlineIsIpMask(gline) ?
378 match(gline->gl_host, cptr->sock_ip) :
379 match(gline->gl_host, cptr->sockhost)) == 0 &&
380 match(gline->gl_user, cptr->user->username) == 0)
388 gline_free(struct Gline *gline)
392 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
394 gline->gl_next->gl_prev_p = gline->gl_prev_p;
396 MyFree(gline->gl_user); /* free up the memory */
398 MyFree(gline->gl_host);
399 MyFree(gline->gl_reason);
404 gline_burst(struct Client *cptr)
407 struct Gline *sgline;
409 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
410 sgline = gline->gl_next;
412 if (gline->gl_expire <= TStime()) /* expire any that need expiring */
414 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
415 sendto_one(cptr, "%s " TOK_GLINE " * %c%s@%s " TIME_T_FMT " " TIME_T_FMT
416 " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
417 gline->gl_user, gline->gl_host, gline->gl_expire,
418 gline->gl_lastmod, gline->gl_reason);
421 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
422 sgline = gline->gl_next;
424 if (gline->gl_expire <= TStime()) /* expire any that need expiring */
426 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
427 sendto_one(cptr, "%s " TOK_GLINE " * %c%s " TIME_T_FMT " " TIME_T_FMT
428 " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
429 gline->gl_user, gline->gl_expire, gline->gl_lastmod,
435 gline_resend(struct Client *cptr, struct Gline *gline)
437 if (GlineIsLocal(gline) || !gline->gl_lastmod)
440 if (GlineIsBadChan(gline))
441 sendto_one(cptr, "%s " TOK_GLINE " * %c%s " TIME_T_FMT " " TIME_T_FMT
442 " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
443 gline->gl_user, gline->gl_expire, gline->gl_lastmod,
446 sendto_one(cptr, "%s " TOK_GLINE " * %c%s@%s " TIME_T_FMT " " TIME_T_FMT
447 " :%s", NumServ(&me), GlineIsActive(gline) ? '+' : '-',
448 gline->gl_user, gline->gl_host, gline->gl_expire,
449 gline->gl_lastmod, gline->gl_reason);
455 gline_list(struct Client *sptr, char *userhost)
458 struct Gline *sgline;
461 if (!(gline = gline_find(userhost, GLINE_ANY))) { /* no such gline */
462 send_error_to_client(sptr, ERR_NOSUCHGLINE, userhost);
466 /* send gline information along */
467 sendto_one(sptr, rpl_str(GLIST), me.name, sptr->name, gline->gl_user,
468 GlineIsBadChan(gline) ? "" : "@",
469 GlineIsBadChan(gline) ? "" : gline->gl_host, gline->gl_expire,
470 GlineIsLocal(gline) ? me.name : "*",
471 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
473 for (gline = GlobalGlineList; gline; gline = sgline) {
474 sgline = gline->gl_next;
476 if (gline->gl_expire <= TStime())
479 sendto_one(sptr, rpl_str(GLIST), me.name, sptr->name, gline->gl_user,
480 "@", gline->gl_host, gline->gl_expire,
481 GlineIsLocal(gline) ? me.name : "*",
482 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
485 for (gline = BadChanGlineList; gline; gline = sgline) {
486 sgline = gline->gl_next;
488 if (gline->gl_expire <= TStime())
491 sendto_one(sptr, rpl_str(GLIST), me.name, sptr->name, gline->gl_user,
492 "", "", gline->gl_expire,
493 GlineIsLocal(gline) ? me.name : "*",
494 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
498 /* end of gline information */
499 sendto_one(cptr, rpl_str(RPL_ENDOFGLIST), me.name, sptr->name);
504 gline_stats(struct Client *sptr)
507 struct Gline *sgline;
509 for (gline = GlobalGlineList; gline; gline = sgline) {
510 sgline = gline->gl_next;
512 if (gline->gl_expire <= TStime())
515 sendto_one(sptr, rpl_str(RPL_STATSGLINE), me.name, sptr->name, 'G',
516 gline->gl_user, gline->gl_host, gline->gl_expire,
522 #if 0 /* forget about it! */
523 struct Gline *make_gline(int is_ipmask, char *host, char *reason,
524 char *name, time_t expire)
526 struct Gline *agline;
530 if ('#' == *host || '&' == *host || '+' == *host)
531 gtype = 1; /* BAD CHANNEL GLINE */
534 agline = (struct Gline*) MyMalloc(sizeof(struct Gline)); /* alloc memory */
536 DupString(agline->host, host); /* copy vital information */
537 DupString(agline->reason, reason);
538 DupString(agline->name, name);
539 agline->expire = expire;
540 agline->gflags = GLINE_ACTIVE; /* gline is active */
542 SetGlineIsIpMask(agline);
546 agline->next = BadChanGlineList; /* link it into the list */
547 return (BadChanGlineList = agline);
550 agline->next = GlobalGlineList; /* link it into the list */
551 return (GlobalGlineList = agline);
554 struct Gline *find_gline(struct Client *cptr, struct Gline **pgline)
556 struct Gline* gline = GlobalGlineList;
557 struct Gline* prev = 0;
561 * look through all glines
563 if (gline->expire <= TStime()) {
565 * handle expired glines
567 free_gline(gline, prev);
568 gline = prev ? prev->next : GlobalGlineList;
570 break; /* gline == NULL means gline == NULL */
574 /* Does gline match? */
575 /* Added a check against the user's IP address as well -Kev */
576 if ((GlineIsIpMask(gline) ?
577 match(gline->host, ircd_ntoa((const char*) &cptr->ip)) :
578 match(gline->host, cptr->sockhost)) == 0 &&
579 match(gline->name, cptr->user->username) == 0) {
581 *pgline = prev; /* If they need it, give them the previous gline
582 entry (probably for free_gline, below) */
590 return 0; /* found no glines */
593 void free_gline(struct Gline* gline, struct Gline* prev)
597 prev->next = gline->next; /* squeeze agline out */
600 assert(0 != gline->host);
601 if ('#' == *gline->host ||
602 '&' == *gline->host ||
603 '+' == *gline->host) {
604 BadChanGlineList = gline->next;
608 GlobalGlineList = gline->next;
611 MyFree(gline->host); /* and free up the memory */
612 MyFree(gline->reason);
617 void gline_remove_expired(time_t now)
620 struct Gline* prev = 0;
622 for (gline = GlobalGlineList; gline; gline = gline->next) {
623 if (gline->expire < now) {
624 free_gline(gline, prev);
625 gline = (prev) ? prev : GlobalGlineList;
635 int bad_channel(const char* name)
637 struct Gline* agline = BadChanGlineList;
641 if ((agline->gflags & GLINE_ACTIVE) && (agline->expire > TStime()) &&
642 !mmatch(agline->host, name)) {
645 agline = agline->next;
650 void bad_channel_remove_expired(time_t now)
653 struct Gline* prev = 0;
655 for (gline = BadChanGlineList; gline; gline = gline->next) {
656 if (gline->expire < now) {
657 free_gline(gline, prev);
658 gline = (prev) ? prev : BadChanGlineList;
670 void add_gline(struct Client *sptr, int ip_mask, char *host, char *comment,
671 char *user, time_t expire, int local)
673 struct Client *acptr;
674 struct Gline *agline;
680 if ('#' == *host || '&' == *host || '+' == *host)
681 gtype = 1; /* BAD CHANNEL */
685 sendto_op_mask(SNO_GLINE,
686 "%s adding %s%s for %s@%s, expiring at " TIME_T_FMT ": %s", sptr->name,
687 local ? "local " : "",
688 gtype ? "BADCHAN" : "GLINE", user, host, expire, comment);
692 "# " TIME_T_FMT " %s adding %s %s for %s@%s, expiring at " TIME_T_FMT
693 ": %s\n", TStime(), sptr->name, local ? "local" : "global",
694 gtype ? "BADCHAN" : "GLINE", user, host, expire, comment);
696 /* this can be inserted into the conf */
698 write_log(GPATH, "%c:%s:%s:%s\n", ip_mask ? 'k' : 'K', host, comment,
702 agline = make_gline(ip_mask, host, comment, user, expire);
704 SetGlineIsLocal(agline);
711 for (fd = HighestFd; fd >= 0; --fd) {
715 if ((acptr = LocalClientArray[fd])) {
722 if (!acptr->user || strlen(acptr->sockhost) > HOSTLEN ||
723 (acptr->user->username ? strlen(acptr->user->username) : 0) > HOSTLEN)
724 continue; /* these tests right out of
725 find_kill for safety's sake */
728 if ((GlineIsIpMask(agline) ? match(agline->host, acptr->sock_ip) :
729 match(agline->host, acptr->sockhost)) == 0 &&
730 (!acptr->user->username ||
731 match(agline->name, acptr->user->username) == 0))
734 /* ok, he was the one that got G-lined */
735 sendto_one(acptr, ":%s %d %s :*** %s.", me.name,
736 ERR_YOUREBANNEDCREEP, acptr->name, agline->reason);
738 /* let the ops know about my first kill */
739 sendto_op_mask(SNO_GLINE, "G-line active for %s",
740 get_client_name(acptr, FALSE));
742 /* and get rid of him */
744 exit_client_msg(sptr->from, acptr, &me, "G-lined (%s)", agline->reason);