]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/config.c
Rework ac/socket.h for HAVE_WINSOCK:
[openldap] / servers / slapd / config.c
index f7aa3b688c523a22c09ec1c6a5bcaaa088fa7787..b02313b5cea80a278d19228788c011051459f7c5 100644 (file)
@@ -3,20 +3,19 @@
 #include "portable.h"
 
 #include <stdio.h>
+#ifdef HAVE_LOCALE_H
+#include <locale.h>
+#endif
 
 #include <ac/string.h>
+#include <ac/ctype.h>
 #include <ac/socket.h>
 
-#include "slap.h"
 #include "ldapconfig.h"
+#include "slap.h"
 
 #define MAXARGS        100
 
-extern Backend *new_backend();
-extern char    *default_referral;
-extern int     ldap_syslog;
-extern int     global_schemacheck;
-
 /*
  * defaults for various global variables
  */
@@ -28,34 +27,40 @@ char                *replogfile;
 int            global_lastmod;
 char           *ldap_srvtab = "";
 
-static char    *fp_getline();
-static void    fp_getline_init();
-static void    fp_parse_line();
+char   *slapd_pid_file  = NULL;
+char   *slapd_args_file = NULL;
+
+static char    *fp_getline(FILE *fp, int *lineno);
+static void    fp_getline_init(int *lineno);
+static int     fp_parse_line(char *line, int *argcp, char **argv);
 
-static char    *strtok_quote();
+static char    *strtok_quote(char *line, char *sep);
 
