]>
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] user_p Gets pointer to user (or channel/realname) part of hostmask.
80 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
81 * @param[in] def_user Default value for user part.
84 canon_userhost(char *userhost
, char **user_p
, char **host_p
, char *def_user
)
88 if (*userhost
== '$') {
94 if (!(tmp
= strchr(userhost
, '@'))) {
104 /** Create a Gline structure.
105 * @param[in] user User part of mask.
106 * @param[in] host Host part of mask (NULL if not applicable).
107 * @param[in] reason Reason for G-line.
108 * @param[in] expire Expiration timestamp.
109 * @param[in] lastmod Last modification timestamp.
110 * @param[in] flags Bitwise combination of GLINE_* bits.
111 * @return Newly allocated G-line.
113 static struct Gline
*
114 make_gline(char *user
, char *host
, char *reason
, time_t expire
, time_t lastmod
,
117 struct Gline
*gline
, *sgline
, *after
= 0;
119 if (!(flags
& GLINE_BADCHAN
)) { /* search for overlapping glines first */
121 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
122 sgline
= gline
->gl_next
;
124 if (gline
->gl_expire
<= CurrentTime
)
126 else if (((gline
->gl_flags
& GLINE_LOCAL
) != (flags
& GLINE_LOCAL
)) ||
127 (gline
->gl_host
&& !host
) || (!gline
->gl_host
&& host
))
129 else if (!mmatch(gline
->gl_user
, user
) /* gline contains new mask */
130 && (gline
->gl_host
== NULL
|| !mmatch(gline
->gl_host
, host
))) {
131 if (expire
<= gline
->gl_expire
) /* will expire before wider gline */
134 after
= gline
; /* stick new gline after this one */
135 } else if (!mmatch(user
, gline
->gl_user
) /* new mask contains gline */
136 && (gline
->gl_host
==NULL
|| !mmatch(host
, gline
->gl_host
))
137 && gline
->gl_expire
<= expire
) /* old expires before new */
138 gline_free(gline
); /* save some memory */
142 gline
= (struct Gline
*)MyMalloc(sizeof(struct Gline
)); /* alloc memory */
145 DupString(gline
->gl_reason
, reason
); /* initialize gline... */
146 gline
->gl_expire
= expire
;
147 gline
->gl_lastmod
= lastmod
;
148 gline
->gl_flags
= flags
& GLINE_MASK
;
150 if (flags
& GLINE_BADCHAN
) { /* set a BADCHAN gline */
151 DupString(gline
->gl_user
, user
); /* first, remember channel */
154 gline
->gl_next
= BadChanGlineList
; /* then link it into list */
155 gline
->gl_prev_p
= &BadChanGlineList
;
156 if (BadChanGlineList
)
157 BadChanGlineList
->gl_prev_p
= &gline
->gl_next
;
158 BadChanGlineList
= gline
;
160 DupString(gline
->gl_user
, user
); /* remember them... */
162 DupString(gline
->gl_host
, host
);
164 gline
->gl_host
= NULL
;
166 if (*user
!= '$' && ipmask_parse(host
, &gline
->gl_addr
, &gline
->gl_bits
))
167 gline
->gl_flags
|= GLINE_IPMASK
;
170 gline
->gl_next
= after
->gl_next
;
171 gline
->gl_prev_p
= &after
->gl_next
;
173 after
->gl_next
->gl_prev_p
= &gline
->gl_next
;
174 after
->gl_next
= gline
;
176 gline
->gl_next
= GlobalGlineList
; /* then link it into list */
177 gline
->gl_prev_p
= &GlobalGlineList
;
179 GlobalGlineList
->gl_prev_p
= &gline
->gl_next
;
180 GlobalGlineList
= gline
;
187 /** Check local clients against a new G-line.
188 * If the G-line is inactive, return immediately.
189 * Otherwise, if any users match it, disconnect them or kick them if the G-line is a BADCHAN.
190 * @param[in] cptr Peer connect that sent the G-line.
191 * @param[in] sptr Client that originated the G-line.
192 * @param[in] gline New G-line to check.
193 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
196 do_gline(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
)
198 struct Client
*acptr
;
199 int fd
, retval
= 0, tval
;
201 if (!GlineIsActive(gline
)) /* no action taken on inactive glines */
204 if (GlineIsBadChan(gline
)) {
205 /* Handle BADCHAN gline */
206 struct Channel
*chptr
,*nchptr
;
207 struct Membership
*member
,*nmember
;
209 if (string_has_wildcards(gline
->gl_user
)) {
210 for(chptr
=GlobalChannelList
;chptr
;chptr
=nchptr
) {
212 if (match(gline
->gl_user
, chptr
->chname
))
214 for (member
=chptr
->members
;member
;member
=nmember
) {
215 nmember
=member
->next_member
;
216 if (!MyUser(member
->user
) || IsZombie(member
) || IsAnOper(member
->user
))
218 sendcmdto_serv_butone(&me
, CMD_KICK
, NULL
, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
219 sendcmdto_channel_butserv_butone(&me
, CMD_KICK
, chptr
, NULL
, 0, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
220 make_zombie(member
, member
->user
, &me
, &me
, chptr
);
225 if ((chptr
=FindChannel(gline
->gl_user
))) {
226 for (member
=chptr
->members
;member
;member
=nmember
) {
227 nmember
=member
->next_member
;
228 if (!MyUser(member
->user
) || IsZombie(member
) || IsAnOper(member
->user
))
230 sendcmdto_serv_butone(&me
, CMD_KICK
, NULL
, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
231 sendcmdto_channel_butserv_butone(&me
, CMD_KICK
, chptr
, NULL
, 0, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
232 make_zombie(member
, member
->user
, &me
, &me
, chptr
);
238 for (fd
= HighestFd
; fd
>= 0; --fd
) {
242 if ((acptr
= LocalClientArray
[fd
])) {
243 if (!cli_user(acptr
))
246 if (GlineIsRealName(gline
)) { /* Realname Gline */
247 Debug((DEBUG_DEBUG
,"Realname Gline: %s %s",(cli_info(acptr
)),
249 if (match(gline
->gl_user
+2, cli_info(acptr
)) != 0)
251 Debug((DEBUG_DEBUG
,"Matched!"));
252 } else { /* Host/IP gline */
253 if (cli_user(acptr
)->username
&&
254 match(gline
->gl_user
, (cli_user(acptr
))->realusername
) != 0)
257 if (GlineIsIpMask(gline
)) {
258 if (!ipmask_check(&cli_ip(acptr
), &gline
->gl_addr
, gline
->gl_bits
))
262 if (match(gline
->gl_host
, cli_sockhost(acptr
)) != 0)
267 /* ok, here's one that got G-lined */
268 send_reply(acptr
, SND_EXPLICIT
| ERR_YOUREBANNEDCREEP
, ":%s",
271 /* let the ops know about it */
272 sendto_opmask_butone(0, SNO_GLINE
, "G-line active for %s",
273 get_client_name(acptr
, SHOW_IP
));
275 /* and get rid of him */
276 /* Asuka - Reimplement HEAD_IN_SAND_GLINE from Lain */
277 if ((tval
= exit_client_msg(cptr
, acptr
, &me
,
278 feature_bool(FEAT_HIS_GLINE
) ? "G-lined" : "G-lined (%s)", gline
->gl_reason
)))
279 retval
= tval
; /* retain killed status */
287 * Implements the mask checking applied to local G-lines.
288 * Basically, host masks must have a minimum of two non-wild domain
289 * fields, and IP masks must have a minimum of 16 bits. If the mask
290 * has even one wild-card, OVERRIDABLE is returned, assuming the other
291 * check doesn't fail.
292 * @param[in] mask G-line mask to check.
293 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
296 gline_checkmask(char *mask
)
298 unsigned int flags
= MASK_IP
;
299 unsigned int dots
= 0;
300 unsigned int ipmask
= 0;
302 for (; *mask
; mask
++) { /* go through given mask */
303 if (*mask
== '.') { /* it's a separator; advance positional wilds */
304 flags
= (flags
& ~MASK_WILD_MASK
) | ((flags
<< 1) & MASK_WILD_MASK
);
307 if ((flags
& (MASK_IP
| MASK_WILDS
)) == MASK_IP
)
308 ipmask
+= 8; /* It's an IP with no wilds, count bits */
309 } else if (*mask
== '*' || *mask
== '?')
310 flags
|= MASK_WILD_0
| MASK_WILDS
; /* found a wildcard */
311 else if (*mask
== '/') { /* n.n.n.n/n notation; parse bit specifier */
313 ipmask
= strtoul(mask
, &mask
, 10);
315 /* sanity-check to date */
316 if (*mask
|| (flags
& (MASK_WILDS
| MASK_IP
)) != MASK_IP
)
317 return CHECK_REJECTED
;
320 return CHECK_REJECTED
;
324 if (dots
!= 3 || ipmask
> 32)
325 return CHECK_REJECTED
;
330 flags
|= MASK_HALT
; /* Halt the ipmask calculation */
331 break; /* get out of the loop */
332 } else if (!IsIP6Char(*mask
)) {
333 flags
&= ~MASK_IP
; /* not an IP anymore! */
338 /* Sanity-check quads */
339 if (dots
> 3 || (!(flags
& MASK_WILDS
) && dots
< 3)) {
344 /* update bit count if necessary */
345 if ((flags
& (MASK_IP
| MASK_WILDS
| MASK_HALT
)) == MASK_IP
)
348 /* Check to see that it's not too wide of a mask */
349 if (flags
& MASK_WILDS
&&
350 ((!(flags
& MASK_IP
) && (dots
< 2 || flags
& MASK_WILD_MASK
)) ||
351 (flags
& MASK_IP
&& ipmask
< 16)))
352 return CHECK_REJECTED
; /* to wide, reject */
354 /* Ok, it's approved; require override if it has wildcards, though */
355 return flags
& MASK_WILDS
? CHECK_OVERRIDABLE
: CHECK_APPROVED
;
358 /** Forward a G-line to other servers.
359 * @param[in] cptr Client that sent us the G-line.
360 * @param[in] sptr Client that originated the G-line.
361 * @param[in] gline G-line to forward.
365 gline_propagate(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
)
367 if (GlineIsLocal(gline
) || (IsUser(sptr
) && !gline
->gl_lastmod
))
370 if (gline
->gl_lastmod
)
371 sendcmdto_serv_butone(sptr
, CMD_GLINE
, cptr
, "* %c%s%s%s %Tu %Tu :%s",
372 GlineIsRemActive(gline
) ? '+' : '-', gline
->gl_user
,
373 gline
->gl_host
? "@" : "",
374 gline
->gl_host
? gline
->gl_host
: "",
375 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
378 sendcmdto_serv_butone(sptr
, CMD_GLINE
, cptr
,
379 (GlineIsRemActive(gline
) ?
380 "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
382 gline
->gl_host
? "@" : "",
383 gline
->gl_host
? gline
->gl_host
: "",
384 gline
->gl_expire
- CurrentTime
, gline
->gl_reason
);
389 /** Create a new G-line and add it to global lists.
390 * \a userhost may be in one of four forms:
391 * \li A channel name, to add a BadChan.
392 * \li A string starting with $R and followed by a mask to match against their realname.
393 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
394 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
396 * @param[in] cptr Client that sent us the G-line.
397 * @param[in] sptr Client that originated the G-line.
398 * @param[in] userhost Text mask for the G-line.
399 * @param[in] reason Reason for G-line.
400 * @param[in] expire Duration of G-line in seconds.
401 * @param[in] lastmod Last modification time of G-line.
402 * @param[in] flags Bitwise combination of GLINE_* flags.
403 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
406 gline_add(struct Client
*cptr
, struct Client
*sptr
, char *userhost
,
407 char *reason
, time_t expire
, time_t lastmod
, unsigned int flags
)
409 struct Gline
*agline
;
410 char uhmask
[USERLEN
+ HOSTLEN
+ 2];
414 assert(0 != userhost
);
417 if (*userhost
== '#' || *userhost
== '&') {
418 if ((flags
& GLINE_LOCAL
) && !HasPriv(sptr
, PRIV_LOCAL_BADCHAN
))
419 return send_reply(sptr
, ERR_NOPRIVILEGES
);
421 flags
|= GLINE_BADCHAN
;
424 } else if (*userhost
== '$') {
425 switch (*userhost
== '$' ? userhost
[1] : userhost
[3]) {
426 case 'R': flags
|= GLINE_REALNAME
; break;
428 /* uh, what to do here? */
429 /* The answer, my dear Watson, is we throw a protocol_violation()
432 return protocol_violation(sptr
,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr
));
434 sendto_opmask_butone(NULL
, SNO_GLINE
, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr
));
439 user
= (*userhost
=='$' ? userhost
: userhost
+2);
442 canon_userhost(userhost
, &user
, &host
, "*");
444 ircd_snprintf(0, uhmask
, sizeof(uhmask
), "%s@%s", user
, host
))
445 return send_reply(sptr
, ERR_LONGMASK
);
446 else if (MyUser(sptr
) || (IsUser(sptr
) && flags
& GLINE_LOCAL
)) {
447 switch (gline_checkmask(host
)) {
448 case CHECK_OVERRIDABLE
: /* oper overrided restriction */
449 if (flags
& GLINE_OPERFORCE
)
453 return send_reply(sptr
, ERR_MASKTOOWIDE
, uhmask
);
457 if ((tmp
= count_users(uhmask
)) >=
458 feature_int(FEAT_GLINEMAXUSERCOUNT
) && !(flags
& GLINE_OPERFORCE
))
459 return send_reply(sptr
, ERR_TOOMANYUSERS
, tmp
);
464 * You cannot set a negative (or zero) expire time, nor can you set an
465 * expiration time for greater than GLINE_MAX_EXPIRE.
467 if (!(flags
& GLINE_FORCE
) && (expire
<= 0 || expire
> GLINE_MAX_EXPIRE
)) {
468 if (!IsServer(sptr
) && MyConnect(sptr
))
469 send_reply(sptr
, ERR_BADEXPIRE
, expire
);
473 expire
+= CurrentTime
; /* convert from lifetime to timestamp */
476 sendto_opmask_butone(0, ircd_strncmp(reason
, "AUTO", 4) ? SNO_GLINE
:
477 SNO_AUTO
, "%s adding %s %s for %s%s%s, expiring at "
479 (feature_bool(FEAT_HIS_SNOTICES
) || IsServer(sptr
)) ?
481 cli_name((cli_user(sptr
))->server
),
482 (flags
& GLINE_LOCAL
) ? "local" : "global",
483 (flags
& GLINE_BADCHAN
) ? "BADCHAN" : "GLINE", user
,
484 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : "@",
485 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : host
,
486 expire
+ TSoffset
, reason
);
489 log_write(LS_GLINE
, L_INFO
, LOG_NOSNOTICE
,
490 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr
,
491 flags
& GLINE_LOCAL
? "local" : "global",
492 flags
& GLINE_BADCHAN
? "BADCHAN" : "GLINE", user
,
493 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : "@",
494 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : host
,
495 expire
+ TSoffset
, reason
);
498 agline
= make_gline(user
, host
, reason
, expire
, lastmod
, flags
);
500 if (!agline
) /* if it overlapped, silently return */
503 gline_propagate(cptr
, sptr
, agline
);
505 return do_gline(cptr
, sptr
, agline
); /* knock off users if necessary */
508 /** Activate a currently inactive G-line.
509 * @param[in] cptr Peer that told us to activate the G-line.
510 * @param[in] sptr Client that originally thought it was a good idea.
511 * @param[in] gline G-line to activate.
512 * @param[in] lastmod New value for last modification timestamp.
513 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
514 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
517 gline_activate(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
,
518 time_t lastmod
, unsigned int flags
)
520 unsigned int saveflags
= 0;
524 saveflags
= gline
->gl_flags
;
526 if (flags
& GLINE_LOCAL
)
527 gline
->gl_flags
&= ~GLINE_LDEACT
;
529 gline
->gl_flags
|= GLINE_ACTIVE
;
531 if (gline
->gl_lastmod
) {
532 if (gline
->gl_lastmod
>= lastmod
) /* force lastmod to increase */
535 gline
->gl_lastmod
= lastmod
;
539 if ((saveflags
& GLINE_ACTMASK
) == GLINE_ACTIVE
)
540 return 0; /* was active to begin with */
542 /* Inform ops and log it */
543 sendto_opmask_butone(0, SNO_GLINE
, "%s activating global %s for %s%s%s, "
544 "expiring at %Tu: %s",
545 (feature_bool(FEAT_HIS_SNOTICES
) || IsServer(sptr
)) ?
547 cli_name((cli_user(sptr
))->server
),
548 GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
549 gline
->gl_user
, gline
->gl_host
? "@" : "",
550 gline
->gl_host
? gline
->gl_host
: "",
551 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
553 log_write(LS_GLINE
, L_INFO
, LOG_NOSNOTICE
,
554 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr
,
555 GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE", gline
->gl_user
,
556 gline
->gl_host
? "@" : "",
557 gline
->gl_host
? gline
->gl_host
: "",
558 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
560 if (!(flags
& GLINE_LOCAL
)) /* don't propagate local changes */
561 gline_propagate(cptr
, sptr
, gline
);
563 return do_gline(cptr
, sptr
, gline
);
566 /** Deactivate a G-line.
567 * @param[in] cptr Peer that gave us the message.
568 * @param[in] sptr Client that initiated the deactivation.
569 * @param[in] gline G-line to deactivate.
570 * @param[in] lastmod New value for G-line last modification timestamp.
571 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
575 gline_deactivate(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
,
576 time_t lastmod
, unsigned int flags
)
578 unsigned int saveflags
= 0;
583 saveflags
= gline
->gl_flags
;
585 if (GlineIsLocal(gline
))
586 msg
= "removing local";
587 else if (!gline
->gl_lastmod
&& !(flags
& GLINE_LOCAL
)) {
588 msg
= "removing global";
589 gline
->gl_flags
&= ~GLINE_ACTIVE
; /* propagate a -<mask> */
591 msg
= "deactivating global";
593 if (flags
& GLINE_LOCAL
)
594 gline
->gl_flags
|= GLINE_LDEACT
;
596 gline
->gl_flags
&= ~GLINE_ACTIVE
;
598 if (gline
->gl_lastmod
) {
599 if (gline
->gl_lastmod
>= lastmod
)
602 gline
->gl_lastmod
= lastmod
;
606 if ((saveflags
& GLINE_ACTMASK
) != GLINE_ACTIVE
)
607 return 0; /* was inactive to begin with */
610 /* Inform ops and log it */
611 sendto_opmask_butone(0, SNO_GLINE
, "%s %s %s for %s%s%s, expiring at %Tu: "
613 (feature_bool(FEAT_HIS_SNOTICES
) || IsServer(sptr
)) ?
615 cli_name((cli_user(sptr
))->server
),
616 msg
, GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
617 gline
->gl_user
, gline
->gl_host
? "@" : "",
618 gline
->gl_host
? gline
->gl_host
: "",
619 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
621 log_write(LS_GLINE
, L_INFO
, LOG_NOSNOTICE
,
622 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr
, msg
,
623 GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE", gline
->gl_user
,
624 gline
->gl_host
? "@" : "",
625 gline
->gl_host
? gline
->gl_host
: "",
626 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
628 if (!(flags
& GLINE_LOCAL
)) /* don't propagate local changes */
629 gline_propagate(cptr
, sptr
, gline
);
631 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
632 if (GlineIsLocal(gline
) || (!gline
->gl_lastmod
&& !(flags
& GLINE_LOCAL
)))
633 gline_free(gline
); /* get rid of it */
638 /** Find a G-line for a particular mask, guided by certain flags.
639 * Certain bits in \a flags are interpreted specially:
641 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
642 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
643 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
644 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
645 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
646 * <dt>anything else</dt><dd>Search user G-lines.</dd>
648 * @param[in] userhost Mask to search for.
649 * @param[in] flags Bitwise combination of GLINE_* flags.
650 * @return First matching G-line, or NULL if none are found.
653 gline_find(char *userhost
, unsigned int flags
)
656 struct Gline
*sgline
;
657 char *user
, *host
, *t_uh
;
659 if (flags
& (GLINE_BADCHAN
| GLINE_ANY
)) {
660 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) {
661 sgline
= gline
->gl_next
;
663 if (gline
->gl_expire
<= CurrentTime
)
665 else if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
666 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
668 else if ((flags
& GLINE_EXACT
? ircd_strcmp(gline
->gl_user
, userhost
) :
669 match(gline
->gl_user
, userhost
)) == 0)
674 if ((flags
& (GLINE_BADCHAN
| GLINE_ANY
)) == GLINE_BADCHAN
||
675 *userhost
== '#' || *userhost
== '&')
678 DupString(t_uh
, userhost
);
679 canon_userhost(t_uh
, &user
, &host
, "*");
681 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
682 sgline
= gline
->gl_next
;
684 if (gline
->gl_expire
<= CurrentTime
)
686 else if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
687 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
689 else if (flags
& GLINE_EXACT
) {
690 if (((gline
->gl_host
&& host
&& ircd_strcmp(gline
->gl_host
, host
) == 0)
691 || (!gline
->gl_host
&& !host
)) &&
692 (ircd_strcmp(gline
->gl_user
, user
) == 0))
695 if (((gline
->gl_host
&& host
&& match(gline
->gl_host
, host
) == 0)
696 || (!gline
->gl_host
&& !host
)) &&
697 (match(gline
->gl_user
, user
) == 0))
707 /** Find a matching G-line for a user.
708 * @param[in] cptr Client to compare against.
709 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
710 * GLINE_LASTMOD to limit matches.
711 * @return Matching G-line, or NULL if none are found.
714 gline_lookup(struct Client
*cptr
, unsigned int flags
)
717 struct Gline
*sgline
;
719 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
720 sgline
= gline
->gl_next
;
722 if (gline
->gl_expire
<= CurrentTime
) {
727 if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
728 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
731 if (GlineIsRealName(gline
)) {
732 Debug((DEBUG_DEBUG
,"realname gline: '%s' '%s'",gline
->gl_user
,cli_info(cptr
)));
733 if (match(gline
->gl_user
+2, cli_info(cptr
)) != 0)
737 if (match(gline
->gl_user
, (cli_user(cptr
))->username
) != 0)
740 if (GlineIsIpMask(gline
)) {
741 if (!ipmask_check(&cli_ip(cptr
), &gline
->gl_addr
, gline
->gl_bits
))
745 if (match(gline
->gl_host
, (cli_user(cptr
))->realhost
) != 0)
749 if (GlineIsActive(gline
))
758 /** Delink and free a G-line.
759 * @param[in] gline G-line to free.
762 gline_free(struct Gline
*gline
)
766 *gline
->gl_prev_p
= gline
->gl_next
; /* squeeze this gline out */
768 gline
->gl_next
->gl_prev_p
= gline
->gl_prev_p
;
770 MyFree(gline
->gl_user
); /* free up the memory */
772 MyFree(gline
->gl_host
);
773 MyFree(gline
->gl_reason
);
777 /** Burst all known global G-lines to another server.
778 * @param[in] cptr Destination of burst.
781 gline_burst(struct Client
*cptr
)
784 struct Gline
*sgline
;
786 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) { /* all glines */
787 sgline
= gline
->gl_next
;
789 if (gline
->gl_expire
<= CurrentTime
) /* expire any that need expiring */
791 else if (!GlineIsLocal(gline
) && gline
->gl_lastmod
)
792 sendcmdto_one(&me
, CMD_GLINE
, cptr
, "* %c%s%s%s %Tu %Tu :%s",
793 GlineIsRemActive(gline
) ? '+' : '-', gline
->gl_user
,
794 gline
->gl_host
? "@" : "",
795 gline
->gl_host
? gline
->gl_host
: "",
796 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
800 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) { /* all glines */
801 sgline
= gline
->gl_next
;
803 if (gline
->gl_expire
<= CurrentTime
) /* expire any that need expiring */
805 else if (!GlineIsLocal(gline
) && gline
->gl_lastmod
)
806 sendcmdto_one(&me
, CMD_GLINE
, cptr
, "* %c%s %Tu %Tu :%s",
807 GlineIsRemActive(gline
) ? '+' : '-', gline
->gl_user
,
808 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
813 /** Send a G-line to another server.
814 * @param[in] cptr Who to inform of the G-line.
815 * @param[in] gline G-line to send.
819 gline_resend(struct Client
*cptr
, struct Gline
*gline
)
821 if (GlineIsLocal(gline
) || !gline
->gl_lastmod
)
824 sendcmdto_one(&me
, CMD_GLINE
, cptr
, "* %c%s%s%s %Tu %Tu :%s",
825 GlineIsRemActive(gline
) ? '+' : '-', gline
->gl_user
,
826 gline
->gl_host
? "@" : "",
827 gline
->gl_host
? gline
->gl_host
: "",
828 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
834 /** Display one or all G-lines to a user.
835 * If \a userhost is not NULL, only send the first matching G-line.
836 * Otherwise send the whole list.
837 * @param[in] sptr User asking for G-line list.
838 * @param[in] userhost G-line mask to search for (or NULL).
842 gline_list(struct Client
*sptr
, char *userhost
)
845 struct Gline
*sgline
;
848 if (!(gline
= gline_find(userhost
, GLINE_ANY
))) /* no such gline */
849 return send_reply(sptr
, ERR_NOSUCHGLINE
, userhost
);
851 /* send gline information along */
852 send_reply(sptr
, RPL_GLIST
, gline
->gl_user
,
853 gline
->gl_host
? "@" : "",
854 gline
->gl_host
? gline
->gl_host
: "",
855 gline
->gl_expire
+ TSoffset
,
856 GlineIsLocal(gline
) ? cli_name(&me
) : "*",
857 GlineIsActive(gline
) ? '+' : '-', gline
->gl_reason
);
859 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
860 sgline
= gline
->gl_next
;
862 if (gline
->gl_expire
<= CurrentTime
)
865 send_reply(sptr
, RPL_GLIST
, gline
->gl_user
,
866 gline
->gl_host
? "@" : "",
867 gline
->gl_host
? gline
->gl_host
: "",
868 gline
->gl_expire
+ TSoffset
,
869 GlineIsLocal(gline
) ? cli_name(&me
) : "*",
870 GlineIsActive(gline
) ? '+' : '-', gline
->gl_reason
);
873 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) {
874 sgline
= gline
->gl_next
;
876 if (gline
->gl_expire
<= CurrentTime
)
879 send_reply(sptr
, RPL_GLIST
, gline
->gl_user
, "", "",
880 gline
->gl_expire
+ TSoffset
,
881 GlineIsLocal(gline
) ? cli_name(&me
) : "*",
882 GlineIsActive(gline
) ? '+' : '-', gline
->gl_reason
);
886 /* end of gline information */
887 return send_reply(sptr
, RPL_ENDOFGLIST
);
890 /** Statistics callback to list G-lines.
891 * @param[in] sptr Client requesting statistics.
892 * @param[in] sd Stats descriptor for request (ignored).
893 * @param[in] param Extra parameter from user (ignored).
896 gline_stats(struct Client
*sptr
, const struct StatDesc
*sd
,
900 struct Gline
*sgline
;
902 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
903 sgline
= gline
->gl_next
;
905 if (gline
->gl_expire
<= CurrentTime
)
908 send_reply(sptr
, RPL_STATSGLINE
, 'G', gline
->gl_user
,
909 gline
->gl_host
? "@" : "",
910 gline
->gl_host
? gline
->gl_host
: "",
911 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
915 /** Calculate memory used by G-lines.
916 * @param[out] gl_size Number of bytes used by G-lines.
917 * @return Number of G-lines in use.
920 gline_memory_count(size_t *gl_size
)
925 for (gline
= GlobalGlineList
; gline
; gline
= gline
->gl_next
)
928 *gl_size
+= sizeof(struct Gline
);
929 *gl_size
+= gline
->gl_user
? (strlen(gline
->gl_user
) + 1) : 0;
930 *gl_size
+= gline
->gl_host
? (strlen(gline
->gl_host
) + 1) : 0;
931 *gl_size
+= gline
->gl_reason
? (strlen(gline
->gl_reason
) + 1) : 0;