X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=ircd%2Fs_user.c;h=1f0e0d92ddd990ff543c0445af741d7d72464555;hb=c3e49ba3f999d28a480f8f66cc633d9ea968099f;hp=cb6fd5af0efaf3449b757161d8ff09879ca7f3d2;hpb=237ccc6d4be14eea579e62b8f2b7be4f54016317;p=ircu2.10.12-pk.git diff --git a/ircd/s_user.c b/ircd/s_user.c index cb6fd5a..1f0e0d9 100644 --- a/ircd/s_user.c +++ b/ircd/s_user.c @@ -19,39 +19,45 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * $Id$ */ +/** @file + * @brief Miscellaneous user-related helper functions. + * @version $Id$ + */ +#include "config.h" + #include "s_user.h" #include "IPcheck.h" #include "channel.h" #include "class.h" #include "client.h" -#include "gline.h" #include "hash.h" #include "ircd.h" #include "ircd_alloc.h" #include "ircd_chattr.h" +#include "ircd_features.h" #include "ircd_log.h" #include "ircd_reply.h" +#include "ircd_snprintf.h" #include "ircd_string.h" #include "list.h" #include "match.h" +#include "motd.h" #include "msg.h" +#include "msgq.h" #include "numeric.h" #include "numnicks.h" #include "parse.h" #include "querycmds.h" #include "random.h" +#include "s_auth.h" #include "s_bsd.h" #include "s_conf.h" #include "s_debug.h" #include "s_misc.h" #include "s_serv.h" /* max_client_count */ #include "send.h" -#include "sprintf_irc.h" #include "struct.h" -#include "support.h" #include "supported.h" #include "sys.h" #include "userload.h" @@ -60,42 +66,41 @@ #include "handlers.h" /* m_motd and m_lusers */ -#include +/* #include -- Now using assert in ircd_log.h */ #include #include #include #include #include - +/** Count of allocated User structures. */ static int userCount = 0; -/* - * 'make_user' add's an User information block to a client - * if it was not previously allocated. +/** Makes sure that \a cptr has a User information block. + * If cli_user(cptr) != NULL, does nothing. + * @param[in] cptr Client to attach User struct to. + * @return User struct associated with \a cptr. */ struct User *make_user(struct Client *cptr) { assert(0 != cptr); - if (!cptr->user) { - cptr->user = (struct User*) MyMalloc(sizeof(struct User)); - assert(0 != cptr->user); + if (!cli_user(cptr)) { + cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User)); + assert(0 != cli_user(cptr)); /* All variables are 0 by default */ - memset(cptr->user, 0, sizeof(struct User)); -#ifdef DEBUGMODE + memset(cli_user(cptr), 0, sizeof(struct User)); ++userCount; -#endif - cptr->user->refcnt = 1; + cli_user(cptr)->refcnt = 1; } - return cptr->user; + return cli_user(cptr); } -/* - * free_user - * - * Decrease user reference count by one and release block, if count reaches 0. +/** Dereference \a user. + * User structures are reference-counted; if the refcount of \a user + * becomes zero, free it. + * @param[in] user User to dereference. */ void free_user(struct User* user) { @@ -113,12 +118,15 @@ void free_user(struct User* user) assert(0 == user->channel); MyFree(user); -#ifdef DEBUGMODE + assert(userCount>0); --userCount; -#endif } } +/** Find number of User structs allocated and memory used by them. + * @param[out] count_out Receives number of User structs allocated. + * @param[out] bytes_out Receives number of bytes used by User structs. + */ void user_count_memory(size_t* count_out, size_t* bytes_out) { assert(0 != count_out); @@ -127,61 +135,15 @@ void user_count_memory(size_t* count_out, size_t* bytes_out) *bytes_out = userCount * sizeof(struct User); } -/* - * user_set_away - set user away state - * returns 1 if client is away or changed away message, 0 if - * client is removing away status. - * NOTE: this function may modify user and message, so they - * must be mutable. - */ -int user_set_away(struct User* user, char* message) -{ - char* away; - assert(0 != user); - - away = user->away; - - if (EmptyString(message)) { - /* - * Marking as not away - */ - if (away) { - MyFree(away); - user->away = 0; - } - } - else { - /* - * Marking as away - */ - unsigned int len = strlen(message); - - if (len > TOPICLEN) { - message[TOPICLEN] = '\0'; - len = TOPICLEN; - } - if (away) - away = (char*) MyRealloc(away, len + 1); - else - away = (char*) MyMalloc(len + 1); - assert(0 != away); - - user->away = away; - strcpy(away, message); - } - return (user->away != 0); -} -/* - * next_client - * - * Local function to find the next matching client. The search - * can be continued from the specified client entry. Normal - * usage loop is: - * +/** Find the next client (starting at \a next) with a name that matches \a ch. + * Normal usage loop is: * for (x = client; x = next_client(x,mask); x = x->next) * HandleMatchingClient; * + * @param[in] next First client to check. + * @param[in] ch Name mask to check against. + * @return Next matching client found, or NULL if none. */ struct Client *next_client(struct Client *next, const char* ch) { @@ -192,89 +154,112 @@ struct Client *next_client(struct Client *next, const char* ch) next = FindClient(ch); next = next ? next : tmp; - if (tmp->prev == next) + if (cli_prev(tmp) == next) return NULL; if (next != tmp) return next; - for (; next; next = next->next) - if (!match(ch, next->name)) + for (; next; next = cli_next(next)) + if (!match(ch, cli_name(next))) break; return next; } -/* - * hunt_server - * - * Do the basic thing in delivering the message (command) - * across the relays to the specific server (server) for - * actions. - * - * Note: The command is a format string and *MUST* be - * of prefixed style (e.g. ":%s COMMAND %s ..."). - * Command can have only max 8 parameters. - * - * server parv[server] is the parameter identifying the - * target server. +/** Find the destination server for a command, and forward it if that is not us. * - * *WARNING* - * parv[server] is replaced with the pointer to the - * real servername from the matched client (I'm lazy - * now --msa). + * \a server may be a nickname, server name, server mask (if \a from + * is a local user) or server numnick (if \a is a server or remote + * user). * - * returns: (see #defines) + * @param[in] from Client that sent the command to us. + * @param[in] cmd Long-form command text. + * @param[in] tok Token-form command text. + * @param[in] one Client that originated the command (ignored). + * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH. + * @param[in] pattern Format string of arguments to command. + * @param[in] server Index of target name or mask in \a parv. + * @param[in] parc Number of valid elements in \a parv (must be less than 9). + * @param[in] parv Array of arguments to command. + * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS. */ -int hunt_server(int MustBeOper, struct Client *cptr, struct Client *sptr, char *command, - int server, int parc, char *parv[]) +int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok, + struct Client *one, int MustBeOper, const char *pattern, + int server, int parc, char *parv[]) { struct Client *acptr; - char y[8]; + char *to; /* Assume it's me, if no server or an unregistered client */ - if (parc <= server || EmptyString(parv[server]) || IsUnknown(sptr)) + if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from)) return (HUNTED_ISME); - /* Make sure it's a server */ - if (MyUser(sptr) || Protocol(cptr) < 10) + if (MustBeOper && !IsPrivileged(from)) { + send_reply(from, ERR_NOPRIVILEGES); + return HUNTED_NOSUCH; + } + + /* Make sure it's a server */ + if (MyUser(from)) { /* Make sure it's a server */ - if (!strchr(parv[server], '*')) { - if (0 == (acptr = FindClient(parv[server]))) + if (!strchr(to, '*')) { + if (0 == (acptr = FindClient(to))) { + send_reply(from, ERR_NOSUCHSERVER, to); return HUNTED_NOSUCH; - if (acptr->user) - acptr = acptr->user->server; - } - else if (!(acptr = find_match_server(parv[server]))) - { - send_reply(sptr, ERR_NOSUCHSERVER, parv[server]); + } + + if (cli_user(acptr)) + acptr = cli_user(acptr)->server; + } else if (!(acptr = find_match_server(to))) { + send_reply(from, ERR_NOSUCHSERVER, to); return (HUNTED_NOSUCH); } - } - else if (!(acptr = FindNServer(parv[server]))) + } else if (!(acptr = FindNServer(to))) { + send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected"); return (HUNTED_NOSUCH); /* Server broke off in the meantime */ + } if (IsMe(acptr)) return (HUNTED_ISME); - if (MustBeOper && !IsPrivileged(sptr)) - { - send_reply(sptr, ERR_NOPRIVILEGES); + if (MustBeOper && !IsPrivileged(from)) { + send_reply(from, ERR_NOPRIVILEGES); return HUNTED_NOSUCH; } - strcpy(y, acptr->yxx); - parv[server] = y; + /* assert(!IsServer(from)); */ + + parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */ - assert(!IsServer(sptr)); - /* XXX sendto_one used with explicit command; must be very careful */ - sendto_one(acptr, command, NumNick(sptr), parv[1], parv[2], parv[3], parv[4], /* XXX hunt_server */ - parv[5], parv[6], parv[7], parv[8]); + sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3], + parv[4], parv[5], parv[6], parv[7], parv[8]); return (HUNTED_PASS); } -int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok, - struct Client *one, int MustBeOper, const char *pattern, - int server, int parc, char *parv[]) +/** Find the destination server for a command, and forward it (as a + * high-priority command) if that is not us. + * + * \a server may be a nickname, server name, server mask (if \a from + * is a local user) or server numnick (if \a is a server or remote + * user). + * Unlike hunt_server_cmd(), this appends the message to the + * high-priority message queue for the destination server. + * + * @param[in] from Client that sent the command to us. + * @param[in] cmd Long-form command text. + * @param[in] tok Token-form command text. + * @param[in] one Client that originated the command (ignored). + * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH. + * @param[in] pattern Format string of arguments to command. + * @param[in] server Index of target name or mask in \a parv. + * @param[in] parc Number of valid elements in \a parv (must be less than 9). + * @param[in] parv Array of arguments to command. + * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS. + */ +int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok, + struct Client *one, int MustBeOper, + const char *pattern, int server, int parc, + char *parv[]) { struct Client *acptr; char *to; @@ -287,11 +272,13 @@ int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok, if (MyUser(from)) { /* Make sure it's a server */ if (!strchr(to, '*')) { - if (0 == (acptr = FindClient(to))) + if (0 == (acptr = FindClient(to))) { + send_reply(from, ERR_NOSUCHSERVER, to); return HUNTED_NOSUCH; + } - if (acptr->user) - acptr = acptr->user->server; + if (cli_user(acptr)) + acptr = cli_user(acptr)->server; } else if (!(acptr = find_match_server(to))) { send_reply(from, ERR_NOSUCHSERVER, to); return (HUNTED_NOSUCH); @@ -307,83 +294,16 @@ int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok, return HUNTED_NOSUCH; } - assert(!IsServer(from)); + /* assert(!IsServer(from)); SETTIME to particular destinations permitted */ parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */ - sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3], - parv[4], parv[5], parv[6], parv[7], parv[8]); + sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3], + parv[4], parv[5], parv[6], parv[7], parv[8]); return (HUNTED_PASS); } -/* - * 'do_nick_name' ensures that the given parameter (nick) is really a proper - * string for a nickname (note, the 'nick' may be modified in the process...) - * - * RETURNS the length of the final NICKNAME (0, if nickname is invalid) - * - * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9' - * anything outside the above set will terminate nickname. - * In addition, the first character cannot be '-' or a Digit. - * - * Note: - * The '~'-character should be allowed, but a change should be global, - * some confusion would result if only few servers allowed it... - */ -int do_nick_name(char* nick) -{ - char* ch = nick; - char* end = ch + NICKLEN; - assert(0 != ch); - - if (*ch == '-' || IsDigit(*ch)) /* first character in [0..9-] */ - return 0; - - for ( ; (ch < end) && *ch; ++ch) - if (!IsNickChar(*ch)) - break; - - *ch = '\0'; - - return (ch - nick); -} - -/* - * clean_user_id - * - * Copy `source' to `dest', replacing all occurances of '~' and characters that - * are not `isIrcUi' by an underscore. - * Copies at most USERLEN - 1 characters or up till the first control character. - * If `tilde' is true, then a tilde is prepended to `dest'. - * Note that `dest' and `source' can point to the same area or to different - * non-overlapping areas. - */ -static char *clean_user_id(char *dest, char *source, int tilde) -{ - char ch; - char *d = dest; - char *s = source; - int rlen = USERLEN; - - ch = *s++; /* Store first character to copy: */ - if (tilde) - { - *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */ - --rlen; - } - while (ch && !IsCntrl(ch) && rlen--) - { - char nch = *s++; /* Store next character to copy */ - *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */ - if (nch == '~') - ch = '_'; - else - ch = nch; - } - *d = 0; - return dest; -} /* * register_user @@ -406,325 +326,204 @@ static char *clean_user_id(char *dest, char *source, int tilde) * this is not fair. It should actually request another * nick from local user or kill him/her... */ -int register_user(struct Client *cptr, struct Client *sptr, - const char *nick, char *username) +/** Finish registering a user who has sent both NICK and USER. + * For local connections, possibly check IAuth; make sure there is a + * matching Client config block; clean the username field; check + * K/k-lines; check for "hacked" looking usernames; assign a numnick; + * and send greeting (WELCOME, ISUPPORT, MOTD, etc). + * For all connections, update the invisible user and operator counts; + * run IPcheck against their address; and forward the NICK. + * + * @param[in] cptr Client who introduced the user. + * @param[in,out] sptr Client who has been fully introduced. + * @return Zero or CPTR_KILLED. + */ +int register_user(struct Client *cptr, struct Client *sptr) { - struct ConfItem* aconf; - char* parv[3]; + char* parv[4]; char* tmpstr; - char* tmpstr2; - char c = 0; /* not alphanum */ - char d = 'a'; /* not a digit */ - short upper = 0; - short lower = 0; - short pos = 0; - short leadcaps = 0; - short other = 0; - short digits = 0; - short badid = 0; - short digitgroups = 0; - struct User* user = sptr->user; - char ip_base64[8]; - char featurebuf[512]; - struct Gline* gline; + struct User* user = cli_user(sptr); + char ip_base64[25]; user->last = CurrentTime; - parv[0] = sptr->name; + parv[0] = cli_name(sptr); parv[1] = parv[2] = NULL; if (MyConnect(sptr)) { - static time_t last_too_many1; - static time_t last_too_many2; - assert(cptr == sptr); - switch (conf_check_client(sptr)) - { - case ACR_OK: - break; - case ACR_NO_AUTHORIZATION: - sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.", - get_client_name(sptr, HIDE_IP)); - ++ServerStats->is_ref; - return exit_client(cptr, sptr, &me, - "No Authorization - use another server"); - case ACR_TOO_MANY_IN_CLASS: - if (CurrentTime - last_too_many1 >= (time_t) 60) - { - last_too_many1 = CurrentTime; - sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in " - "class for %s.", - get_client_name(sptr, HIDE_IP)); - } - ++ServerStats->is_ref; - ip_registry_connect_fail(sptr->ip.s_addr); - return exit_client(cptr, sptr, &me, - "Sorry, your connection class is full - try " - "again later or try another server"); - case ACR_TOO_MANY_FROM_IP: - if (CurrentTime - last_too_many2 >= (time_t) 60) - { - last_too_many2 = CurrentTime; - sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from " - "same IP for %s.", - get_client_name(sptr, HIDE_IP)); - } - ++ServerStats->is_ref; - return exit_client(cptr, sptr, &me, - "Too many connections from your host"); - case ACR_ALREADY_AUTHORIZED: - /* Can this ever happen? */ - case ACR_BAD_SOCKET: - ++ServerStats->is_ref; - ip_registry_connect_fail(sptr->ip.s_addr); - return exit_client(cptr, sptr, &me, "Unknown error -- Try again"); - } - ircd_strncpy(user->host, sptr->sockhost, HOSTLEN); - aconf = sptr->confs->value.aconf; - - clean_user_id(user->username, - (sptr->flags & FLAGS_GOTID) ? sptr->username : username, - (sptr->flags & FLAGS_DOID) && !(sptr->flags & FLAGS_GOTID)); - - if ((user->username[0] == '\0') - || ((user->username[0] == '~') && (user->username[1] == '\000'))) - return exit_client(cptr, sptr, &me, "USER: Bogus userid."); - - if (!EmptyString(aconf->passwd) - && !(IsDigit(*aconf->passwd) && !aconf->passwd[1]) -#ifdef USEONE - && strcmp("ONE", aconf->passwd) -#endif - && strcmp(sptr->passwd, aconf->passwd)) - { - ServerStats->is_ref++; - ip_registry_connect_fail(sptr->ip.s_addr); - send_reply(sptr, ERR_PASSWDMISMATCH); - return exit_client(cptr, sptr, &me, "Bad Password"); - } - memset(sptr->passwd, 0, sizeof(sptr->passwd)); - /* - * following block for the benefit of time-dependent K:-lines - */ - if (find_kill(sptr)) { - ServerStats->is_ref++; - ip_registry_connect_fail(sptr->ip.s_addr); - return exit_client(cptr, sptr, &me, "K-lined"); - } + + Count_unknownbecomesclient(sptr, UserStats); + /* - * Check for mixed case usernames, meaning probably hacked. Jon2 3-94 - * Summary of rules now implemented in this patch: Ensor 11-94 - * In a mixed-case name, if first char is upper, one more upper may - * appear anywhere. (A mixed-case name *must* have an upper first - * char, and may have one other upper.) - * A third upper may appear if all 3 appear at the beginning of the - * name, separated only by "others" (-/_/.). - * A single group of digits is allowed anywhere. - * Two groups of digits are allowed if at least one of the groups is - * at the beginning or the end. - * Only one '-', '_', or '.' is allowed (or two, if not consecutive). - * But not as the first or last char. - * No other special characters are allowed. - * Name must contain at least one letter. + * Set user's initial modes */ - tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username); - while (*tmpstr && !badid) - { - pos++; - c = *tmpstr; - tmpstr++; - if (IsLower(c)) - { - lower++; - } - else if (IsUpper(c)) - { - upper++; - if ((leadcaps || pos == 1) && !lower && !digits) - leadcaps++; - } - else if (IsDigit(c)) - { - digits++; - if (pos == 1 || !IsDigit(d)) - { - digitgroups++; - if (digitgroups > 2) - badid = 1; - } - } - else if (c == '-' || c == '_' || c == '.') - { - other++; - if (pos == 1) - badid = 1; - else if (d == '-' || d == '_' || d == '.' || other > 2) - badid = 1; - } - else - badid = 1; - d = c; - } - if (!badid) - { - if (lower && upper && (!leadcaps || leadcaps > 3 || - (upper > 2 && upper > leadcaps))) - badid = 1; - else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c))) - badid = 1; - else if ((!lower && !upper) || !IsAlnum(c)) - badid = 1; + tmpstr = (char*)client_get_default_umode(sptr); + if (tmpstr) { + char *umodev[] = { NULL, NULL, NULL, NULL }; + umodev[2] = tmpstr; + set_user_mode(cptr, sptr, 3, umodev, ALLOWMODES_ANY); } - if (badid && (!(sptr->flags & FLAGS_GOTID) || - strcmp(sptr->username, username) != 0)) - { - ServerStats->is_ref++; - - send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME, - ":Your username is invalid."); - send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME, - ":Connect with your real username, in lowercase."); - send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME, - ":If your mail address were foo@bar.com, your username " - "would be foo."); - return exit_client(cptr, sptr, &me, "USER: Bad username"); - } - Count_unknownbecomesclient(sptr, UserStats); - } - else { - ircd_strncpy(user->username, username, USERLEN); - Count_newremoteclient(UserStats, user->server); - if ((gline = gline_lookup(sptr)) && GlineIsActive(gline)) - gline_resend(cptr, gline); - } - SetUser(sptr); - if (IsInvisible(sptr)) - ++UserStats.inv_clients; - if (IsOper(sptr)) - ++UserStats.opers; - - if (MyConnect(sptr)) { - sptr->handler = CLIENT_HANDLER; - release_dns_reply(sptr); - - send_reply(sptr, RPL_WELCOME, nick); + SetUser(sptr); + cli_handler(sptr) = CLIENT_HANDLER; + SetLocalNumNick(sptr); + send_reply(sptr, + RPL_WELCOME, + feature_str(FEAT_NETWORK), + feature_str(FEAT_PROVIDER) ? " via " : "", + feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "", + cli_name(sptr)); /* * This is a duplicate of the NOTICE but see below... */ - send_reply(sptr, RPL_YOURHOST, me.name, version); + send_reply(sptr, RPL_YOURHOST, cli_name(&me), version); send_reply(sptr, RPL_CREATED, creation); - send_reply(sptr, RPL_MYINFO, me.name, version); - sprintf_irc(featurebuf,FEATURES,FEATURESVALUES); - send_reply(sptr, RPL_ISUPPORT, featurebuf); + send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes, + infochanmodes, infochanmodeswithparams); + send_supported(sptr); m_lusers(sptr, sptr, 1, parv); update_load(); -#ifdef NODEFAULTMOTD - m_motd(sptr, NULL, 1, parv); -#else - m_motd(sptr, sptr, 1, parv); -#endif - nextping = CurrentTime; - if (sptr->snomask & SNO_NOISY) - set_snomask(sptr, sptr->snomask & SNO_NOISY, SNO_ADD); - ip_registry_connect_succeeded(sptr); + motd_signon(sptr); + if (cli_snomask(sptr) & SNO_NOISY) + set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD); + if (feature_bool(FEAT_CONNEXIT_NOTICES)) + sendto_opmask_butone(0, SNO_CONNEXIT, + "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>", + cli_name(sptr), user->username, user->host, + cli_sock_ip(sptr), get_client_class(sptr), + cli_info(sptr), NumNick(cptr) /* two %s's */); + + IPcheck_connect_succeeded(sptr); } - else - /* if (IsServer(cptr)) */ - { - struct Client *acptr; + else { + struct Client *acptr = user->server; - acptr = user->server; - if (acptr->from != sptr->from) + if (cli_from(acptr) != cli_from(sptr)) { sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])", - sptr, me.name, user->server->name, acptr->from->name, - acptr->from->sockhost); - sptr->flags |= FLAGS_KILLED; + sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)), + cli_sockhost(cli_from(acptr))); + SetFlag(sptr, FLAG_KILLED); return exit_client(cptr, sptr, &me, "NICK server wrong direction"); } - else - sptr->flags |= (acptr->flags & FLAGS_TS8); + else if (HasFlag(acptr, FLAG_TS8)) + SetFlag(sptr, FLAG_TS8); /* - * Check to see if this user is being propogated + * Check to see if this user is being propagated * as part of a net.burst, or is using protocol 9. - * FIXME: This can be speeded up - its stupid to check it for + * FIXME: This can be sped up - its stupid to check it for * every NICK message in a burst again --Run. */ - for (acptr = user->server; acptr != &me; acptr = acptr->serv->up) { + for (; acptr != &me; acptr = cli_serv(acptr)->up) + { if (IsBurst(acptr) || Protocol(acptr) < 10) break; } - if (!ip_registry_check_remote(sptr, (acptr != &me))) + if (!IPcheck_remote_connect(sptr, (acptr != &me))) + { /* * We ran out of bits to count this */ - return exit_client(cptr, sptr, &me, "More than 255 connections from this address"); + sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)", + sptr, cli_name(&me)); + return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled"); + } + SetUser(sptr); } + /* If they get both +x and an account during registration, hide + * their hostmask here. Calling hide_hostmask() from IAuth's + * account assignment causes a numeric reply during registration. + */ + if (HasHiddenHost(sptr)) + hide_hostmask(sptr, FLAG_HIDDENHOST); + if (IsInvisible(sptr)) + ++UserStats.inv_clients; + if (IsOper(sptr)) + ++UserStats.opers; + tmpstr = umode_str(sptr); - sendcmdto_serv_butone(user->server, CMD_NICK, cptr, *tmpstr ? - "%s %d %d %s %s +%s %s %s%s :%s" : - "%s %d %d %s %s %s%s %s%s :%s", - nick, sptr->hopcount + 1, sptr->lastnick, - user->username, user->host, tmpstr, - inttobase64(ip_base64, ntohl(sptr->ip.s_addr), 6), - NumNick(sptr), sptr->info); - - /* Send umode to client */ + /* Send full IP address to IPv6-grokking servers. */ + sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr, + FLAG_IPV6, FLAG_LAST_FLAG, + "%s %d %Tu %s %s %s%s%s%s %s%s :%s", + cli_name(sptr), cli_hopcount(sptr) + 1, + cli_lastnick(sptr), + user->username, user->realhost, + *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "", + iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1), + NumNick(sptr), cli_info(sptr)); + /* Send fake IPv6 addresses to pre-IPv6 servers. */ + sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr, + FLAG_LAST_FLAG, FLAG_IPV6, + "%s %d %Tu %s %s %s%s%s%s %s%s :%s", + cli_name(sptr), cli_hopcount(sptr) + 1, + cli_lastnick(sptr), + user->username, user->realhost, + *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "", + iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0), + NumNick(sptr), cli_info(sptr)); + + /* Send user mode to client */ if (MyUser(sptr)) { - send_umode(cptr, sptr, 0, ALL_UMODES); - if (sptr->snomask != SNO_DEFAULT && (sptr->flags & FLAGS_SERVNOTICE)) - send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask); + static struct Flags flags; /* automatically initialized to zeros */ + /* To avoid sending +r to the client due to auth-on-connect, set + * the "old" FLAG_ACCOUNT bit to match the client's value. + */ + if (IsAccount(cptr)) + FlagSet(&flags, FLAG_ACCOUNT); + else + FlagClr(&flags, FLAG_ACCOUNT); + client_set_privs(sptr, NULL); + send_umode(cptr, sptr, &flags, ALL_UMODES); + if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE)) + send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); } - return 0; } - +/** List of user mode characters. */ static const struct UserMode { - unsigned int flag; - char c; + unsigned int flag; /**< User mode constant. */ + char c; /**< Character corresponding to the mode. */ } userModeList[] = { - { FLAGS_OPER, 'o' }, - { FLAGS_LOCOP, 'O' }, - { FLAGS_INVISIBLE, 'i' }, - { FLAGS_WALLOP, 'w' }, - { FLAGS_SERVNOTICE, 's' }, - { FLAGS_DEAF, 'd' }, - { FLAGS_CHSERV, 'k' }, - { FLAGS_DEBUG, 'g' } + { FLAG_OPER, 'o' }, + { FLAG_LOCOP, 'O' }, + { FLAG_INVISIBLE, 'i' }, + { FLAG_WALLOP, 'w' }, + { FLAG_SERVNOTICE, 's' }, + { FLAG_DEAF, 'd' }, + { FLAG_CHSERV, 'k' }, + { FLAG_DEBUG, 'g' }, + { FLAG_ACCOUNT, 'r' }, + { FLAG_HIDDENHOST, 'x' } }; +/** Length of #userModeList. */ #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode) -#if 0 -static int user_modes[] = { - FLAGS_OPER, 'o', - FLAGS_LOCOP, 'O', - FLAGS_INVISIBLE, 'i', - FLAGS_WALLOP, 'w', - FLAGS_SERVNOTICE, 's', - FLAGS_DEAF, 'd', - FLAGS_CHSERV, 'k', - FLAGS_DEBUG, 'g', - 0, 0 -}; -#endif - /* * XXX - find a way to get rid of this */ +/** Nasty global buffer used for communications with umode_str() and others. */ static char umodeBuf[BUFSIZE]; +/** Try to set a user's nickname. + * If \a sptr is a server, the client is being introduced for the first time. + * @param[in] cptr Client to set nickname. + * @param[in] sptr Client sending the NICK. + * @param[in] nick New nickname. + * @param[in] parc Number of arguments to NICK. + * @param[in] parv Argument list to NICK. + * @return CPTR_KILLED if \a cptr was killed, else 0. + */ int set_nick_name(struct Client* cptr, struct Client* sptr, const char* nick, int parc, char* parv[]) { if (IsServer(sptr)) { - int i; - const char* p; /* * A server introducing a new client, change source @@ -732,40 +531,41 @@ int set_nick_name(struct Client* cptr, struct Client* sptr, struct Client* new_client = make_client(cptr, STAT_UNKNOWN); assert(0 != new_client); - new_client->hopcount = atoi(parv[2]); - new_client->lastnick = atoi(parv[3]); - if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') { - for (p = parv[6] + 1; *p; p++) { - for (i = 0; i < USERMODELIST_SIZE; ++i) { - if (userModeList[i].c == *p) { - new_client->flags |= userModeList[i].flag; - break; - } - } - } - } + cli_hopcount(new_client) = atoi(parv[2]); + cli_lastnick(new_client) = atoi(parv[3]); + /* * Set new nick name. */ - strcpy(new_client->name, nick); - new_client->user = make_user(new_client); - new_client->user->server = sptr; + strcpy(cli_name(new_client), nick); + cli_user(new_client) = make_user(new_client); + cli_user(new_client)->server = sptr; SetRemoteNumNick(new_client, parv[parc - 2]); /* * IP# of remote client */ - new_client->ip.s_addr = htonl(base64toint(parv[parc - 3])); + base64toip(parv[parc - 3], &cli_ip(new_client)); add_client_to_list(new_client); hAddClient(new_client); - sptr->serv->ghost = 0; /* :server NICK means end of net.burst */ - ircd_strncpy(new_client->username, parv[4], USERLEN); - ircd_strncpy(new_client->user->host, parv[5], HOSTLEN); - ircd_strncpy(new_client->info, parv[parc - 1], REALLEN); - return register_user(cptr, new_client, new_client->name, parv[4]); + cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */ + ircd_strncpy(cli_username(new_client), parv[4], USERLEN); + ircd_strncpy(cli_user(new_client)->username, parv[4], USERLEN); + ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN); + ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN); + ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN); + + Count_newremoteclient(UserStats, sptr); + + if (parc > 7 && *parv[6] == '+') { + /* (parc-4) -3 for the ip, numeric nick, realname */ + set_user_mode(cptr, new_client, parc-7, parv+4, ALLOWMODES_ANY); + } + + return register_user(cptr, new_client); } - else if (sptr->name[0]) { + else if ((cli_name(sptr))[0]) { /* * Client changing its nick * @@ -775,39 +575,45 @@ int set_nick_name(struct Client* cptr, struct Client* sptr, */ if (MyUser(sptr)) { const char* channel_name; + struct Membership *member; if ((channel_name = find_no_nickchange_channel(sptr))) { - return send_reply(cptr, ERR_BANNICKCHANGE, channel_name); + return send_reply(cptr, ERR_BANNICKCHANGE, channel_name); } /* * Refuse nick change if the last nick change was less * then 30 seconds ago. This is intended to get rid of * clone bots doing NICK FLOOD. -SeKs * If someone didn't change their nick for more then 60 seconds - * however, allow to do two nick changes immedately after another + * however, allow to do two nick changes immediately after another * before limiting the nick flood. -Run */ - if (CurrentTime < cptr->nextnick) { - cptr->nextnick += 2; - send_reply(cptr, ERR_NICKTOOFAST, parv[1], - cptr->nextnick - CurrentTime); + if (CurrentTime < cli_nextnick(cptr)) + { + cli_nextnick(cptr) += 2; + send_reply(cptr, ERR_NICKTOOFAST, parv[1], + cli_nextnick(cptr) - CurrentTime); /* Send error message */ - sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cptr->name); + sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr)); /* bounce NICK to user */ return 0; /* ignore nick change! */ } else { /* Limit total to 1 change per NICK_DELAY seconds: */ - cptr->nextnick += NICK_DELAY; + cli_nextnick(cptr) += NICK_DELAY; /* However allow _maximal_ 1 extra consecutive nick change: */ - if (cptr->nextnick < CurrentTime) - cptr->nextnick = CurrentTime; + if (cli_nextnick(cptr) < CurrentTime) + cli_nextnick(cptr) = CurrentTime; } + /* Invalidate all bans against the user so we check them again */ + for (member = (cli_user(cptr))->channel; member; + member = member->next_channel) + ClearBanValid(member); } /* * Also set 'lastnick' to current time, if changed. */ if (0 != ircd_strcmp(parv[0], nick)) - sptr->lastnick = (sptr == cptr) ? TStime() : atoi(parv[2]); + cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]); /* * Client just changing his/her nick. If he/she is @@ -815,75 +621,54 @@ int set_nick_name(struct Client* cptr, struct Client* sptr, * on that channel. Propagate notice to other servers. */ if (IsUser(sptr)) { - sendcmdto_common_channels(sptr, CMD_NICK, ":%s", nick); + sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick); add_history(sptr, 1); sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick, - sptr->lastnick); + cli_lastnick(sptr)); } else sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick); - if (sptr->name[0]) + if ((cli_name(sptr))[0]) hRemClient(sptr); - strcpy(sptr->name, nick); + strcpy(cli_name(sptr), nick); hAddClient(sptr); } else { /* Local client setting NICK the first time */ - - strcpy(sptr->name, nick); - if (!sptr->user) { - sptr->user = make_user(sptr); - sptr->user->server = &me; - } - SetLocalNumNick(sptr); + strcpy(cli_name(sptr), nick); hAddClient(sptr); - - /* - * If the client hasn't gotten a cookie-ping yet, - * choose a cookie and send it. -record!jegelhof@cloud9.net - */ - if (!sptr->cookie) { - do { - sptr->cookie = (ircrandom() & 0x7fffffff); - } while (!sptr->cookie); - sendrawto_one(cptr, MSG_PING " :%u", sptr->cookie); - } - else if (*sptr->user->host && sptr->cookie == COOKIE_VERIFIED) { - /* - * USER and PONG already received, now we have NICK. - * register_user may reject the client and call exit_client - * for it - must test this and exit m_nick too ! - */ - sptr->lastnick = TStime(); /* Always local client */ - if (register_user(cptr, sptr, nick, sptr->user->username) == CPTR_KILLED) - return CPTR_KILLED; - } + return auth_set_nick(cli_auth(sptr), nick); } return 0; } +/** Calculate the hash value for a target. + * @param[in] target Pointer to target, cast to unsigned int. + * @return Hash value constructed from the pointer. + */ static unsigned char hash_target(unsigned int target) { return (unsigned char) (target >> 16) ^ (target >> 8); } -/* - * add_target - * - * sptr must be a local client! - * - * Cannonifies target for client `sptr'. +/** Records \a target as a recent target for \a sptr. + * @param[in] sptr User who has sent to a new target. + * @param[in] target Target to add. */ -void add_target(struct Client *sptr, void *target) +void +add_target(struct Client *sptr, void *target) { - unsigned char hash = hash_target((unsigned int) target); + /* Ok, this shouldn't work esp on alpha + */ + unsigned char hash = hash_target((unsigned long) target); unsigned char* targets; int i; assert(0 != sptr); - assert(sptr->local); + assert(cli_local(sptr)); + + targets = cli_targets(sptr); - targets = sptr->targets; /* * Already in table? */ @@ -899,24 +684,23 @@ void add_target(struct Client *sptr, void *target) targets[RESERVEDTARGETS] = hash; } -/* - * check_target_limit - * - * sptr must be a local client ! - * - * Returns 'true' (1) when too many targets are addressed. - * Returns 'false' (0) when it's ok to send to this target. +/** Check whether \a sptr can send to or join \a target yet. + * @param[in] sptr User trying to join a channel or send a message. + * @param[in] target Target of the join or message. + * @param[in] name Name of the target. + * @param[in] created If non-zero, trying to join a new channel. + * @return Non-zero if too many target changes; zero if okay to send. */ int check_target_limit(struct Client *sptr, void *target, const char *name, int created) { - unsigned char hash = hash_target((unsigned int) target); + unsigned char hash = hash_target((unsigned long) target); int i; unsigned char* targets; assert(0 != sptr); - assert(sptr->local); - targets = sptr->targets; + assert(cli_local(sptr)); + targets = cli_targets(sptr); /* * Same target as last time? @@ -934,26 +718,25 @@ int check_target_limit(struct Client *sptr, void *target, const char *name, * New target */ if (!created) { - if (CurrentTime < sptr->nexttarget) { - if (sptr->nexttarget - CurrentTime < TARGET_DELAY + 8) { + if (CurrentTime < cli_nexttarget(sptr)) { + /* If user is invited to channel, give him/her a free target */ + if (IsChannelName(name) && IsInvited(sptr, target)) + return 0; + + if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) { /* * No server flooding */ - sptr->nexttarget += 2; - send_reply(sptr, ERR_TARGETTOOFAST, name, - sptr->nexttarget - CurrentTime); + cli_nexttarget(sptr) += 2; + send_reply(sptr, ERR_TARGETTOOFAST, name, + cli_nexttarget(sptr) - CurrentTime); } return 1; } else { -#ifdef GODMODE - /* XXX Let's get rid of GODMODE */ - sendto_one(sptr, ":%s NOTICE %s :New target: %s; ft " TIME_T_FMT, /* XXX Possibly DEAD */ - me.name, sptr->name, name, (CurrentTime - sptr->nexttarget) / TARGET_DELAY); -#endif - sptr->nexttarget += TARGET_DELAY; - if (sptr->nexttarget < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1))) - sptr->nexttarget = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)); + cli_nexttarget(sptr) += TARGET_DELAY; + if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1))) + cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)); } } memmove(&targets[1], &targets[0], MAXTARGETS - 1); @@ -961,23 +744,15 @@ int check_target_limit(struct Client *sptr, void *target, const char *name, return 0; } -/* - * whisper - called from m_cnotice and m_cprivmsg. - * - * parv[0] = sender prefix - * parv[1] = nick - * parv[2] = #channel - * parv[3] = Private message text - * - * Added 971023 by Run. - * Reason: Allows channel operators to sent an arbitrary number of private - * messages to users on their channel, avoiding the max.targets limit. - * Building this into m_private would use too much cpu because we'd have - * to a cross channel lookup for every private message! - * Note that we can't allow non-chan ops to use this command, it would be - * abused by mass advertisers. - * +/** Allows a channel operator to avoid target change checks when + * sending messages to users on their channel. + * @param[in] source User sending the message. + * @param[in] nick Destination of the message. + * @param[in] channel Name of channel being sent to. + * @param[in] text Message to send. + * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG. */ +/* Added 971023 by Run. */ int whisper(struct Client* source, const char* nick, const char* channel, const char* text, int is_notice) { @@ -1001,7 +776,7 @@ int whisper(struct Client* source, const char* nick, const char* channel, * since the link is the same, this should be a little faster for channels * with a lot of users */ - for (membership = source->user->channel; membership; membership = membership->next_channel) { + for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) { if (chptr == membership->channel) break; } @@ -1014,138 +789,195 @@ int whisper(struct Client* source, const char* nick, const char* channel, /* * lookup channel in destination */ - assert(0 != dest->user); - for (membership = dest->user->channel; membership; membership = membership->next_channel) { + assert(0 != cli_user(dest)); + for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) { if (chptr == membership->channel) break; } if (0 == membership || IsZombie(membership)) { - return send_reply(source, ERR_USERNOTINCHANNEL, dest->name, chptr->chname); + return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname); } if (is_silenced(source, dest)) return 0; - if (dest->user->away) - send_reply(source, RPL_AWAY, dest->name, dest->user->away); if (is_notice) sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text); else + { + if (cli_user(dest)->away) + send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away); sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text); + } return 0; } -/* - * added Sat Jul 25 07:30:42 EST 1992 +/** Send a user mode change for \a cptr to neighboring servers. + * @param[in] cptr User whose mode is changing. + * @param[in] sptr Client who sent us the mode change message. + * @param[in] old Prior set of user flags. + * @param[in] prop If non-zero, also include FLAG_OPER. */ -void send_umode_out(struct Client *cptr, struct Client *sptr, int old) +void send_umode_out(struct Client *cptr, struct Client *sptr, + struct Flags *old, int prop) { int i; struct Client *acptr; - send_umode(NULL, sptr, old, SEND_UMODES); + send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER); - for (i = HighestFd; i >= 0; i--) { + for (i = HighestFd; i >= 0; i--) + { if ((acptr = LocalClientArray[i]) && IsServer(acptr) && (acptr != cptr) && (acptr != sptr) && *umodeBuf) - sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", sptr->name, umodeBuf); + sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf); } if (cptr && MyUser(cptr)) send_umode(cptr, sptr, old, ALL_UMODES); } -/* - * send_user_info - send user info userip/userhost - * NOTE: formatter must put info into buffer and return a pointer to the end of - * the data it put in the buffer. +/** Call \a fmt for each Client named in \a names. + * @param[in] sptr Client requesting information. + * @param[in] names Space-delimited list of nicknames. + * @param[in] rpl Base reply string for messages. + * @param[in] fmt Formatting callback function. */ void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt) { - char* sbuf; char* name; char* p = 0; int arg_count = 0; int users_found = 0; struct Client* acptr; - char buf[BUFSIZE * 2]; + struct MsgBuf* mb; assert(0 != sptr); assert(0 != names); assert(0 != fmt); - sbuf = sprintf_irc(buf, rpl_str(rpl), me.name, sptr->name); + mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr)); for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) { if ((acptr = FindUser(name))) { if (users_found++) - *sbuf++ = ' '; - sbuf = (*fmt)(acptr, sbuf); + msgq_append(0, mb, " "); + (*fmt)(acptr, sptr, mb); } if (5 == ++arg_count) break; } - if (users_found) - send_buffer(sptr, buf); + send_buffer(sptr, mb, 0); + msgq_clean(mb); } +/** Set \a flag on \a cptr and possibly hide the client's hostmask. + * @param[in,out] cptr User who is getting a new flag. + * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT). + * @return Zero. + */ +int +hide_hostmask(struct Client *cptr, unsigned int flag) +{ + struct Membership *chan; -/* - * set_user_mode() added 15/10/91 By Darren Reed. + switch (flag) { + case FLAG_HIDDENHOST: + /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */ + if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING)) + return 0; + break; + case FLAG_ACCOUNT: + /* Invalidate all bans against the user so we check them again */ + for (chan = (cli_user(cptr))->channel; chan; + chan = chan->next_channel) + ClearBanValid(chan); + break; + default: + return 0; + } + + SetFlag(cptr, flag); + if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT)) + return 0; + + sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered"); + ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s", + cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST)); + + /* ok, the client is now fully hidden, so let them know -- hikari */ + if (MyConnect(cptr)) + send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host); + + /* + * Go through all channels the client was on, rejoin him + * and set the modes, if any + */ + for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) + { + if (IsZombie(chan)) + continue; + /* Send a JOIN unless the user's join has been delayed. */ + if (!IsDelayedJoin(chan)) + sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0, + "%H", chan->channel); + if (IsChanOp(chan) && HasVoice(chan)) + sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0, + "%H +ov %C %C", chan->channel, cptr, + cptr); + else if (IsChanOp(chan) || HasVoice(chan)) + sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0, + "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr); + } + return 0; +} + +/** Set a user's mode. This function checks that \a cptr is trying to + * set his own mode, prevents local users from setting inappropriate + * modes through this function, and applies any other side effects of + * a successful mode change. * - * parv[0] - sender - * parv[1] - username to change mode for - * parv[2] - modes to change + * @param[in,out] cptr User setting someone's mode. + * @param[in] sptr Client who sent the mode change message. + * @param[in] parc Number of parameters in \a parv. + * @param[in] parv Parameters to MODE. + * @param[in] allow_modes ALLOWMODES_ANY for any mode, ALLOWMODES_DEFAULT for + * only permitting legitimate default user modes. + * @return Zero. */ -int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[]) +int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, + char *parv[], int allow_modes) { char** p; char* m; - struct Client *acptr; int what; int i; - int setflags; + struct Flags setflags; unsigned int tmpmask = 0; int snomask_given = 0; char buf[BUFSIZE]; + int prop = 0; + int do_host_hiding = 0; + char* account = NULL; what = MODE_ADD; - if (parc < 2) - return need_more_params(sptr, "MODE"); - - if (!(acptr = FindUser(parv[1]))) - { - if (MyConnect(sptr)) - send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]); - return 0; - } - - if (IsServer(sptr) || sptr != acptr) - { - if (IsServer(cptr)) - sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP, - ":MODE for User %s from %s!%s", parv[1], - cptr->name, sptr->name); - else - send_reply(sptr, ERR_USERSDONTMATCH); - return 0; - } - if (parc < 3) { m = buf; *m++ = '+'; - for (i = 0; i < USERMODELIST_SIZE; ++i) { - if ( (userModeList[i].flag & sptr->flags)) + for (i = 0; i < USERMODELIST_SIZE; i++) + { + if (HasFlag(sptr, userModeList[i].flag) && + userModeList[i].flag != FLAG_ACCOUNT) *m++ = userModeList[i].c; } *m = '\0'; send_reply(sptr, RPL_UMODEIS, buf); - if ((sptr->flags & FLAGS_SERVNOTICE) && MyConnect(sptr) - && sptr->snomask != + if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr) + && cli_snomask(sptr) != (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT)) - send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask); + send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); return 0; } @@ -1153,21 +985,15 @@ int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv * find flags already set for user * why not just copy them? */ - setflags = sptr->flags; -#if 0 - setflags = 0; - for (i = 0; i < USERMODELIST_SIZE; ++i) { - if (sptr->flags & userModeList[i].flag) - setflags |= userModeList[i].flag; - } -#endif + setflags = cli_flags(sptr); + if (MyConnect(sptr)) - tmpmask = sptr->snomask; + tmpmask = cli_snomask(sptr); /* * parse mode change string(s) */ - for (p = &parv[2]; *p; p++) { /* p is changed in loop too */ + for (p = &parv[2]; *p && p<&parv[parc]; p++) { /* p is changed in loop too */ for (m = *p; *m; m++) { switch (*m) { case '+': @@ -1186,9 +1012,9 @@ int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv tmpmask = (what == MODE_ADD) ? (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0; if (tmpmask) - sptr->flags |= FLAGS_SERVNOTICE; + SetServNotice(sptr); else - sptr->flags &= ~FLAGS_SERVNOTICE; + ClearServNotice(sptr); break; case 'w': if (what == MODE_ADD) @@ -1200,21 +1026,26 @@ int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv if (what == MODE_ADD) SetOper(sptr); else { - sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP); - if (MyConnect(sptr)) { - tmpmask = sptr->snomask & ~SNO_OPER; - sptr->handler = CLIENT_HANDLER; + ClrFlag(sptr, FLAG_OPER); + ClrFlag(sptr, FLAG_LOCOP); + if (MyConnect(sptr)) + { + tmpmask = cli_snomask(sptr) & ~SNO_OPER; + cli_handler(sptr) = CLIENT_HANDLER; } } break; case 'O': if (what == MODE_ADD) SetLocOp(sptr); - else { - sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP); - if (MyConnect(sptr)) { - tmpmask = sptr->snomask & ~SNO_OPER; - sptr->handler = CLIENT_HANDLER; + else + { + ClrFlag(sptr, FLAG_OPER); + ClrFlag(sptr, FLAG_LOCOP); + if (MyConnect(sptr)) + { + tmpmask = cli_snomask(sptr) & ~SNO_OPER; + cli_handler(sptr) = CLIENT_HANDLER; } } break; @@ -1242,7 +1073,19 @@ int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv else ClearDebug(sptr); break; + case 'x': + if (what == MODE_ADD) + do_host_hiding = 1; + break; + case 'r': + if (*(p + 1) && (what == MODE_ADD)) { + account = *(++p); + SetAccount(sptr); + } + /* There is no -r */ + break; default: + send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m); break; } } @@ -1251,77 +1094,162 @@ int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv * Evaluate rules for new user mode * Stop users making themselves operators too easily: */ - if (!(setflags & FLAGS_OPER) && IsOper(sptr) && !IsServer(cptr)) - ClearOper(sptr); - if (!(setflags & FLAGS_LOCOP) && IsLocOp(sptr) && !IsServer(cptr)) - ClearLocOp(sptr); -#ifdef WALLOPS_OPER_ONLY - /* - * only send wallops to opers - */ - if (!IsAnOper(sptr) && !(setflags & FLAGS_WALLOP) && !IsServer(cptr)) - ClearWallops(sptr); -#endif - if ((setflags & (FLAGS_OPER | FLAGS_LOCOP)) && !IsAnOper(sptr) && - MyConnect(sptr)) - det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPS); - /* - * new umode; servers can set it, local users cannot; - * prevents users from /kick'ing or /mode -o'ing - */ - if (!(setflags & FLAGS_CHSERV) && !IsServer(cptr)) - ClearChannelService(sptr); + if (!IsServer(cptr)) + { + if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) + ClearOper(sptr); + if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr)) + ClearLocOp(sptr); + if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr)) + ClrFlag(sptr, FLAG_ACCOUNT); + /* + * new umode; servers can set it, local users cannot; + * prevents users from /kick'ing or /mode -o'ing + */ + if (!FlagHas(&setflags, FLAG_CHSERV)) + ClearChannelService(sptr); + /* + * only send wallops to opers + */ + if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) && + !FlagHas(&setflags, FLAG_WALLOP)) + ClearWallops(sptr); + if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) && + !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE)) + { + ClearServNotice(sptr); + set_snomask(sptr, 0, SNO_SET); + } + if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) && + !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG)) + ClearDebug(sptr); + } + if (MyConnect(sptr)) + { + if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) && + !IsAnOper(sptr)) + { + det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR); + client_set_privs(sptr, NULL); + } + + if (SendServNotice(sptr)) + { + if (tmpmask != cli_snomask(sptr)) + set_snomask(sptr, tmpmask, SNO_SET); + if (cli_snomask(sptr) && snomask_given) + send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); + } + else + set_snomask(sptr, 0, SNO_SET); + } /* * Compare new flags with old flags and send string which * will cause servers to update correctly. */ - if ((setflags & FLAGS_OPER) && !IsOper(sptr)) - --UserStats.opers; - if (!(setflags & FLAGS_OPER) && IsOper(sptr)) - ++UserStats.opers; - if ((setflags & FLAGS_INVISIBLE) && !IsInvisible(sptr)) - --UserStats.inv_clients; - if (!(setflags & FLAGS_INVISIBLE) && IsInvisible(sptr)) - ++UserStats.inv_clients; - send_umode_out(cptr, sptr, setflags); - - if (MyConnect(sptr)) { - if (tmpmask != sptr->snomask) - set_snomask(sptr, tmpmask, SNO_SET); - if (sptr->snomask && snomask_given) - send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask); + if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr)) { + int len = ACCOUNTLEN; + char *ts; + if ((ts = strchr(account, ':'))) { + len = (ts++) - account; + cli_user(sptr)->acc_create = atoi(ts); + Debug((DEBUG_DEBUG, "Received timestamped account in user mode; " + "account \"%s\", timestamp %Tu", account, + cli_user(sptr)->acc_create)); + } + ircd_strncpy(cli_user(sptr)->account, account, len); + } + if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding && allow_modes != ALLOWMODES_DEFAULT) + hide_hostmask(sptr, FLAG_HIDDENHOST); + + if (IsRegistered(sptr)) { + if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) { + /* user now oper */ + ++UserStats.opers; + client_set_privs(sptr, NULL); /* may set propagate privilege */ + } + /* remember propagate privilege setting */ + if (HasPriv(sptr, PRIV_PROPAGATE)) { + prop = 1; + } + if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) { + /* user no longer oper */ + assert(UserStats.opers > 0); + --UserStats.opers; + client_set_privs(sptr, NULL); /* will clear propagate privilege */ + } + if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr)) { + assert(UserStats.inv_clients > 0); + --UserStats.inv_clients; + } + if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr)) { + ++UserStats.inv_clients; + } + assert(UserStats.opers <= UserStats.clients + UserStats.unknowns); + assert(UserStats.inv_clients <= UserStats.clients + UserStats.unknowns); + send_umode_out(cptr, sptr, &setflags, prop); } return 0; } -/* - * Build umode string for BURST command - * --Run +/** Build a mode string to describe modes for \a cptr. + * @param[in] cptr Some user. + * @return Pointer to a static buffer. */ char *umode_str(struct Client *cptr) { - char* m = umodeBuf; /* Maximum string size: "owidg\0" */ - int i; - int c_flags; + /* Maximum string size: "owidgrx\0" */ + char *m = umodeBuf; + int i; + struct Flags c_flags = cli_flags(cptr); - c_flags = cptr->flags & SEND_UMODES; /* cleaning up the original code */ + if (!HasPriv(cptr, PRIV_PROPAGATE)) + FlagClr(&c_flags, FLAG_OPER); - for (i = 0; i < USERMODELIST_SIZE; ++i) { - if ( (c_flags & userModeList[i].flag)) + for (i = 0; i < USERMODELIST_SIZE; ++i) + { + if (FlagHas(&c_flags, userModeList[i].flag) && + userModeList[i].flag >= FLAG_GLOBAL_UMODES) *m++ = userModeList[i].c; } + + if (IsAccount(cptr)) + { + char* t = cli_user(cptr)->account; + + *m++ = ' '; + while ((*m++ = *t++)) + ; /* Empty loop */ + + if (cli_user(cptr)->acc_create) { + char nbuf[20]; + Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for " + "account \"%s\"; timestamp %Tu", cli_user(cptr)->account, + cli_user(cptr)->acc_create)); + ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu", + cli_user(cptr)->acc_create); + m--; /* back up over previous nul-termination */ + while ((*m++ = *t++)) + ; /* Empty loop */ + } + } + *m = '\0'; return umodeBuf; /* Note: static buffer, gets overwritten by send_umode() */ } -/* - * Send the MODE string for user (user) to connection cptr - * -avalon +/** Send a mode change string for \a sptr to \a cptr. + * @param[in] cptr Destination of mode change message. + * @param[in] sptr User whose mode has changed. + * @param[in] old Pre-change set of modes for \a sptr. + * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER, + * SEND_UMODES, to select which changed user modes to send. */ -void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask) +void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old, + int sendset) { int i; int flag; @@ -1330,15 +1258,31 @@ void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask) /* * Build a string in umodeBuf to represent the change in the user's - * mode between the new (sptr->flag) and 'old'. + * mode between the new (cli_flags(sptr)) and 'old', but skipping + * the modes indicated by sendset. */ m = umodeBuf; *m = '\0'; - for (i = 0; i < USERMODELIST_SIZE; ++i) { + for (i = 0; i < USERMODELIST_SIZE; ++i) + { flag = userModeList[i].flag; - if (MyUser(sptr) && !(flag & sendmask)) + if (FlagHas(old, flag) + == HasFlag(sptr, flag)) continue; - if ( (flag & old) && !(sptr->flags & flag)) + switch (sendset) + { + case ALL_UMODES: + break; + case SEND_UMODES_BUT_OPER: + if (flag == FLAG_OPER) + continue; + /* and fall through */ + case SEND_UMODES: + if (flag < FLAG_GLOBAL_UMODES) + continue; + break; + } + if (FlagHas(old, flag)) { if (what == MODE_DEL) *m++ = userModeList[i].c; @@ -1349,7 +1293,7 @@ void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask) *m++ = userModeList[i].c; } } - else if (!(flag & old) && (sptr->flags & flag)) + else /* !FlagHas(old, flag) */ { if (what == MODE_ADD) *m++ = userModeList[i].c; @@ -1363,13 +1307,15 @@ void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask) } *m = '\0'; if (*umodeBuf && cptr) - sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", sptr->name, umodeBuf); + sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf); } -/* +/** * Check to see if this resembles a sno_mask. It is if 1) there is * at least one digit and 2) The first digit occurs before the first * alphabetic character. + * @param[in] word Word to check for sno_mask-ness. + * @return Non-zero if \a word looks like a server notice mask; zero if not. */ int is_snomask(char *word) { @@ -1384,9 +1330,11 @@ int is_snomask(char *word) return 0; } -/* - * If it begins with a +, count this as an additive mask instead of just - * a replacement. If what == MODE_DEL, "+" has no special effect. +/** Update snomask \a oldmask according to \a arg and \a what. + * @param[in] oldmask Original user mask. + * @param[in] arg Update string (either a number or '+'/'-' followed by a number). + * @param[in] what MODE_ADD if adding the mask. + * @return New value of service notice mask. */ unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what) { @@ -1419,6 +1367,10 @@ unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what) return newmask; } +/** Remove \a cptr from the singly linked list \a list. + * @param[in] cptr Client to remove from list. + * @param[in,out] list Pointer to head of list containing \a cptr. + */ static void delfrom_list(struct Client *cptr, struct SLink **list) { struct SLink* tmp; @@ -1437,10 +1389,10 @@ static void delfrom_list(struct Client *cptr, struct SLink **list) } } -/* - * This function sets a Client's server notices mask, according to - * the parameter 'what'. This could be even faster, but the code - * gets mighty hard to read :) +/** Set \a cptr's server notice mask, according to \a what. + * @param[in,out] cptr Client whose snomask is updating. + * @param[in] newmask Base value for new snomask. + * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation. */ void set_snomask(struct Client *cptr, unsigned int newmask, int what) { @@ -1448,7 +1400,7 @@ void set_snomask(struct Client *cptr, unsigned int newmask, int what) int i; struct SLink *tmp; - oldmask = cptr->snomask; + oldmask = cli_snomask(cptr); if (what == SNO_ADD) newmask |= oldmask; @@ -1476,113 +1428,75 @@ void set_snomask(struct Client *cptr, unsigned int newmask, int what) delfrom_list(cptr, &opsarray[i]); } } - cptr->snomask = newmask; + cli_snomask(cptr) = newmask; } -/* - * is_silenced : Does the actual check wether sptr is allowed - * to send a message to acptr. - * Both must be registered persons. - * If sptr is silenced by acptr, his message should not be propagated, - * but more over, if this is detected on a server not local to sptr - * the SILENCE mask is sent upstream. +/** Check whether \a sptr is allowed to send a message to \a acptr. + * If \a sptr is a remote user, it means some server has an outdated + * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back + * in the direction of \a sptr. Skip the check if \a sptr is a server. + * @param[in] sptr Client trying to send a message. + * @param[in] acptr Destination of message. + * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not. */ int is_silenced(struct Client *sptr, struct Client *acptr) { - struct SLink *lp; + struct Ban *found; struct User *user; - static char sender[HOSTLEN + NICKLEN + USERLEN + 5]; - static char senderip[16 + NICKLEN + USERLEN + 5]; + size_t buf_used, slen; + char buf[BUFSIZE]; - if (!(acptr->user) || !(lp = acptr->user->silence) || !(user = sptr->user)) + if (IsServer(sptr) || !(user = cli_user(acptr)) + || !(found = find_ban(sptr, user->silence))) return 0; - sprintf_irc(sender, "%s!%s@%s", sptr->name, user->username, user->host); - sprintf_irc(senderip, "%s!%s@%s", sptr->name, user->username, - ircd_ntoa((const char*) &sptr->ip)); - for (; lp; lp = lp->next) - { - if ((!(lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, sender)) || - ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip))) - { - if (!MyConnect(sptr)) - { - sendcmdto_one(acptr, CMD_SILENCE, sptr->from, "%C %s", sptr, - lp->value.cp); + assert(!(found->flags & BAN_EXCEPTION)); + if (!MyConnect(sptr)) { + /* Buffer positive silence to send back. */ + buf_used = strlen(found->banstr); + memcpy(buf, found->banstr, buf_used); + /* Add exceptions to buffer. */ + for (found = user->silence; found; found = found->next) { + if (!(found->flags & BAN_EXCEPTION)) + continue; + slen = strlen(found->banstr); + if (buf_used + slen + 4 > 400) { + buf[buf_used] = '\0'; + sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf); + buf_used = 0; } - return 1; + if (buf_used) + buf[buf_used++] = ','; + buf[buf_used++] = '+'; + buf[buf_used++] = '~'; + memcpy(buf + buf_used, found->banstr, slen); + buf_used += slen; + } + /* Flush silence buffer. */ + if (buf_used) { + buf[buf_used] = '\0'; + sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf); + buf_used = 0; } } - return 0; + return 1; } -/* - * del_silence - * - * Removes all silence masks from the list of sptr that fall within `mask' - * Returns -1 if none where found, 0 otherwise. +/** Send RPL_ISUPPORT lines to \a cptr. + * @param[in] cptr Client to send ISUPPORT to. + * @return Zero. */ -int del_silence(struct Client *sptr, char *mask) +int +send_supported(struct Client *cptr) { - struct SLink **lp; - struct SLink *tmp; - int ret = -1; + char featurebuf[512]; - for (lp = &sptr->user->silence; *lp;) { - if (!mmatch(mask, (*lp)->value.cp)) - { - tmp = *lp; - *lp = tmp->next; - MyFree(tmp->value.cp); - free_link(tmp); - ret = 0; - } - else - lp = &(*lp)->next; - } - return ret; -} - -int add_silence(struct Client* sptr, const char* mask) -{ - struct SLink *lp, **lpp; - int cnt = 0, len = strlen(mask); - char *ip_start; + ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1); + send_reply(cptr, RPL_ISUPPORT, featurebuf); + ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2); + send_reply(cptr, RPL_ISUPPORT, featurebuf); - for (lpp = &sptr->user->silence, lp = *lpp; lp;) - { - if (0 == ircd_strcmp(mask, lp->value.cp)) - return -1; - if (!mmatch(mask, lp->value.cp)) - { - struct SLink *tmp = lp; - *lpp = lp = lp->next; - MyFree(tmp->value.cp); - free_link(tmp); - continue; - } - if (MyUser(sptr)) - { - len += strlen(lp->value.cp); - if ((len > MAXSILELENGTH) || (++cnt >= MAXSILES)) - { - send_reply(sptr, ERR_SILELISTFULL, mask); - return -1; - } - else if (!mmatch(lp->value.cp, mask)) - return -1; - } - lpp = &lp->next; - lp = *lpp; - } - lp = make_link(); - memset(lp, 0, sizeof(struct SLink)); - lp->next = sptr->user->silence; - lp->value.cp = (char*) MyMalloc(strlen(mask) + 1); - assert(0 != lp->value.cp); - strcpy(lp->value.cp, mask); - if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1)) - lp->flags = CHFL_SILENCE_IPMASK; - sptr->user->silence = lp; - return 0; + return 0; /* convenience return, if it's ever needed */ } +/* vim: shiftwidth=2 + */