]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conf.c
- Konfiguration wird nun besser validiert (Laenge, Zahlen).
[ngircd-alex.git] / src / ngircd / conf.c
index bd8b5eeb8d674636807e7d958cea9c1fcb364ad4..36e14aad3d80e9ff68a5aa0337127d25dc639800 100644 (file)
@@ -9,7 +9,7 @@
  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
  * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
  *
- * $Id: conf.c,v 1.29 2002/09/16 09:13:06 alex Exp $
+ * $Id: conf.c,v 1.41 2002/11/30 22:15:49 alex Exp $
  *
  * conf.h: Konfiguration des ngircd
  */
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <pwd.h>
+#include <grp.h>
+#include <sys/types.h>
+
+#ifdef HAVE_CTYPE_H
+# include <ctype.h>
+#endif
 
 #include "ngircd.h"
 #include "conn.h"
@@ -67,12 +74,13 @@ Conf_Test( VOID )
 {
        /* Konfiguration einlesen, ueberpruefen und ausgeben. */
 
-       UINT i;
+       struct passwd *pwd;
+       struct group *grp;
+       INT i;
 
        Use_Log = FALSE;
        Set_Defaults( );
 
-       printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
        Read_Config( );
 
        /* Wenn stdin ein ein TTY ist: auf Taste warten */
@@ -91,19 +99,25 @@ Conf_Test( VOID )
        printf( "  AdminInfo2 = %s\n", Conf_ServerAdmin2 );
        printf( "  AdminEMail = %s\n", Conf_ServerAdminMail );
        printf( "  MotdFile = %s\n", Conf_MotdFile );
-       printf( "  ListenPorts = " );
+       printf( "  Ports = " );
        for( i = 0; i < Conf_ListenPorts_Count; i++ )
        {
                if( i != 0 ) printf( ", " );
                printf( "%u", Conf_ListenPorts[i] );
        }
        puts( "" );
-       printf( "  ServerUID = %ld\n", (INT32)Conf_UID );
-       printf( "  ServerGID = %ld\n", (INT32)Conf_GID );
+       pwd = getpwuid( Conf_UID );
+       if( pwd ) printf( "  ServerUID = %s\n", pwd->pw_name );
+       else printf( "  ServerUID = %ld\n", (LONG)Conf_UID );
+       grp = getgrgid( Conf_GID );
+       if( grp ) printf( "  ServerGID = %s\n", grp->gr_name );
+       else printf( "  ServerGID = %ld\n", (LONG)Conf_GID );
        printf( "  PingTimeout = %d\n", Conf_PingTimeout );
        printf( "  PongTimeout = %d\n", Conf_PongTimeout );
        printf( "  ConnectRetry = %d\n", Conf_ConnectRetry );
        printf( "  OperCanUseMode = %s\n", Conf_OperCanMode == TRUE ? "yes" : "no" );
+       if( Conf_MaxConnections > 0 ) printf( "  MaxConnections = %ld\n", Conf_MaxConnections );
+       else printf( "  MaxConnections = -1\n" );
        puts( "" );
 
        for( i = 0; i < Conf_Oper_Count; i++ )
@@ -127,7 +141,8 @@ Conf_Test( VOID )
                printf( "  Name = %s\n", Conf_Server[i].name );
                printf( "  Host = %s\n", Conf_Server[i].host );
                printf( "  Port = %d\n", Conf_Server[i].port );
-               printf( "  Password = %s\n", Conf_Server[i].pwd );
+               printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
+               printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
                printf( "  Group = %d\n", Conf_Server[i].group );
                puts( "" );
        }
@@ -177,6 +192,8 @@ Set_Defaults( VOID )
        Conf_Channel_Count = 0;
 
        Conf_OperCanMode = FALSE;
+       
+       Conf_MaxConnections = 0;
 } /* Set_Defaults */
 
 
@@ -198,6 +215,8 @@ Read_Config( VOID )
                exit( 1 );
        }
 
+       Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
+
        line = 0;
        strcpy( section, "" );
        while( TRUE )
@@ -235,7 +254,8 @@ Read_Config( VOID )
                                        strcpy( Conf_Server[Conf_Server_Count].host, "" );
                                        strcpy( Conf_Server[Conf_Server_Count].ip, "" );
                                        strcpy( Conf_Server[Conf_Server_Count].name, "" );
-                                       strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
+                                       strcpy( Conf_Server[Conf_Server_Count].pwd_in, "" );
+                                       strcpy( Conf_Server[Conf_Server_Count].pwd_out, "" );
                                        Conf_Server[Conf_Server_Count].port = 0;
                                        Conf_Server[Conf_Server_Count].group = -1;
                                        Conf_Server[Conf_Server_Count].lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
