]> 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 0b21d3a12b66e27fdfd392f20b235e19ba6a9d7d..c6095a31c613bc5ca127d55b8723e15b836f1cca 100644 (file)
@@ -116,6 +116,7 @@ cb_listen(int sock, short irrelevant)
        (void) irrelevant;
        if (New_Connection( sock ) >= 0)
                NumConnections++;
+       LogDebug("Total number of connections now %ld.", NumConnections);
 }
 
 
@@ -130,6 +131,7 @@ cb_listen_ssl(int sock, short irrelevant)
                return;
 
        NumConnections++;
+       LogDebug("Total number of connections now %ld.", NumConnections);
        io_event_setcb(My_Connections[fd].sock, cb_clientserver_ssl);
 }
 #endif
@@ -232,12 +234,12 @@ cb_connserver_login_ssl(int sock, short unused)
        case 0: LogDebug("ConnSSL_Connect: not ready");
                return;
        case -1:
-               Log(LOG_INFO, "SSL connection on socket %d failed", sock);
+               Log(LOG_ERR, "SSL connection on socket %d failed!", sock);
                Conn_Close(idx, "Can't connect!", NULL, false);
                return;
        }
 
-       Log( LOG_INFO, "SSL Connection %d with \"%s:%d\" established.", idx,
+       Log( LOG_INFO, "SSL connection %d with \"%s:%d\" established.", idx,
                        My_Connections[idx].host, Conf_Server[Conf_GetServer( idx )].port );
 
        server_login(idx);
@@ -302,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 */
@@ -339,16 +339,11 @@ Conn_Init( void )
 GLOBAL void
 Conn_Exit( void )
 {
-       /* Modul abmelden: alle noch offenen Connections
-        * schliessen und freigeben. */
-
        CONN_ID idx;
 
-       LogDebug("Shutting down all connections ..." );
-
        Conn_ExitListeners();
 
-       /* Sockets schliessen */
+       LogDebug("Shutting down all connections ..." );
        for( idx = 0; idx < Pool_Size; idx++ ) {
                if( My_Connections[idx].sock > NONE ) {
                        Conn_Close( idx, NULL, NGIRCd_SignalRestart ?
@@ -449,7 +444,8 @@ Conn_ExitListeners( void )
 #endif
 
        arraylen = array_length(&My_Listeners, sizeof (int));
-       Log( LOG_INFO, "Shutting down all listening sockets (%d total)...", arraylen );
+       Log(LOG_INFO,
+           "Shutting down all listening sockets (%d total) ...", arraylen);
        fd = array_start(&My_Listeners);
        while(arraylen--) {
                assert(fd != NULL);
@@ -885,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
@@ -958,7 +954,7 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
        c = Conn_GetClient( Idx );
 #ifdef SSL_SUPPORT
        if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_SSL )) {
-               Log( LOG_INFO, "SSL Connection %d shutting down", Idx );
+               Log(LOG_INFO, "SSL connection %d shutting down ...", Idx);
                ConnSSL_Free(&My_Connections[Idx]);
        }
 #endif
@@ -1035,7 +1031,8 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
        assert(NumConnections > 0);
        if (NumConnections)
                NumConnections--;
-       LogDebug("Shutdown of connection %d completed", Idx );
+       LogDebug("Shutdown of connection %d completed, %ld connection%s left.",
+                Idx, NumConnections, NumConnections != 1 ? "s" : "");
 } /* Conn_Close */
 
 
@@ -1164,7 +1161,7 @@ New_Connection( int Sock )
 #endif
        ng_ipaddr_t new_addr;
        char ip_str[NG_INET_ADDRSTRLEN];
-       int new_sock, new_sock_len;
+       int new_sock, new_sock_len, identsock;
        CLIENT *c;
        long cnt;
 
@@ -1259,18 +1256,29 @@ 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)
+               identsock = -1;
+#endif
        if (!Conf_NoDNS)
                Resolve_Addr(&My_Connections[new_sock].res_stat, &new_addr,
-                       My_Connections[new_sock].sock, cb_Read_Resolver_Result);
-
+                            identsock, cb_Read_Resolver_Result);
        Conn_SetPenalty(new_sock, 4);
        return new_sock;
 } /* New_Connection */
@@ -1279,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;
        }
@@ -1534,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)
@@ -1553,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) {
@@ -1717,8 +1724,9 @@ New_Server( int Server , ng_ipaddr_t *dest)
                Conf_Server[Server].conn_id = NONE;
        }
 #endif
-       LogDebug("Registered new connection %d on socket %d.",
-                               new_sock, My_Connections[new_sock].sock );
+       NumConnections++;
+       LogDebug("Registered new connection %d on socket %d (%ld in total).",
+                new_sock, My_Connections[new_sock].sock, NumConnections);
        Conn_OPTION_ADD( &My_Connections[new_sock], CONN_ISCONNECTING );
 } /* New_Server */
 
@@ -1817,8 +1825,8 @@ cb_Connect_to_Server(int fd, UNUSED short events)
                len -= sizeof(ng_ipaddr_t);
                if (len > sizeof(&Conf_Server[i].dst_addr)) {
                        len = sizeof(&Conf_Server[i].dst_addr);
-                       Log(LOG_NOTICE, "Notice: Resolver returned more IP Addresses for host than we can handle,"
-                                       " additional addresses dropped");
+                       Log(LOG_NOTICE,
+                               "Notice: Resolver returned more IP Addresses for host than we can handle, additional addresses dropped.");
                }
                memcpy(&Conf_Server[i].dst_addr, &dest_addrs[1], len);
        }
@@ -1943,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);
 }
 
@@ -1950,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