-void
-read_config( char *fname, Backend **bep, FILE *pfp )
+int
+read_config( char *fname )
 {
        FILE    *fp;
-       char    *line, *savefname, *dn;
+       char    *line, *savefname;
        int     cargc, savelineno;
        char    *cargv[MAXARGS];
        int     lineno, i;
-       Backend *be;
 
-       if ( (fp = pfp) == NULL && (fp = fopen( fname, "r" )) == NULL ) {
+       static BackendInfo *bi = NULL;
+       static BackendDB        *be = NULL;
+
+       if ( (fp = fopen( fname, "r" )) == NULL ) {
                ldap_syslog = 1;
                Debug( LDAP_DEBUG_ANY,
                    "could not open config file \"%s\" - absolute path?\n",
                    fname, 0, 0 );
                perror( fname );
-               exit( 1 );
+               return 1;
        }
 
        Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
-       be = *bep;
+
        fp_getline_init( &lineno );
+
        while ( (line = fp_getline( fp, &lineno )) != NULL ) {
                /* skip comments and blank lines */
                if ( line[0] == '#' || line[0] == '\0' ) {
@@ -64,7 +69,9 @@ read_config( char *fname, Backend **bep, FILE *pfp )
 
                Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
 
-               fp_parse_line( line, &cargc, cargv );
+               if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
+                       return( 1 );
+               }
 
                if ( cargc < 1 ) {
                        Debug( LDAP_DEBUG_ANY,
@@ -73,27 +80,66 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                        continue;
                }
 
+               if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
+                       if ( cargc < 2 ) {
+                               Debug( LDAP_DEBUG_ANY,
+               "%s: line %d: missing type in \"backend <type>\" line\n",
+                                   fname, lineno, 0 );
+                               return( 1 );
+                       }
+
+                       if( be != NULL ) {
+                               Debug( LDAP_DEBUG_ANY,
+"%s: line %d: backend line must appear before any database definition\n",
+                                   fname, lineno, 0 );
+                               return( 1 );
+                       }
+
+                       bi = backend_info( cargv[1] );
+
                /* start of a new database definition */
-               if ( strcasecmp( cargv[0], "database" ) == 0 ) {
+               } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
                        if ( cargc < 2 ) {
                                Debug( LDAP_DEBUG_ANY,
                "%s: line %d: missing type in \"database <type>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
-                       *bep = new_backend( cargv[1] );
-                       be = *bep;
+                       bi = NULL;
+                       be = backend_db_init( cargv[1] );
 
                /* assign a default depth limit for alias deref */
                be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
 
+               /* get pid file name */
+               } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
+                       if ( cargc < 2 ) {
+                               Debug( LDAP_DEBUG_ANY,
+           "%s: line %d: missing file name in \"pidfile <file>\" line\n",
+                                   fname, lineno, 0 );
+                               return( 1 );
+                       }
+
+                       slapd_pid_file = ch_strdup( cargv[1] );
+
+               /* get args file name */
+               } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
+                       if ( cargc < 2 ) {
+                               Debug( LDAP_DEBUG_ANY,
+           "%s: line %d: missing file name in \"argsfile <file>\" line\n",
+                                   fname, lineno, 0 );
+                               return( 1 );
+                       }
+
+                       slapd_args_file = ch_strdup( cargv[1] );
+
                /* set size limit */
                } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
                        if ( cargc < 2 ) {
                                Debug( LDAP_DEBUG_ANY,
            "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be == NULL ) {
                                defsize = atoi( cargv[1] );
@@ -107,7 +153,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
            "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be == NULL ) {
                                deftime = atoi( cargv[1] );
@@ -121,7 +167,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
                    "%s: line %d: missing dn in \"suffix <dn>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        } else if ( cargc > 2 ) {
                                Debug( LDAP_DEBUG_ANY,
     "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
@@ -132,8 +178,8 @@ read_config( char *fname, Backend **bep, FILE *pfp )
 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
                                    fname, lineno, 0 );
                        } else {
-                               dn = strdup( cargv[1] );
-                               (void) dn_normalize( dn );
+                               char *dn = ch_strdup( cargv[1] );
+                               (void) dn_normalize_case( dn );
                                charray_add( &be->be_suffix, dn );
                        }
 
@@ -143,12 +189,12 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                 Debug( LDAP_DEBUG_ANY,
                     "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
                                     fname, lineno, 0 );
-                                exit( 1 );
+                                return( 1 );
                         } else if ( cargc < 3 ) {
                                 Debug( LDAP_DEBUG_ANY,
                     "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
                                     fname, lineno, 0 );
-                                exit( 1 );
+                                return( 1 );
                         } else if ( cargc > 3 ) {
                                 Debug( LDAP_DEBUG_ANY,
     "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
@@ -159,13 +205,28 @@ read_config( char *fname, Backend **bep, FILE *pfp )
 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
                                     fname, lineno, 0 );
                         } else {
-                                dn = strdup( cargv[1] );
-                                (void) dn_normalize( dn );
-                                charray_add( &be->be_suffixAlias, dn );
+                                char *alias, *aliased_dn;
 
-                                dn = strdup( cargv[2] );
-                                (void) dn_normalize( dn );
-                                charray_add( &be->be_suffixAlias, dn );
+                                                               alias = ch_strdup( cargv[1] );
+                                (void) dn_normalize( alias );
+
+                                aliased_dn = ch_strdup( cargv[2] );
+                                (void) dn_normalize( aliased_dn );
+
+
+                                                               if ( strcasecmp( alias, aliased_dn) == 0 ) {
+                                       Debug( LDAP_DEBUG_ANY,
+"%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
+                                    fname, lineno, alias );
+                                                               } else {
+                                       (void) dn_normalize_case( alias );
+                                       (void) dn_normalize_case( aliased_dn );
+                                       charray_add( &be->be_suffixAlias, alias );
+                                       charray_add( &be->be_suffixAlias, aliased_dn );
+                                                               }
+
+                                                               free(alias);
+                                                               free(aliased_dn);
                         }
 
                /* set max deref depth */
@@ -174,7 +235,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
                    "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be == NULL ) {
                                Debug( LDAP_DEBUG_ANY,
@@ -191,16 +252,15 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
                    "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be == NULL ) {
                                Debug( LDAP_DEBUG_ANY,
 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
                                    fname, lineno, 0 );
                        } else {
-                               dn = strdup( cargv[1] );
-                               (void) dn_normalize( dn );
-                               be->be_rootdn = dn;
+                               be->be_root_dn = ch_strdup( cargv[1] );
+                               be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
                        }
 
                /* set super-secret magic database password */
@@ -209,14 +269,14 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
            "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be == NULL ) {
                                Debug( LDAP_DEBUG_ANY,
 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
                                    fname, lineno, 0 );
                        } else {
-                               be->be_rootpw = strdup( cargv[1] );
+                               be->be_root_pw = ch_strdup( cargv[1] );
                        }
 
                /* make this database read-only */
@@ -225,7 +285,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
            "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be == NULL ) {
                                Debug( LDAP_DEBUG_ANY,
@@ -245,13 +305,42 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
                    "%s: line %d: missing URL in \"referral <URL>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
-                       default_referral = (char *) malloc( strlen( cargv[1] )
+                       default_referral = (char *) ch_malloc( strlen( cargv[1] )
                            + sizeof("Referral:\n") + 1 );
                        strcpy( default_referral, "Referral:\n" );
                        strcat( default_referral, cargv[1] );
 
+               /* specify locale */
+               } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
+#ifdef HAVE_LOCALE_H
+                       char *locale;
+                       if ( cargc < 2 ) {
+                               Debug( LDAP_DEBUG_ANY,
+       "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
+                                      fname, lineno, 0 );
+                               return( 1 );
+                       }
+
+                       locale = (strcasecmp(   cargv[1], "on"  ) == 0 ? ""
+                                 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
+                                 : ch_strdup( cargv[1] )                    );
+
+                       if ( setlocale( LC_CTYPE, locale ) == 0 ) {
+                               Debug( LDAP_DEBUG_ANY,
+                                      (*locale
+                                       ? "%s: line %d: bad locale \"%s\"\n"
+                                       : "%s: line %d: bad locale\n"),
+                                      fname, lineno, locale );
+                               return( 1 );
+                       }
+#else
+                       Debug( LDAP_DEBUG_ANY,
+                              "%s: line %d: \"locale\" unsupported\n",
+                              fname, lineno, 0 );
+                       return( 1 );
+#endif
                /* specify an objectclass */
                } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
                        parse_oc( be, fname, lineno, cargc, cargv );
@@ -267,7 +356,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
     "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( strcasecmp( cargv[1], "on" ) == 0 ) {
                                global_schemacheck = 1;
@@ -285,7 +374,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
            "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be == NULL ) {
                                if ( (global_default_access =
@@ -293,7 +382,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                        Debug( LDAP_DEBUG_ANY,
 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
                                            fname, lineno, cargv[1] );
-                                       exit( 1 );
+                                       return( 1 );
                                }
                        } else {
                                if ( (be->be_dfltaccess =
@@ -301,7 +390,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                        Debug( LDAP_DEBUG_ANY,
 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
                                            fname, lineno, cargv[1] );
-                                       exit( 1 );
+                                       return( 1 );
                                }
                        }
 
@@ -311,7 +400,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
                    "%s: line %d: missing level in \"loglevel <level>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        ldap_syslog = atoi( cargv[1] );
 
@@ -321,7 +410,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
            "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be == NULL ) {
                                Debug( LDAP_DEBUG_ANY,
@@ -332,7 +421,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                        if ( strncasecmp( cargv[i], "host=", 5 )
                                            == 0 ) {
                                                charray_add( &be->be_replica,
-                                                   strdup( cargv[i] + 5 ) );
+                                                   ch_strdup( cargv[i] + 5 ) );
                                                break;
                                        }
                                }
@@ -349,15 +438,15 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
                    "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be == NULL ) {
                                Debug( LDAP_DEBUG_ANY,
 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
                                    fname, lineno, 0 );
                        } else {
-                               be->be_updatedn = strdup( cargv[1] );
-                               (void) dn_normalize( be->be_updatedn );
+                               be->be_update_ndn = ch_strdup( cargv[1] );
+                               (void) dn_normalize_case( be->be_update_ndn );
                        }
 
                /* replication log file to which changes are appended */
@@ -366,12 +455,12 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
            "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( be ) {
-                               be->be_replogfile = strdup( cargv[1] );
+                               be->be_replogfile = ch_strdup( cargv[1] );
                        } else {
-                               replogfile = strdup( cargv[1] );
+                               replogfile = ch_strdup( cargv[1] );
                        }
 
                /* maintain lastmodified{by,time} attributes */
