2 * IRC - Internet Relay Chat, ircd/s_user.c (formerly ircd/s_msg.c)
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * @brief Miscellaneous user-related helper functions.
25 * @version $Id: s_user.c,v 1.99.2.1 2006/01/10 02:06:36 entrope Exp $
36 #include "ircd_alloc.h"
37 #include "ircd_auth.h"
38 #include "ircd_chattr.h"
39 #include "ircd_features.h"
41 #include "ircd_reply.h"
42 #include "ircd_snprintf.h"
43 #include "ircd_string.h"
52 #include "querycmds.h"
58 #include "s_serv.h" /* max_client_count */
61 #include "supported.h"
67 #include "handlers.h" /* m_motd and m_lusers */
69 /* #include <assert.h> -- Now using assert in ircd_log.h */
76 static char *IsVhost(char *hostmask
, int oper
);
77 static char *IsVhostPass(char *hostmask
);
79 /** Count of allocated User structures. */
80 static int userCount
= 0;
82 /** Makes sure that \a cptr has a User information block.
83 * If cli_user(cptr) != NULL, does nothing.
84 * @param[in] cptr Client to attach User struct to.
85 * @return User struct associated with \a cptr.
87 struct User
*make_user(struct Client
*cptr
)
91 if (!cli_user(cptr
)) {
92 cli_user(cptr
) = (struct User
*) MyMalloc(sizeof(struct User
));
93 assert(0 != cli_user(cptr
));
95 /* All variables are 0 by default */
96 memset(cli_user(cptr
), 0, sizeof(struct User
));
98 cli_user(cptr
)->refcnt
= 1;
100 return cli_user(cptr
);
103 /** Dereference \a user.
104 * User structures are reference-counted; if the refcount of \a user
105 * becomes zero, free it.
106 * @param[in] user User to dereference.
108 void free_user(struct User
* user
)
111 assert(0 < user
->refcnt
);
113 if (--user
->refcnt
== 0) {
119 assert(0 == user
->joined
);
120 assert(0 == user
->invited
);
121 assert(0 == user
->channel
);
128 /** Find number of User structs allocated and memory used by them.
129 * @param[out] count_out Receives number of User structs allocated.
130 * @param[out] bytes_out Receives number of bytes used by User structs.
132 void user_count_memory(size_t* count_out
, size_t* bytes_out
)
134 assert(0 != count_out
);
135 assert(0 != bytes_out
);
136 *count_out
= userCount
;
137 *bytes_out
= userCount
* sizeof(struct User
);
141 /** Find the next client (starting at \a next) with a name that matches \a ch.
142 * Normal usage loop is:
143 * for (x = client; x = next_client(x,mask); x = x->next)
144 * HandleMatchingClient;
146 * @param[in] next First client to check.
147 * @param[in] ch Name mask to check against.
148 * @return Next matching client found, or NULL if none.
150 struct Client
*next_client(struct Client
*next
, const char* ch
)
152 struct Client
*tmp
= next
;
157 next
= FindClient(ch
);
158 next
= next
? next
: tmp
;
159 if (cli_prev(tmp
) == next
)
163 for (; next
; next
= cli_next(next
))
164 if (!match(ch
, cli_name(next
)))
169 /** Find the destination server for a command, and forward it if that is not us.
171 * \a server may be a nickname, server name, server mask (if \a from
172 * is a local user) or server numnick (if \a is a server or remote
175 * @param[in] from Client that sent the command to us.
176 * @param[in] cmd Long-form command text.
177 * @param[in] tok Token-form command text.
178 * @param[in] one Client that originated the command (ignored).
179 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
180 * @param[in] pattern Format string of arguments to command.
181 * @param[in] server Index of target name or mask in \a parv.
182 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
183 * @param[in] parv Array of arguments to command.
184 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
186 int hunt_server_cmd(struct Client
*from
, const char *cmd
, const char *tok
,
187 struct Client
*one
, int MustBeOper
, const char *pattern
,
188 int server
, int parc
, char *parv
[])
190 struct Client
*acptr
;
193 /* Assume it's me, if no server or an unregistered client */
194 if (parc
<= server
|| EmptyString((to
= parv
[server
])) || IsUnknown(from
))
195 return (HUNTED_ISME
);
197 if (MustBeOper
&& !IsPrivileged(from
))
199 send_reply(from
, ERR_NOPRIVILEGES
);
200 return HUNTED_NOSUCH
;
203 /* Make sure it's a server */
205 /* Make sure it's a server */
206 if (!strchr(to
, '*')) {
207 if (0 == (acptr
= FindClient(to
))) {
208 send_reply(from
, ERR_NOSUCHSERVER
, to
);
209 return HUNTED_NOSUCH
;
213 acptr
= cli_user(acptr
)->server
;
214 } else if (!(acptr
= find_match_server(to
))) {
215 send_reply(from
, ERR_NOSUCHSERVER
, to
);
216 return (HUNTED_NOSUCH
);
218 } else if (!(acptr
= FindNServer(to
))) {
219 send_reply(from
, SND_EXPLICIT
| ERR_NOSUCHSERVER
, "* :Server has disconnected");
220 return (HUNTED_NOSUCH
); /* Server broke off in the meantime */
224 return (HUNTED_ISME
);
226 if (MustBeOper
&& !IsPrivileged(from
)) {
227 send_reply(from
, ERR_NOPRIVILEGES
);
228 return HUNTED_NOSUCH
;
231 /* assert(!IsServer(from)); */
233 parv
[server
] = (char *) acptr
; /* HACK! HACK! HACK! ARGH! */
235 sendcmdto_one(from
, cmd
, tok
, acptr
, pattern
, parv
[1], parv
[2], parv
[3],
236 parv
[4], parv
[5], parv
[6], parv
[7], parv
[8]);
238 return (HUNTED_PASS
);
241 /** Find the destination server for a command, and forward it (as a
242 * high-priority command) if that is not us.
244 * \a server may be a nickname, server name, server mask (if \a from
245 * is a local user) or server numnick (if \a is a server or remote
247 * Unlike hunt_server_cmd(), this appends the message to the
248 * high-priority message queue for the destination server.
250 * @param[in] from Client that sent the command to us.
251 * @param[in] cmd Long-form command text.
252 * @param[in] tok Token-form command text.
253 * @param[in] one Client that originated the command (ignored).
254 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
255 * @param[in] pattern Format string of arguments to command.
256 * @param[in] server Index of target name or mask in \a parv.
257 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
258 * @param[in] parv Array of arguments to command.
259 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
261 int hunt_server_prio_cmd(struct Client
*from
, const char *cmd
, const char *tok
,
262 struct Client
*one
, int MustBeOper
,
263 const char *pattern
, int server
, int parc
,
266 struct Client
*acptr
;
269 /* Assume it's me, if no server or an unregistered client */
270 if (parc
<= server
|| EmptyString((to
= parv
[server
])) || IsUnknown(from
))
271 return (HUNTED_ISME
);
273 /* Make sure it's a server */
275 /* Make sure it's a server */
276 if (!strchr(to
, '*')) {
277 if (0 == (acptr
= FindClient(to
))) {
278 send_reply(from
, ERR_NOSUCHSERVER
, to
);
279 return HUNTED_NOSUCH
;
283 acptr
= cli_user(acptr
)->server
;
284 } else if (!(acptr
= find_match_server(to
))) {
285 send_reply(from
, ERR_NOSUCHSERVER
, to
);
286 return (HUNTED_NOSUCH
);
288 } else if (!(acptr
= FindNServer(to
)))
289 return (HUNTED_NOSUCH
); /* Server broke off in the meantime */
292 return (HUNTED_ISME
);
294 if (MustBeOper
&& !IsPrivileged(from
)) {
295 send_reply(from
, ERR_NOPRIVILEGES
);
296 return HUNTED_NOSUCH
;
299 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
301 parv
[server
] = (char *) acptr
; /* HACK! HACK! HACK! ARGH! */
303 sendcmdto_prio_one(from
, cmd
, tok
, acptr
, pattern
, parv
[1], parv
[2], parv
[3],
304 parv
[4], parv
[5], parv
[6], parv
[7], parv
[8]);
306 return (HUNTED_PASS
);
310 /** Copy a cleaned-up version of a username.
311 * Replace all instances of '~' and "invalid" username characters
312 * (!isIrcUi()) with underscores, truncating at USERLEN or the first
313 * control character. \a dest and \a source may be the same buffer.
314 * @param[out] dest Destination buffer.
315 * @param[in] source Source buffer.
316 * @param[in] tilde If non-zero, prepend a '~' to \a dest.
318 static char *clean_user_id(char *dest
, char *source
, int tilde
)
325 ch
= *s
++; /* Store first character to copy: */
328 *d
++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
331 while (ch
&& !IsCntrl(ch
) && rlen
--)
333 char nch
= *s
++; /* Store next character to copy */
334 *d
++ = IsUserChar(ch
) ? ch
: '_'; /* This possibly overwrites it */
347 * This function is called when both NICK and USER messages
348 * have been accepted for the client, in whatever order. Only
349 * after this the USER message is propagated.
351 * NICK's must be propagated at once when received, although
352 * it would be better to delay them too until full info is
353 * available. Doing it is not so simple though, would have
354 * to implement the following:
356 * 1) user telnets in and gives only "NICK foobar" and waits
357 * 2) another user far away logs in normally with the nick
358 * "foobar" (quite legal, as this server didn't propagate it).
359 * 3) now this server gets nick "foobar" from outside, but
360 * has already the same defined locally. Current server
361 * would just issue "KILL foobar" to clean out dups. But,
362 * this is not fair. It should actually request another
363 * nick from local user or kill him/her...
365 /** Finish registering a user who has sent both NICK and USER.
366 * For local connections, possibly check IAuth; make sure there is a
367 * matching Client config block; clean the username field; check
368 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
369 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
370 * For all connections, update the invisible user and operator counts;
371 * run IPcheck against their address; and forward the NICK.
373 * @param[in] cptr Client who introduced the user.
374 * @param[in,out] sptr Client who has been fully introduced.
375 * @param[in] nick Client's new nickname.
376 * @param[in] username Client's username.
377 * @return Zero or CPTR_KILLED.
379 int register_user(struct Client
*cptr
, struct Client
*sptr
,
380 const char *nick
, char *username
)
382 struct ConfItem
* aconf
;
386 char c
= 0; /* not alphanum */
387 char d
= 'a'; /* not a digit */
395 short digitgroups
= 0;
396 struct User
* user
= cli_user(sptr
);
400 user
->last
= CurrentTime
;
401 parv
[0] = cli_name(sptr
);
402 parv
[1] = parv
[2] = NULL
;
406 static time_t last_too_many1
;
407 static time_t last_too_many2
;
409 assert(cptr
== sptr
);
410 assert(cli_unreg(sptr
) == 0);
411 if (!IsIAuthed(sptr
)) {
413 return iauth_start_client(iauth_active
, sptr
);
417 switch (conf_check_client(sptr
))
421 case ACR_NO_AUTHORIZATION
:
422 sendto_opmask_butone(0, SNO_UNAUTH
, "Unauthorized connection from %s.",
423 get_client_name(sptr
, HIDE_IP
));
424 ++ServerStats
->is_ref
;
425 return exit_client(cptr
, sptr
, &me
,
426 "No Authorization - use another server");
427 case ACR_TOO_MANY_IN_CLASS
:
428 if (CurrentTime
- last_too_many1
>= (time_t) 60)
430 last_too_many1
= CurrentTime
;
431 sendto_opmask_butone(0, SNO_TOOMANY
, "Too many connections in "
432 "class %s for %s.", get_client_class(sptr
),
433 get_client_name(sptr
, SHOW_IP
));
435 ++ServerStats
->is_ref
;
436 IPcheck_connect_fail(sptr
);
437 return exit_client(cptr
, sptr
, &me
,
438 "Sorry, your connection class is full - try "
439 "again later or try another server");
440 case ACR_TOO_MANY_FROM_IP
:
441 if (CurrentTime
- last_too_many2
>= (time_t) 60)
443 last_too_many2
= CurrentTime
;
444 sendto_opmask_butone(0, SNO_TOOMANY
, "Too many connections from "
446 get_client_name(sptr
, SHOW_IP
));
448 ++ServerStats
->is_ref
;
449 return exit_client(cptr
, sptr
, &me
,
450 "Too many connections from your host");
451 case ACR_ALREADY_AUTHORIZED
:
452 /* Can this ever happen? */
454 ++ServerStats
->is_ref
;
455 IPcheck_connect_fail(sptr
);
456 return exit_client(cptr
, sptr
, &me
, "Unknown error -- Try again");
458 ircd_strncpy(user
->host
, cli_sockhost(sptr
), HOSTLEN
);
459 ircd_strncpy(user
->realhost
, cli_sockhost(sptr
), HOSTLEN
);
460 aconf
= cli_confs(sptr
)->value
.aconf
;
462 clean_user_id(user
->username
,
463 HasFlag(sptr
, FLAG_GOTID
) ? cli_username(sptr
) : username
,
464 HasFlag(sptr
, FLAG_DOID
) && !HasFlag(sptr
, FLAG_GOTID
)
465 && !(HasSetHost(sptr
))); /* No tilde for S-lined users. */
467 /* Have to set up "realusername" before doing the gline check below */
468 ircd_strncpy(user
->realusername
, user
->username
, USERLEN
);
470 if ((user
->username
[0] == '\0')
471 || ((user
->username
[0] == '~') && (user
->username
[1] == '\000')))
472 return exit_client(cptr
, sptr
, &me
, "USER: Bogus userid.");
474 if (!EmptyString(aconf
->passwd
)
475 && strcmp(cli_passwd(sptr
), aconf
->passwd
))
477 ServerStats
->is_ref
++;
478 send_reply(sptr
, ERR_PASSWDMISMATCH
);
479 return exit_client(cptr
, sptr
, &me
, "Bad Password");
481 memset(cli_passwd(sptr
), 0, sizeof(cli_passwd(sptr
)));
483 * following block for the benefit of time-dependent K:-lines
485 killreason
= find_kill(sptr
, 1);
487 ServerStats
->is_ref
++;
488 return exit_client(cptr
, sptr
, &me
,
489 (killreason
== -1 ? "K-lined" : "G-lined"));
492 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
493 * Summary of rules now implemented in this patch: Ensor 11-94
494 * In a mixed-case name, if first char is upper, one more upper may
495 * appear anywhere. (A mixed-case name *must* have an upper first
496 * char, and may have one other upper.)
497 * A third upper may appear if all 3 appear at the beginning of the
498 * name, separated only by "others" (-/_/.).
499 * A single group of digits is allowed anywhere.
500 * Two groups of digits are allowed if at least one of the groups is
501 * at the beginning or the end.
502 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
503 * But not as the first or last char.
504 * No other special characters are allowed.
505 * Name must contain at least one letter.
507 tmpstr2
= tmpstr
= (username
[0] == '~' ? &username
[1] : username
);
508 while (*tmpstr
&& !badid
)
520 if ((leadcaps
|| pos
== 1) && !lower
&& !digits
)
526 if (pos
== 1 || !IsDigit(d
))
533 else if (c
== '-' || c
== '_' || c
== '.')
538 else if (d
== '-' || d
== '_' || d
== '.' || other
> 2)
547 if (lower
&& upper
&& (!leadcaps
|| leadcaps
> 3 ||
548 (upper
> 2 && upper
> leadcaps
)))
550 else if (digitgroups
== 2 && !(IsDigit(tmpstr2
[0]) || IsDigit(c
)))
552 else if ((!lower
&& !upper
) || !IsAlnum(c
))
555 if (badid
&& (!HasFlag(sptr
, FLAG_GOTID
) ||
556 strcmp(cli_username(sptr
), username
) != 0))
558 ServerStats
->is_ref
++;
560 send_reply(cptr
, SND_EXPLICIT
| ERR_INVALIDUSERNAME
,
561 ":Your username is invalid.");
562 send_reply(cptr
, SND_EXPLICIT
| ERR_INVALIDUSERNAME
,
563 ":Connect with your real username, in lowercase.");
564 send_reply(cptr
, SND_EXPLICIT
| ERR_INVALIDUSERNAME
,
565 ":If your mail address were foo@bar.com, your username "
567 return exit_client(cptr
, sptr
, &me
, "USER: Bad username");
569 Count_unknownbecomesclient(sptr
, UserStats
);
571 if (MyConnect(sptr
) && feature_bool(FEAT_AUTOINVISIBLE
))
574 if(MyConnect(sptr
) && feature_bool(FEAT_SETHOST_AUTO
)) {
575 if (conf_check_slines(sptr
)) {
576 send_reply(sptr
, RPL_USINGSLINE
);
582 cli_handler(sptr
) = CLIENT_HANDLER
;
583 SetLocalNumNick(sptr
);
586 feature_str(FEAT_NETWORK
),
587 feature_str(FEAT_PROVIDER
) ? " via " : "",
588 feature_str(FEAT_PROVIDER
) ? feature_str(FEAT_PROVIDER
) : "",
591 * This is a duplicate of the NOTICE but see below...
593 send_reply(sptr
, RPL_YOURHOST
, cli_name(&me
), version
);
594 send_reply(sptr
, RPL_CREATED
, creation
);
595 send_reply(sptr
, RPL_MYINFO
, cli_name(&me
), version
, infousermodes
,
596 infochanmodes
, infochanmodeswithparams
);
597 send_supported(sptr
);
598 m_lusers(sptr
, sptr
, 1, parv
);
601 if (cli_snomask(sptr
) & SNO_NOISY
)
602 set_snomask(sptr
, cli_snomask(sptr
) & SNO_NOISY
, SNO_ADD
);
603 if (feature_bool(FEAT_CONNEXIT_NOTICES
))
604 sendto_opmask_butone(0, SNO_CONNEXIT
,
605 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
606 cli_name(sptr
), user
->username
, user
->host
,
607 cli_sock_ip(sptr
), get_client_class(sptr
),
608 cli_info(sptr
), NumNick(cptr
) /* two %s's */);
610 IPcheck_connect_succeeded(sptr
);
612 * Set user's initial modes
614 tmpstr
= (char*)client_get_default_umode(sptr
);
615 if (tmpstr
) for (; *tmpstr
; ++tmpstr
) {
618 if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY
)) {
620 set_snomask(sptr
, SNO_DEFAULT
, SNO_SET
);
624 if (!feature_bool(FEAT_WALLOPS_OPER_ONLY
))
634 if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY
))
641 struct Client
*acptr
;
643 ircd_strncpy(user
->username
, username
, USERLEN
);
644 Count_newremoteclient(UserStats
, user
->server
);
646 acptr
= user
->server
;
647 if (cli_from(acptr
) != cli_from(sptr
))
649 sendcmdto_one(&me
, CMD_KILL
, cptr
, "%C :%s (%s != %s[%s])",
650 sptr
, cli_name(&me
), cli_name(user
->server
), cli_name(cli_from(acptr
)),
651 cli_sockhost(cli_from(acptr
)));
652 SetFlag(sptr
, FLAG_KILLED
);
653 return exit_client(cptr
, sptr
, &me
, "NICK server wrong direction");
655 else if (HasFlag(acptr
, FLAG_TS8
))
656 SetFlag(sptr
, FLAG_TS8
);
659 * Check to see if this user is being propagated
660 * as part of a net.burst, or is using protocol 9.
661 * FIXME: This can be sped up - its stupid to check it for
662 * every NICK message in a burst again --Run.
664 for (; acptr
!= &me
; acptr
= cli_serv(acptr
)->up
)
666 if (IsBurst(acptr
) || Protocol(acptr
) < 10)
669 if (!IPcheck_remote_connect(sptr
, (acptr
!= &me
)))
672 * We ran out of bits to count this
674 sendcmdto_one(&me
, CMD_KILL
, sptr
, "%C :%s (Too many connections from your host -- Ghost)",
675 sptr
, cli_name(&me
));
676 return exit_client(cptr
, sptr
, &me
,"Too many connections from your host -- throttled");
679 if(MyConnect(sptr
) && feature_bool(FEAT_SETHOST_AUTO
)) {
680 if (conf_check_slines(sptr
)) {
681 send_reply(sptr
, RPL_USINGSLINE
);
689 if (IsInvisible(sptr
))
690 ++UserStats
.inv_clients
;
694 tmpstr
= umode_str(sptr
);
695 /* Send full IP address to IPv6-grokking servers. */
696 sendcmdto_flag_serv_butone(user
->server
, CMD_NICK
, cptr
,
697 FLAG_IPV6
, FLAG_LAST_FLAG
,
698 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
699 nick
, cli_hopcount(sptr
) + 1, cli_lastnick(sptr
),
700 user
->realusername
, user
->realhost
,
701 *tmpstr
? "+" : "", tmpstr
, *tmpstr
? " " : "",
702 iptobase64(ip_base64
, &cli_ip(sptr
), sizeof(ip_base64
), 1),
703 NumNick(sptr
), cli_info(sptr
));
704 /* Send fake IPv6 addresses to pre-IPv6 servers. */
705 sendcmdto_flag_serv_butone(user
->server
, CMD_NICK
, cptr
,
706 FLAG_LAST_FLAG
, FLAG_IPV6
,
707 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
708 nick
, cli_hopcount(sptr
) + 1, cli_lastnick(sptr
),
709 user
->realusername
, user
->realhost
,
710 *tmpstr
? "+" : "", tmpstr
, *tmpstr
? " " : "",
711 iptobase64(ip_base64
, &cli_ip(sptr
), sizeof(ip_base64
), 0),
712 NumNick(sptr
), cli_info(sptr
));
714 /* Send user mode to client */
717 static struct Flags flags
; /* automatically initialized to zeros */
718 /* To avoid sending +r to the client due to auth-on-connect, set
719 * the "old" FLAG_ACCOUNT bit to match the client's value.
722 FlagSet(&flags
, FLAG_ACCOUNT
);
724 FlagClr(&flags
, FLAG_ACCOUNT
);
725 send_umode(cptr
, sptr
, &flags
, ALL_UMODES
);
726 if ((cli_snomask(sptr
) != SNO_DEFAULT
) && HasFlag(sptr
, FLAG_SERVNOTICE
))
727 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
732 /** List of user mode characters. */
733 static const struct UserMode
{
734 unsigned int flag
; /**< User mode constant. */
735 char c
; /**< Character corresponding to the mode. */
739 { FLAG_INVISIBLE
, 'i' },
740 { FLAG_WALLOP
, 'w' },
741 { FLAG_SERVNOTICE
, 's' },
743 { FLAG_CHSERV
, 'k' },
745 { FLAG_ACCOUNT
, 'r' },
746 { FLAG_HIDDENHOST
, 'x' },
747 { FLAG_ACCOUNTONLY
, 'R' },
748 { FLAG_XTRAOP
, 'X' },
749 { FLAG_NOCHAN
, 'n' },
750 { FLAG_NOIDLE
, 'I' },
751 { FLAG_SETHOST
, 'h' },
752 { FLAG_PARANOID
, 'P' }
755 /** Length of #userModeList. */
756 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
759 * XXX - find a way to get rid of this
761 /** Nasty global buffer used for communications with umode_str() and others. */
762 static char umodeBuf
[BUFSIZE
];
764 /** Try to set a user's nickname.
765 * If \a sptr is a server, the client is being introduced for the first time.
766 * @param[in] cptr Client to set nickname.
767 * @param[in] sptr Client sending the NICK.
768 * @param[in] nick New nickname.
769 * @param[in] parc Number of arguments to NICK.
770 * @param[in] parv Argument list to NICK.
771 * @return CPTR_KILLED if \a cptr was killed, else 0.
773 int set_nick_name(struct Client
* cptr
, struct Client
* sptr
,
774 const char* nick
, int parc
, char* parv
[])
776 if (IsServer(sptr
)) {
778 const char* account
= 0;
784 * A server introducing a new client, change source
786 struct Client
* new_client
= make_client(cptr
, STAT_UNKNOWN
);
787 assert(0 != new_client
);
789 cli_hopcount(new_client
) = atoi(parv
[2]);
790 cli_lastnick(new_client
) = atoi(parv
[3]);
791 if (Protocol(cptr
) > 9 && parc
> 7 && *parv
[6] == '+')
793 for (p
= parv
[6] + 1; *p
; p
++)
795 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
797 if (userModeList
[i
].c
== *p
)
799 SetFlag(new_client
, userModeList
[i
].flag
);
800 if (userModeList
[i
].flag
== FLAG_ACCOUNT
)
802 if (userModeList
[i
].flag
== FLAG_SETHOST
)
803 hostmask
= parv
[parc
- 4];
809 client_set_privs(new_client
, NULL
); /* set privs on user */
813 strcpy(cli_name(new_client
), nick
);
814 cli_user(new_client
) = make_user(new_client
);
815 cli_user(new_client
)->server
= sptr
;
816 SetRemoteNumNick(new_client
, parv
[parc
- 2]);
818 * IP# of remote client
820 base64toip(parv
[parc
- 3], &cli_ip(new_client
));
822 add_client_to_list(new_client
);
823 hAddClient(new_client
);
825 cli_serv(sptr
)->ghost
= 0; /* :server NICK means end of net.burst */
826 ircd_strncpy(cli_username(new_client
), parv
[4], USERLEN
);
827 ircd_strncpy(cli_user(new_client
)->realusername
, parv
[4], USERLEN
);
828 ircd_strncpy(cli_user(new_client
)->host
, parv
[5], HOSTLEN
);
829 ircd_strncpy(cli_user(new_client
)->realhost
, parv
[5], HOSTLEN
);
830 ircd_strncpy(cli_info(new_client
), parv
[parc
- 1], REALLEN
);
832 int len
= ACCOUNTLEN
;
833 if ((p
= strchr(account
, ':'))) {
834 len
= (p
++) - account
;
835 cli_user(new_client
)->acc_create
= atoi(p
);
836 Debug((DEBUG_DEBUG
, "Received timestamped account in user mode; "
837 "account \"%s\", timestamp %Tu", account
,
838 cli_user(new_client
)->acc_create
));
840 ircd_strncpy(cli_user(new_client
)->account
, account
, len
);
842 if (HasHiddenHost(new_client
))
843 ircd_snprintf(0, cli_user(new_client
)->host
, HOSTLEN
, "%s.%s",
844 account
, feature_str(FEAT_HIDDEN_HOST
));
845 if (HasSetHost(new_client
)) {
846 if ((host
= strrchr(hostmask
, '@')) != NULL
) {
848 ircd_strncpy(cli_username(new_client
), hostmask
, USERLEN
);
849 ircd_strncpy(cli_user(new_client
)->host
, host
, HOSTLEN
);
853 return register_user(cptr
, new_client
, cli_name(new_client
), cli_username(new_client
));
855 else if ((cli_name(sptr
))[0]) {
857 * Client changing its nick
859 * If the client belongs to me, then check to see
860 * if client is on any channels where it is currently
861 * banned. If so, do not allow the nick change to occur.
864 const char* channel_name
;
865 struct Membership
*member
;
866 if ((channel_name
= find_no_nickchange_channel(sptr
)) && !IsXtraOp(sptr
)) {
867 return send_reply(cptr
, ERR_BANNICKCHANGE
, channel_name
);
870 * Refuse nick change if the last nick change was less
871 * then 30 seconds ago. This is intended to get rid of
872 * clone bots doing NICK FLOOD. -SeKs
873 * If someone didn't change their nick for more then 60 seconds
874 * however, allow to do two nick changes immediately after another
875 * before limiting the nick flood. -Run
877 if (CurrentTime
< cli_nextnick(cptr
))
879 cli_nextnick(cptr
) += 2;
880 send_reply(cptr
, ERR_NICKTOOFAST
, parv
[1],
881 cli_nextnick(cptr
) - CurrentTime
);
882 /* Send error message */
883 sendcmdto_one(cptr
, CMD_NICK
, cptr
, "%s", cli_name(cptr
));
884 /* bounce NICK to user */
885 return 0; /* ignore nick change! */
888 /* Limit total to 1 change per NICK_DELAY seconds: */
889 cli_nextnick(cptr
) += NICK_DELAY
;
890 /* However allow _maximal_ 1 extra consecutive nick change: */
891 if (cli_nextnick(cptr
) < CurrentTime
)
892 cli_nextnick(cptr
) = CurrentTime
;
894 /* Invalidate all bans against the user so we check them again */
895 for (member
= (cli_user(cptr
))->channel
; member
;
896 member
= member
->next_channel
)
897 ClearBanValid(member
);
900 * Also set 'lastnick' to current time, if changed.
902 if (0 != ircd_strcmp(parv
[0], nick
))
903 cli_lastnick(sptr
) = (sptr
== cptr
) ? TStime() : atoi(parv
[2]);
906 * Client just changing his/her nick. If he/she is
907 * on a channel, send note of change to all clients
908 * on that channel. Propagate notice to other servers.
911 sendcmdto_common_channels_butone(sptr
, CMD_NICK
, NULL
, ":%s", nick
);
912 add_history(sptr
, 1);
913 sendcmdto_serv_butone(sptr
, CMD_NICK
, cptr
, "%s %Tu", nick
,
917 sendcmdto_one(sptr
, CMD_NICK
, sptr
, ":%s", nick
);
919 if ((cli_name(sptr
))[0])
921 strcpy(cli_name(sptr
), nick
);
925 /* Local client setting NICK the first time */
927 strcpy(cli_name(sptr
), nick
);
928 if (!cli_user(sptr
)) {
929 cli_user(sptr
) = make_user(sptr
);
930 cli_user(sptr
)->server
= &me
;
934 cli_unreg(sptr
) &= ~CLIREG_NICK
; /* nickname now set */
937 * If the client hasn't gotten a cookie-ping yet,
938 * choose a cookie and send it. -record!jegelhof@cloud9.net
940 if (!cli_cookie(sptr
)) {
942 cli_cookie(sptr
) = (ircrandom() & 0x7fffffff);
943 } while (!cli_cookie(sptr
));
944 sendrawto_one(cptr
, MSG_PING
" :%u", cli_cookie(sptr
));
946 else if (!cli_unreg(sptr
)) {
948 * USER and PONG already received, now we have NICK.
949 * register_user may reject the client and call exit_client
950 * for it - must test this and exit m_nick too !
952 cli_lastnick(sptr
) = TStime(); /* Always local client */
953 if (register_user(cptr
, sptr
, nick
, cli_user(sptr
)->username
) == CPTR_KILLED
)
960 /** Calculate the hash value for a target.
961 * @param[in] target Pointer to target, cast to unsigned int.
962 * @return Hash value constructed from the pointer.
964 static unsigned char hash_target(unsigned int target
)
966 return (unsigned char) (target
>> 16) ^ (target
>> 8);
969 /** Records \a target as a recent target for \a sptr.
970 * @param[in] sptr User who has sent to a new target.
971 * @param[in] target Target to add.
974 add_target(struct Client
*sptr
, void *target
)
976 /* Ok, this shouldn't work esp on alpha
978 unsigned char hash
= hash_target((unsigned long) target
);
979 unsigned char* targets
;
982 assert(cli_local(sptr
));
984 targets
= cli_targets(sptr
);
989 for (i
= 0; i
< MAXTARGETS
; ++i
) {
990 if (targets
[i
] == hash
)
996 memmove(&targets
[RESERVEDTARGETS
+ 1],
997 &targets
[RESERVEDTARGETS
], MAXTARGETS
- RESERVEDTARGETS
- 1);
998 targets
[RESERVEDTARGETS
] = hash
;
1001 /** Check whether \a sptr can send to or join \a target yet.
1002 * @param[in] sptr User trying to join a channel or send a message.
1003 * @param[in] target Target of the join or message.
1004 * @param[in] name Name of the target.
1005 * @param[in] created If non-zero, trying to join a new channel.
1006 * @return Non-zero if too many target changes; zero if okay to send.
1008 int check_target_limit(struct Client
*sptr
, void *target
, const char *name
,
1011 unsigned char hash
= hash_target((unsigned long) target
);
1013 unsigned char* targets
;
1016 assert(cli_local(sptr
));
1017 targets
= cli_targets(sptr
);
1019 /* If user is invited to channel, give him/her a free target */
1020 if (IsChannelName(name
) && IsInvited(sptr
, target
))
1023 /* opers always have a free target */
1028 * Same target as last time?
1030 if (targets
[0] == hash
)
1032 for (i
= 1; i
< MAXTARGETS
; ++i
) {
1033 if (targets
[i
] == hash
) {
1034 memmove(&targets
[1], &targets
[0], i
);
1043 if (CurrentTime
< cli_nexttarget(sptr
)) {
1044 if (cli_nexttarget(sptr
) - CurrentTime
< TARGET_DELAY
+ 8) {
1046 * No server flooding
1048 cli_nexttarget(sptr
) += 2;
1049 send_reply(sptr
, ERR_TARGETTOOFAST
, name
,
1050 cli_nexttarget(sptr
) - CurrentTime
);
1055 cli_nexttarget(sptr
) += TARGET_DELAY
;
1056 if (cli_nexttarget(sptr
) < CurrentTime
- (TARGET_DELAY
* (MAXTARGETS
- 1)))
1057 cli_nexttarget(sptr
) = CurrentTime
- (TARGET_DELAY
* (MAXTARGETS
- 1));
1060 memmove(&targets
[1], &targets
[0], MAXTARGETS
- 1);
1065 /** Allows a channel operator to avoid target change checks when
1066 * sending messages to users on their channel.
1067 * @param[in] source User sending the message.
1068 * @param[in] nick Destination of the message.
1069 * @param[in] channel Name of channel being sent to.
1070 * @param[in] text Message to send.
1071 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
1073 /* Added 971023 by Run. */
1074 int whisper(struct Client
* source
, const char* nick
, const char* channel
,
1075 const char* text
, int is_notice
)
1077 struct Client
* dest
;
1078 struct Channel
* chptr
;
1079 struct Membership
* membership
;
1081 assert(0 != source
);
1083 assert(0 != channel
);
1084 assert(MyUser(source
));
1086 if (!(dest
= FindUser(nick
))) {
1087 return send_reply(source
, ERR_NOSUCHNICK
, nick
);
1089 if (!(chptr
= FindChannel(channel
))) {
1090 return send_reply(source
, ERR_NOSUCHCHANNEL
, channel
);
1093 * compare both users channel lists, instead of the channels user list
1094 * since the link is the same, this should be a little faster for channels
1095 * with a lot of users
1097 for (membership
= cli_user(source
)->channel
; membership
; membership
= membership
->next_channel
) {
1098 if (chptr
== membership
->channel
)
1101 if (0 == membership
) {
1102 return send_reply(source
, ERR_NOTONCHANNEL
, chptr
->chname
);
1104 if (!IsVoicedOrOpped(membership
)) {
1105 return send_reply(source
, ERR_VOICENEEDED
, chptr
->chname
);
1108 * lookup channel in destination
1110 assert(0 != cli_user(dest
));
1111 for (membership
= cli_user(dest
)->channel
; membership
; membership
= membership
->next_channel
) {
1112 if (chptr
== membership
->channel
)
1115 if (0 == membership
|| IsZombie(membership
)) {
1116 return send_reply(source
, ERR_USERNOTINCHANNEL
, cli_name(dest
), chptr
->chname
);
1118 if (is_silenced(source
, dest
))
1121 if (cli_user(dest
)->away
)
1122 send_reply(source
, RPL_AWAY
, cli_name(dest
), cli_user(dest
)->away
);
1124 sendcmdto_one(source
, CMD_NOTICE
, dest
, "%C :%s", dest
, text
);
1126 sendcmdto_one(source
, CMD_PRIVATE
, dest
, "%C :%s", dest
, text
);
1131 /** Send a user mode change for \a cptr to neighboring servers.
1132 * @param[in] cptr User whose mode is changing.
1133 * @param[in] sptr Client who sent us the mode change message.
1134 * @param[in] old Prior set of user flags.
1135 * @param[in] prop If non-zero, also include FLAG_OPER.
1137 void send_umode_out(struct Client
*cptr
, struct Client
*sptr
,
1138 struct Flags
*old
, int prop
)
1141 struct Client
*acptr
;
1143 send_umode(NULL
, sptr
, old
, prop
? SEND_UMODES
: SEND_UMODES_BUT_OPER
);
1145 for (i
= HighestFd
; i
>= 0; i
--)
1147 if ((acptr
= LocalClientArray
[i
]) && IsServer(acptr
) &&
1148 (acptr
!= cptr
) && (acptr
!= sptr
) && *umodeBuf
)
1149 sendcmdto_one(sptr
, CMD_MODE
, acptr
, "%s %s", cli_name(sptr
), umodeBuf
);
1151 if (cptr
&& MyUser(cptr
))
1152 send_umode(cptr
, sptr
, old
, ALL_UMODES
);
1156 /** Call \a fmt for each Client named in \a names.
1157 * @param[in] sptr Client requesting information.
1158 * @param[in] names Space-delimited list of nicknames.
1159 * @param[in] rpl Base reply string for messages.
1160 * @param[in] fmt Formatting callback function.
1162 void send_user_info(struct Client
* sptr
, char* names
, int rpl
, InfoFormatter fmt
)
1167 int users_found
= 0;
1168 struct Client
* acptr
;
1175 mb
= msgq_make(sptr
, rpl_str(rpl
), cli_name(&me
), cli_name(sptr
));
1177 for (name
= ircd_strtok(&p
, names
, " "); name
; name
= ircd_strtok(&p
, 0, " ")) {
1178 if ((acptr
= FindUser(name
))) {
1180 msgq_append(0, mb
, " ");
1181 (*fmt
)(acptr
, sptr
, mb
);
1183 if (5 == ++arg_count
)
1186 send_buffer(sptr
, mb
, 0);
1190 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
1191 * @param[in,out] cptr User who is getting a new flag.
1192 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
1196 hide_hostmask(struct Client
*cptr
, unsigned int flag
)
1198 struct Membership
*chan
;
1201 case FLAG_HIDDENHOST
:
1202 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
1203 if (MyConnect(cptr
) && !feature_bool(FEAT_HOST_HIDING
))
1205 /* If the user is +h, we don't hide the hostmask. Set the flag to keep sync though */
1206 if (HasSetHost(cptr
)) {
1207 SetFlag(cptr
, flag
);
1212 /* Invalidate all bans against the user so we check them again */
1213 for (chan
= (cli_user(cptr
))->channel
; chan
;
1214 chan
= chan
->next_channel
)
1215 ClearBanValid(chan
);
1221 SetFlag(cptr
, flag
);
1222 if (!HasFlag(cptr
, FLAG_HIDDENHOST
) || !HasFlag(cptr
, FLAG_ACCOUNT
))
1225 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Registered");
1226 ircd_snprintf(0, cli_user(cptr
)->host
, HOSTLEN
, "%s.%s",
1227 cli_user(cptr
)->account
, feature_str(FEAT_HIDDEN_HOST
));
1229 /* ok, the client is now fully hidden, so let them know -- hikari */
1230 if (MyConnect(cptr
))
1231 send_reply(cptr
, RPL_HOSTHIDDEN
, cli_user(cptr
)->host
);
1234 * Go through all channels the client was on, rejoin him
1235 * and set the modes, if any
1237 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
)
1241 /* Send a JOIN unless the user's join has been delayed. */
1242 if (!IsDelayedJoin(chan
))
1243 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
, 0,
1244 "%H", chan
->channel
);
1245 if (IsChanOp(chan
) && HasVoice(chan
))
1246 sendcmdto_channel_butserv_butone(&his
, CMD_MODE
, chan
->channel
, cptr
, 0,
1247 "%H +ov %C %C", chan
->channel
, cptr
,
1249 else if (IsChanOp(chan
) || HasVoice(chan
))
1250 sendcmdto_channel_butserv_butone(&his
, CMD_MODE
, chan
->channel
, cptr
, 0,
1251 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1257 * set_hostmask() - derived from hide_hostmask()
1260 int set_hostmask(struct Client
*cptr
, char *hostmask
, char *password
)
1264 char *host
, *new_vhost
, *vhost_pass
;
1265 char hiddenhost
[USERLEN
+ HOSTLEN
+ 2];
1266 struct Membership
*chan
;
1268 Debug((DEBUG_INFO
, "set_hostmask() %C, %s, %s", cptr
, hostmask
, password
));
1270 /* sethost enabled? */
1271 if (MyConnect(cptr
) && !feature_bool(FEAT_SETHOST
)) {
1272 send_reply(cptr
, ERR_DISABLED
, "SETHOST");
1276 /* sethost enabled for users? */
1277 if (MyConnect(cptr
) && !IsAnOper(cptr
) && !feature_bool(FEAT_SETHOST_USER
)) {
1278 send_reply(cptr
, ERR_NOPRIVILEGES
);
1282 /* MODE_DEL: restore original hostmask */
1283 if (EmptyString(hostmask
)) {
1284 /* is already sethost'ed? */
1285 if (IsSetHost(cptr
)) {
1287 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1288 /* If they are +rx, we need to return to their +x host, not their "real" host */
1289 if (HasHiddenHost(cptr
))
1290 ircd_snprintf(0, cli_user(cptr
)->host
, HOSTLEN
, "%s.%s",
1291 cli_user(cptr
)->account
, feature_str(FEAT_HIDDEN_HOST
));
1293 strncpy(cli_user(cptr
)->host
, cli_user(cptr
)->realhost
, HOSTLEN
);
1294 strncpy(cli_user(cptr
)->username
, cli_user(cptr
)->realusername
, USERLEN
);
1296 if (MyConnect(cptr
))
1297 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
,
1298 "SETHOST (%s@%s) by (%#R): restoring real hostmask",
1299 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
);
1302 /* MODE_ADD: set a new hostmask */
1304 /* chop up ident and host.cc */
1305 if ((host
= strrchr(hostmask
, '@'))) /* oper can specifiy ident@host.cc */
1307 else /* user can only specifiy host.cc [password] */
1312 if (MyConnect(cptr
)) {
1313 if (IsAnOper(cptr
)) {
1314 if ((new_vhost
= IsVhost(host
, 1)) == NULL
) {
1315 if (!feature_bool(FEAT_SETHOST_FREEFORM
)) {
1316 send_reply(cptr
, ERR_HOSTUNAVAIL
, hostmask
);
1317 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
,
1318 "SETHOST (%s@%s) by (%#R): no such s-line",
1319 (host
!= hostmask
) ? hostmask
: cli_user(cptr
)->username
, host
, cptr
);
1321 } else /* freeform active, log and go */
1324 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1325 /* set the new ident and host */
1326 if (host
!= hostmask
) /* oper only specified host.cc */
1327 strncpy(cli_user(cptr
)->username
, hostmask
, USERLEN
);
1328 strncpy(cli_user(cptr
)->host
, host
, HOSTLEN
);
1330 log_write(LS_SETHOST
, (freeform
) ? L_NOTICE
: L_INFO
,
1331 (freeform
) ? 0 : LOG_NOSNOTICE
, "SETHOST (%s@%s) by (%#R)%s",
1332 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
,
1333 (freeform
) ? ": using freeform" : "");
1335 * plain user sethost, handled here
1338 /* empty password? */
1339 if (EmptyString(password
)) {
1340 send_reply(cptr
, ERR_NEEDMOREPARAMS
, "MODE");
1343 /* no such s-line */
1344 if ((new_vhost
= IsVhost(host
, 0)) == NULL
) {
1345 send_reply(cptr
, ERR_HOSTUNAVAIL
, hostmask
);
1346 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
, "SETHOST (%s@%s %s) by (%#R): no such s-line",
1347 cli_user(cptr
)->username
, host
, password
, cptr
);
1351 if ((vhost_pass
= IsVhostPass(new_vhost
)) == NULL
) {
1352 send_reply(cptr
, ERR_PASSWDMISMATCH
);
1353 log_write(LS_SETHOST
, L_INFO
, 0, "SETHOST (%s@%s %s) by (%#R): trying to use an oper s-line",
1354 cli_user(cptr
)->username
, host
, password
, cptr
);
1357 /* incorrect password */
1358 if (strCasediff(vhost_pass
, password
)) {
1359 send_reply(cptr
, ERR_PASSWDMISMATCH
);
1360 log_write(LS_SETHOST
, L_NOTICE
, 0, "SETHOST (%s@%s %s) by (%#R): incorrect password",
1361 cli_user(cptr
)->username
, host
, password
, cptr
);
1364 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1365 /* set the new host */
1366 strncpy(cli_user(cptr
)->host
, new_vhost
, HOSTLEN
);
1368 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
, "SETHOST (%s@%s) by (%#R)",
1369 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
);
1371 } else { /* remote user */
1372 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1373 if (host
!= hostmask
) /* oper only specified host.cc */
1374 strncpy(cli_user(cptr
)->username
, hostmask
, USERLEN
);
1375 strncpy(cli_user(cptr
)->host
, host
, HOSTLEN
);
1384 if (MyConnect(cptr
)) {
1385 ircd_snprintf(0, hiddenhost
, HOSTLEN
+ USERLEN
+ 2, "%s@%s",
1386 cli_user(cptr
)->username
, cli_user(cptr
)->host
);
1387 send_reply(cptr
, RPL_HOSTHIDDEN
, hiddenhost
);
1391 /* Code copied from hide_hostmask(). This is the old (pre-delayedjoin)
1392 * version. Switch this in if you're not using the delayed join patch. */
1394 * Go through all channels the client was on, rejoin him
1395 * and set the modes, if any
1397 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
) {
1400 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
,
1401 "%H", chan
->channel
);
1402 if (IsChanOp(chan
) && HasVoice(chan
)) {
1403 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
,
1404 "%H +ov %C %C", chan
->channel
, cptr
, cptr
);
1405 } else if (IsChanOp(chan
) || HasVoice(chan
)) {
1406 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
,
1407 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1413 * Go through all channels the client was on, rejoin him
1414 * and set the modes, if any
1416 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
) {
1419 /* If this channel has delayed joins and the user has no modes, just set
1420 * the delayed join flag rather than showing the join, even if the user
1421 * was visible before */
1422 if (!IsChanOp(chan
) && !HasVoice(chan
)
1423 && (chan
->channel
->mode
.mode
& MODE_DELJOINS
)) {
1424 SetDelayedJoin(chan
);
1426 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
, 0,
1427 "%H", chan
->channel
);
1429 if (IsChanOp(chan
) && HasVoice(chan
)) {
1430 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
, 0,
1431 "%H +ov %C %C", chan
->channel
, cptr
, cptr
);
1432 } else if (IsChanOp(chan
) || HasVoice(chan
)) {
1433 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
, 0,
1434 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1440 /** Set a user's mode. This function checks that \a cptr is trying to
1441 * set his own mode, prevents local users from setting inappropriate
1442 * modes through this function, and applies any other side effects of
1443 * a successful mode change.
1445 * @param[in,out] cptr User setting someone's mode.
1446 * @param[in] sptr Client who sent the mode change message.
1447 * @param[in] parc Number of parameters in \a parv.
1448 * @param[in] parv Parameters to MODE.
1451 int set_user_mode(struct Client
*cptr
, struct Client
*sptr
, int parc
, char *parv
[])
1455 struct Client
*acptr
;
1458 struct Flags setflags
;
1459 unsigned int tmpmask
= 0;
1460 int snomask_given
= 0;
1462 char *hostmask
, *password
;
1464 int do_host_hiding
= 0;
1465 int do_set_host
= 0;
1467 hostmask
= password
= NULL
;
1471 return need_more_params(sptr
, "MODE");
1473 if (!(acptr
= FindUser(parv
[1])))
1475 if (MyConnect(sptr
))
1476 send_reply(sptr
, ERR_NOSUCHCHANNEL
, parv
[1]);
1480 if (IsServer(sptr
) || sptr
!= acptr
)
1483 sendwallto_group_butone(&me
, WALL_WALLOPS
, 0,
1484 "MODE for User %s from %s!%s", parv
[1],
1485 cli_name(cptr
), cli_name(sptr
));
1487 send_reply(sptr
, ERR_USERSDONTMATCH
);
1495 for (i
= 0; i
< USERMODELIST_SIZE
; i
++)
1497 if (HasFlag(sptr
, userModeList
[i
].flag
) &&
1498 ((userModeList
[i
].flag
!= FLAG_ACCOUNT
) &&
1499 (userModeList
[i
].flag
!= FLAG_SETHOST
)))
1500 *m
++ = userModeList
[i
].c
;
1503 send_reply(sptr
, RPL_UMODEIS
, buf
);
1504 if (HasFlag(sptr
, FLAG_SERVNOTICE
) && MyConnect(sptr
)
1505 && cli_snomask(sptr
) !=
1506 (unsigned int)(IsOper(sptr
) ? SNO_OPERDEFAULT
: SNO_DEFAULT
))
1507 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
1512 * find flags already set for user
1513 * why not just copy them?
1515 setflags
= cli_flags(sptr
);
1517 if (MyConnect(sptr
))
1518 tmpmask
= cli_snomask(sptr
);
1521 * parse mode change string(s)
1523 for (p
= &parv
[2]; *p
; p
++) { /* p is changed in loop too */
1524 for (m
= *p
; *m
; m
++) {
1533 if (*(p
+ 1) && is_snomask(*(p
+ 1))) {
1535 tmpmask
= umode_make_snomask(tmpmask
, *++p
, what
);
1536 tmpmask
&= (IsAnOper(sptr
) ? SNO_ALL
: SNO_USER
);
1539 tmpmask
= (what
== MODE_ADD
) ?
1540 (IsAnOper(sptr
) ? SNO_OPERDEFAULT
: SNO_DEFAULT
) : 0;
1542 SetServNotice(sptr
);
1544 ClearServNotice(sptr
);
1547 if (what
== MODE_ADD
)
1553 if (what
== MODE_ADD
)
1556 ClrFlag(sptr
, FLAG_OPER
);
1557 ClrFlag(sptr
, FLAG_LOCOP
);
1558 if (MyConnect(sptr
))
1560 tmpmask
= cli_snomask(sptr
) & ~SNO_OPER
;
1561 cli_handler(sptr
) = CLIENT_HANDLER
;
1566 if (what
== MODE_ADD
)
1570 ClrFlag(sptr
, FLAG_OPER
);
1571 ClrFlag(sptr
, FLAG_LOCOP
);
1572 if (MyConnect(sptr
))
1574 tmpmask
= cli_snomask(sptr
) & ~SNO_OPER
;
1575 cli_handler(sptr
) = CLIENT_HANDLER
;
1580 if (what
== MODE_ADD
)
1583 if (!feature_bool(FEAT_AUTOINVISIBLE
) || IsOper(sptr
)) /* Don't allow non-opers to -i if FEAT_AUTOINVISIBLE is set */
1584 ClearInvisible(sptr
);
1587 if (what
== MODE_ADD
)
1593 if (what
== MODE_ADD
)
1594 SetChannelService(sptr
);
1596 ClearChannelService(sptr
);
1599 if (what
== MODE_ADD
)
1605 if (what
== MODE_ADD
)
1611 if (what
== MODE_ADD
)
1617 if (what
== MODE_ADD
)
1623 if (what
== MODE_ADD
)
1627 if (what
== MODE_ADD
) {
1628 if (*(p
+ 1) && is_hostmask(*(p
+ 1))) {
1631 /* DON'T step p onto the trailing NULL in the parameter array! - splidge */
1638 send_reply(sptr
, ERR_NEEDMOREPARAMS
, "SETHOST");
1640 send_reply(sptr
, ERR_BADHOSTMASK
, *(p
+1));
1641 p
++; /* Swallow the arg anyway */
1644 } else { /* MODE_DEL */
1651 if (what
== MODE_ADD
)
1652 SetAccountOnly(sptr
);
1654 ClearAccountOnly(sptr
);
1657 if (what
== MODE_ADD
)
1660 ClearParanoid(sptr
);
1663 send_reply(sptr
, ERR_UMODEUNKNOWNFLAG
, *m
);
1669 * Evaluate rules for new user mode
1670 * Stop users making themselves operators too easily:
1672 if (!IsServer(cptr
))
1674 if (!FlagHas(&setflags
, FLAG_OPER
) && IsOper(sptr
))
1676 if (!FlagHas(&setflags
, FLAG_LOCOP
) && IsLocOp(sptr
))
1679 * new umode; servers can set it, local users cannot;
1680 * prevents users from /kick'ing or /mode -o'ing
1682 if (!FlagHas(&setflags
, FLAG_CHSERV
) && !IsOper(sptr
))
1683 ClearChannelService(sptr
);
1684 if (!FlagHas(&setflags
, FLAG_XTRAOP
) && !IsOper(sptr
))
1686 if (!FlagHas(&setflags
, FLAG_NOCHAN
) && !(IsOper(sptr
) || feature_bool(FEAT_USER_HIDECHANS
)))
1688 if (!FlagHas(&setflags
, FLAG_NOIDLE
) && !IsOper(sptr
))
1690 if (!FlagHas(&setflags
, FLAG_PARANOID
) && !IsOper(sptr
))
1691 ClearParanoid(sptr
);
1694 * only send wallops to opers
1696 if (feature_bool(FEAT_WALLOPS_OPER_ONLY
) && !IsAnOper(sptr
) &&
1697 !FlagHas(&setflags
, FLAG_WALLOP
))
1699 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY
) && MyConnect(sptr
) &&
1700 !IsAnOper(sptr
) && !FlagHas(&setflags
, FLAG_SERVNOTICE
))
1702 ClearServNotice(sptr
);
1703 set_snomask(sptr
, 0, SNO_SET
);
1705 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY
) &&
1706 !IsAnOper(sptr
) && !FlagHas(&setflags
, FLAG_DEBUG
))
1709 if (MyConnect(sptr
))
1711 if ((FlagHas(&setflags
, FLAG_OPER
) || FlagHas(&setflags
, FLAG_LOCOP
)) &&
1713 det_confs_butmask(sptr
, CONF_CLIENT
& ~CONF_OPERATOR
);
1715 if (SendServNotice(sptr
))
1717 if (tmpmask
!= cli_snomask(sptr
))
1718 set_snomask(sptr
, tmpmask
, SNO_SET
);
1719 if (cli_snomask(sptr
) && snomask_given
)
1720 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
1723 set_snomask(sptr
, 0, SNO_SET
);
1726 * Compare new flags with old flags and send string which
1727 * will cause servers to update correctly.
1729 if (!FlagHas(&setflags
, FLAG_OPER
) && IsOper(sptr
))
1733 client_set_privs(sptr
, NULL
); /* may set propagate privilege */
1735 /* remember propagate privilege setting */
1736 if (HasPriv(sptr
, PRIV_PROPAGATE
))
1738 if (FlagHas(&setflags
, FLAG_OPER
) && !IsOper(sptr
))
1740 /* user no longer oper */
1742 client_set_privs(sptr
, NULL
); /* will clear propagate privilege */
1744 if (FlagHas(&setflags
, FLAG_INVISIBLE
) && !IsInvisible(sptr
))
1745 --UserStats
.inv_clients
;
1746 if (!FlagHas(&setflags
, FLAG_INVISIBLE
) && IsInvisible(sptr
))
1747 ++UserStats
.inv_clients
;
1748 if (!FlagHas(&setflags
, FLAG_HIDDENHOST
) && do_host_hiding
)
1749 hide_hostmask(sptr
, FLAG_HIDDENHOST
);
1751 /* We clear the flag in the old mask, so that the +h will be sent */
1752 /* Only do this if we're SETTING +h and it succeeded */
1753 if (set_hostmask(sptr
, hostmask
, password
) && hostmask
)
1754 FlagClr(&setflags
, FLAG_SETHOST
);
1756 send_umode_out(cptr
, sptr
, &setflags
, prop
);
1761 /** Build a mode string to describe modes for \a cptr.
1762 * @param[in] cptr Some user.
1763 * @return Pointer to a static buffer.
1765 char *umode_str(struct Client
*cptr
)
1767 /* Maximum string size: "owidgrx\0" */
1770 struct Flags c_flags
= cli_flags(cptr
);
1772 if (!HasPriv(cptr
, PRIV_PROPAGATE
))
1773 FlagClr(&c_flags
, FLAG_OPER
);
1775 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
1777 if (FlagHas(&c_flags
, userModeList
[i
].flag
) &&
1778 userModeList
[i
].flag
>= FLAG_GLOBAL_UMODES
)
1779 *m
++ = userModeList
[i
].c
;
1782 if (IsAccount(cptr
))
1784 char* t
= cli_user(cptr
)->account
;
1787 while ((*m
++ = *t
++))
1790 if (cli_user(cptr
)->acc_create
) {
1792 Debug((DEBUG_DEBUG
, "Sending timestamped account in user mode for "
1793 "account \"%s\"; timestamp %Tu", cli_user(cptr
)->account
,
1794 cli_user(cptr
)->acc_create
));
1795 ircd_snprintf(0, t
= nbuf
, sizeof(nbuf
), ":%Tu",
1796 cli_user(cptr
)->acc_create
);
1797 m
--; /* back up over previous nul-termination */
1798 while ((*m
++ = *t
++))
1801 m
--; /* Step back over the '\0' */
1804 if (IsSetHost(cptr
)) {
1806 ircd_snprintf(0, m
, USERLEN
+ HOSTLEN
+ 2, "%s@%s", cli_user(cptr
)->username
,
1807 cli_user(cptr
)->host
);
1810 return umodeBuf
; /* Note: static buffer, gets
1811 overwritten by send_umode() */
1814 /** Send a mode change string for \a sptr to \a cptr.
1815 * @param[in] cptr Destination of mode change message.
1816 * @param[in] sptr User whose mode has changed.
1817 * @param[in] old Pre-change set of modes for \a sptr.
1818 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1819 * SEND_UMODES, to select which changed user modes to send.
1821 void send_umode(struct Client
*cptr
, struct Client
*sptr
, struct Flags
*old
,
1828 int what
= MODE_NULL
;
1831 * Build a string in umodeBuf to represent the change in the user's
1832 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1833 * the modes indicated by sendset.
1837 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
1839 flag
= userModeList
[i
].flag
;
1840 if (FlagHas(old
, flag
)
1841 == HasFlag(sptr
, flag
))
1847 case SEND_UMODES_BUT_OPER
:
1848 if (flag
== FLAG_OPER
)
1850 /* and fall through */
1852 if (flag
< FLAG_GLOBAL_UMODES
)
1856 /* Special case for SETHOST.. */
1857 if (flag
== FLAG_SETHOST
) {
1858 /* Don't send to users */
1859 if (cptr
&& MyUser(cptr
))
1862 /* If we're setting +h, add the parameter later */
1863 if (!FlagHas(old
, flag
))
1866 if (FlagHas(old
, flag
))
1868 if (what
== MODE_DEL
)
1869 *m
++ = userModeList
[i
].c
;
1874 *m
++ = userModeList
[i
].c
;
1877 else /* !FlagHas(old, flag) */
1879 if (what
== MODE_ADD
)
1880 *m
++ = userModeList
[i
].c
;
1885 *m
++ = userModeList
[i
].c
;
1891 ircd_snprintf(0, m
, USERLEN
+ HOSTLEN
+ 1, "%s@%s", cli_user(sptr
)->username
,
1892 cli_user(sptr
)->host
);
1895 if (*umodeBuf
&& cptr
)
1896 sendcmdto_one(sptr
, CMD_MODE
, cptr
, "%s %s", cli_name(sptr
), umodeBuf
);
1900 * Check to see if this resembles a sno_mask. It is if 1) there is
1901 * at least one digit and 2) The first digit occurs before the first
1902 * alphabetic character.
1903 * @param[in] word Word to check for sno_mask-ness.
1904 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1906 int is_snomask(char *word
)
1910 for (; *word
; word
++)
1913 else if (IsAlpha(*word
))
1920 * Check to see if it resembles a valid hostmask.
1922 int is_hostmask(char *word
)
1927 Debug((DEBUG_INFO
, "is_hostmask() %s", word
));
1929 if (strlen(word
) > (HOSTLEN
+ USERLEN
+ 1) || strlen(word
) <= 0)
1932 /* if a host is specified, make sure it's valid */
1933 host
= strrchr(word
, '@');
1935 if (strlen(++host
) < 1)
1937 if (strlen(host
) > HOSTLEN
)
1942 if ('@' == *word
) /* no leading @'s */
1945 if ('#' == *word
) { /* numeric index given? */
1946 for (word
++; *word
; word
++) {
1947 if (!IsDigit(*word
))
1953 /* normal hostmask, account for at most one '@' */
1954 for (; *word
; word
++) {
1959 if (!IsHostChar(*word
))
1962 return (1 < i
) ? 0 : 1; /* no more than on '@' */
1968 * IsVhost() - Check if given host is a valid spoofhost
1969 * (ie: configured thru a S:line)
1971 static char *IsVhost(char *hostmask
, int oper
)
1973 unsigned int i
= 0, y
= 0;
1974 struct sline
*sconf
;
1976 Debug((DEBUG_INFO
, "IsVhost() %s", hostmask
));
1978 if (EmptyString(hostmask
))
1981 /* spoofhost specified as index, ie: #27 */
1982 if ('#' == hostmask
[0]) {
1983 y
= atoi(hostmask
+ 1);
1984 for (i
= 0, sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
) {
1985 if (!oper
&& EmptyString(sconf
->passwd
))
1988 return sconf
->spoofhost
;
1993 /* spoofhost specified as host, ie: host.cc */
1994 for (sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
)
1995 if (strCasediff(hostmask
, sconf
->spoofhost
) == 0)
1996 return sconf
->spoofhost
;
2002 * IsVhostPass() - Check if given spoofhost has a password
2003 * associated with it, and if, return the password (cleartext)
2005 static char *IsVhostPass(char *hostmask
)
2007 struct sline
*sconf
;
2009 Debug((DEBUG_INFO
, "IsVhostPass() %s", hostmask
));
2011 if (EmptyString(hostmask
))
2014 for (sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
)
2015 if (strCasediff(hostmask
, sconf
->spoofhost
) == 0) {
2016 Debug((DEBUG_INFO
, "sconf->passwd %s", sconf
->passwd
));
2017 return EmptyString(sconf
->passwd
) ? NULL
: sconf
->passwd
;
2023 /** Update snomask \a oldmask according to \a arg and \a what.
2024 * @param[in] oldmask Original user mask.
2025 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
2026 * @param[in] what MODE_ADD if adding the mask.
2027 * @return New value of service notice mask.
2029 unsigned int umode_make_snomask(unsigned int oldmask
, char *arg
, int what
)
2031 unsigned int sno_what
;
2032 unsigned int newmask
;
2036 if (what
== MODE_ADD
)
2041 else if (*arg
== '-')
2044 if (what
== MODE_ADD
)
2050 sno_what
= (what
== MODE_ADD
) ? SNO_SET
: SNO_DEL
;
2051 /* pity we don't have strtoul everywhere */
2052 newmask
= (unsigned int)atoi(arg
);
2053 if (sno_what
== SNO_DEL
)
2054 newmask
= oldmask
& ~newmask
;
2055 else if (sno_what
== SNO_ADD
)
2060 /** Remove \a cptr from the singly linked list \a list.
2061 * @param[in] cptr Client to remove from list.
2062 * @param[in,out] list Pointer to head of list containing \a cptr.
2064 static void delfrom_list(struct Client
*cptr
, struct SLink
**list
)
2067 struct SLink
* prv
= NULL
;
2069 for (tmp
= *list
; tmp
; tmp
= tmp
->next
) {
2070 if (tmp
->value
.cptr
== cptr
) {
2072 prv
->next
= tmp
->next
;
2082 /** Set \a cptr's server notice mask, according to \a what.
2083 * @param[in,out] cptr Client whose snomask is updating.
2084 * @param[in] newmask Base value for new snomask.
2085 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
2087 void set_snomask(struct Client
*cptr
, unsigned int newmask
, int what
)
2089 unsigned int oldmask
, diffmask
; /* unsigned please */
2093 oldmask
= cli_snomask(cptr
);
2095 if (what
== SNO_ADD
)
2097 else if (what
== SNO_DEL
)
2098 newmask
= oldmask
& ~newmask
;
2099 else if (what
!= SNO_SET
) /* absolute set, no math needed */
2100 sendto_opmask_butone(0, SNO_OLDSNO
, "setsnomask called with %d ?!", what
);
2102 newmask
&= (IsAnOper(cptr
) ? SNO_ALL
: SNO_USER
);
2104 diffmask
= oldmask
^ newmask
;
2106 for (i
= 0; diffmask
>> i
; i
++) {
2107 if (((diffmask
>> i
) & 1))
2109 if (((newmask
>> i
) & 1))
2112 tmp
->next
= opsarray
[i
];
2113 tmp
->value
.cptr
= cptr
;
2117 /* not real portable :( */
2118 delfrom_list(cptr
, &opsarray
[i
]);
2121 cli_snomask(cptr
) = newmask
;
2124 /** Check whether \a sptr is allowed to send a message to \a acptr.
2125 * If \a sptr is a remote user, it means some server has an outdated
2126 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
2127 * in the direction of \a sptr. Skip the check if \a sptr is a server.
2128 * @param[in] sptr Client trying to send a message.
2129 * @param[in] acptr Destination of message.
2130 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
2132 int is_silenced(struct Client
*sptr
, struct Client
*acptr
)
2136 size_t buf_used
, slen
;
2139 if (IsServer(sptr
) || !(user
= cli_user(acptr
))
2140 || !(found
= find_ban(sptr
, user
->silence
)))
2142 assert(!(found
->flags
& BAN_EXCEPTION
));
2143 if (!MyConnect(sptr
)) {
2144 /* Buffer positive silence to send back. */
2145 buf_used
= strlen(found
->banstr
);
2146 memcpy(buf
, found
->banstr
, buf_used
);
2147 /* Add exceptions to buffer. */
2148 for (found
= user
->silence
; found
; found
= found
->next
) {
2149 if (!(found
->flags
& BAN_EXCEPTION
))
2151 slen
= strlen(found
->banstr
);
2152 if (buf_used
+ slen
+ 4 > 400) {
2153 buf
[buf_used
] = '\0';
2154 sendcmdto_one(acptr
, CMD_SILENCE
, cli_from(sptr
), "%C %s", sptr
, buf
);
2158 buf
[buf_used
++] = ',';
2159 buf
[buf_used
++] = '+';
2160 buf
[buf_used
++] = '~';
2161 memcpy(buf
+ buf_used
, found
->banstr
, slen
);
2164 /* Flush silence buffer. */
2166 buf
[buf_used
] = '\0';
2167 sendcmdto_one(acptr
, CMD_SILENCE
, cli_from(sptr
), "%C %s", sptr
, buf
);
2174 /** Send RPL_ISUPPORT lines to \a cptr.
2175 * @param[in] cptr Client to send ISUPPORT to.
2179 send_supported(struct Client
*cptr
)
2181 char featurebuf
[512];
2183 ircd_snprintf(0, featurebuf
, sizeof(featurebuf
), FEATURES1
, FEATURESVALUES1
);
2184 send_reply(cptr
, RPL_ISUPPORT
, featurebuf
);
2185 ircd_snprintf(0, featurebuf
, sizeof(featurebuf
), FEATURES2
, FEATURESVALUES2
);
2186 send_reply(cptr
, RPL_ISUPPORT
, featurebuf
);
2188 return 0; /* convenience return, if it's ever needed */