@@ -294,8 +314,10 @@ Read_Config( VOID )
 LOCAL VOID
 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
 {
+       struct passwd *pwd;
+       struct group *grp;
        CHAR *ptr;
-       INT32 port;
+       LONG port;
        
        assert( Line > 0 );
        assert( Var != NULL );
@@ -306,6 +328,7 @@ Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
                /* Der Server-Name */
                strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
                Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_ID_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "Info" ) == 0 )
@@ -313,6 +336,7 @@ Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
                /* Server-Info-Text */
                strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
                Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_INFO_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Info\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "Password" ) == 0 )
@@ -320,6 +344,7 @@ Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
                /* Server-Passwort */
                strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
                Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_PASS_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Password\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "AdminInfo1" ) == 0 )
@@ -327,6 +352,7 @@ Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
                /* Server-Info-Text */
                strncpy( Conf_ServerAdmin1, Arg, CLIENT_INFO_LEN - 1 );
                Conf_ServerAdmin1[CLIENT_INFO_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_INFO_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminInfo1\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "AdminInfo2" ) == 0 )
@@ -334,6 +360,7 @@ Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
                /* Server-Info-Text */
                strncpy( Conf_ServerAdmin2, Arg, CLIENT_INFO_LEN - 1 );
                Conf_ServerAdmin2[CLIENT_INFO_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_INFO_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminInfo2\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "AdminEMail" ) == 0 )
@@ -341,6 +368,7 @@ Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
                /* Server-Info-Text */
                strncpy( Conf_ServerAdminMail, Arg, CLIENT_INFO_LEN - 1 );
                Conf_ServerAdminMail[CLIENT_INFO_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_INFO_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminEMail\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "Ports" ) == 0 )
@@ -367,39 +395,70 @@ Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
                /* Datei mit der "message of the day" (MOTD) */
                strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
                Conf_MotdFile[FNAME_LEN - 1] = '\0';
+               if( strlen( Arg ) > FNAME_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MotdFile\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "ServerUID" ) == 0 )
        {
                /* UID, mit der der Daemon laufen soll */
-               Conf_UID = (UINT)atoi( Arg );
+               pwd = getpwnam( Arg );
+               if( pwd ) Conf_UID = pwd->pw_uid;
+               else
+               {
+#ifdef HAVE_ISDIGIT
+                       if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerUID\" is not a number!", NGIRCd_ConfFile, Line );
+                       else
+#endif
+                       Conf_UID = (UINT)atoi( Arg );
+               }
                return;
        }
        if( strcasecmp( Var, "ServerGID" ) == 0 )
        {
                /* GID, mit der der Daemon laufen soll */
-               Conf_GID = (UINT)atoi( Arg );
+               grp = getgrnam( Arg );
+               if( grp ) Conf_GID = grp->gr_gid;
+               else
+               {
+#ifdef HAVE_ISDIGIT
+                       if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerGID\" is not a number!", NGIRCd_ConfFile, Line );
+                       else
+#endif
+                       Conf_GID = (UINT)atoi( Arg );
+               }
                return;
        }
        if( strcasecmp( Var, "PingTimeout" ) == 0 )
        {
                /* PING-Timeout */
                Conf_PingTimeout = atoi( Arg );
-               if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
+               if( Conf_PingTimeout < 5 )
+               {
+                       Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
+                       Conf_PingTimeout = 5;
+               }
                return;
        }
        if( strcasecmp( Var, "PongTimeout" ) == 0 )
        {
                /* PONG-Timeout */
                Conf_PongTimeout = atoi( Arg );
-               if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
+               if( Conf_PongTimeout < 5 )
+               {
+                       Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
+                       Conf_PongTimeout = 5;
+               }
                return;
        }
        if( strcasecmp( Var, "ConnectRetry" ) == 0 )
        {
                /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
                Conf_ConnectRetry = atoi( Arg );
-               if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
+               if( Conf_ConnectRetry < 5 )
+               {
+                       Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
+                       Conf_ConnectRetry = 5;
+               }
                return;
        }
        if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
@@ -411,6 +470,17 @@ Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
                else Conf_OperCanMode = FALSE;
                return;
        }
+       if( strcasecmp( Var, "MaxConnections" ) == 0 )
+       {
+               /* Maximale Anzahl von Verbindungen. Werte <= 0 stehen
+                * fuer "kein Limit". */
+#ifdef HAVE_ISDIGIT
+               if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxConnections\" is not a number!", NGIRCd_ConfFile, Line );
+               else
+#endif
+               Conf_MaxConnections = atol( Arg );
+               return;
+       }
                
        Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
 } /* Handle_GLOBAL */
