]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conf.c
internal changes needed for future ssl support
[ngircd-alex.git] / src / ngircd / conf.c
index 77e52cb69b571d2863d54243276130fd4956a900..b4e56172d0e7237120efb3ace301e7a6080405c1 100644 (file)
@@ -14,7 +14,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: conf.c,v 1.77 2005/06/17 19:16:53 fw Exp $";
+static char UNUSED id[] = "$Id: conf.c,v 1.82 2005/07/29 09:29:47 fw Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -38,6 +38,7 @@ static char UNUSED id[] = "$Id: conf.c,v 1.77 2005/06/17 19:16:53 fw Exp $";
 # include <ctype.h>
 #endif
 
+#include "array.h"
 #include "ngircd.h"
 #include "conn.h"
 #include "client.h"
@@ -72,6 +73,54 @@ LOCAL void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
 LOCAL void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
 
 
+static void
+ports_puts(array *a)
+{
+       unsigned int len;
+       UINT16 *ports;
+       len = array_length(a, sizeof(UINT16));
+       if (len--) {
+               ports = (UINT16*) array_start(a);
+               printf("%u", (unsigned int) *ports);
+               while (len--) {
+                       ports++;
+                       printf(", %u", (unsigned int) *ports);
+               }
+       }
+       putc('\n', stdout);
+}
+
+
+static void
+ports_parse(array *a, int Line, char *Arg)
+{
+       char *ptr;
+       int port;
+       UINT16 port16;
+
+       array_trunc(a);
+
+       /* Ports on that the server should listen. More port numbers
+        * must be separated by "," */
+       ptr = strtok( Arg, "," );
+       while (ptr) {
+               ngt_TrimStr( ptr );
+               port = atol( ptr );
+               if (port > 0 && port < 0xFFFF) {
+                       port16 = (UINT16) port;
+                       if (!array_catb(a, (char*)&port16, sizeof port16))
+                               Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
+                                                       NGIRCd_ConfFile, Line, port, strerror(errno));
+               } else {
+                       Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
+                                                                       NGIRCd_ConfFile, Line, port );
+               }
+
+               ptr = strtok( NULL, "," );
+       }
+}
+
+
 GLOBAL void
 Conf_Init( void )
 {
@@ -97,7 +146,8 @@ Conf_Test( void )
 
        struct passwd *pwd;
        struct group *grp;
-       int i;
+       unsigned int i;
+       char *topic;
 
        Use_Log = false;
        Set_Defaults( true );
@@ -125,13 +175,10 @@ Conf_Test( void )
        printf( "  MotdPhrase = %s\n", Conf_MotdPhrase );
        printf( "  ChrootDir = %s\n", Conf_Chroot );
        printf( "  PidFile = %s\n", Conf_PidFile);
-       printf( "  Ports = " );
-       for( i = 0; i < Conf_ListenPorts_Count; i++ )
-       {
-               if( i != 0 ) printf( ", " );
-               printf( "%u", (unsigned int) Conf_ListenPorts[i] );
-       }
-       puts( "" );
+       fputs("  Ports = ", stdout);
+
+       ports_puts(&Conf_ListenPorts);
+
        printf( "  Listen = %s\n", Conf_ListenAddress );
        pwd = getpwuid( Conf_UID );
        if( pwd ) printf( "  ServerUID = %s\n", pwd->pw_name );
@@ -187,7 +234,9 @@ Conf_Test( void )
                puts( "[CHANNEL]" );
                printf( "  Name = %s\n", Conf_Channel[i].name );
                printf( "  Modes = %s\n", Conf_Channel[i].modes );
-               printf( "  Topic = %s\n", Conf_Channel[i].topic );
+
+               topic = (char*)array_start(&Conf_Channel[i].topic);
+               printf( "  Topic = %s\n", topic ? topic : "");
                puts( "" );
        }
        
@@ -362,7 +411,6 @@ Set_Defaults( bool InitServers )
 
        strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
 
-       Conf_ListenPorts_Count = 0;
        strcpy( Conf_ListenAddress, "" );
 
        Conf_UID = Conf_GID = 0;
@@ -393,6 +441,7 @@ Read_Config( void )
        /* Read configuration file. */
 
        char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
+       const UINT16 defaultport = 6667;
        int line, i, n;
        FILE *fd;
 
@@ -508,15 +557,14 @@ Read_Config( void )
                                else New_Server_Idx = i;
                                continue;
                        }
-                       if( strcasecmp( section, "[CHANNEL]" ) == 0 )
-                       {
-                               if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
-                               else
-                               {
+                       if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
+                               if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
+                                       Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
+                               } else {
                                        /* Initialize new channel structure */
                                        strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
                                        strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
-                                       strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
+                                       array_free(&Conf_Channel[Conf_Channel_Count].topic);
                                        Conf_Channel_Count++;
                                }
                                continue;
@@ -554,12 +602,13 @@ Read_Config( void )
                assert( New_Server_Idx > NONE );
                Conf_Server[New_Server_Idx] = New_Server;
        }
