Rewrite DNS lookup API to remove a memory leak and keep it from coming back.
[ircu2.10.12-pk.git] / ircd / s_user.c
index 3e7fd6b8d2b261aa57797e7c9e2ce6b1d9fb5fe8..aac289f98867623214decfa73e2e806a452cf46c 100644 (file)
@@ -91,9 +91,7 @@ struct User *make_user(struct Client *cptr)
 
     /* All variables are 0 by default */
     memset(cli_user(cptr), 0, sizeof(struct User));
-#ifdef  DEBUGMODE
     ++userCount;
-#endif
     cli_user(cptr)->refcnt = 1;
   }
   return cli_user(cptr);
@@ -120,9 +118,7 @@ void free_user(struct User* user)
     assert(0 == user->channel);
 
     MyFree(user);
-#ifdef  DEBUGMODE
     --userCount;
-#endif
   }
 }
 
@@ -408,6 +404,7 @@ int register_user(struct Client *cptr, struct Client *sptr,
     static time_t last_too_many2;
 
     assert(cptr == sptr);
+    assert(cli_unreg(sptr) == 0);
     if (!IsIAuthed(sptr)) {
       if (iauth_active)
         return iauth_start_client(iauth_active, sptr);
@@ -429,11 +426,11 @@ int register_user(struct Client *cptr, struct Client *sptr,
         {
           last_too_many1 = CurrentTime;
           sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
-                               "class %i for %s.", get_client_class(sptr),
+                               "class %s for %s.", get_client_class(sptr),
                                get_client_name(sptr, SHOW_IP));
         }
         ++ServerStats->is_ref;
-        IPcheck_connect_fail(&cli_ip(sptr));
+        IPcheck_connect_fail(sptr);
         return exit_client(cptr, sptr, &me,
                            "Sorry, your connection class is full - try "
                            "again later or try another server");
@@ -452,7 +449,7 @@ int register_user(struct Client *cptr, struct Client *sptr,
         /* Can this ever happen? */
       case ACR_BAD_SOCKET:
         ++ServerStats->is_ref;
-        IPcheck_connect_fail(&cli_ip(sptr));
+        IPcheck_connect_fail(sptr);
         return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
     }
     ircd_strncpy(user->host, cli_sockhost(sptr), HOSTLEN);
@@ -468,7 +465,6 @@ int register_user(struct Client *cptr, struct Client *sptr,
       return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
 
     if (!EmptyString(aconf->passwd)
-        && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
         && strcmp(cli_passwd(sptr), aconf->passwd))
     {
       ServerStats->is_ref++;
@@ -564,21 +560,9 @@ int register_user(struct Client *cptr, struct Client *sptr,
       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);
-  }
-  SetUser(sptr);
-
-  if (IsInvisible(sptr))
-    ++UserStats.inv_clients;
-  if (IsOper(sptr))
-    ++UserStats.opers;
 
-  if (MyConnect(sptr)) {
+    SetUser(sptr);
     cli_handler(sptr) = CLIENT_HANDLER;
-    release_dns_reply(sptr);
     SetLocalNumNick(sptr);
     send_reply(sptr,
                RPL_WELCOME,
@@ -591,13 +575,12 @@ int register_user(struct Client *cptr, struct Client *sptr,
      */
     send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
     send_reply(sptr, RPL_CREATED, creation);
-    send_reply(sptr, RPL_MYINFO, cli_name(&me), infousermodes, infochanmodes,
-               infochanmodeswithparams, version);
+    send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes,
+               infochanmodes, infochanmodeswithparams);
     send_supported(sptr);
     m_lusers(sptr, sptr, 1, parv);
     update_load();
     motd_signon(sptr);
-/*      nextping = CurrentTime; */
     if (cli_snomask(sptr) & SNO_NOISY)
       set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
     if (feature_bool(FEAT_CONNEXIT_NOTICES))
@@ -611,18 +594,38 @@ int register_user(struct Client *cptr, struct Client *sptr,
     /*
      * Set user's initial modes
      */
-    parv[0] = (char*)nick;
-    parv[1] = (char*)nick;
-    parv[2] = (char*)client_get_default_umode(sptr);
-    parv[3] = NULL; /* needed in case of +s */
-    set_user_mode(sptr, sptr, 3, parv);
-    ClearHiddenHost(sptr); /* just in case somebody stuck +x in there */
+    tmpstr = (char*)client_get_default_umode(sptr);
+    if (tmpstr) for (; *tmpstr; ++tmpstr) {
+      switch (*tmpstr) {
+      case 's':
+        if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY)) {
+          SetServNotice(sptr);
+          set_snomask(sptr, SNO_DEFAULT, SNO_SET);
+        }
+        break;
+      case 'w':
+        if (!feature_bool(FEAT_WALLOPS_OPER_ONLY))
+          SetWallops(sptr);
+        break;
+      case 'i':
+        SetInvisible(sptr);
+        break;
+      case 'd':
+        SetDeaf(sptr);
+        break;
+      case 'g':
+        if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY))
+          SetDebug(sptr);
+        break;
+      }
+    }
   }
-  else
-    /* if (IsServer(cptr)) */
-  {
+  else {
     struct Client *acptr;
 
+    ircd_strncpy(user->username, username, USERLEN);
+    Count_newremoteclient(UserStats, user->server);
+
     acptr = user->server;
     if (cli_from(acptr) != cli_from(sptr))
     {
@@ -636,12 +639,12 @@ int register_user(struct Client *cptr, struct Client *sptr,
       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 = cli_serv(acptr)->up)
+    for (; acptr != &me; acptr = cli_serv(acptr)->up)
     {
       if (IsBurst(acptr) || Protocol(acptr) < 10)
         break;
@@ -655,20 +658,42 @@ int register_user(struct Client *cptr, struct Client *sptr,
                     sptr, cli_name(&me));
       return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
     }
+    SetUser(sptr);
   }
-  tmpstr = umode_str(sptr);
-  sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
-                       "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
-                       nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
-                       user->username, user->realhost,
-                       *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
-                       iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64)),
-                       NumNick(sptr), cli_info(sptr));
-
-  /* Send server notice mask to client */
-  if (MyUser(sptr) && (cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
-    send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
 
+  if (IsInvisible(sptr))
+    ++UserStats.inv_clients;
+  if (IsOper(sptr))
+    ++UserStats.opers;
+
+  tmpstr = umode_str(sptr);
+  /* 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",
+                             nick, 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",
+                             nick, 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))
+  {
+    static struct Flags flags; /* automatically initialized to zeros */
+    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;
 }
 
@@ -856,6 +881,8 @@ int set_nick_name(struct Client* cptr, struct Client* sptr,
     }
     hAddClient(sptr);
 
+    cli_unreg(sptr) &= ~CLIREG_NICK; /* nickname now set */
+
     /*
      * If the client hasn't gotten a cookie-ping yet,
      * choose a cookie and send it. -record!jegelhof@cloud9.net
@@ -866,7 +893,7 @@ int set_nick_name(struct Client* cptr, struct Client* sptr,
       } while (!cli_cookie(sptr));
       sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr));
     }
-    else if (*(cli_user(sptr))->host && cli_cookie(sptr) == COOKIE_VERIFIED) {
+    else if (!cli_unreg(sptr)) {
       /*
        * USER and PONG already received, now we have NICK.
        * register_user may reject the client and call exit_client
@@ -1127,6 +1154,7 @@ hide_hostmask(struct Client *cptr, unsigned int flag)
     for (chan = (cli_user(cptr))->channel; chan;
          chan = chan->next_channel)
       ClearBanValid(chan);
+    break;
   default:
     return 0;
   }
@@ -1151,12 +1179,8 @@ hide_hostmask(struct Client *cptr, unsigned int flag)
   {
     if (IsZombie(chan))
       continue;
-    /* For a user with no modes in a join-delayed channel, do not show
-     * the rejoin. */
-    if (!IsChanOp(chan) && !HasVoice(chan)
-        && (chan->channel->mode.mode & MODE_DELJOINS))
-      SetDelayedJoin(chan);
-    else
+    /* 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))
@@ -1428,9 +1452,7 @@ char *umode_str(struct Client *cptr)
   int i;
   struct Flags c_flags = cli_flags(cptr);
 
-  if (HasPriv(cptr, PRIV_PROPAGATE))
-    FlagSet(&c_flags, FLAG_OPER);
-  else
+  if (!HasPriv(cptr, PRIV_PROPAGATE))
     FlagClr(&c_flags, FLAG_OPER);
 
   for (i = 0; i < USERMODELIST_SIZE; ++i)
@@ -1660,7 +1682,7 @@ void set_snomask(struct Client *cptr, unsigned int newmask, int what)
 /** 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.
+ * 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.
@@ -1672,7 +1694,7 @@ int is_silenced(struct Client *sptr, struct Client *acptr)
   size_t buf_used, slen;
   char buf[BUFSIZE];
 
-  if (!(user = cli_user(acptr))
+  if (IsServer(sptr) || !(user = cli_user(acptr))
       || !(found = find_ban(sptr, user->silence)))
     return 0;
   assert(!(found->flags & BAN_EXCEPTION));