@@ -429,6 +499,7 @@ Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
                /* Name des IRC Operator */
                strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_PASS_LEN - 1 );
                Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_PASS_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_PASS_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "Password" ) == 0 )
@@ -436,6 +507,7 @@ Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
                /* Passwort des IRC Operator */
                strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
                Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_PASS_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Password\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        
@@ -446,7 +518,7 @@ Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
 LOCAL VOID
 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
 {
-       INT32 port;
+       LONG port;
        
        assert( Line > 0 );
        assert( Var != NULL );
@@ -457,6 +529,7 @@ Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
                /* Hostname des Servers */
                strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
                Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
+               if( strlen( Arg ) > HOST_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Host\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "Name" ) == 0 )
@@ -464,13 +537,23 @@ Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
                /* Name des Servers ("Nick") */
                strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
                Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_ID_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
-       if( strcasecmp( Var, "Password" ) == 0 )
+       if( strcasecmp( Var, "MyPassword" ) == 0 )
        {
-               /* Passwort des Servers */
-               strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
-               Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
+               /* Passwort dieses Servers, welches empfangen werden muss */
+               strncpy( Conf_Server[Conf_Server_Count - 1].pwd_in, Arg, CLIENT_PASS_LEN - 1 );
+               Conf_Server[Conf_Server_Count - 1].pwd_in[CLIENT_PASS_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_PASS_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MyPassword\" too long!", NGIRCd_ConfFile, Line );
+               return;
+       }
+       if( strcasecmp( Var, "PeerPassword" ) == 0 )
+       {
+               /* Passwort des anderen Servers, welches gesendet werden muss */
+               strncpy( Conf_Server[Conf_Server_Count - 1].pwd_out, Arg, CLIENT_PASS_LEN - 1 );
+               Conf_Server[Conf_Server_Count - 1].pwd_out[CLIENT_PASS_LEN - 1] = '\0';
+               if( strlen( Arg ) > CLIENT_PASS_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"PeerPassword\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "Port" ) == 0 )
@@ -484,6 +567,10 @@ Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
        if( strcasecmp( Var, "Group" ) == 0 )
        {
                /* Server-Gruppe */
+#ifdef HAVE_ISDIGIT
+               if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Group\" is not a number!", NGIRCd_ConfFile, Line );
+               else
+#endif
                Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
                return;
        }
@@ -504,6 +591,7 @@ Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
                /* Hostname des Servers */
                strncpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, CHANNEL_NAME_LEN - 1 );
                Conf_Channel[Conf_Channel_Count - 1].name[CHANNEL_NAME_LEN - 1] = '\0';
+               if( strlen( Arg ) > CHANNEL_NAME_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "Modes" ) == 0 )
@@ -511,6 +599,7 @@ Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
                /* Name des Servers ("Nick") */
                strncpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, CHANNEL_MODE_LEN - 1 );
                Conf_Channel[Conf_Channel_Count - 1].modes[CHANNEL_MODE_LEN - 1] = '\0';
+               if( strlen( Arg ) > CHANNEL_MODE_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Modes\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
        if( strcasecmp( Var, "Topic" ) == 0 )
@@ -518,6 +607,7 @@ Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
                /* Passwort des Servers */
                strncpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, CHANNEL_TOPIC_LEN - 1 );
                Conf_Channel[Conf_Channel_Count - 1].topic[CHANNEL_TOPIC_LEN - 1] = '\0';
+               if( strlen( Arg ) > CHANNEL_TOPIC_LEN - 1 ) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Topic\" too long!", NGIRCd_ConfFile, Line );
                return;
        }
 
@@ -539,7 +629,7 @@ Validate_Config( VOID )
        }
 
 #ifdef STRICT_RFC
-       if( ! ConfAdminMail[0] )
+       if( ! Conf_ServerAdminMail[0] )
        {
                /* Keine Server-Information konfiguriert */
                Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
@@ -548,10 +638,10 @@ Validate_Config( VOID )
        }
 #endif
 
-       if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin1[0] && ! Conf_ServerAdminMail[0] )
+       if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
        {
                /* Keine Server-Information konfiguriert */
-               Log( LOG_WARNING, "No server information configured but required by RFC!" );
+               Log( LOG_WARNING, "No administrative information configured but required by RFC!" );
        }
 } /* Validate_Config */
 
@@ -584,7 +674,7 @@ va_dcl
        /* Im "normalen Betrieb" soll der Log-Mechanismus des ngIRCd verwendet
         * werden, beim Testen der Konfiguration jedoch nicht, hier sollen alle
         * Meldungen direkt auf die Konsole ausgegeben werden: */
-       if( Use_Log ) Log( Level, msg );
+       if( Use_Log ) Log( Level, "%s", msg );
        else puts( msg );
 } /* Config_Error */