-       
-       /* If there are no ports configured use the default: 6667 */
-       if( Conf_ListenPorts_Count < 1 )
-       {
-               Conf_ListenPorts_Count = 1;
-               Conf_ListenPorts[0] = 6667;
+
+       if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
+               if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
+                       Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
+                                                       (unsigned int) defaultport, strerror(errno));
+                       exit( 1 );
+               }
        }
 } /* Read_Config */
 
@@ -580,8 +629,6 @@ Handle_GLOBAL( int Line, char *Var, char *Arg )
 {
        struct passwd *pwd;
        struct group *grp;
-       char *ptr;
-       long port;
        
        assert( Line > 0 );
        assert( Var != NULL );
@@ -629,23 +676,9 @@ Handle_GLOBAL( int Line, char *Var, char *Arg )
                if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error_TooLong( Line, Var );
                return;
        }
-       if( strcasecmp( Var, "Ports" ) == 0 )
-       {
-               /* Ports on that the server should listen. More port numbers
-                * must be separated by "," */
-               ptr = strtok( Arg, "," );
-               while( ptr )
-               {
-                       ngt_TrimStr( ptr );
-                       port = atol( ptr );
-                       if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
-                       else
-                       {
-                               if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT16)port;
-                               else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
-                       }
-                       ptr = strtok( NULL, "," );
-               }
+
+       if( strcasecmp( Var, "Ports" ) == 0 ) {
+               ports_parse(&Conf_ListenPorts, Line, Arg);
                return;
        }
        if( strcasecmp( Var, "MotdFile" ) == 0 )
@@ -804,35 +837,47 @@ Handle_GLOBAL( int Line, char *Var, char *Arg )
 LOCAL void
 Handle_OPERATOR( int Line, char *Var, char *Arg )
 {
+       unsigned int opercount;
+       size_t len;
        assert( Line > 0 );
        assert( Var != NULL );
        assert( Arg != NULL );
        assert( Conf_Oper_Count > 0 );
 
-       if( strcasecmp( Var, "Name" ) == 0 )
-       {
+       if ( Conf_Oper_Count == 0 )
+               return;
+
+       opercount = Conf_Oper_Count - 1;
+
+       if( strcasecmp( Var, "Name" ) == 0 ) {
                /* Name of IRC operator */
-               if( strlcpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, sizeof( Conf_Oper[Conf_Oper_Count - 1].name )) >= sizeof( Conf_Oper[Conf_Oper_Count - 1].name )) Config_Error_TooLong( Line, Var );
+               len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
+               if (len >= sizeof( Conf_Oper[opercount].name ))
+                               Config_Error_TooLong( Line, Var );
+
                return;
        }
-       if( strcasecmp( Var, "Password" ) == 0 )
-       {
+       if( strcasecmp( Var, "Password" ) == 0 ) {
                /* Password of IRC operator */
-               if( strlcpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, sizeof( Conf_Oper[Conf_Oper_Count - 1].pwd )) >= sizeof( Conf_Oper[Conf_Oper_Count - 1].pwd )) Config_Error_TooLong( Line, Var );
+               len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
+               if (len >= sizeof( Conf_Oper[opercount].pwd ))
+                               Config_Error_TooLong( Line, Var );
                return;
        }
-       if( strcasecmp( Var, "Mask" ) == 0 )
-       {
-               if (Conf_Oper[Conf_Oper_Count - 1].mask) return; /* Hostname already configured */
-               Conf_Oper[Conf_Oper_Count - 1].mask = strdup( Arg );
-               if (! Conf_Oper[Conf_Oper_Count - 1].mask) {
-                       Config_Error( LOG_ERR, "%s, line %d: Cannot allocate memory for operator mask: %s", NGIRCd_ConfFile, Line, strerror(errno) );
+       if( strcasecmp( Var, "Mask" ) == 0 ) {
+               if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
+
+               Conf_Oper[opercount].mask = strdup( Arg );
+               if (! Conf_Oper[opercount].mask) {
+                       Config_Error( LOG_ERR, "%s, line %d: Cannot allocate memory for operator mask: %s",
+                                                               NGIRCd_ConfFile, Line, strerror(errno) );
                        return;
                }
 
                return;
        }
-       Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
+       Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
+                                                               NGIRCd_ConfFile, Line, Var );
 } /* Handle_OPERATOR */
 
 
@@ -922,7 +967,7 @@ Handle_CHANNEL( int Line, char *Var, char *Arg )
        if( strcasecmp( Var, "Topic" ) == 0 )
        {
                /* Initial topic */
-               if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].topic )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].topic ))
+               if (!array_copys( &Conf_Channel[Conf_Channel_Count - 1].topic, Arg))
                        Config_Error_TooLong( Line, Var );
  
                return;
@@ -981,16 +1026,6 @@ Validate_Config( bool Configtest )
                /* No administrative information configured! */
                Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
        }
-#ifdef FD_SETSIZE      
-       if(( Conf_MaxConnections > (long)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
-       {
-               Conf_MaxConnections = (long)FD_SETSIZE;
-               Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
-       }
-#else
-       Config_Error( LOG_WARN, "Don't know how many file descriptors select() can handle on this system, don't set MaxConnections too high!" );
-#endif
-
 #ifdef DEBUG
        servers = servers_once = 0;
        for( i = 0; i < MAX_SERVERS; i++ )