]>
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.2.1 2006/06/09 02:12:25 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 for(chptr
=GlobalChannelList
;chptr
;chptr
=nchptr
) {
228 if (match(gline
->gl_user
, chptr
->chname
))
230 for (member
=chptr
->members
;member
;member
=nmember
) {
231 nmember
=member
->next_member
;
232 if (!MyUser(member
->user
) || IsZombie(member
) || IsAnOper(member
->user
))
234 sendcmdto_serv_butone(&me
, CMD_KICK
, NULL
, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
235 sendcmdto_channel_butserv_butone(&me
, CMD_KICK
, chptr
, NULL
, 0, "%H %C :Badchanneled (%s)", chptr
, member
->user
, gline
->gl_reason
);
236 make_zombie(member
, member
->user
, &me
, &me
, chptr
);
241 for (fd
= HighestFd
; fd
>= 0; --fd
) {
245 if ((acptr
= LocalClientArray
[fd
])) {
246 if (!cli_user(acptr
))
249 if (GlineIsRealName(gline
)) { /* Realname Gline */
250 Debug((DEBUG_DEBUG
,"Realname Gline: %s %s",(cli_info(acptr
)),
252 if (match(gline
->gl_user
+2, cli_info(acptr
)) != 0)
254 Debug((DEBUG_DEBUG
,"Matched!"));
255 } else { /* Host/IP gline */
256 if (cli_name(acptr
) &&
257 match(gline
->gl_nick
, cli_name(acptr
)) !=0)
260 if (cli_user(acptr
)->username
&&
261 match(gline
->gl_user
, (cli_user(acptr
))->realusername
) != 0)
264 if (GlineIsIpMask(gline
)) {
265 if (!ipmask_check(&cli_ip(acptr
), &gline
->gl_addr
, gline
->gl_bits
))
269 if (match(gline
->gl_host
, cli_sockhost(acptr
)) != 0)
274 /* ok, here's one that got G-lined */
275 send_reply(acptr
, SND_EXPLICIT
| ERR_YOUREBANNEDCREEP
, ":%s",
278 /* let the ops know about it */
279 sendto_opmask_butone(0, SNO_GLINE
, "G-line active for %s",
280 get_client_name(acptr
, SHOW_IP
));
282 /* and get rid of him */
283 /* Asuka - Reimplement HEAD_IN_SAND_GLINE from Lain */
284 if ((tval
= exit_client_msg(cptr
, acptr
, &me
,
285 feature_bool(FEAT_HIS_GLINE
) ? "G-lined" : "G-lined (%s)", gline
->gl_reason
)))
286 retval
= tval
; /* retain killed status */
294 * Implements the mask checking applied to local G-lines.
295 * Basically, host masks must have a minimum of two non-wild domain
296 * fields, and IP masks must have a minimum of 16 bits. If the mask
297 * has even one wild-card, OVERRIDABLE is returned, assuming the other
298 * check doesn't fail.
299 * @param[in] mask G-line mask to check.
300 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
303 gline_checkmask(char *mask
)
305 unsigned int flags
= MASK_IP
;
306 unsigned int dots
= 0;
307 unsigned int ipmask
= 0;
309 for (; *mask
; mask
++) { /* go through given mask */
310 if (*mask
== '.') { /* it's a separator; advance positional wilds */
311 flags
= (flags
& ~MASK_WILD_MASK
) | ((flags
<< 1) & MASK_WILD_MASK
);
314 if ((flags
& (MASK_IP
| MASK_WILDS
)) == MASK_IP
)
315 ipmask
+= 8; /* It's an IP with no wilds, count bits */
316 } else if (*mask
== '*' || *mask
== '?')
317 flags
|= MASK_WILD_0
| MASK_WILDS
; /* found a wildcard */
318 else if (*mask
== '/') { /* n.n.n.n/n notation; parse bit specifier */
320 ipmask
= strtoul(mask
, &mask
, 10);
322 /* sanity-check to date */
323 if (*mask
|| (flags
& (MASK_WILDS
| MASK_IP
)) != MASK_IP
)
324 return CHECK_REJECTED
;
327 return CHECK_REJECTED
;
331 if (dots
!= 3 || ipmask
> 32)
332 return CHECK_REJECTED
;
337 flags
|= MASK_HALT
; /* Halt the ipmask calculation */
338 break; /* get out of the loop */
339 } else if (!IsIP6Char(*mask
)) {
340 flags
&= ~MASK_IP
; /* not an IP anymore! */
345 /* Sanity-check quads */
346 if (dots
> 3 || (!(flags
& MASK_WILDS
) && dots
< 3)) {
351 /* update bit count if necessary */
352 if ((flags
& (MASK_IP
| MASK_WILDS
| MASK_HALT
)) == MASK_IP
)
355 /* Check to see that it's not too wide of a mask */
356 if (flags
& MASK_WILDS
&&
357 ((!(flags
& MASK_IP
) && (dots
< 2 || flags
& MASK_WILD_MASK
)) ||
358 (flags
& MASK_IP
&& ipmask
< 16)))
359 return CHECK_REJECTED
; /* to wide, reject */
361 /* Ok, it's approved; require override if it has wildcards, though */
362 return flags
& MASK_WILDS
? CHECK_OVERRIDABLE
: CHECK_APPROVED
;
365 /** Forward a G-line to other servers.
366 * @param[in] cptr Client that sent us the G-line.
367 * @param[in] sptr Client that originated the G-line.
368 * @param[in] gline G-line to forward.
372 gline_propagate(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
)
374 if (GlineIsLocal(gline
) || (IsUser(sptr
) && !gline
->gl_lastmod
))
377 if (gline
->gl_lastmod
)
378 sendcmdto_serv_butone(sptr
, CMD_GLINE
, cptr
, "* %c%s%s%s%s%s %Tu %Tu :%s",
379 GlineIsRemActive(gline
) ? '+' : '-',
380 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
381 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
383 gline
->gl_host
? "@" : "",
384 gline
->gl_host
? gline
->gl_host
: "",
385 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
388 sendcmdto_serv_butone(sptr
, CMD_GLINE
, cptr
,
389 (GlineIsRemActive(gline
) ?
390 "* +%s%s%s%s%s %Tu :%s" : "* -%s%s%s%s%s"),
391 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
392 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
394 gline
->gl_host
? "@" : "",
395 gline
->gl_host
? gline
->gl_host
: "",
396 gline
->gl_expire
- CurrentTime
, gline
->gl_reason
);
401 /** Create a new G-line and add it to global lists.
402 * \a userhost may be in one of four forms:
403 * \li A channel name, to add a BadChan.
404 * \li A string starting with $R and followed by a mask to match against their realname.
405 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
406 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
408 * @param[in] cptr Client that sent us the G-line.
409 * @param[in] sptr Client that originated the G-line.
410 * @param[in] userhost Text mask for the G-line.
411 * @param[in] reason Reason for G-line.
412 * @param[in] expire Duration of G-line in seconds.
413 * @param[in] lastmod Last modification time of G-line.
414 * @param[in] flags Bitwise combination of GLINE_* flags.
415 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
418 gline_add(struct Client
*cptr
, struct Client
*sptr
, char *userhost
,
419 char *reason
, time_t expire
, time_t lastmod
, unsigned int flags
)
421 struct Gline
*agline
;
422 char uhmask
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 3];
423 char *nick
, *user
, *host
;
426 assert(0 != userhost
);
429 if (*userhost
== '#' || *userhost
== '&') {
430 if ((flags
& GLINE_LOCAL
) && !HasPriv(sptr
, PRIV_LOCAL_BADCHAN
))
431 return send_reply(sptr
, ERR_NOPRIVILEGES
);
433 flags
|= GLINE_BADCHAN
;
436 } else if (*userhost
== '$') {
437 switch (*userhost
== '$' ? userhost
[1] : userhost
[3]) {
438 case 'R': flags
|= GLINE_REALNAME
; break;
440 /* uh, what to do here? */
441 /* The answer, my dear Watson, is we throw a protocol_violation()
444 return protocol_violation(sptr
,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr
));
446 sendto_opmask_butone(NULL
, SNO_GLINE
, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr
));
451 user
= (*userhost
=='$' ? userhost
: userhost
+2);
454 canon_userhost(userhost
, &nick
, &user
, &host
, "*");
456 ircd_snprintf(0, uhmask
, sizeof(uhmask
), "%s@%s", user
, host
))
457 return send_reply(sptr
, ERR_LONGMASK
);
458 else if (MyUser(sptr
) || (IsUser(sptr
) && flags
& GLINE_LOCAL
)) {
459 switch (gline_checkmask(host
)) {
460 case CHECK_OVERRIDABLE
: /* oper overrided restriction */
461 if (flags
& GLINE_OPERFORCE
)
465 return send_reply(sptr
, ERR_MASKTOOWIDE
, uhmask
);
469 if ((tmp
= count_users(uhmask
)) >=
470 feature_int(FEAT_GLINEMAXUSERCOUNT
) && !(flags
& GLINE_OPERFORCE
))
471 return send_reply(sptr
, ERR_TOOMANYUSERS
, tmp
);
476 * You cannot set a negative (or zero) expire time, nor can you set an
477 * expiration time for greater than GLINE_MAX_EXPIRE.
479 if (!(flags
& GLINE_FORCE
) && (expire
<= 0 || expire
> GLINE_MAX_EXPIRE
)) {
480 if (!IsServer(sptr
) && MyConnect(sptr
))
481 send_reply(sptr
, ERR_BADEXPIRE
, expire
);
485 expire
+= CurrentTime
; /* convert from lifetime to timestamp */
488 sendto_opmask_butone(0, ircd_strncmp(reason
, "AUTO", 4) ? SNO_GLINE
:
489 SNO_AUTO
, "%s adding %s %s for %s%s%s%s%s, expiring at "
491 (feature_bool(FEAT_HIS_SNOTICES
) || IsServer(sptr
)) ?
493 cli_name((cli_user(sptr
))->server
),
494 (flags
& GLINE_LOCAL
) ? "local" : "global",
495 (flags
& GLINE_BADCHAN
) ? "BADCHAN" : "GLINE",
496 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : nick
,
497 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : "!",
499 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : "@",
500 (flags
& (GLINE_BADCHAN
|GLINE_REALNAME
)) ? "" : host
,
501 expire
+ TSoffset
, reason
);
504 log_write(LS_GLINE
, L_INFO
, LOG_NOSNOTICE
,
505 "%#C adding %s %s for %s%s%s%s%s, expiring at %Tu: %s", sptr
,
506 flags
& GLINE_LOCAL
? "local" : "global",
507 flags
& GLINE_BADCHAN
? "BADCHAN" : "GLINE",
508 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : nick
,
509 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : "!",
511 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : "@",
512 flags
& (GLINE_BADCHAN
|GLINE_REALNAME
) ? "" : host
,
513 expire
+ TSoffset
, reason
);
516 agline
= make_gline(nick
, user
, host
, reason
, expire
, lastmod
, flags
);
518 if (!agline
) /* if it overlapped, silently return */
521 gline_propagate(cptr
, sptr
, agline
);
523 return do_gline(cptr
, sptr
, agline
); /* knock off users if necessary */
526 /** Activate a currently inactive G-line.
527 * @param[in] cptr Peer that told us to activate the G-line.
528 * @param[in] sptr Client that originally thought it was a good idea.
529 * @param[in] gline G-line to activate.
530 * @param[in] lastmod New value for last modification timestamp.
531 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
532 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
535 gline_activate(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
,
536 time_t lastmod
, unsigned int flags
)
538 unsigned int saveflags
= 0;
542 saveflags
= gline
->gl_flags
;
544 if (flags
& GLINE_LOCAL
)
545 gline
->gl_flags
&= ~GLINE_LDEACT
;
547 gline
->gl_flags
|= GLINE_ACTIVE
;
549 if (gline
->gl_lastmod
) {
550 if (gline
->gl_lastmod
>= lastmod
) /* force lastmod to increase */
553 gline
->gl_lastmod
= lastmod
;
557 if ((saveflags
& GLINE_ACTMASK
) == GLINE_ACTIVE
)
558 return 0; /* was active to begin with */
560 /* Inform ops and log it */
561 sendto_opmask_butone(0, SNO_GLINE
, "%s activating global %s for %s%s%s%s%s, "
562 "expiring at %Tu: %s",
563 (feature_bool(FEAT_HIS_SNOTICES
) || IsServer(sptr
)) ?
565 cli_name((cli_user(sptr
))->server
),
566 GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
567 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
568 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
569 gline
->gl_user
, gline
->gl_host
? "@" : "",
570 gline
->gl_host
? gline
->gl_host
: "",
571 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
573 log_write(LS_GLINE
, L_INFO
, LOG_NOSNOTICE
,
574 "%#C activating global %s for %s%s%s%s%s, expiring at %Tu: %s", sptr
,
575 GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
576 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
577 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
579 gline
->gl_host
? "@" : "",
580 gline
->gl_host
? gline
->gl_host
: "",
581 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
583 if (!(flags
& GLINE_LOCAL
)) /* don't propagate local changes */
584 gline_propagate(cptr
, sptr
, gline
);
586 return do_gline(cptr
, sptr
, gline
);
589 /** Deactivate a G-line.
590 * @param[in] cptr Peer that gave us the message.
591 * @param[in] sptr Client that initiated the deactivation.
592 * @param[in] gline G-line to deactivate.
593 * @param[in] lastmod New value for G-line last modification timestamp.
594 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
598 gline_deactivate(struct Client
*cptr
, struct Client
*sptr
, struct Gline
*gline
,
599 time_t lastmod
, unsigned int flags
)
601 unsigned int saveflags
= 0;
606 saveflags
= gline
->gl_flags
;
608 if (GlineIsLocal(gline
))
609 msg
= "removing local";
610 else if (!gline
->gl_lastmod
&& !(flags
& GLINE_LOCAL
)) {
611 msg
= "removing global";
612 gline
->gl_flags
&= ~GLINE_ACTIVE
; /* propagate a -<mask> */
614 msg
= "deactivating global";
616 if (flags
& GLINE_LOCAL
)
617 gline
->gl_flags
|= GLINE_LDEACT
;
619 gline
->gl_flags
&= ~GLINE_ACTIVE
;
621 if (gline
->gl_lastmod
) {
622 if (gline
->gl_lastmod
>= lastmod
)
625 gline
->gl_lastmod
= lastmod
;
629 if ((saveflags
& GLINE_ACTMASK
) != GLINE_ACTIVE
)
630 return 0; /* was inactive to begin with */
633 /* Inform ops and log it */
634 sendto_opmask_butone(0, SNO_GLINE
, "%s %s %s for %s%s%s%s%s, expiring at %Tu: "
636 (feature_bool(FEAT_HIS_SNOTICES
) || IsServer(sptr
)) ?
638 cli_name((cli_user(sptr
))->server
),
639 msg
, GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
640 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
641 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
642 gline
->gl_user
, gline
->gl_host
? "@" : "",
643 gline
->gl_host
? gline
->gl_host
: "",
644 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
646 log_write(LS_GLINE
, L_INFO
, LOG_NOSNOTICE
,
647 "%#C %s %s for %s%s%s%s%s, expiring at %Tu: %s", sptr
, msg
,
648 GlineIsBadChan(gline
) ? "BADCHAN" : "GLINE",
649 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
650 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
652 gline
->gl_host
? "@" : "",
653 gline
->gl_host
? gline
->gl_host
: "",
654 gline
->gl_expire
+ TSoffset
, gline
->gl_reason
);
656 if (!(flags
& GLINE_LOCAL
)) /* don't propagate local changes */
657 gline_propagate(cptr
, sptr
, gline
);
659 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
660 if (GlineIsLocal(gline
) || (!gline
->gl_lastmod
&& !(flags
& GLINE_LOCAL
)))
661 gline_free(gline
); /* get rid of it */
666 /** Find a G-line for a particular mask, guided by certain flags.
667 * Certain bits in \a flags are interpreted specially:
669 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
670 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
671 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
672 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
673 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
674 * <dt>anything else</dt><dd>Search user G-lines.</dd>
676 * @param[in] userhost Mask to search for.
677 * @param[in] flags Bitwise combination of GLINE_* flags.
678 * @return First matching G-line, or NULL if none are found.
681 gline_find(char *userhost
, unsigned int flags
)
684 struct Gline
*sgline
;
685 char *nick
, *user
, *host
, *t_uh
;
687 if (flags
& (GLINE_BADCHAN
| GLINE_ANY
)) {
688 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) {
689 sgline
= gline
->gl_next
;
691 if (gline
->gl_expire
<= CurrentTime
)
693 else if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
694 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
696 else if ((flags
& GLINE_EXACT
? ircd_strcmp(gline
->gl_user
, userhost
) :
697 match(gline
->gl_user
, userhost
)) == 0)
702 if ((flags
& (GLINE_BADCHAN
| GLINE_ANY
)) == GLINE_BADCHAN
||
703 *userhost
== '#' || *userhost
== '&')
706 DupString(t_uh
, userhost
);
707 canon_userhost(t_uh
, &nick
, &user
, &host
, "*");
709 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
710 sgline
= gline
->gl_next
;
712 if (gline
->gl_expire
<= CurrentTime
)
714 else if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
715 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
717 else if (flags
& GLINE_EXACT
) {
718 if (((gline
->gl_host
&& host
&& ircd_strcmp(gline
->gl_host
, host
) == 0)
719 || (!gline
->gl_host
&& !host
)) &&
720 ((!user
&& ircd_strcmp(gline
->gl_user
, "*") == 0) ||
721 (user
&& ircd_strcmp(gline
->gl_user
, user
) == 0)) &&
722 ((!nick
&& gline
->gl_nick
&& ircd_strcmp(gline
->gl_nick
, "*") == 0) ||
723 (nick
&& gline
->gl_nick
&& ircd_strcmp(gline
->gl_nick
, nick
) == 0) || (!gline
->gl_nick
&& !nick
)))
726 if (((gline
->gl_host
&& host
&& match(gline
->gl_host
, host
) == 0)
727 || (!gline
->gl_host
&& !host
)) &&
728 ((!user
&& ircd_strcmp(gline
->gl_user
, "*") == 0) ||
729 (user
&& match(gline
->gl_user
, user
) == 0)) &&
730 ((!nick
&& gline
->gl_nick
&& ircd_strcmp(gline
->gl_nick
, "*") == 0) ||
731 (nick
&& gline
->gl_nick
&& (match(gline
->gl_nick
, nick
) == 0)) || (!gline
->gl_nick
&& !nick
)))
742 gline_lookup_badchan(char *userhost
, unsigned int flags
)
745 struct Gline
*sgline
;
747 if (flags
& (GLINE_BADCHAN
| GLINE_ANY
)) {
748 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) {
749 sgline
= gline
->gl_next
;
751 if (gline
->gl_expire
<= CurrentTime
)
753 else if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
754 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
756 else if ((flags
& GLINE_EXACT
? ircd_strcmp(gline
->gl_user
, userhost
) :
757 match(gline
->gl_user
, userhost
)) == 0)
758 if (GlineIsActive(gline
))
766 /** Find a matching G-line for a user.
767 * @param[in] cptr Client to compare against.
768 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
769 * GLINE_LASTMOD to limit matches.
770 * @return Matching G-line, or NULL if none are found.
773 gline_lookup(struct Client
*cptr
, unsigned int flags
)
776 struct Gline
*sgline
;
778 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
779 sgline
= gline
->gl_next
;
781 if (gline
->gl_expire
<= CurrentTime
) {
786 if ((flags
& GLINE_GLOBAL
&& gline
->gl_flags
& GLINE_LOCAL
) ||
787 (flags
& GLINE_LASTMOD
&& !gline
->gl_lastmod
))
790 if (GlineIsRealName(gline
)) {
791 Debug((DEBUG_DEBUG
,"realname gline: '%s' '%s'",gline
->gl_user
,cli_info(cptr
)));
792 if (match(gline
->gl_user
+2, cli_info(cptr
)) != 0)
796 if (match(gline
->gl_nick
, cli_name(cptr
)) != 0)
798 if (match(gline
->gl_user
, (cli_user(cptr
))->realusername
) != 0)
801 if (GlineIsIpMask(gline
)) {
802 if (!ipmask_check(&cli_ip(cptr
), &gline
->gl_addr
, gline
->gl_bits
))
806 if (match(gline
->gl_host
, (cli_user(cptr
))->realhost
) != 0)
810 if (GlineIsActive(gline
))
819 /** Delink and free a G-line.
820 * @param[in] gline G-line to free.
823 gline_free(struct Gline
*gline
)
827 *gline
->gl_prev_p
= gline
->gl_next
; /* squeeze this gline out */
829 gline
->gl_next
->gl_prev_p
= gline
->gl_prev_p
;
831 MyFree(gline
->gl_user
); /* free up the memory */
833 MyFree(gline
->gl_host
);
834 MyFree(gline
->gl_reason
);
838 /** Burst all known global G-lines to another server.
839 * @param[in] cptr Destination of burst.
842 gline_burst(struct Client
*cptr
)
845 struct Gline
*sgline
;
847 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) { /* all glines */
848 sgline
= gline
->gl_next
;
850 if (gline
->gl_expire
<= CurrentTime
) /* expire any that need expiring */
852 else if (!GlineIsLocal(gline
) && gline
->gl_lastmod
)
853 sendcmdto_one(&me
, CMD_GLINE
, cptr
, "* %c%s%s%s%s%s %Tu %Tu :%s",
854 GlineIsRemActive(gline
) ? '+' : '-',
855 gline
->gl_nick
? gline
->gl_nick
: "",
856 gline
->gl_nick
? "!" : "",
859 gline
->gl_host
? "@" : "",
860 gline
->gl_host
? gline
->gl_host
: "",
861 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
865 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) { /* all glines */
866 sgline
= gline
->gl_next
;
868 if (gline
->gl_expire
<= CurrentTime
) /* expire any that need expiring */
870 else if (!GlineIsLocal(gline
) && gline
->gl_lastmod
)
871 sendcmdto_one(&me
, CMD_GLINE
, cptr
, "* %c%s %Tu %Tu :%s",
872 GlineIsRemActive(gline
) ? '+' : '-', gline
->gl_user
,
873 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
878 /** Send a G-line to another server.
879 * @param[in] cptr Who to inform of the G-line.
880 * @param[in] gline G-line to send.
884 gline_resend(struct Client
*cptr
, struct Gline
*gline
)
886 if (GlineIsLocal(gline
) || !gline
->gl_lastmod
)
889 sendcmdto_one(&me
, CMD_GLINE
, cptr
, "* %c%s%s%s%s%s %Tu %Tu :%s",
890 GlineIsRemActive(gline
) ? '+' : '-',
891 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
892 GlineIsBadChan(gline
)|GlineIsRealName(gline
) ? "" : "!",
894 gline
->gl_host
? "@" : "",
895 gline
->gl_host
? gline
->gl_host
: "",
896 gline
->gl_expire
- CurrentTime
, gline
->gl_lastmod
,
902 /** Display one or all G-lines to a user.
903 * If \a userhost is not NULL, only send the first matching G-line.
904 * Otherwise send the whole list.
905 * @param[in] sptr User asking for G-line list.
906 * @param[in] userhost G-line mask to search for (or NULL).
910 gline_list(struct Client
*sptr
, char *userhost
)
913 struct Gline
*sgline
;
916 if (!(gline
= gline_find(userhost
, GLINE_ANY
))) /* no such gline */
917 return send_reply(sptr
, ERR_NOSUCHGLINE
, userhost
);
919 /* send gline information along */
920 send_reply(sptr
, RPL_GLIST
,
921 GlineIsBadChan(gline
) | GlineIsRealName(gline
) ? "" : gline
->gl_nick
,
922 GlineIsBadChan(gline
) | GlineIsRealName(gline
) ? "" : "!",
923 gline
->gl_user
, GlineIsBadChan(gline
) || GlineIsRealName(gline
) ? "" : "@",
924 gline
->gl_host
? gline
->gl_host
: "",
925 gline
->gl_expire
+ TSoffset
,
926 GlineIsLocal(gline
) ? cli_name(&me
) : "*",
927 GlineIsActive(gline
) ? '+' : '-', gline
->gl_reason
);
929 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
930 sgline
= gline
->gl_next
;
932 if (gline
->gl_expire
<= CurrentTime
)
935 send_reply(sptr
, RPL_GLIST
,
936 gline
->gl_nick
? gline
->gl_nick
: "",
937 gline
->gl_nick
? "!" : "",
939 gline
->gl_host
? "@" : "",
940 gline
->gl_host
? gline
->gl_host
: "",
941 gline
->gl_expire
+ TSoffset
,
942 GlineIsLocal(gline
) ? cli_name(&me
) : "*",
943 GlineIsActive(gline
) ? '+' : '-', gline
->gl_reason
);
946 for (gline
= BadChanGlineList
; gline
; gline
= sgline
) {
947 sgline
= gline
->gl_next
;
949 if (gline
->gl_expire
<= CurrentTime
)
952 send_reply(sptr
, RPL_GLIST
, gline
->gl_user
, "", "", "", "",
953 gline
->gl_expire
+ TSoffset
,
954 GlineIsLocal(gline
) ? cli_name(&me
) : "*",
955 GlineIsActive(gline
) ? '+' : '-', gline
->gl_reason
);
959 /* end of gline information */
960 return send_reply(sptr
, RPL_ENDOFGLIST
);
963 /** Statistics callback to list G-lines.
964 * @param[in] sptr Client requesting statistics.
965 * @param[in] sd Stats descriptor for request (ignored).
966 * @param[in] param Extra parameter from user (ignored).
969 gline_stats(struct Client
*sptr
, const struct StatDesc
*sd
,
973 struct Gline
*sgline
;
975 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
976 sgline
= gline
->gl_next
;
978 if (gline
->gl_expire
<= CurrentTime
)
981 send_reply(sptr
, RPL_STATSGLINE
, 'G',
982 gline
->gl_nick
? gline
->gl_nick
: "",
983 gline
->gl_nick
? "!" : "",
985 gline
->gl_host
? "@" : "",
986 gline
->gl_host
? gline
->gl_host
: "",
987 gline
->gl_expire
+ TSoffset
,
988 GlineIsActive(gline
) ? '+' : '-',
993 /** Calculate memory used by G-lines.
994 * @param[out] gl_size Number of bytes used by G-lines.
995 * @return Number of G-lines in use.
998 gline_memory_count(size_t *gl_size
)
1000 struct Gline
*gline
;
1001 unsigned int gl
= 0;
1003 for (gline
= GlobalGlineList
; gline
; gline
= gline
->gl_next
)
1006 *gl_size
+= sizeof(struct Gline
);
1007 *gl_size
+= gline
->gl_nick
? (strlen(gline
->gl_nick
) +1) : 0;
1008 *gl_size
+= gline
->gl_user
? (strlen(gline
->gl_user
) + 1) : 0;
1009 *gl_size
+= gline
->gl_host
? (strlen(gline
->gl_host
) + 1) : 0;
1010 *gl_size
+= gline
->gl_reason
? (strlen(gline
->gl_reason
) + 1) : 0;
1016 IsNickGlined(struct Client
*cptr
, char *nick
)
1018 struct Gline
*gline
;
1019 struct Gline
*sgline
;
1021 for (gline
= GlobalGlineList
; gline
; gline
= sgline
) {
1022 sgline
= gline
->gl_next
;
1024 if (gline
->gl_expire
<= CurrentTime
) {
1029 if (GlineIsRealName(gline
)) /* skip realname glines */
1032 if (!ircd_strcmp(gline
->gl_nick
, "*")) /* skip glines w. wildcarded nick */
1035 if (match(gline
->gl_nick
, nick
) != 0)
1038 if (match(gline
->gl_user
, (cli_user(cptr
))->username
) != 0)
1041 if (GlineIsIpMask(gline
)) {
1042 if (!ipmask_check(&(cli_ip(cptr
)), &gline
->gl_addr
, gline
->gl_bits
))
1046 if (match(gline
->gl_host
, (cli_user(cptr
))->realhost
) != 0)