]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_ident.cpp
Revert automated conversion by Special, as it (unfortunately) neglects some details...
[user/henk/code/inspircd.git] / src / modules / m_ident.cpp
index fd76e87be803cb425c126fafac7044140b4902ef..3eded4daf59e2eb7a21eb59076d8c6647a05bfb3 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
  * See: http://www.inspircd.org/wiki/index.php/Credits
  *
  * This program is free but copyrighted software; see
@@ -40,7 +40,7 @@
  * Using this framework we have a much more stable module.
  *
  * A few things to note:
- * 
+ *
  *   O  The only place that may *delete* an active or inactive
  *      ident socket is OnUserDisconnect in the module class.
  *      Because this is out of scope of the socket class there is
@@ -49,7 +49,7 @@
  *
  *   O  Closure of the ident socket with the Close() method will
  *      not cause removal of the socket from memory or detatchment
- *      from its 'parent' User class. It will only flag it as an 
+ *      from its 'parent' User class. It will only flag it as an
  *      inactive socket in the socket engine.
  *
  *   O  Timeouts are handled in OnCheckReaady at the same time as
@@ -106,7 +106,7 @@ class IdentRequestSocket : public EventHandler
                /* We allocate two of these because sizeof(sockaddr_in6) > sizeof(sockaddr_in) */
                sockaddr* s = new sockaddr[2];
                sockaddr* addr = new sockaddr[2];
-       
+
 #ifdef IPV6
                /* Horrid icky nasty ugly berkely socket crap. */
                if (v6)
@@ -148,6 +148,7 @@ class IdentRequestSocket : public EventHandler
                {
                        this->Close();
                        delete[] s;
+                       delete[] addr;
                        throw ModuleException("failed to bind()");
                }
 
