]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/gline.c
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 2, 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.
21 * @brief Implementation of Gline manipulation functions.
22 * @version $Id: gline.c,v 1.61 2005/09/22 20:42:21 entrope Exp $
30 #include "ircd_alloc.h"
31 #include "ircd_features.h"
33 #include "ircd_reply.h"
34 #include "ircd_snprintf.h"
35 #include "ircd_string.h"
50 /* #include <assert.h> -- Now using assert in ircd_log.h */
55 #define CHECK_APPROVED 0 /**< Mask is acceptable */
56 #define CHECK_OVERRIDABLE 1 /**< Mask is acceptable, but not by default */
57 #define CHECK_REJECTED 2 /**< Mask is totally unacceptable */
59 #define MASK_WILD_0 0x01 /**< Wildcards in the last position */
60 #define MASK_WILD_1 0x02 /**< Wildcards in the next-to-last position */
62 #define MASK_WILD_MASK 0x03 /**< Mask out the positional wildcards */
64 #define MASK_WILDS 0x10 /**< Mask contains wildcards */
65 #define MASK_IP 0x20 /**< Mask is an IP address */
66 #define MASK_HALT 0x40 /**< Finished processing mask */
68 /** List of user G-lines. */
69 struct Gline
* GlobalGlineList
= 0;
70 /** List of BadChan G-lines. */
71 struct Gline
* BadChanGlineList
= 0;
73 /** Find canonical user and host for a string.
74 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
75 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
76 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
78 * @param[in] userhost Input string from user.
79 * @param[out] nick_p Gets pointer to nick part of hostmask.
80 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
81 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
82 * @param[in] def_user Default value for user part.
85 canon_userhost(char *userhost
, char **nick_p
, char **user_p
, char **host_p
, char *def_user
)
89 if (*userhost
== '$') {
96 if ((tmp
= strchr(userhost
, '!'))) {
104 if (!(s
= strchr(tmp
, '@'))) {
114 /** Create a Gline structure.
115 * @param[in] nick Nick part of mask.
116 * @param[in] user User part of mask.
117 * @param[in] host Host part of mask (NULL if not applicable).
118 * @param[in] reason Reason for G-line.
119 * @param[in] expire Expiration timestamp.
120 * @param[in] lastmod Last modification timestamp.
121 * @param[in] flags Bitwise combination of GLINE_* bits.
122 * @return Newly allocated G-line.
124 static struct Gline
*
125 make_gline(char *nick
, char *user
, char *host
, char *reason
, time_t expire
, time_t lastmod
,
128 struct Gline
*gline
, *sgline
, *after
= 0;
130 if (!(flags
& (GLINE_BADCHAN
|GLINE_REALNAME
))) { /* search for overlapping glines first */
132 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
133 sgline
= gline
->gl_next
;
135 if (gline
->gl_expire
<= CurrentTime
)
137 else if (((gline
->gl_flags
& GLINE_LOCAL
) != (flags
& GLINE_LOCAL
)) ||
138 (gline
->gl_host
&& !host
) || (!gline
->gl_host
&& host
))
140 else if (!mmatch(gline
->gl_nick
, nick
) && /* gline contains new mask */
141 !mmatch(gline
->gl_user
, user
)
142 && (gline
->gl_host
== NULL
|| !mmatch(gline
->gl_host
, host
))) {
143 if (expire
<= gline
->gl_expire
) /* will expire before wider gline */
146 after
= gline
; /* stick new gline after this one */
147 } else if (!mmatch(nick
, gline
->gl_nick
) && /* new mask contains gline */
148 !mmatch(user
, gline
->gl_user
)
149 && (gline
->gl_host
==NULL
|| !mmatch(host
, gline
->gl_host
))
150 && gline
->gl_expire
<= expire
) /* old expires before new */
151 gline_free(gline
); /* save some memory */
155 gline
= (struct Gline
*)MyMalloc(sizeof(struct Gline
)); /* alloc memory */
158 DupString(gline
->gl_reason
, reason
); /* initialize gline... */
159 gline
->gl_expire
= expire
;
160 gline
->gl_lastmod
= lastmod
;
161 gline
->gl_flags
= flags
& GLINE_MASK
;
163 if (flags
& GLINE_BADCHAN
) { /* set a BADCHAN gline */
164 DupString(gline
->gl_user
, user
); /* first, remember channel */
167 gline
->gl_next
= BadChanGlineList
; /* then link it into list */
168 gline
->gl_prev_p
= &BadChanGlineList
;
169 if (BadChanGlineList
)
170 BadChanGlineList
->gl_prev_p
= &gline
->gl_next
;
171 BadChanGlineList
= gline
;
174 DupString(gline
->gl_nick
, nick
); /* remember them... */
177 DupString(gline
->gl_user
, user
);
179 DupString(gline
->gl_host
, host
);
181 gline
->gl_host
= NULL
;
183 if (*user
!= '$' && ipmask_parse(host
, &gline
->gl_addr
, &gline
->gl_bits
))
184 gline
->gl_flags
|= GLINE_IPMASK
;
187 gline
->gl_next
= after
->gl_next
;
188 gline
->gl_prev_p
= &after
->gl_next
;
190 after
->gl_next
->gl_prev_p
= &gline
->gl_next
;
191 after
->gl_next
= gline
;
193 gline
->gl_next
= GlobalGlineList
; /* then link it into list */
194 gline
->gl_prev_p
= &GlobalGlineList
;
196 GlobalGlineList
->gl_prev_p
= &gline
->gl_next
;
197 GlobalGlineList
= gline
;
204 /** Check local clients against a new G-line.
205 * If the G-line is inactive, return immediately.
206 * Otherwise, if any users match it, disconnect them or kick them if the G-line is a BADCHAN.
207 * @param[in] cptr Peer connect that sent the G-line.
208 * @param[in] sptr Client that originated the G-line.
209 * @param[in] gline New G-line to check.
210 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
213 do_gline(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
)
215 struct Client
*acptr
;
216 int fd
, retval
= 0, tval
;
218 if (!GlineIsActive(gline
)) /* no action taken on inactive glines */
221 if (GlineIsBadChan(gline
)) {
222 /* Handle BADCHAN gline */
223 struct Channel
*chptr
,*nchptr
;
224 struct Membership
*member
,*nmember
;
226 if (string_has_wildcards(gline
->gl_user
)) {
227 for(chptr
=GlobalChannelList
;chptr
;chptr
=nchptr
) {
229 if (match(gline
->gl_user
, chptr
->chname
))
231 for (member
=chptr
->members
;member
;member
=nmember
) {
232 nmember
=member
->next_member
;
233 if (!MyUser(member
->user
) || IsZombie(member
) || IsAnOper(member
->user
))
235 sendcmdto_serv_butone(&me
, CMD_KICK
, NULL
, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
236 sendcmdto_channel_butserv_butone(&me
, CMD_KICK
, chptr
, NULL
, 0, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
237 make_zombie(member
, member
->user
, &me
, &me
, chptr
);
242 if ((chptr
=FindChannel(gline
->gl_user
))) {
243 for (member
=chptr
->members
;member
;member
=nmember
) {
244 nmember
=member
->next_member
;
245 if (!MyUser(member
->user
) || IsZombie(member
) || IsAnOper(member
->user
))
247 sendcmdto_serv_butone(&me
, CMD_KICK
, NULL
, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
248 sendcmdto_channel_butserv_butone(&me
, CMD_KICK
, chptr
, NULL
, 0, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
249 make_zombie(member
, member
->user
, &me
, &me
, chptr
);
255 for (fd
= HighestFd
; fd
>= 0; --fd
) {
259 if ((acptr
= LocalClientArray
[fd
])) {
260 if (!cli_user(acptr
))
263 if (GlineIsRealName(gline
)) { /* Realname Gline */
264 Debug((DEBUG_DEBUG
,"Realname Gline: %s %s",(cli_info(acptr
)),
266 if (match(gline
->gl_user
+2, cli_info(acptr
)) != 0)
268 Debug((DEBUG_DEBUG
,"Matched!"));
269 } else { /* Host/IP gline */
270 if (cli_name(acptr
) &&
271 match(gline
->gl_nick
, cli_name(acptr
)) !=0)
274 if (cli_user(acptr
)->username
&&
275 match(gline
->gl_user
, (cli_user(acptr
))->realusername
) != 0)
278 if (GlineIsIpMask(gline
)) {
279 if (!ipmask_check(&cli_ip(acptr
), &gline
->gl_addr
, gline
->gl_bits
))
283 if (match(gline
->gl_host
, cli_sockhost(acptr
)) != 0)
288 /* ok, here's one that got G-lined */
289 send_reply(acptr
, SND_EXPLICIT
| ERR_YOUREBANNEDCREEP
, ":%s",
292 /* let the ops know about it */
293 sendto_opmask_butone(0, SNO_GLINE
, "G-line active for %s",
294 get_client_name(acptr
, SHOW_IP
));
296 /* and get rid of him */
297 /* Asuka - Reimplement HEAD_IN_SAND_GLINE from Lain */
298 if ((tval
= exit_client_msg(cptr
, acptr
, &me
,
299 feature_bool(FEAT_HIS_GLINE
) ? "G-lined" : "G-lined (%s)", gline
->gl_reason
)))
300 retval
= tval
; /* retain killed status */
308 * Implements the mask checking applied to local G-lines.
309 * Basically, host masks must have a minimum of two non-wild domain
310 * fields, and IP masks must have a minimum of 16 bits. If the mask
311 * has even one wild-card, OVERRIDABLE is returned, assuming the other
312 * check doesn't fail.
313 * @param[in] mask G-line mask to check.
314 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
317 gline_checkmask(char *mask
)
319 unsigned int flags
= MASK_IP
;
320 unsigned int dots
= 0;
321 unsigned int ipmask
= 0;
323 for (; *mask
; mask
++) { /* go through given mask */
324 if (*mask
== '.') { /* it's a separator; advance positional wilds */
325 flags
= (flags
& ~MASK_WILD_MASK
) | ((flags
<< 1) & MASK_WILD_MASK
);
328 if ((flags
& (MASK_IP
| MASK_WILDS
)) == MASK_IP
)
329 ipmask
+= 8; /* It's an IP with no wilds, count bits */
330 } else if (*mask
== '*' || *mask
== '?')
331 flags
|= MASK_WILD_0
| MASK_WILDS
; /* found a wildcard */
332 else if (*mask
== '/') { /* n.n.n.n/n notation; parse bit specifier */
334 ipmask
= strtoul(mask
, &mask
, 10);
336 /* sanity-check to date */
337 if (*mask
|| (flags
& (MASK_WILDS
| MASK_IP
)) != MASK_IP
)
338 return CHECK_REJECTED
;
341 return CHECK_REJECTED
;
345 if (dots
!= 3 || ipmask
> 32)
346 return CHECK_REJECTED
;
351 flags
|= MASK_HALT
; /* Halt the ipmask calculation */
352 break; /* get out of the loop */
353 } else if (!IsIP6Char(*mask
)) {
354 flags
&= ~MASK_IP
; /* not an IP anymore! */
359 /* Sanity-check quads */
360 if (dots
> 3 || (!(flags
& MASK_WILDS
) && dots
< 3)) {
365 /* update bit count if necessary */
366 if ((flags
& (MASK_IP
| MASK_WILDS
| MASK_HALT
)) == MASK_IP
)
369 /* Check to see that it's not too wide of a mask */
370 if (flags
& MASK_WILDS
&&
371 ((!(flags
& MASK_IP
) && (dots
< 2 || flags
& MASK_WILD_MASK
)) ||
372 (flags
& MASK_IP
&& ipmask
< 16)))
373 return CHECK_REJECTED
; /* to wide, reject */
375 /* Ok, it's approved; require override if it has wildcards, though */
376 return flags
& MASK_WILDS
? CHECK_OVERRIDABLE
: CHECK_APPROVED
;
379 /** Forward a G-line to other servers.
380 * @param[in] cptr Client that sent us the G-line.
381 * @param[in] sptr Client that originated the G-line.
382 * @param[in] gline G-line to forward.
386 gline_propagate(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
)
388 if (GlineIsLocal(gline
) || (IsUser(sptr
) && !gline
->gl_lastmod
))
391 if (gline
->gl_lastmod
)
392 sendcmdto_serv_butone(sptr
, CMD_GLINE
, cptr
, "* %c%s%s%s%s%s %Tu %Tu :%s",
393 GlineIsRemActive(gline
) ? '+' : '-',
394 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
395 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
397 gline
->gl_host
? "@" : "",
398 gline
->gl_host
? gline
->gl_host
: "",
399 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
402 sendcmdto_serv_butone(sptr
, CMD_GLINE
, cptr
,
403 (GlineIsRemActive(gline
) ?
404 "* +%s%s%s%s%s %Tu :%s" : "* -%s%s%s%s%s"),
405 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
406 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
408 gline
->gl_host
? "@" : "",
409 gline
->gl_host
? gline
->gl_host
: "",
410 gline
->gl_expire
- CurrentTime
, gline
->gl_reason
);
415 /** Create a new G-line and add it to global lists.
416 * \a userhost may be in one of four forms:
417 * \li A channel name, to add a BadChan.
418 * \li A string starting with $R and followed by a mask to match against their realname.
419 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
420 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
422 * @param[in] cptr Client that sent us the G-line.
423 * @param[in] sptr Client that originated the G-line.
424 * @param[in] userhost Text mask for the G-line.
425 * @param[in] reason Reason for G-line.
426 * @param[in] expire Duration of G-line in seconds.
427 * @param[in] lastmod Last modification time of G-line.
428 * @param[in] flags Bitwise combination of GLINE_* flags.
429 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
432 gline_add(struct Client
*cptr
, struct Client
*sptr
, char *userhost
,
433 char *reason
, time_t expire
, time_t lastmod
, unsigned int flags
)
435 struct Gline
*agline
;
436 char uhmask
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 3];
437 char *nick
, *user
, *host
;
440 assert(0 != userhost
);
443 if (*userhost
== '#' || *userhost
== '&') {
444 if ((flags
& GLINE_LOCAL
) && !HasPriv(sptr
, PRIV_LOCAL_BADCHAN
))
445 return send_reply(sptr
, ERR_NOPRIVILEGES
);
447 flags
|= GLINE_BADCHAN
;
450 } else if (*userhost
== '$') {
451 switch (*userhost
== '$' ? userhost
[1] : userhost
[3]) {
452 case 'R': flags
|= GLINE_REALNAME
; break;
454 /* uh, what to do here? */
455 /* The answer, my dear Watson, is we throw a protocol_violation()
458 return protocol_violation(sptr
,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr
));
460 sendto_opmask_butone(NULL
, SNO_GLINE
, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr
));
465 user
= (*userhost
=='$' ? userhost
: userhost
+2);
468 canon_userhost(userhost
, &nick
, &user
, &host
, "*");
470 ircd_snprintf(0, uhmask
, sizeof(uhmask
), "%s@%s", user
, host
))
471 return send_reply(sptr
, ERR_LONGMASK
);
472 else if (MyUser(sptr
) || (IsUser(sptr
) && flags
& GLINE_LOCAL
)) {
473 switch (gline_checkmask(host
)) {
474 case CHECK_OVERRIDABLE
: /* oper overrided restriction */
475 if (flags
& GLINE_OPERFORCE
)
479 return send_reply(sptr
, ERR_MASKTOOWIDE
, uhmask
);
483 if ((tmp
= count_users(uhmask
)) >=
484 feature_int(FEAT_GLINEMAXUSERCOUNT
) && !(flags
& GLINE_OPERFORCE
))
485 return send_reply(sptr
, ERR_TOOMANYUSERS
, tmp
);
490 * You cannot set a negative (or zero) expire time, nor can you set an
491 * expiration time for greater than GLINE_MAX_EXPIRE.
493 if (!(flags
& GLINE_FORCE
) && (expire
<= 0 || expire
> GLINE_MAX_EXPIRE
)) {
494 if (!IsServer(sptr
) && MyConnect(sptr
))
495 send_reply(sptr
, ERR_BADEXPIRE
, expire
);
499 expire
+= CurrentTime
; /* convert from lifetime to timestamp */
502 sendto_opmask_butone(0, ircd_strncmp(reason
, "AUTO", 4) ? SNO_GLINE
:
503 SNO_AUTO
, "%s adding %s %s for %s%s%s%s%s, expiring at "
505 (feature_bool(FEAT_HIS_SNOTICES
) || IsServer(sptr
)) ?
507 cli_name((cli_user(sptr
))->server
),
508 (flags
& GLINE_LOCAL
) ? "local" : "global",
509 (flags
& GLINE_BADCHAN
) ? "BADCHAN" : "GLINE",
510 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : nick
,
511 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : "!",
513 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : "@",
514 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : host
,
515 expire
+ TSoffset
, reason
);
518 log_write(LS_GLINE
, L_INFO
, LOG_NOSNOTICE
,
519 "%#C adding %s %s for %s%s%s%s%s, expiring at %Tu: %s", sptr
,
520 flags
& GLINE_LOCAL
? "local" : "global",
521 flags
& GLINE_BADCHAN
? "BADCHAN" : "GLINE",
522 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : nick
,
523 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : "!",
525 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : "@",
526 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : host
,
527 expire
+ TSoffset
, reason
);
530 agline
= make_gline(nick
, user
, host
, reason
, expire
, lastmod
, flags
);
532 if (!agline
) /* if it overlapped, silently return */
535 gline_propagate(cptr
, sptr
, agline
);
537 return do_gline(cptr
, sptr
, agline
); /* knock off users if necessary */
540 /** Activate a currently inactive G-line.
541 * @param[in] cptr Peer that told us to activate the G-line.
542 * @param[in] sptr Client that originally thought it was a good idea.
543 * @param[in] gline G-line to activate.
544 * @param[in] lastmod New value for last modification timestamp.
545 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
546 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
549 gline_activate(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
,
550 time_t lastmod
, unsigned int flags
)
552 unsigned int saveflags
= 0;
556 saveflags
= gline
->gl_flags
;
558 if (flags
& GLINE_LOCAL
)
559 gline
->gl_flags
&= ~GLINE_LDEACT
;
561 gline
->gl_flags
|= GLINE_ACTIVE
;
563 if (gline
->gl_lastmod
) {
564 if (gline
->gl_lastmod
>= lastmod
) /* force lastmod to increase */
567 gline
->gl_lastmod
= lastmod
;
571 if ((saveflags
& GLINE_ACTMASK
) == GLINE_ACTIVE
)
572 return 0; /* was active to begin with */
574 /* Inform ops and log it */
575 sendto_opmask_butone(0, SNO_GLINE
, "%s activating global %s for %s%s%s%s%s, "
576 "expiring at %Tu: %s",
577 (feature_bool(FEAT_HIS_SNOTICES
) || IsServer(sptr
)) ?
579 cli_name((cli_user(sptr
))->server
),
580 GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
581 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
582 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
583 gline
->gl_user
, gline
->gl_host
? "@" : "",
584 gline
->gl_host
? gline
->gl_host
: "",
585 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
587 log_write(LS_GLINE
, L_INFO
, LOG_NOSNOTICE
,
588 "%#C activating global %s for %s%s%s%s%s, expiring at %Tu: %s", sptr
,
589 GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
590 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
591 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
593 gline
->gl_host
? "@" : "",
594 gline
->gl_host
? gline
->gl_host
: "",
595 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
597 if (!(flags
& GLINE_LOCAL
)) /* don't propagate local changes */
598 gline_propagate(cptr
, sptr
, gline
);
600 return do_gline(cptr
, sptr
, gline
);
603 /** Deactivate a G-line.
604 * @param[in] cptr Peer that gave us the message.
605 * @param[in] sptr Client that initiated the deactivation.
606 * @param[in] gline G-line to deactivate.
607 * @param[in] lastmod New value for G-line last modification timestamp.
608 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
612 gline_deactivate(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
,
613 time_t lastmod
, unsigned int flags
)
615 unsigned int saveflags
= 0;
620 saveflags
= gline
->gl_flags
;
622 if (GlineIsLocal(gline
))
623 msg
= "removing local";
624 else if (!gline
->gl_lastmod
&& !(flags
& GLINE_LOCAL
)) {
625 msg
= "removing global";
626 gline
->gl_flags
&= ~GLINE_ACTIVE
; /* propagate a -<mask> */
628 msg
= "deactivating global";
630 if (flags
& GLINE_LOCAL
)
631 gline
->gl_flags
|= GLINE_LDEACT
;
633 gline
->gl_flags
&= ~GLINE_ACTIVE
;
635 if (gline
->gl_lastmod
) {
636 if (gline
->gl_lastmod
>= lastmod
)
639 gline
->gl_lastmod
= lastmod
;
643 if ((saveflags
& GLINE_ACTMASK
) != GLINE_ACTIVE
)
644 return 0; /* was inactive to begin with */
647 /* Inform ops and log it */
648 sendto_opmask_butone(0, SNO_GLINE
, "%s %s %s for %s%s%s%s%s, expiring at %Tu: "
650 (feature_bool(FEAT_HIS_SNOTICES
) || IsServer(sptr
)) ?
652 cli_name((cli_user(sptr
))->server
),
653 msg
, GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
654 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
655 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
656 gline
->gl_user
, gline
->gl_host
? "@" : "",
657 gline
->gl_host
? gline
->gl_host
: "",
658 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
660 log_write(LS_GLINE
, L_INFO
, LOG_NOSNOTICE
,
661 "%#C %s %s for %s%s%s%s%s, expiring at %Tu: %s", sptr
, msg
,
662 GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
663 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
664 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
666 gline
->gl_host
? "@" : "",
667 gline
->gl_host
? gline
->gl_host
: "",
668 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
670 if (!(flags
& GLINE_LOCAL
)) /* don't propagate local changes */
671 gline_propagate(cptr
, sptr
, gline
);
673 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
674 if (GlineIsLocal(gline
) || (!gline
->gl_lastmod
&& !(flags
& GLINE_LOCAL
)))
675 gline_free(gline
); /* get rid of it */
680 /** Find a G-line for a particular mask, guided by certain flags.
681 * Certain bits in \a flags are interpreted specially:
683 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
684 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
685 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
686 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
687 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
688 * <dt>anything else</dt><dd>Search user G-lines.</dd>
690 * @param[in] userhost Mask to search for.
691 * @param[in] flags Bitwise combination of GLINE_* flags.
692 * @return First matching G-line, or NULL if none are found.
695 gline_find(char *userhost
, unsigned int flags
)
698 struct Gline
*sgline
;
699 char *nick
, *user
, *host
, *t_uh
;
701 if (flags
& (GLINE_BADCHAN
| GLINE_ANY
)) {
702 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) {
703 sgline
= gline
->gl_next
;
705 if (gline
->gl_expire
<= CurrentTime
)
707 else if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
708 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
710 else if ((flags
& GLINE_EXACT
? ircd_strcmp(gline
->gl_user
, userhost
) :
711 match(gline
->gl_user
, userhost
)) == 0)
716 if ((flags
& (GLINE_BADCHAN
| GLINE_ANY
)) == GLINE_BADCHAN
||
717 *userhost
== '#' || *userhost
== '&')
720 DupString(t_uh
, userhost
);
721 canon_userhost(t_uh
, &nick
, &user
, &host
, "*");
723 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
724 sgline
= gline
->gl_next
;
726 if (gline
->gl_expire
<= CurrentTime
)
728 else if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
729 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
731 else if (flags
& GLINE_EXACT
) {
732 if (((gline
->gl_host
&& host
&& ircd_strcmp(gline
->gl_host
, host
) == 0)
733 || (!gline
->gl_host
&& !host
)) &&
734 ((!user
&& ircd_strcmp(gline
->gl_user
, "*") == 0) ||
735 (user
&& ircd_strcmp(gline
->gl_user
, user
) == 0)) &&
736 ((!nick
&& gline
->gl_nick
&& ircd_strcmp(gline
->gl_nick
, "*") == 0) ||
737 (nick
&& gline
->gl_nick
&& ircd_strcmp(gline
->gl_nick
, nick
) == 0) || (!gline
->gl_nick
&& !nick
)))
740 if (((gline
->gl_host
&& host
&& match(gline
->gl_host
, host
) == 0)
741 || (!gline
->gl_host
&& !host
)) &&
742 ((!user
&& ircd_strcmp(gline
->gl_user
, "*") == 0) ||
743 (user
&& match(gline
->gl_user
, user
) == 0)) &&
744 ((!nick
&& gline
->gl_nick
&& ircd_strcmp(gline
->gl_nick
, "*") == 0) ||
745 (nick
&& gline
->gl_nick
&& (match(gline
->gl_nick
, nick
) == 0)) || (!gline
->gl_nick
&& !nick
)))
755 /** Find a matching G-line for a user.
756 * @param[in] cptr Client to compare against.
757 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
758 * GLINE_LASTMOD to limit matches.
759 * @return Matching G-line, or NULL if none are found.
762 gline_lookup(struct Client
*cptr
, unsigned int flags
)
765 struct Gline
*sgline
;
767 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
768 sgline
= gline
->gl_next
;
770 if (gline
->gl_expire
<= CurrentTime
) {
775 if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
776 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
779 if (GlineIsRealName(gline
)) {
780 Debug((DEBUG_DEBUG
,"realname gline: '%s' '%s'",gline
->gl_user
,cli_info(cptr
)));
781 if (match(gline
->gl_user
+2, cli_info(cptr
)) != 0)
785 if (match(gline
->gl_nick
, cli_name(cptr
)) != 0)
787 if (match(gline
->gl_user
, (cli_user(cptr
))->realusername
) != 0)
790 if (GlineIsIpMask(gline
)) {
791 if (!ipmask_check(&cli_ip(cptr
), &gline
->gl_addr
, gline
->gl_bits
))
795 if (match(gline
->gl_host
, (cli_user(cptr
))->realhost
) != 0)
799 if (GlineIsActive(gline
))
808 /** Delink and free a G-line.
809 * @param[in] gline G-line to free.
812 gline_free(struct Gline
*gline
)
816 *gline
->gl_prev_p
= gline
->gl_next
; /* squeeze this gline out */
818 gline
->gl_next
->gl_prev_p
= gline
->gl_prev_p
;
820 MyFree(gline
->gl_user
); /* free up the memory */
822 MyFree(gline
->gl_host
);
823 MyFree(gline
->gl_reason
);
827 /** Burst all known global G-lines to another server.
828 * @param[in] cptr Destination of burst.
831 gline_burst(struct Client
*cptr
)
834 struct Gline
*sgline
;
836 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) { /* all glines */
837 sgline
= gline
->gl_next
;
839 if (gline
->gl_expire
<= CurrentTime
) /* expire any that need expiring */
841 else if (!GlineIsLocal(gline
) && gline
->gl_lastmod
)
842 sendcmdto_one(&me
, CMD_GLINE
, cptr
, "* %c%s%s%s%s%s %Tu %Tu :%s",
843 GlineIsRemActive(gline
) ? '+' : '-',
844 gline
->gl_nick
? gline
->gl_nick
: "",
845 gline
->gl_nick
? "!" : "",
848 gline
->gl_host
? "@" : "",
849 gline
->gl_host
? gline
->gl_host
: "",
850 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
854 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) { /* all glines */
855 sgline
= gline
->gl_next
;
857 if (gline
->gl_expire
<= CurrentTime
) /* expire any that need expiring */
859 else if (!GlineIsLocal(gline
) && gline
->gl_lastmod
)
860 sendcmdto_one(&me
, CMD_GLINE
, cptr
, "* %c%s %Tu %Tu :%s",
861 GlineIsRemActive(gline
) ? '+' : '-', gline
->gl_user
,
862 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
867 /** Send a G-line to another server.
868 * @param[in] cptr Who to inform of the G-line.
869 * @param[in] gline G-line to send.
873 gline_resend(struct Client
*cptr
, struct Gline
*gline
)
875 if (GlineIsLocal(gline
) || !gline
->gl_lastmod
)
878 sendcmdto_one(&me
, CMD_GLINE
, cptr
, "* %c%s%s%s %Tu %Tu :%s",
879 GlineIsRemActive(gline
) ? '+' : '-', gline
->gl_user
,
880 gline
->gl_host
? "@" : "",
881 gline
->gl_host
? gline
->gl_host
: "",
882 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
888 /** Display one or all G-lines to a user.
889 * If \a userhost is not NULL, only send the first matching G-line.
890 * Otherwise send the whole list.
891 * @param[in] sptr User asking for G-line list.
892 * @param[in] userhost G-line mask to search for (or NULL).
896 gline_list(struct Client
*sptr
, char *userhost
)
899 struct Gline
*sgline
;
902 if (!(gline
= gline_find(userhost
, GLINE_ANY
))) /* no such gline */
903 return send_reply(sptr
, ERR_NOSUCHGLINE
, userhost
);
905 /* send gline information along */
906 send_reply(sptr
, RPL_GLIST
,
907 GlineIsBadChan(gline
) | GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
908 GlineIsBadChan(gline
) | GlineIsRealName(gline
) ? "" : "!",
909 gline
->gl_user
, GlineIsBadChan(gline
) || GlineIsRealName(gline
) ? "" : "@",
910 gline
->gl_host
? "@" : "",
911 gline
->gl_host
? gline
->gl_host
: "",
912 gline
->gl_expire
+ TSoffset
,
913 GlineIsLocal(gline
) ? cli_name(&me
) : "*",
914 GlineIsActive(gline
) ? '+' : '-', gline
->gl_reason
);
916 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
917 sgline
= gline
->gl_next
;
919 if (gline
->gl_expire
<= CurrentTime
)
922 send_reply(sptr
, RPL_GLIST
,
923 gline
->gl_nick
? gline
->gl_nick
: "",
924 gline
->gl_nick
? "!" : "",
926 gline
->gl_host
? "@" : "",
927 gline
->gl_host
? gline
->gl_host
: "",
928 gline
->gl_expire
+ TSoffset
,
929 GlineIsLocal(gline
) ? cli_name(&me
) : "*",
930 GlineIsActive(gline
) ? '+' : '-', gline
->gl_reason
);
933 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) {
934 sgline
= gline
->gl_next
;
936 if (gline
->gl_expire
<= CurrentTime
)
939 send_reply(sptr
, RPL_GLIST
, gline
->gl_user
, "", "", "", "",
940 gline
->gl_expire
+ TSoffset
,
941 GlineIsLocal(gline
) ? cli_name(&me
) : "*",
942 GlineIsActive(gline
) ? '+' : '-', gline
->gl_reason
);
946 /* end of gline information */
947 return send_reply(sptr
, RPL_ENDOFGLIST
);
950 /** Statistics callback to list G-lines.
951 * @param[in] sptr Client requesting statistics.
952 * @param[in] sd Stats descriptor for request (ignored).
953 * @param[in] param Extra parameter from user (ignored).
956 gline_stats(struct Client
*sptr
, const struct StatDesc
*sd
,
960 struct Gline
*sgline
;
962 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
963 sgline
= gline
->gl_next
;
965 if (gline
->gl_expire
<= CurrentTime
)
968 send_reply(sptr
, RPL_STATSGLINE
, 'G',
969 gline
->gl_nick
? gline
->gl_nick
: "",
970 gline
->gl_nick
? "!" : "",
972 gline
->gl_host
? "@" : "",
973 gline
->gl_host
? gline
->gl_host
: "",
974 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
978 /** Calculate memory used by G-lines.
979 * @param[out] gl_size Number of bytes used by G-lines.
980 * @return Number of G-lines in use.
983 gline_memory_count(size_t *gl_size
)
988 for (gline
= GlobalGlineList
; gline
; gline
= gline
->gl_next
)
991 *gl_size
+= sizeof(struct Gline
);
992 *gl_size
+= gline
->gl_nick
? (strlen(gline
->gl_nick
) +1) : 0;
993 *gl_size
+= gline
->gl_user
? (strlen(gline
->gl_user
) + 1) : 0;
994 *gl_size
+= gline
->gl_host
? (strlen(gline
->gl_host
) + 1) : 0;
995 *gl_size
+= gline
->gl_reason
? (strlen(gline
->gl_reason
) + 1) : 0;
1001 IsNickGlined(struct Client
*cptr
, char *nick
)
1003 struct Gline
*gline
;
1004 struct Gline
*sgline
;
1006 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
1007 sgline
= gline
->gl_next
;
1009 if (gline
->gl_expire
<= CurrentTime
) {
1014 if (GlineIsRealName(gline
)) /* skip realname glines */
1017 if (!ircd_strcmp(gline
->gl_nick
, "*")) /* skip glines w. wildcarded nick */
1020 if (match(gline
->gl_nick
, nick
) != 0)
1023 if (match(gline
->gl_user
, (cli_user(cptr
))->username
) != 0)
1026 if (GlineIsIpMask(gline
)) {
1027 if (!ipmask_check(&(cli_ip(cptr
)), &gline
->gl_addr
, gline
->gl_bits
))
1031 if (match(gline
->gl_host
, (cli_user(cptr
))->realhost
) != 0)