1 /* config.c - configuration file handling routines */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
15 #include <ac/string.h>
17 #include <ac/socket.h>
25 * defaults for various global variables
27 int defsize = SLAPD_DEFAULT_SIZELIMIT;
28 int deftime = SLAPD_DEFAULT_TIMELIMIT;
29 AccessControl *global_acl = NULL;
30 slap_access_t global_default_access = ACL_READ;
31 int global_readonly = 0;
33 int global_lastmod = ON;
34 int global_idletimeout = 0;
35 char *global_realm = NULL;
36 char *ldap_srvtab = "";
37 char *default_passwd_hash;
39 char *slapd_pid_file = NULL;
40 char *slapd_args_file = NULL;
42 static char *fp_getline(FILE *fp, int *lineno);
43 static void fp_getline_init(int *lineno);
44 static int fp_parse_line(char *line, int *argcp, char **argv);
46 static char *strtok_quote(char *line, char *sep);
49 read_config( const char *fname )
52 char *line, *savefname, *saveline;
53 int cargc, savelineno;
59 struct berval *vals[2];
62 static BackendInfo *bi = NULL;
63 static BackendDB *be = NULL;
68 if ( (fp = fopen( fname, "r" )) == NULL ) {
70 Debug( LDAP_DEBUG_ANY,
71 "could not open config file \"%s\" - absolute path?\n",
77 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
79 if ( schema_init( ) != 0 ) {
80 Debug( LDAP_DEBUG_ANY,
81 "error initializing the schema\n",
86 fp_getline_init( &lineno );
88 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
89 /* skip comments and blank lines */
90 if ( line[0] == '#' || line[0] == '\0' ) {
94 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
96 /* fp_parse_line is destructive, we save a copy */
97 saveline = ch_strdup( line );
99 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
104 Debug( LDAP_DEBUG_ANY,
105 "%s: line %d: bad config line (ignored)\n",
110 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
112 Debug( LDAP_DEBUG_ANY,
113 "%s: line %d: missing type in \"backend <type>\" line\n",
119 Debug( LDAP_DEBUG_ANY,
120 "%s: line %d: backend line must appear before any database definition\n",
125 bi = backend_info( cargv[1] );
128 Debug( LDAP_DEBUG_ANY,
129 "backend %s initialization failed.n",
134 /* start of a new database definition */
135 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
137 Debug( LDAP_DEBUG_ANY,
138 "%s: line %d: missing type in \"database <type>\" line\n",
144 be = backend_db_init( cargv[1] );
147 Debug( LDAP_DEBUG_ANY,
148 "database %s initialization failed.n",
153 /* get pid file name */
154 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
156 Debug( LDAP_DEBUG_ANY,
157 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
162 slapd_pid_file = ch_strdup( cargv[1] );
164 /* get args file name */
165 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
167 Debug( LDAP_DEBUG_ANY,
168 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
173 slapd_args_file = ch_strdup( cargv[1] );
175 /* default password hash */
176 } else if ( strcasecmp( cargv[0], "password-hash" ) == 0 ) {
178 Debug( LDAP_DEBUG_ANY,
179 "%s: line %d: missing realm in \"password-hash <hash>\" line\n",
183 if ( default_passwd_hash != NULL ) {
184 Debug( LDAP_DEBUG_ANY,
185 "%s: line %d: already set default password_hash!\n",
190 default_passwd_hash = ch_strdup( cargv[1] );
193 /* set DIGEST realm */
194 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
196 Debug( LDAP_DEBUG_ANY,
197 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
202 be->be_realm = ch_strdup( cargv[1] );
204 } else if ( global_realm != NULL ) {
205 Debug( LDAP_DEBUG_ANY,
206 "%s: line %d: already set global realm!\n",
211 global_realm = ch_strdup( cargv[1] );
215 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
217 Debug( LDAP_DEBUG_ANY,
218 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
223 defsize = atoi( cargv[1] );
225 be->be_sizelimit = atoi( cargv[1] );
229 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
231 Debug( LDAP_DEBUG_ANY,
232 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
237 deftime = atoi( cargv[1] );
239 be->be_timelimit = atoi( cargv[1] );
242 /* set database suffix */
243 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
246 Debug( LDAP_DEBUG_ANY,
247 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
250 } else if ( cargc > 2 ) {
251 Debug( LDAP_DEBUG_ANY,
252 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
253 fname, lineno, cargv[1] );
256 Debug( LDAP_DEBUG_ANY,
257 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
259 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
260 Debug( LDAP_DEBUG_ANY,
261 "%s: line %d: suffix already served by this backend (ignored)\n",
263 } else if ( tmp_be != NULL ) {
264 Debug( LDAP_DEBUG_ANY,
265 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
266 fname, lineno, tmp_be->be_suffix[0] );
268 char *dn = ch_strdup( cargv[1] );
269 (void) dn_validate( dn );
270 charray_add( &be->be_suffix, dn );
271 (void) ldap_pvt_str2upper( dn );
272 charray_add( &be->be_nsuffix, dn );
276 /* set database suffixAlias */
277 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
280 Debug( LDAP_DEBUG_ANY,
281 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
284 } else if ( cargc < 3 ) {
285 Debug( LDAP_DEBUG_ANY,
286 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
289 } else if ( cargc > 3 ) {
290 Debug( LDAP_DEBUG_ANY,
291 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
296 Debug( LDAP_DEBUG_ANY,
297 "%s: line %d: suffixAlias line"
298 " must appear inside a database definition (ignored)\n",
300 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
301 Debug( LDAP_DEBUG_ANY,
302 "%s: line %d: suffixAlias served by"
303 " a preceeding backend \"%s\" (ignored)\n",
304 fname, lineno, tmp_be->be_suffix[0] );
306 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
307 Debug( LDAP_DEBUG_ANY,
308 "%s: line %d: suffixAlias derefs to differnet backend"
309 " a preceeding backend \"%s\" (ignored)\n",
310 fname, lineno, tmp_be->be_suffix[0] );
313 char *alias, *aliased_dn;
315 alias = ch_strdup( cargv[1] );
316 (void) dn_normalize( alias );
318 aliased_dn = ch_strdup( cargv[2] );
319 (void) dn_normalize( aliased_dn );
321 charray_add( &be->be_suffixAlias, alias );
322 charray_add( &be->be_suffixAlias, aliased_dn );
328 /* set max deref depth */
329 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
332 Debug( LDAP_DEBUG_ANY,
333 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
338 Debug( LDAP_DEBUG_ANY,
339 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
341 } else if ((i = atoi(cargv[1])) < 0) {
342 Debug( LDAP_DEBUG_ANY,
343 "%s: line %d: depth must be positive (ignored)\n",
347 be->be_max_deref_depth = i;
351 /* set magic "root" dn for this database */
352 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
354 Debug( LDAP_DEBUG_ANY,
355 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
360 Debug( LDAP_DEBUG_ANY,
361 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
364 be->be_root_dn = ch_strdup( cargv[1] );
365 be->be_root_ndn = ch_strdup( cargv[1] );
367 if( dn_normalize( be->be_root_ndn ) == NULL ) {
368 free( be->be_root_dn );
369 free( be->be_root_ndn );
370 Debug( LDAP_DEBUG_ANY,
371 "%s: line %d: rootdn DN is invalid\n",
377 /* set super-secret magic database password */
378 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
380 Debug( LDAP_DEBUG_ANY,
381 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
386 Debug( LDAP_DEBUG_ANY,
387 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
390 be->be_root_pw.bv_val = ch_strdup( cargv[1] );
391 be->be_root_pw.bv_len = strlen( be->be_root_pw.bv_val );
394 /* make this database read-only */
395 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
397 Debug( LDAP_DEBUG_ANY,
398 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
403 global_readonly = (strcasecmp( cargv[1], "on" ) == 0);
405 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
412 /* where to send clients when we don't hold it */
413 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
415 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: missing URL in \"referral <URL>\" line\n",
421 vals[0]->bv_val = cargv[1];
422 vals[0]->bv_len = strlen( vals[0]->bv_val );
423 value_add( &default_referral, vals );
426 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
430 Debug( LDAP_DEBUG_ANY,
431 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
436 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
437 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
438 : ch_strdup( cargv[1] ) );
440 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
441 Debug( LDAP_DEBUG_ANY,
443 ? "%s: line %d: bad locale \"%s\"\n"
444 : "%s: line %d: bad locale\n"),
445 fname, lineno, locale );
449 Debug( LDAP_DEBUG_ANY,
450 "%s: line %d: \"locale\" unsupported\n",
454 /* specify an Object Identifier macro */
455 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
456 parse_oidm( fname, lineno, cargc, cargv );
458 /* specify an objectclass */
459 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
460 if ( *cargv[1] == '(' ) {
462 p = strchr(saveline,'(');
463 parse_oc( fname, lineno, p, cargv );
465 #ifdef SLAPD_SCHEMA_NOT_COMPAT
466 Debug( LDAP_DEBUG_ANY,
467 "%s: line %d: old objectclass format not supported.\n",
470 parse_oc_old( be, fname, lineno, cargc, cargv );
474 /* specify an attribute type */
475 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
476 || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
478 if ( *cargv[1] == '(' ) {
480 p = strchr(saveline,'(');
481 parse_at( fname, lineno, p, cargv );
483 #ifdef SLAPD_SCHEMA_NOT_COMPAT
484 Debug( LDAP_DEBUG_ANY,
485 "%s: line %d: old attribute type format not supported.\n",
488 at_config( fname, lineno, cargc - 1,
493 /* turn on/off schema checking */
494 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
496 Debug( LDAP_DEBUG_ANY,
497 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
501 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
502 global_schemacheck = 0;
504 global_schemacheck = 1;
507 /* specify access control info */
508 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
509 parse_acl( be, fname, lineno, cargc, cargv );
511 /* specify default access control info */
512 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
513 slap_access_t access;
516 Debug( LDAP_DEBUG_ANY,
517 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
522 access = str2access( cargv[1] );
524 if ( access == ACL_INVALID_ACCESS ) {
525 Debug( LDAP_DEBUG_ANY,
526 "%s: line %d: bad access level \"%s\", "
527 "expecting none|auth|compare|search|read|write\n",
528 fname, lineno, cargv[1] );
533 global_default_access = access;
535 be->be_dfltaccess = access;
538 /* debug level to log things to syslog */
539 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
541 Debug( LDAP_DEBUG_ANY,
542 "%s: line %d: missing level in \"loglevel <level>\" line\n",
546 ldap_syslog = atoi( cargv[1] );
548 /* list of replicas of the data in this backend (master only) */
549 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
551 Debug( LDAP_DEBUG_ANY,
552 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
557 Debug( LDAP_DEBUG_ANY,
558 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
561 for ( i = 1; i < cargc; i++ ) {
562 if ( strncasecmp( cargv[i], "host=", 5 )
564 charray_add( &be->be_replica,
570 Debug( LDAP_DEBUG_ANY,
571 "%s: line %d: missing host in \"replica\" line (ignored)\n",
576 /* dn of master entity allowed to write to replica */
577 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
579 Debug( LDAP_DEBUG_ANY,
580 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
585 Debug( LDAP_DEBUG_ANY,
586 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
589 be->be_update_ndn = ch_strdup( cargv[1] );
590 if( dn_normalize( be->be_update_ndn ) == NULL ) {
591 Debug( LDAP_DEBUG_ANY,
592 "%s: line %d: updatedn DN is invalid\n",
598 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
600 Debug( LDAP_DEBUG_ANY,
601 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
606 Debug( LDAP_DEBUG_ANY,
607 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
609 } else if ( be->be_update_ndn == NULL ) {
610 Debug( LDAP_DEBUG_ANY,
611 "%s: line %d: updateref line must after updatedn (ignored)\n",
614 vals[0]->bv_val = cargv[1];
615 vals[0]->bv_len = strlen( vals[0]->bv_val );
616 value_add( &be->be_update_refs, vals );
619 /* replication log file to which changes are appended */
620 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
622 Debug( LDAP_DEBUG_ANY,
623 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
628 be->be_replogfile = ch_strdup( cargv[1] );
630 replogfile = ch_strdup( cargv[1] );
633 /* maintain lastmodified{by,time} attributes */
634 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
636 Debug( LDAP_DEBUG_ANY,
637 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
641 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
648 be->be_lastmod = OFF;
650 global_lastmod = OFF;
653 /* set idle timeout value */
654 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
657 Debug( LDAP_DEBUG_ANY,
658 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
663 i = atoi( cargv[1] );
666 Debug( LDAP_DEBUG_ANY,
667 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
672 global_idletimeout = i;
674 /* include another config file */
675 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
677 Debug( LDAP_DEBUG_ANY,
678 "%s: line %d: missing filename in \"include <filename>\" line\n",
682 savefname = ch_strdup( cargv[1] );
685 if ( read_config( savefname ) != 0 ) {
690 lineno = savelineno - 1;
692 /* location of kerberos srvtab file */
693 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
695 Debug( LDAP_DEBUG_ANY,
696 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
700 ldap_srvtab = ch_strdup( cargv[1] );
703 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
705 Debug( LDAP_DEBUG_ANY,
706 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
708 exit( EXIT_FAILURE );
710 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
711 Debug( LDAP_DEBUG_ANY,
712 "%s: line %d: failed to load or initialize module %s\n",
713 fname, lineno, cargv[1]);
714 exit( EXIT_FAILURE );
716 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
718 Debug( LDAP_DEBUG_ANY,
719 "%s: line %d: missing path in \"modulepath <path>\" line\n",
721 exit( EXIT_FAILURE );
723 if (module_path( cargv[1] )) {
724 Debug( LDAP_DEBUG_ANY,
725 "%s: line %d: failed to set module search path to %s\n",
726 fname, lineno, cargv[1]);
727 exit( EXIT_FAILURE );
730 #endif /*SLAPD_MODULES*/
733 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
734 rc = ldap_pvt_tls_set_option( NULL,
735 LDAP_OPT_X_TLS_PROTOCOL,
740 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
741 rc = ldap_pvt_tls_set_option( NULL,
742 LDAP_OPT_X_TLS_CIPHER_SUITE,
747 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
748 rc = ldap_pvt_tls_set_option( NULL,
749 LDAP_OPT_X_TLS_CERTFILE,
754 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
755 rc = ldap_pvt_tls_set_option( NULL,
756 LDAP_OPT_X_TLS_KEYFILE,
761 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
762 rc = ldap_pvt_tls_set_option( NULL,
763 LDAP_OPT_X_TLS_CACERTDIR,
768 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
769 rc = ldap_pvt_tls_set_option( NULL,
770 LDAP_OPT_X_TLS_CACERTFILE,
774 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
775 rc = ldap_pvt_tls_set_option( NULL,
776 LDAP_OPT_X_TLS_REQUIRE_CERT,
783 /* pass anything else to the current backend info/db config routine */
786 if ( bi->bi_config == 0 ) {
787 Debug( LDAP_DEBUG_ANY,
788 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
789 fname, lineno, cargv[0] );
791 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
797 } else if ( be != NULL ) {
798 if ( be->be_config == 0 ) {
799 Debug( LDAP_DEBUG_ANY,
800 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
801 fname, lineno, cargv[0] );
803 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
810 Debug( LDAP_DEBUG_ANY,
811 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
812 fname, lineno, cargv[0] );
831 for ( token = strtok_quote( line, " \t" ); token != NULL;
832 token = strtok_quote( NULL, " \t" ) ) {
833 if ( *argcp == MAXARGS ) {
834 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
838 argv[(*argcp)++] = token;
845 strtok_quote( char *line, char *sep )
851 if ( line != NULL ) {
854 while ( *next && strchr( sep, *next ) ) {
858 if ( *next == '\0' ) {
864 for ( inquote = 0; *next; ) {
872 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
878 next + 1, strlen( next + 1 ) + 1 );
879 next++; /* dont parse the escaped character */
884 if ( strchr( sep, *next ) != NULL ) {
897 static char buf[BUFSIZ];
899 static int lmax, lcur;
901 #define CATLINE( buf ) { \
903 len = strlen( buf ); \
904 while ( lcur + len + 1 > lmax ) { \
906 line = (char *) ch_realloc( line, lmax ); \
908 strcpy( line + lcur, buf ); \
913 fp_getline( FILE *fp, int *lineno )
921 /* hack attack - keeps us from having to keep a stack of bufs... */
922 if ( strncasecmp( line, "include", 7 ) == 0 ) {
927 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
928 if ( (p = strchr( buf, '\n' )) != NULL ) {
931 if ( ! isspace( (unsigned char) buf[0] ) ) {
940 return( line[0] ? line : NULL );
944 fp_getline_init( int *lineno )