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 fp_getline_init( &lineno );
81 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
82 /* skip comments and blank lines */
83 if ( line[0] == '#' || line[0] == '\0' ) {
87 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
89 /* fp_parse_line is destructive, we save a copy */
90 saveline = ch_strdup( line );
92 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
97 Debug( LDAP_DEBUG_ANY,
98 "%s: line %d: bad config line (ignored)\n",
103 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
105 Debug( LDAP_DEBUG_ANY,
106 "%s: line %d: missing type in \"backend <type>\" line\n",
112 Debug( LDAP_DEBUG_ANY,
113 "%s: line %d: backend line must appear before any database definition\n",
118 bi = backend_info( cargv[1] );
121 Debug( LDAP_DEBUG_ANY,
122 "backend %s initialization failed.n",
127 /* start of a new database definition */
128 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
130 Debug( LDAP_DEBUG_ANY,
131 "%s: line %d: missing type in \"database <type>\" line\n",
137 be = backend_db_init( cargv[1] );
140 Debug( LDAP_DEBUG_ANY,
141 "database %s initialization failed.n",
146 /* set thread concurrency */
147 } else if ( strcasecmp( cargv[0], "concurrency" ) == 0 ) {
150 Debug( LDAP_DEBUG_ANY,
151 "%s: line %d: missing level in \"concurrency <level>\" line\n",
156 c = atoi( cargv[1] );
159 Debug( LDAP_DEBUG_ANY,
160 "%s: line %d: invalid level (%d) in \"concurrency <level>\" line\n",
165 ldap_pvt_thread_set_concurrency( c );
167 /* get pid file name */
168 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
170 Debug( LDAP_DEBUG_ANY,
171 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
176 slapd_pid_file = ch_strdup( cargv[1] );
178 /* get args file name */
179 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
181 Debug( LDAP_DEBUG_ANY,
182 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
187 slapd_args_file = ch_strdup( cargv[1] );
189 /* default password hash */
190 } else if ( strcasecmp( cargv[0], "password-hash" ) == 0 ) {
192 Debug( LDAP_DEBUG_ANY,
193 "%s: line %d: missing realm in \"password-hash <hash>\" line\n",
197 if ( default_passwd_hash != NULL ) {
198 Debug( LDAP_DEBUG_ANY,
199 "%s: line %d: already set default password_hash!\n",
204 default_passwd_hash = ch_strdup( cargv[1] );
207 /* set DIGEST realm */
208 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
210 Debug( LDAP_DEBUG_ANY,
211 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
216 be->be_realm = ch_strdup( cargv[1] );
218 } else if ( global_realm != NULL ) {
219 Debug( LDAP_DEBUG_ANY,
220 "%s: line %d: already set global realm!\n",
225 global_realm = ch_strdup( cargv[1] );
229 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
231 Debug( LDAP_DEBUG_ANY,
232 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
237 defsize = atoi( cargv[1] );
239 be->be_sizelimit = atoi( cargv[1] );
243 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
245 Debug( LDAP_DEBUG_ANY,
246 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
251 deftime = atoi( cargv[1] );
253 be->be_timelimit = atoi( cargv[1] );
256 /* set database suffix */
257 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
260 Debug( LDAP_DEBUG_ANY,
261 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
264 } else if ( cargc > 2 ) {
265 Debug( LDAP_DEBUG_ANY,
266 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
267 fname, lineno, cargv[1] );
270 Debug( LDAP_DEBUG_ANY,
271 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
273 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
274 Debug( LDAP_DEBUG_ANY,
275 "%s: line %d: suffix already served by this backend (ignored)\n",
277 } else if ( tmp_be != NULL ) {
278 Debug( LDAP_DEBUG_ANY,
279 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
280 fname, lineno, tmp_be->be_suffix[0] );
282 char *dn = ch_strdup( cargv[1] );
283 (void) dn_validate( dn );
284 charray_add( &be->be_suffix, dn );
285 (void) ldap_pvt_str2upper( dn );
286 charray_add( &be->be_nsuffix, dn );
290 /* set database suffixAlias */
291 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
294 Debug( LDAP_DEBUG_ANY,
295 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
298 } else if ( cargc < 3 ) {
299 Debug( LDAP_DEBUG_ANY,
300 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
303 } else if ( cargc > 3 ) {
304 Debug( LDAP_DEBUG_ANY,
305 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
310 Debug( LDAP_DEBUG_ANY,
311 "%s: line %d: suffixAlias line"
312 " must appear inside a database definition (ignored)\n",
314 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
315 Debug( LDAP_DEBUG_ANY,
316 "%s: line %d: suffixAlias served by"
317 " a preceeding backend \"%s\" (ignored)\n",
318 fname, lineno, tmp_be->be_suffix[0] );
320 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
321 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: suffixAlias derefs to differnet backend"
323 " a preceeding backend \"%s\" (ignored)\n",
324 fname, lineno, tmp_be->be_suffix[0] );
327 char *alias, *aliased_dn;
329 alias = ch_strdup( cargv[1] );
330 (void) dn_normalize( alias );
332 aliased_dn = ch_strdup( cargv[2] );
333 (void) dn_normalize( aliased_dn );
335 charray_add( &be->be_suffixAlias, alias );
336 charray_add( &be->be_suffixAlias, aliased_dn );
342 /* set max deref depth */
343 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
346 Debug( LDAP_DEBUG_ANY,
347 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
352 Debug( LDAP_DEBUG_ANY,
353 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
355 } else if ((i = atoi(cargv[1])) < 0) {
356 Debug( LDAP_DEBUG_ANY,
357 "%s: line %d: depth must be positive (ignored)\n",
361 be->be_max_deref_depth = i;
365 /* set magic "root" dn for this database */
366 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
368 Debug( LDAP_DEBUG_ANY,
369 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
374 Debug( LDAP_DEBUG_ANY,
375 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
378 be->be_root_dn = ch_strdup( cargv[1] );
379 be->be_root_ndn = ch_strdup( cargv[1] );
381 if( dn_normalize( be->be_root_ndn ) == NULL ) {
382 free( be->be_root_dn );
383 free( be->be_root_ndn );
384 Debug( LDAP_DEBUG_ANY,
385 "%s: line %d: rootdn DN is invalid\n",
391 /* set super-secret magic database password */
392 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
394 Debug( LDAP_DEBUG_ANY,
395 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
400 Debug( LDAP_DEBUG_ANY,
401 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
404 be->be_root_pw.bv_val = ch_strdup( cargv[1] );
405 be->be_root_pw.bv_len = strlen( be->be_root_pw.bv_val );
408 /* make this database read-only */
409 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
411 Debug( LDAP_DEBUG_ANY,
412 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
417 global_readonly = (strcasecmp( cargv[1], "on" ) == 0);
419 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
426 /* where to send clients when we don't hold it */
427 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
429 Debug( LDAP_DEBUG_ANY,
430 "%s: line %d: missing URL in \"referral <URL>\" line\n",
435 vals[0]->bv_val = cargv[1];
436 vals[0]->bv_len = strlen( vals[0]->bv_val );
437 value_add( &default_referral, vals );
440 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
444 Debug( LDAP_DEBUG_ANY,
445 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
450 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
451 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
452 : ch_strdup( cargv[1] ) );
454 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
455 Debug( LDAP_DEBUG_ANY,
457 ? "%s: line %d: bad locale \"%s\"\n"
458 : "%s: line %d: bad locale\n"),
459 fname, lineno, locale );
463 Debug( LDAP_DEBUG_ANY,
464 "%s: line %d: \"locale\" unsupported\n",
468 /* specify an Object Identifier macro */
469 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
470 parse_oidm( fname, lineno, cargc, cargv );
472 /* specify an objectclass */
473 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
474 if ( *cargv[1] == '(' ) {
476 p = strchr(saveline,'(');
477 parse_oc( fname, lineno, p, cargv );
479 Debug( LDAP_DEBUG_ANY,
480 "%s: line %d: old objectclass format not supported.\n",
484 /* specify an attribute type */
485 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
486 || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
488 if ( *cargv[1] == '(' ) {
490 p = strchr(saveline,'(');
491 parse_at( fname, lineno, p, cargv );
493 Debug( LDAP_DEBUG_ANY,
494 "%s: line %d: old attribute type format not supported.\n",
498 /* turn on/off schema checking */
499 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
501 Debug( LDAP_DEBUG_ANY,
502 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
506 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
507 global_schemacheck = 0;
509 global_schemacheck = 1;
512 /* specify access control info */
513 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
514 parse_acl( be, fname, lineno, cargc, cargv );
516 /* specify default access control info */
517 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
518 slap_access_t access;
521 Debug( LDAP_DEBUG_ANY,
522 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
527 access = str2access( cargv[1] );
529 if ( access == ACL_INVALID_ACCESS ) {
530 Debug( LDAP_DEBUG_ANY,
531 "%s: line %d: bad access level \"%s\", "
532 "expecting none|auth|compare|search|read|write\n",
533 fname, lineno, cargv[1] );
538 global_default_access = access;
540 be->be_dfltaccess = access;
543 /* debug level to log things to syslog */
544 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
546 Debug( LDAP_DEBUG_ANY,
547 "%s: line %d: missing level in \"loglevel <level>\" line\n",
551 ldap_syslog = atoi( cargv[1] );
553 /* list of replicas of the data in this backend (master only) */
554 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
556 Debug( LDAP_DEBUG_ANY,
557 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
562 Debug( LDAP_DEBUG_ANY,
563 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
566 for ( i = 1; i < cargc; i++ ) {
567 if ( strncasecmp( cargv[i], "host=", 5 )
569 charray_add( &be->be_replica,
575 Debug( LDAP_DEBUG_ANY,
576 "%s: line %d: missing host in \"replica\" line (ignored)\n",
581 /* dn of master entity allowed to write to replica */
582 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
584 Debug( LDAP_DEBUG_ANY,
585 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
590 Debug( LDAP_DEBUG_ANY,
591 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
594 be->be_update_ndn = ch_strdup( cargv[1] );
595 if( dn_normalize( be->be_update_ndn ) == NULL ) {
596 Debug( LDAP_DEBUG_ANY,
597 "%s: line %d: updatedn DN is invalid\n",
603 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
605 Debug( LDAP_DEBUG_ANY,
606 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
611 Debug( LDAP_DEBUG_ANY,
612 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
614 } else if ( be->be_update_ndn == NULL ) {
615 Debug( LDAP_DEBUG_ANY,
616 "%s: line %d: updateref line must after updatedn (ignored)\n",
619 vals[0]->bv_val = cargv[1];
620 vals[0]->bv_len = strlen( vals[0]->bv_val );
621 value_add( &be->be_update_refs, vals );
624 /* replication log file to which changes are appended */
625 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
627 Debug( LDAP_DEBUG_ANY,
628 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
633 be->be_replogfile = ch_strdup( cargv[1] );
635 replogfile = ch_strdup( cargv[1] );
638 /* maintain lastmodified{by,time} attributes */
639 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
641 Debug( LDAP_DEBUG_ANY,
642 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
646 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
653 be->be_lastmod = OFF;
655 global_lastmod = OFF;
658 /* set idle timeout value */
659 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
662 Debug( LDAP_DEBUG_ANY,
663 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
668 i = atoi( cargv[1] );
671 Debug( LDAP_DEBUG_ANY,
672 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
677 global_idletimeout = i;
679 /* include another config file */
680 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
682 Debug( LDAP_DEBUG_ANY,
683 "%s: line %d: missing filename in \"include <filename>\" line\n",
687 savefname = ch_strdup( cargv[1] );
690 if ( read_config( savefname ) != 0 ) {
695 lineno = savelineno - 1;
697 /* location of kerberos srvtab file */
698 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
700 Debug( LDAP_DEBUG_ANY,
701 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
705 ldap_srvtab = ch_strdup( cargv[1] );
708 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
710 Debug( LDAP_DEBUG_ANY,
711 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
713 exit( EXIT_FAILURE );
715 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
716 Debug( LDAP_DEBUG_ANY,
717 "%s: line %d: failed to load or initialize module %s\n",
718 fname, lineno, cargv[1]);
719 exit( EXIT_FAILURE );
721 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
723 Debug( LDAP_DEBUG_ANY,
724 "%s: line %d: missing path in \"modulepath <path>\" line\n",
726 exit( EXIT_FAILURE );
728 if (module_path( cargv[1] )) {
729 Debug( LDAP_DEBUG_ANY,
730 "%s: line %d: failed to set module search path to %s\n",
731 fname, lineno, cargv[1]);
732 exit( EXIT_FAILURE );
735 #endif /*SLAPD_MODULES*/
738 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
739 rc = ldap_pvt_tls_set_option( NULL,
740 LDAP_OPT_X_TLS_PROTOCOL,
745 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
746 rc = ldap_pvt_tls_set_option( NULL,
747 LDAP_OPT_X_TLS_CIPHER_SUITE,
752 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
753 rc = ldap_pvt_tls_set_option( NULL,
754 LDAP_OPT_X_TLS_CERTFILE,
759 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
760 rc = ldap_pvt_tls_set_option( NULL,
761 LDAP_OPT_X_TLS_KEYFILE,
766 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
767 rc = ldap_pvt_tls_set_option( NULL,
768 LDAP_OPT_X_TLS_CACERTDIR,
773 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
774 rc = ldap_pvt_tls_set_option( NULL,
775 LDAP_OPT_X_TLS_CACERTFILE,
779 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
780 rc = ldap_pvt_tls_set_option( NULL,
781 LDAP_OPT_X_TLS_REQUIRE_CERT,
788 /* pass anything else to the current backend info/db config routine */
791 if ( bi->bi_config == 0 ) {
792 Debug( LDAP_DEBUG_ANY,
793 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
794 fname, lineno, cargv[0] );
796 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
802 } else if ( be != NULL ) {
803 if ( be->be_config == 0 ) {
804 Debug( LDAP_DEBUG_ANY,
805 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
806 fname, lineno, cargv[0] );
808 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
815 Debug( LDAP_DEBUG_ANY,
816 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
817 fname, lineno, cargv[0] );
836 for ( token = strtok_quote( line, " \t" ); token != NULL;
837 token = strtok_quote( NULL, " \t" ) ) {
838 if ( *argcp == MAXARGS ) {
839 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
843 argv[(*argcp)++] = token;
850 strtok_quote( char *line, char *sep )
856 if ( line != NULL ) {
859 while ( *next && strchr( sep, *next ) ) {
863 if ( *next == '\0' ) {
869 for ( inquote = 0; *next; ) {
877 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
883 next + 1, strlen( next + 1 ) + 1 );
884 next++; /* dont parse the escaped character */
889 if ( strchr( sep, *next ) != NULL ) {
902 static char buf[BUFSIZ];
904 static int lmax, lcur;
906 #define CATLINE( buf ) { \
908 len = strlen( buf ); \
909 while ( lcur + len + 1 > lmax ) { \
911 line = (char *) ch_realloc( line, lmax ); \
913 strcpy( line + lcur, buf ); \
918 fp_getline( FILE *fp, int *lineno )
926 /* hack attack - keeps us from having to keep a stack of bufs... */
927 if ( strncasecmp( line, "include", 7 ) == 0 ) {
932 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
933 if ( (p = strchr( buf, '\n' )) != NULL ) {
936 if ( ! isspace( (unsigned char) buf[0] ) ) {
945 return( line[0] ? line : NULL );
949 fp_getline_init( int *lineno )