@@ -380,7 +469,7 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
            "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
                        if ( strcasecmp( cargv[1], "on" ) == 0 ) {
                                if ( be )
@@ -400,12 +489,15 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
     "%s: line %d: missing filename in \"include <filename>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
-                       savefname = strdup( cargv[1] );
+                       savefname = ch_strdup( cargv[1] );
                        savelineno = lineno;
-                       read_config( savefname, bep, NULL );
-                       be = *bep;
+
+                       if ( read_config( savefname ) != 0 ) {
+                               return( 1 );
+                       }
+
                        free( savefname );
                        lineno = savelineno - 1;
 
@@ -415,30 +507,48 @@ read_config( char *fname, Backend **bep, FILE *pfp )
                                Debug( LDAP_DEBUG_ANY,
            "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
                                    fname, lineno, 0 );
-                               exit( 1 );
+                               return( 1 );
                        }
-                       ldap_srvtab = strdup( cargv[1] );
+                       ldap_srvtab = ch_strdup( cargv[1] );
 
-               /* pass anything else to the current backend config routine */
+               /* pass anything else to the current backend info/db config routine */
                } else {
-                       if ( be == NULL ) {
-                               Debug( LDAP_DEBUG_ANY,
-"%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
-                                   fname, lineno, cargv[0] );
-                       } else if ( be->be_config == NULL ) {
+                       if ( bi != NULL ) {
+                               if ( bi->bi_config == 0 ) {
+                                       Debug( LDAP_DEBUG_ANY,
+"%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
+                                               fname, lineno, cargv[0] );
+                               } else {
+                                       if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
+                                               != 0 )
+                                       {
+                                               return( 1 );
+                                       }
+                               }
+                       } else if ( be != NULL ) {
+                               if ( be->be_config == 0 ) {
+                                       Debug( LDAP_DEBUG_ANY,
+"%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
+                                       fname, lineno, cargv[0] );
+                               } else {
+                                       if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
+                                               != 0 )
+                                       {
+                                               return( 1 );
+                                       }
+                               }
+                       } else {
                                Debug( LDAP_DEBUG_ANY,
-"%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
+"%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
                                    fname, lineno, cargv[0] );
-                       } else {
-                               (*be->be_config)( be, fname, lineno, cargc,
-                                   cargv );
                        }
                }
        }
        fclose( fp );