@@ -179,7 +180,7 @@ class IdentRequestSocket : public EventHandler
 
        virtual void OnConnected()
        {
-               ServerInstance->Log(DEBUG,"OnConnected()");
+               ServerInstance->Logs->Log("m_ident",DEBUG,"OnConnected()");
 
                /* Both sockaddr_in and sockaddr_in6 can be safely casted to sockaddr, especially since the
                 * only members we use are in a part of the struct that should always be identical (at the
@@ -207,7 +208,7 @@ class IdentRequestSocket : public EventHandler
                #else
                int req_size = snprintf(req, sizeof(req), "%d,%d\r\n", ntohs(raddr.sin6_port), ntohs(laddr.sin6_port));
                #endif
-               
+
                /* Send failed if we didnt write the whole ident request --
                 * might as well give up if this happens!
                 */
@@ -229,7 +230,7 @@ class IdentRequestSocket : public EventHandler
                        break;
                        case EVENT_ERROR:
                                /* fd error event, ohshi- */
-                               ServerInstance->Log(DEBUG,"EVENT_ERROR");
+                               ServerInstance->Logs->Log("m_ident",DEBUG,"EVENT_ERROR");
                                /* We *must* Close() here immediately or we get a
                                 * huge storm of EVENT_ERROR events!
                                 */
@@ -246,7 +247,7 @@ class IdentRequestSocket : public EventHandler
                 */
                if (GetFd() > -1)
                {
-                       ServerInstance->Log(DEBUG,"Close ident socket %d", GetFd());
+                       ServerInstance->Logs->Log("m_ident",DEBUG,"Close ident socket %d", GetFd());
                        ServerInstance->SE->DelFd(this);
                        ServerInstance->SE->Close(GetFd());
                        ServerInstance->SE->Shutdown(GetFd(), SHUT_WR);
@@ -269,8 +270,6 @@ class IdentRequestSocket : public EventHandler
 
        void ReadResponse()
        {
-               ServerInstance->Log(DEBUG,"ReadResponse()");
-
                /* We don't really need to buffer for incomplete replies here, since IDENT replies are
                 * extremely short - there is *no* sane reason it'd be in more than one packet
                 */
@@ -282,10 +281,13 @@ class IdentRequestSocket : public EventHandler
                 */
                if (recvresult < 3)
                {
+                       Close();
                        done = true;
                        return;
                }
 
+               ServerInstance->Logs->Log("m_ident",DEBUG,"ReadResponse()");
+
                irc::sepstream sep(ibuf, ':');
                std::string token;
                for (int i = 0; sep.GetToken(token); i++)
@@ -294,11 +296,11 @@ class IdentRequestSocket : public EventHandler
                        if (i < 3)
                                continue;
 
-                       char ident[IDENTMAX + 2];
+                       std::string ident;
 
                        /* Truncate the ident at any characters we don't like, skip leading spaces */
-                       int k = 0;
-                       for (const char *j = token.c_str(); *j && (k < IDENTMAX + 1); j++)
+                       size_t k = 0;
+                       for (const char *j = token.c_str(); *j && (k < ServerInstance->Config->Limits.IdentMax + 1); j++)
                        {
                                if (*j == ' ')
                                        continue;
@@ -306,20 +308,17 @@ class IdentRequestSocket : public EventHandler
                                /* Rules taken from InspIRCd::IsIdent */
                                if (((*j >= 'A') && (*j <= '}')) || ((*j >= '0') && (*j <= '9')) || (*j == '-') || (*j == '.'))
                                {
-                                       ident[k++] = *j;
+                                       ident += *j;
                                        continue;
                                }
 
                                break;
                        }
 
-                       ident[k] = '\0';
-
                        /* Re-check with IsIdent, in case that changes and this doesn't (paranoia!) */
-                       if (*ident && ServerInstance->IsIdent(ident))
+                       if (!ident.empty() && ServerInstance->IsIdent(ident.c_str()))
                        {
                                result = ident;
-                               ServerInstance->next_call = ServerInstance->Time();
                        }
 
                        break;
@@ -343,35 +342,32 @@ class ModuleIdent : public Module
                : Module(Me)
        {
                OnRehash(NULL, "");
+               Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCheckReady, I_OnCleanup, I_OnUserDisconnect };
+               ServerInstance->Modules->Attach(eventlist, this, 5);
        }
-       
+
        virtual Version GetVersion()
        {
-               return Version(1, 1, 1, 0, VF_VENDOR, API_VERSION);
+               return Version(1, 2, 1, 0, VF_VENDOR, API_VERSION);
        }
-       
-       virtual void Implements(char *List)
-       {
-               List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnCleanup] = List[I_OnUserDisconnect] = 1;
-       }
-       
+
+
        virtual void OnRehash(User *user, const std::string &param)
        {
                ConfigReader MyConf(ServerInstance);
-               
+
                RequestTimeout = MyConf.ReadInteger("ident", "timeout", 0, true);
                if (!RequestTimeout)
                        RequestTimeout = 5;
        }
-       
+
        virtual int OnUserRegister(User *user)
        {
                /* User::ident is currently the username field from USER; with m_ident loaded, that
-                * should be preceded by a ~. The field is actually IDENTMAX+2 characters wide. */
-               memmove(user->ident + 1, user->ident, IDENTMAX);
-               user->ident[0] = '~';
-               /* Ensure that it is null terminated */
-               user->ident[IDENTMAX + 1] = '\0';
+                * should be preceded by a ~. The field is actually IdentMax+2 characters wide. */
+               if (user->ident.length() > ServerInstance->Config->Limits.IdentMax + 1)
+                       user->ident.assign(user->ident, 0, ServerInstance->Config->Limits.IdentMax);
+               user->ident.insert(0, "~");
 
                user->WriteServ("NOTICE Auth :*** Looking up your ident...");
 
@@ -385,7 +381,7 @@ class ModuleIdent : public Module
 
                if (getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0)
                {
-                       user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", user->ident);
+                       user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", user->ident.c_str());
                        return 0;
                }
 
@@ -403,7 +399,7 @@ class ModuleIdent : public Module
                }
                catch (ModuleException &e)
                {
-                       ServerInstance->Log(DEBUG,"Ident exception: %s", e.GetReason());
+                       ServerInstance->Logs->Log("m_ident",DEBUG,"Ident exception: %s", e.GetReason());
                        return 0;
                }
 
@@ -417,24 +413,25 @@ class ModuleIdent : public Module
         */
        virtual bool OnCheckReady(User *user)
        {
-               ServerInstance->Log(DEBUG,"OnCheckReady %s", user->nick);
-
                /* Does user have an ident socket attached at all? */
                IdentRequestSocket *isock = NULL;
                if (!user->GetExt("ident_socket", isock))
                {
-                       ServerInstance->Log(DEBUG, "No ident socket :(");
+                       ServerInstance->Logs->Log("m_ident",DEBUG, "No ident socket :(");
                        return true;
                }
 
-               ServerInstance->Log(DEBUG, "Has ident_socket");
+               ServerInstance->Logs->Log("m_ident",DEBUG, "Has ident_socket");
+
+               time_t compare = isock->age;
+               compare += RequestTimeout;
 
                /* Check for timeout of the socket */
-               if (isock->age + RequestTimeout > ServerInstance->Time() && !isock->HasResult())
+               if (ServerInstance->Time() >= compare)
                {
                        /* Ident timeout */
                        user->WriteServ("NOTICE Auth :*** Ident request timed out.");
-                       ServerInstance->Log(DEBUG, "Timeout");
+                       ServerInstance->Logs->Log("m_ident",DEBUG, "Timeout");
                        /* The user isnt actually disconnecting,
                         * we call this to clean up the user
                         */
@@ -445,11 +442,11 @@ class ModuleIdent : public Module
                /* Got a result yet? */
                if (!isock->HasResult())
                {
-                       ServerInstance->Log(DEBUG, "No result yet");
+                       ServerInstance->Logs->Log("m_ident",DEBUG, "No result yet");
                        return false;
                }
 
-               ServerInstance->Log(DEBUG, "Yay, result!");
+               ServerInstance->Logs->Log("m_ident",DEBUG, "Yay, result!");
 
                /* wooo, got a result (it will be good, or bad) */
                if (*(isock->GetResult()) != '~')
@@ -458,7 +455,7 @@ class ModuleIdent : public Module
                        user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", isock->GetResult());
 
                /* Copy the ident string to the user */
-               strlcpy(user->ident, isock->GetResult(), IDENTMAX+1);
+               user->ident.assign(isock->GetResult(), 0, ServerInstance->Config->Limits.IdentMax + 1);
 
                /* The user isnt actually disconnecting, we call this to clean up the user */
                OnUserDisconnect(user);
@@ -481,10 +478,9 @@ class ModuleIdent : public Module
                        isock->Close();
                        delete isock;
                        user->Shrink("ident_socket");
-                       ServerInstance->Log(DEBUG, "Removed ident socket from %s", user->nick);
                }
        }
 };
 
-MODULE_INIT(ModuleIdent);
+MODULE_INIT(ModuleIdent)