1 /* config.c - configuration file handling routines */
4 * Copyright 1998-1999 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 parse_oc_old( be, fname, lineno, cargc, cargv );
468 /* specify an attribute type */
469 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
470 || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
472 if ( *cargv[1] == '(' ) {
474 p = strchr(saveline,'(');
475 parse_at( fname, lineno, p, cargv );
477 attr_syntax_config( fname, lineno, cargc - 1,
481 /* turn on/off schema checking */
482 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
484 Debug( LDAP_DEBUG_ANY,
485 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
489 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
490 global_schemacheck = 0;
492 global_schemacheck = 1;
495 /* specify access control info */
496 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
497 parse_acl( be, fname, lineno, cargc, cargv );
499 /* specify default access control info */
500 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
501 slap_access_t access;
504 Debug( LDAP_DEBUG_ANY,
505 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
510 access = str2access( cargv[1] );
512 if ( access == ACL_INVALID_ACCESS ) {
513 Debug( LDAP_DEBUG_ANY,
514 "%s: line %d: bad access level \"%s\", "
515 "expecting none|auth|compare|search|read|write\n",
516 fname, lineno, cargv[1] );
521 global_default_access = access;
523 be->be_dfltaccess = access;
526 /* debug level to log things to syslog */
527 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
529 Debug( LDAP_DEBUG_ANY,
530 "%s: line %d: missing level in \"loglevel <level>\" line\n",
534 ldap_syslog = atoi( cargv[1] );
536 /* list of replicas of the data in this backend (master only) */
537 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
539 Debug( LDAP_DEBUG_ANY,
540 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
545 Debug( LDAP_DEBUG_ANY,
546 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
549 for ( i = 1; i < cargc; i++ ) {
550 if ( strncasecmp( cargv[i], "host=", 5 )
552 charray_add( &be->be_replica,
558 Debug( LDAP_DEBUG_ANY,
559 "%s: line %d: missing host in \"replica\" line (ignored)\n",
564 /* dn of master entity allowed to write to replica */
565 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
567 Debug( LDAP_DEBUG_ANY,
568 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
573 Debug( LDAP_DEBUG_ANY,
574 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
577 be->be_update_ndn = ch_strdup( cargv[1] );
578 if( dn_normalize( be->be_update_ndn ) == NULL ) {
579 Debug( LDAP_DEBUG_ANY,
580 "%s: line %d: updatedn DN is invalid\n",
586 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
588 Debug( LDAP_DEBUG_ANY,
589 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
594 Debug( LDAP_DEBUG_ANY,
595 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
597 } else if ( be->be_update_ndn == NULL ) {
598 Debug( LDAP_DEBUG_ANY,
599 "%s: line %d: updateref line must after updatedn (ignored)\n",
602 vals[0]->bv_val = cargv[1];
603 vals[0]->bv_len = strlen( vals[0]->bv_val );
604 value_add( &be->be_update_refs, vals );
607 /* replication log file to which changes are appended */
608 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
610 Debug( LDAP_DEBUG_ANY,
611 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
616 be->be_replogfile = ch_strdup( cargv[1] );
618 replogfile = ch_strdup( cargv[1] );
621 /* maintain lastmodified{by,time} attributes */
622 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
624 Debug( LDAP_DEBUG_ANY,
625 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
629 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
636 be->be_lastmod = OFF;
638 global_lastmod = OFF;
641 /* set idle timeout value */
642 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
645 Debug( LDAP_DEBUG_ANY,
646 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
651 i = atoi( cargv[1] );
654 Debug( LDAP_DEBUG_ANY,
655 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
660 global_idletimeout = i;
662 /* include another config file */
663 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
665 Debug( LDAP_DEBUG_ANY,
666 "%s: line %d: missing filename in \"include <filename>\" line\n",
670 savefname = ch_strdup( cargv[1] );
673 if ( read_config( savefname ) != 0 ) {
678 lineno = savelineno - 1;
680 /* location of kerberos srvtab file */
681 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
683 Debug( LDAP_DEBUG_ANY,
684 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
688 ldap_srvtab = ch_strdup( cargv[1] );
691 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
693 Debug( LDAP_DEBUG_ANY,
694 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
696 exit( EXIT_FAILURE );
698 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
699 Debug( LDAP_DEBUG_ANY,
700 "%s: line %d: failed to load or initialize module %s\n",
701 fname, lineno, cargv[1]);
702 exit( EXIT_FAILURE );
704 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
706 Debug( LDAP_DEBUG_ANY,
707 "%s: line %d: missing path in \"modulepath <path>\" line\n",
709 exit( EXIT_FAILURE );
711 if (module_path( cargv[1] )) {
712 Debug( LDAP_DEBUG_ANY,
713 "%s: line %d: failed to set module search path to %s\n",
714 fname, lineno, cargv[1]);
715 exit( EXIT_FAILURE );
718 #endif /*SLAPD_MODULES*/
721 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
722 rc = ldap_pvt_tls_set_option( NULL,
723 LDAP_OPT_X_TLS_PROTOCOL,
728 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
729 rc = ldap_pvt_tls_set_option( NULL,
730 LDAP_OPT_X_TLS_CIPHER_SUITE,
735 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
736 rc = ldap_pvt_tls_set_option( NULL,
737 LDAP_OPT_X_TLS_CERTFILE,
742 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
743 rc = ldap_pvt_tls_set_option( NULL,
744 LDAP_OPT_X_TLS_KEYFILE,
749 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
750 rc = ldap_pvt_tls_set_option( NULL,
751 LDAP_OPT_X_TLS_CACERTDIR,
756 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
757 rc = ldap_pvt_tls_set_option( NULL,
758 LDAP_OPT_X_TLS_CACERTFILE,
762 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
763 rc = ldap_pvt_tls_set_option( NULL,
764 LDAP_OPT_X_TLS_REQUIRE_CERT,
771 /* pass anything else to the current backend info/db config routine */
774 if ( bi->bi_config == 0 ) {
775 Debug( LDAP_DEBUG_ANY,
776 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
777 fname, lineno, cargv[0] );
779 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
785 } else if ( be != NULL ) {
786 if ( be->be_config == 0 ) {
787 Debug( LDAP_DEBUG_ANY,
788 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
789 fname, lineno, cargv[0] );
791 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
798 Debug( LDAP_DEBUG_ANY,
799 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
800 fname, lineno, cargv[0] );
819 for ( token = strtok_quote( line, " \t" ); token != NULL;
820 token = strtok_quote( NULL, " \t" ) ) {
821 if ( *argcp == MAXARGS ) {
822 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
826 argv[(*argcp)++] = token;
833 strtok_quote( char *line, char *sep )
839 if ( line != NULL ) {
842 while ( *next && strchr( sep, *next ) ) {
846 if ( *next == '\0' ) {
852 for ( inquote = 0; *next; ) {
860 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
866 next + 1, strlen( next + 1 ) + 1 );
867 next++; /* dont parse the escaped character */
872 if ( strchr( sep, *next ) != NULL ) {
885 static char buf[BUFSIZ];
887 static int lmax, lcur;
889 #define CATLINE( buf ) { \
891 len = strlen( buf ); \
892 while ( lcur + len + 1 > lmax ) { \
894 line = (char *) ch_realloc( line, lmax ); \
896 strcpy( line + lcur, buf ); \
901 fp_getline( FILE *fp, int *lineno )
909 /* hack attack - keeps us from having to keep a stack of bufs... */
910 if ( strncasecmp( line, "include", 7 ) == 0 ) {
915 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
916 if ( (p = strchr( buf, '\n' )) != NULL ) {
919 if ( ! isspace( (unsigned char) buf[0] ) ) {
928 return( line[0] ? line : NULL );
932 fp_getline_init( int *lineno )