+       return( 0 );
 }
 
-static void
+static int
 fp_parse_line(
     char       *line,
     int                *argcp,
@@ -453,11 +563,12 @@ fp_parse_line(
                if ( *argcp == MAXARGS ) {
                        Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
                            MAXARGS, 0, 0 );
-                       exit( 1 );
+                       return( 1 );
                }
                argv[(*argcp)++] = token;
        }
        argv[*argcp] = NULL;
+       return 0;
 }
 
 static char *
@@ -488,11 +599,14 @@ strtok_quote( char *line, char *sep )
                        } else {
                                inquote = 1;
                        }
-                       strcpy( next, next + 1 );
+                       SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
                        break;
 
                case '\\':
-                       strcpy( next, next + 1 );
+                       if ( next[1] )
+                               SAFEMEMCPY( next,
+                                           next + 1, strlen( next + 1 ) + 1 );
+                       next++;         /* dont parse the escaped character */
                        break;
 
                default:
@@ -544,7 +658,7 @@ fp_getline( FILE *fp, int *lineno )
                if ( (p = strchr( buf, '\n' )) != NULL ) {
                        *p = '\0';
                }
-               if ( ! isspace( buf[0] ) ) {
+               if ( ! isspace( (unsigned char) buf[0] ) ) {
                        return( line );
                }