]> arthur.barton.de Git - ngircd.git/blobdiff - src/ngircd/conn.c
security: fix remotely triggerable crash in SSL/TLS code
[ngircd.git] / src / ngircd / conn.c
index bd1a5bddffbd00c14c02a45ec259c0a0af0dbfe6..c6095a31c613bc5ca127d55b8723e15b836f1cca 100644 (file)
@@ -304,8 +304,6 @@ cb_clientserver_ssl(int sock, short what)
 GLOBAL void
 Conn_Init( void )
 {
-       /* Modul initialisieren: statische Strukturen "ausnullen". */
-
        CONN_ID i;
 
        /* Speicher fuer Verbindungs-Pool anfordern */
@@ -341,9 +339,6 @@ Conn_Init( void )
 GLOBAL void
 Conn_Exit( void )
 {
-       /* Modul abmelden: alle noch offenen Connections
-        * schliessen und freigeben. */
-
        CONN_ID idx;
 
        Conn_ExitListeners();
@@ -886,13 +881,13 @@ Conn_Write( CONN_ID Idx, char *Data, size_t Len )
 
 
 GLOBAL void
-Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
+Conn_Close( CONN_ID Idx, const char *LogMsg, const char *FwdMsg, bool InformClient )
 {
        /* Close connection. Open pipes of asyncronous resolver
         * sub-processes are closed down. */
 
        CLIENT *c;
-       char *txt;
+       const char *txt;
        double in_k, out_k;
        UINT16 port;
 #ifdef ZLIB
@@ -1261,14 +1256,21 @@ New_Connection( int Sock )
        My_Connections[new_sock].addr = new_addr;
        My_Connections[new_sock].client = c;
 
-       Log( LOG_INFO, "Accepted connection %d from %s:%d on socket %d.", new_sock,
-                       ip_str, ng_ipaddr_getport(&new_addr), Sock);
-
-       /* Hostnamen ermitteln */
-       strlcpy(My_Connections[new_sock].host, ip_str, sizeof(My_Connections[new_sock].host));
+       /* Set initial hostname to IP address. This becomes overwritten when
+        * the DNS lookup is enabled and succeeds, but is used otherwise. */
+       if (ng_ipaddr_af(&new_addr) != AF_INET)
+               snprintf(My_Connections[new_sock].host,
+                        sizeof(My_Connections[new_sock].host), "[%s]", ip_str);
+       else
+               strlcpy(My_Connections[new_sock].host, ip_str,
+                       sizeof(My_Connections[new_sock].host));
 
        Client_SetHostname(c, My_Connections[new_sock].host);
 
+       Log(LOG_INFO, "Accepted connection %d from %s:%d on socket %d.",
+           new_sock, My_Connections[new_sock].host,
+           ng_ipaddr_getport(&new_addr), Sock);
+
        identsock = new_sock;
 #ifdef IDENTAUTH
        if (Conf_NoIdent)
@@ -1285,13 +1287,11 @@ New_Connection( int Sock )
 static CONN_ID
 Socket2Index( int Sock )
 {
-       /* zum Socket passende Connection suchen */
-
        assert( Sock >= 0 );
 
        if( Sock >= Pool_Size || My_Connections[Sock].sock != Sock ) {
-               /* die Connection wurde vermutlich (wegen eines
-                * Fehlers) bereits wieder abgebaut ... */
+               /* the Connection was already closed again, likely due to
+                * an error. */
                LogDebug("Socket2Index: can't get connection for socket %d!", Sock);
                return NONE;
        }
@@ -1540,6 +1540,7 @@ Check_Connections(void)
         * if this doesn't help either, disconnect client. */
        CLIENT *c;
        CONN_ID i;
+       char msg[64];
 
        for (i = 0; i < Pool_Size; i++) {
                if (My_Connections[i].sock < 0)
@@ -1559,8 +1560,8 @@ Check_Connections(void)
                                        LogDebug
                                            ("Connection %d: Ping timeout: %d seconds.",
                                             i, Conf_PongTimeout);
-                                       Conn_Close(i, NULL, "Ping timeout",
-                                                  true);
+                                       snprintf(msg, sizeof(msg), "Ping timeout: %d seconds", Conf_PongTimeout);
+                                       Conn_Close(i, NULL, msg, true);
                                }
                        } else if (My_Connections[i].lastdata <
                                   time(NULL) - Conf_PingTimeout) {
@@ -1950,6 +1951,9 @@ Conn_GetClient( CONN_ID Idx )
 GLOBAL bool
 Conn_GetCipherInfo(CONN_ID Idx, char *buf, size_t len)
 {
+       if (Idx < 0)
+               return false;
+       assert(Idx < (int) array_length(&My_ConnArray, sizeof(CONNECTION)));
        return ConnSSL_GetCipherInfo(&My_Connections[Idx], buf, len);
 }
 
@@ -1957,6 +1961,9 @@ Conn_GetCipherInfo(CONN_ID Idx, char *buf, size_t len)
 GLOBAL bool
 Conn_UsesSSL(CONN_ID Idx)
 {
+       if (Idx < 0)
+               return false;
+       assert(Idx < (int) array_length(&My_ConnArray, sizeof(CONNECTION)));
        return Conn_OPTION_ISSET(&My_Connections[Idx], CONN_SSL);
 }
 #endif