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,
68 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
71 DupString(gline->gl_reason, reason); /* initialize gline... */
72 gline->gl_expire = expire;
73 gline->gl_lastmod = lastmod;
74 gline->gl_flags = flags & GLINE_MASK;
76 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
77 DupString(gline->gl_user, userhost); /* first, remember channel */
80 gline->gl_next = BadChanGlineList; /* then link it into list */
81 gline->gl_prev_p = &BadChanGlineList;
83 BadChanGlineList->gl_prev_p = &gline->gl_next;
84 BadChanGlineList = gline;
86 canon_userhost(userhost, &user, &host, "*"); /* find user and host */
88 DupString(gline->gl_user, user); /* remember them... */
89 DupString(gline->gl_host, host);
91 if (check_if_ipmask(host)) /* mark if it's an IP mask */
92 gline->gl_flags |= GLINE_IPMASK;
94 gline->gl_next = GlobalGlineList; /* then link it into list */
95 gline->gl_prev_p = &GlobalGlineList;
97 GlobalGlineList->gl_prev_p = &gline->gl_next;
98 GlobalGlineList = gline;
105 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
107 struct Client *acptr;
108 int fd, retval = 0, tval;
110 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
113 for (fd = HighestFd; fd >= 0; --fd) {
117 if ((acptr = LocalClientArray[fd])) {
121 if ((GlineIsIpMask(gline) ? match(gline->gl_host, acptr->sock_ip) :
122 match(gline->gl_host, acptr->sockhost)) == 0 &&
123 (!acptr->user->username ||
124 match(gline->gl_user, acptr->user->username) == 0)) {
125 /* ok, here's one that got G-lined */
126 sendto_one(acptr, ":%s %d %s :*** %s.", me.name, ERR_YOUREBANNEDCREEP,
127 acptr->name, gline->gl_reason);
129 /* let the ops know about it */
130 sendto_op_mask(SNO_GLINE, "G-line active for %s",
131 get_client_name(acptr, FALSE));
133 /* and get rid of him */
134 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
136 retval = tval; /* retain killed status */
145 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
147 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
150 if (gline->gl_lastmod)
151 sendcmdto_serv_butone(cptr, CMD_GLINE, sptr, "* %c%s%s%s %Tu %Tu :%s",
152 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
153 GlineIsBadChan(gline) ? "" : "@",
154 GlineIsBadChan(gline) ? "" : gline->gl_host,
155 gline->gl_expire - CurrentTime, gline->gl_lastmod,
158 sendcmdto_serv_butone(cptr, CMD_GLINE, sptr, "* %c%s%s%s %Tu :%s",
159 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
160 GlineIsBadChan(gline) ? "" : "@",
161 GlineIsBadChan(gline) ? "" : gline->gl_host,
162 gline->gl_expire - CurrentTime, gline->gl_reason);
166 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
167 char *reason, time_t expire, time_t lastmod, unsigned int flags)
169 struct Gline *agline;
171 assert(0 != userhost);
175 * You cannot set a negative (or zero) expire time, nor can you set an
176 * expiration time for greater than GLINE_MAX_EXPIRE.
178 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
179 if (!IsServer(sptr) && MyConnect(sptr))
180 send_error_to_client(sptr, ERR_BADEXPIRE, expire);
184 expire += CurrentTime; /* convert from lifetime to timestamp */
186 /* NO_OLD_GLINE allows *@#channel to work correctly */
188 if (*userhost == '#' || *userhost == '&' || *userhost == '+'
189 # ifndef NO_OLD_GLINE
190 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
191 # endif /* OLD_GLINE */
193 # ifndef LOCAL_BADCHAN
194 if (flags & GLINE_LOCAL)
197 flags |= GLINE_BADCHAN;
202 sendto_op_mask(SNO_GLINE, "%s adding %s %s for %s, expiring at "
204 IsServer(sptr) ? sptr->name : sptr->user->server->name,
205 flags & GLINE_LOCAL ? "local" : "global",
206 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
207 expire + TSoffset, reason);
212 write_log(GPATH, "# " TIME_T_FMT " %s adding %s %s for %s, expiring at "
213 TIME_T_FMT ": %s\n", TStime(), sptr->name,
214 flags & GLINE_LOCAL ? "local" : "global",
215 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
216 expire + TSoffset, reason);
218 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s adding %s %s for %s, "
219 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
220 sptr->user->username, sptr->user->host,
221 flags & GLINE_LOCAL ? "local" : "global",
222 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
223 expire + TSoffset, reason);
227 agline = make_gline(userhost, reason, expire, lastmod, flags);
229 propagate_gline(cptr, sptr, agline);
231 if (GlineIsBadChan(agline))
235 /* this can be inserted into the conf */
236 write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
237 GlineHost(agline), GlineReason(agline), GlineUser(agline));
240 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
244 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
245 time_t lastmod, unsigned int flags)
248 assert(!GlineIsLocal(gline));
250 gline->gl_flags |= GLINE_ACTIVE;
252 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
255 gline->gl_lastmod = lastmod;
257 /* Inform ops and log it */
258 sendto_op_mask(SNO_GLINE, "%s activating global %s for %s%s%s, expiring at "
260 IsServer(sptr) ? sptr->name : sptr->user->server->name,
261 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
262 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
263 GlineIsBadChan(gline) ? "" : gline->gl_host,
264 gline->gl_expire + TSoffset, gline->gl_reason);
268 write_log(GPATH, "# " TIME_T_FMT " %s activating global %s for %s%s%s, "
269 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
270 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
271 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
272 GlineIsBadChan(gline) ? "" : gline->gl_host,
273 gline->gl_expire + TSoffset, gline->gl_reason);
275 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s activating %s for "
276 "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
277 sptr->user->username, sptr->user->host,
278 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
279 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
280 GlineIsBadChan(gline) ? "" : gline->gl_host,
281 gline->gl_expire + TSoffset, gline->gl_reason);
284 propagate_gline(cptr, sptr, gline);
286 return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
290 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
291 time_t lastmod, unsigned int flags)
295 if (!GlineIsLocal(gline)) {
296 gline->gl_flags &= ~GLINE_ACTIVE;
298 if (gline->gl_lastmod >= lastmod)
301 gline->gl_lastmod = lastmod;
304 /* Inform ops and log it */
305 sendto_op_mask(SNO_GLINE, "%s %s %s for %s%s%s, expiring at "
307 IsServer(sptr) ? sptr->name : sptr->user->server->name,
308 GlineIsLocal(gline) ? "removing local" :
309 "deactivating global",
310 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
311 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
312 GlineIsBadChan(gline) ? "" : gline->gl_host,
313 gline->gl_expire + TSoffset, gline->gl_reason);
317 write_log(GPATH, "# " TIME_T_FMT " %s %s %s for %s%s%s, "
318 "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
319 GlineIsLocal(gline) ? "removing local" : "deactivating global",
320 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
321 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
322 GlineIsBadChan(gline) ? "" : gline->gl_host,
323 gline->gl_expire + TSoffset, gline->gl_reason);
325 write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s %s %s for "
326 "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
327 sptr->user->username, sptr->user->host,
328 GlineIsLocal(gline) ? "removing local" : "deactivating global",
329 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
330 gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
331 GlineIsBadChan(gline) ? "" : gline->gl_host,
332 gline->gl_expire + TSoffset, gline->gl_reason);
335 if (GlineIsLocal(gline))
338 propagate_gline(cptr, sptr, gline);
344 gline_find(char *userhost, unsigned int flags)
347 struct Gline *sgline;
348 char *user, *host, *t_uh;
350 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
351 for (gline = BadChanGlineList; gline; gline = sgline) {
352 sgline = gline->gl_next;
354 if (gline->gl_expire <= CurrentTime)
356 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
357 match(gline->gl_user, userhost)) == 0)
362 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
363 *userhost == '#' || *userhost == '&' || *userhost == '+'
365 || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
366 #endif /* NO_OLD_GLINE */
370 DupString(t_uh, userhost);
371 canon_userhost(t_uh, &user, &host, 0);
373 for (gline = GlobalGlineList; gline; gline = sgline) {
374 sgline = gline->gl_next;
376 if (gline->gl_expire <= CurrentTime)
378 else if (flags & GLINE_EXACT) {
379 if (ircd_strcmp(gline->gl_host, host) == 0 &&
380 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
381 ircd_strcmp(gline->gl_user, user) == 0))
384 if (match(gline->gl_host, host) == 0 &&
385 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
386 match(gline->gl_user, user) == 0))
397 gline_lookup(struct Client *cptr)
400 struct Gline *sgline;
402 for (gline = GlobalGlineList; gline; gline = sgline) {
403 sgline = gline->gl_next;
405 if (gline->gl_expire <= CurrentTime)
407 else if ((GlineIsIpMask(gline) ?
408 match(gline->gl_host, ircd_ntoa((const char *)&cptr->ip)) :
409 match(gline->gl_host, cptr->user->host)) == 0 &&
410 match(gline->gl_user, cptr->user->username) == 0)
418 gline_free(struct Gline *gline)
422 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
424 gline->gl_next->gl_prev_p = gline->gl_prev_p;
426 MyFree(gline->gl_user); /* free up the memory */
428 MyFree(gline->gl_host);
429 MyFree(gline->gl_reason);
434 gline_burst(struct Client *cptr)
437 struct Gline *sgline;
439 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
440 sgline = gline->gl_next;
442 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
444 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
445 sendcmdto_one(cptr, CMD_GLINE, &me, "* %c%s@%s %Tu %Tu :%s",
446 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
447 gline->gl_host, gline->gl_expire - CurrentTime,
448 gline->gl_lastmod, gline->gl_reason);
451 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
452 sgline = gline->gl_next;
454 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
456 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
457 sendcmdto_one(cptr, CMD_GLINE, &me, "* %c%s %Tu %Tu :%s",
458 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
459 gline->gl_expire - CurrentTime, gline->gl_lastmod,
465 gline_resend(struct Client *cptr, struct Gline *gline)
467 if (GlineIsLocal(gline) || !gline->gl_lastmod)
470 sendcmdto_one(cptr, CMD_GLINE, &me, "* %c%s%s%s %Tu %Tu :%s",
471 GlineIsActive(gline) ? '+' : '-', gline->gl_user,
472 GlineIsBadChan(gline) ? "" : "@",
473 GlineIsBadChan(gline) ? "" : gline->gl_host,
474 gline->gl_expire - CurrentTime, gline->gl_lastmod,
481 gline_list(struct Client *sptr, char *userhost)
484 struct Gline *sgline;
487 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
488 return send_error_to_client(sptr, ERR_NOSUCHGLINE, userhost);
490 /* send gline information along */
491 sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name, gline->gl_user,
492 GlineIsBadChan(gline) ? "" : "@",
493 GlineIsBadChan(gline) ? "" : gline->gl_host,
494 gline->gl_expire + TSoffset,
495 GlineIsLocal(gline) ? me.name : "*",
496 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
498 for (gline = GlobalGlineList; gline; gline = sgline) {
499 sgline = gline->gl_next;
501 if (gline->gl_expire <= CurrentTime)
504 sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name,
505 gline->gl_user, "@", gline->gl_host,
506 gline->gl_expire + TSoffset,
507 GlineIsLocal(gline) ? me.name : "*",
508 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
511 for (gline = BadChanGlineList; gline; gline = sgline) {
512 sgline = gline->gl_next;
514 if (gline->gl_expire <= CurrentTime)
517 sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name,
518 gline->gl_user, "", "", gline->gl_expire + TSoffset,
519 GlineIsLocal(gline) ? me.name : "*",
520 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
524 /* end of gline information */
525 sendto_one(sptr, rpl_str(RPL_ENDOFGLIST), me.name, sptr->name);
530 gline_stats(struct Client *sptr)
533 struct Gline *sgline;
535 for (gline = GlobalGlineList; gline; gline = sgline) {
536 sgline = gline->gl_next;
538 if (gline->gl_expire <= CurrentTime)
541 sendto_one(sptr, rpl_str(RPL_STATSGLINE), me.name, sptr->name, 'G',
542 gline->gl_user, gline->gl_host, gline->gl_expire + TSoffset,