1 /* config.c - configuration file handling routines */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
14 #include <ac/string.h>
16 #include <ac/socket.h>
19 #include "ldap_defaults.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 int global_default_access = ACL_READ;
32 int global_lastmod = ON;
33 int global_idletimeout = 0;
34 char *global_realm = NULL;
35 char *ldap_srvtab = "";
37 char *slapd_pid_file = NULL;
38 char *slapd_args_file = NULL;
40 static char *fp_getline(FILE *fp, int *lineno);
41 static void fp_getline_init(int *lineno);
42 static int fp_parse_line(char *line, int *argcp, char **argv);
44 static char *strtok_quote(char *line, char *sep);
47 read_config( const char *fname )
50 char *line, *savefname, *saveline;
51 int cargc, savelineno;
57 struct berval *vals[2];
60 static BackendInfo *bi = NULL;
61 static BackendDB *be = NULL;
66 if ( (fp = fopen( fname, "r" )) == NULL ) {
68 Debug( LDAP_DEBUG_ANY,
69 "could not open config file \"%s\" - absolute path?\n",
75 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
77 if ( schema_init( ) != 0 ) {
78 Debug( LDAP_DEBUG_ANY,
79 "error initializing the schema\n",
84 fp_getline_init( &lineno );
86 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
87 /* skip comments and blank lines */
88 if ( line[0] == '#' || line[0] == '\0' ) {
92 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
94 /* fp_parse_line is destructive, we save a copy */
95 saveline = ch_strdup( line );
97 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
102 Debug( LDAP_DEBUG_ANY,
103 "%s: line %d: bad config line (ignored)\n",
108 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
110 Debug( LDAP_DEBUG_ANY,
111 "%s: line %d: missing type in \"backend <type>\" line\n",
117 Debug( LDAP_DEBUG_ANY,
118 "%s: line %d: backend line must appear before any database definition\n",
123 bi = backend_info( cargv[1] );
125 /* start of a new database definition */
126 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
128 Debug( LDAP_DEBUG_ANY,
129 "%s: line %d: missing type in \"database <type>\" line\n",
134 be = backend_db_init( cargv[1] );
136 /* get pid file name */
137 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
139 Debug( LDAP_DEBUG_ANY,
140 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
145 slapd_pid_file = ch_strdup( cargv[1] );
147 /* get args file name */
148 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
150 Debug( LDAP_DEBUG_ANY,
151 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
156 slapd_args_file = ch_strdup( cargv[1] );
158 /* set DIGEST realm */
159 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
161 Debug( LDAP_DEBUG_ANY,
162 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
167 be->be_realm = ch_strdup( cargv[1] );
169 } else if ( global_realm != NULL ) {
170 Debug( LDAP_DEBUG_ANY,
171 "%s: line %d: already set global realm!\n",
176 global_realm = ch_strdup( cargv[1] );
180 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
182 Debug( LDAP_DEBUG_ANY,
183 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
188 defsize = atoi( cargv[1] );
190 be->be_sizelimit = atoi( cargv[1] );
194 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
196 Debug( LDAP_DEBUG_ANY,
197 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
202 deftime = atoi( cargv[1] );
204 be->be_timelimit = atoi( cargv[1] );
207 /* set database suffix */
208 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
211 Debug( LDAP_DEBUG_ANY,
212 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
215 } else if ( cargc > 2 ) {
216 Debug( LDAP_DEBUG_ANY,
217 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
218 fname, lineno, cargv[1] );
221 Debug( LDAP_DEBUG_ANY,
222 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
224 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
225 Debug( LDAP_DEBUG_ANY,
226 "%s: line %d: suffix already served by this backend (ignored)\n",
228 } else if ( tmp_be != NULL ) {
229 Debug( LDAP_DEBUG_ANY,
230 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
231 fname, lineno, tmp_be->be_suffix[0] );
233 char *dn = ch_strdup( cargv[1] );
234 (void) dn_normalize( dn );
235 charray_add( &be->be_suffix, dn );
236 (void) ldap_pvt_str2upper( dn );
237 charray_add( &be->be_nsuffix, dn );
241 /* set database suffixAlias */
242 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
245 Debug( LDAP_DEBUG_ANY,
246 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
249 } else if ( cargc < 3 ) {
250 Debug( LDAP_DEBUG_ANY,
251 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
254 } else if ( cargc > 3 ) {
255 Debug( LDAP_DEBUG_ANY,
256 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
261 Debug( LDAP_DEBUG_ANY,
262 "%s: line %d: suffixAlias line"
263 " must appear inside a database definition (ignored)\n",
265 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
266 Debug( LDAP_DEBUG_ANY,
267 "%s: line %d: suffixAlias served by"
268 " a preceeding backend \"%s\" (ignored)\n",
269 fname, lineno, tmp_be->be_suffix[0] );
271 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
272 Debug( LDAP_DEBUG_ANY,
273 "%s: line %d: suffixAlias derefs to differnet backend"
274 " a preceeding backend \"%s\" (ignored)\n",
275 fname, lineno, tmp_be->be_suffix[0] );
278 char *alias, *aliased_dn;
280 alias = ch_strdup( cargv[1] );
281 (void) dn_normalize( alias );
283 aliased_dn = ch_strdup( cargv[2] );
284 (void) dn_normalize( aliased_dn );
286 (void) dn_normalize_case( alias );
287 (void) dn_normalize_case( aliased_dn );
288 charray_add( &be->be_suffixAlias, alias );
289 charray_add( &be->be_suffixAlias, aliased_dn );
295 /* set max deref depth */
296 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
299 Debug( LDAP_DEBUG_ANY,
300 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
305 Debug( LDAP_DEBUG_ANY,
306 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
308 } else if ((i = atoi(cargv[1])) < 0) {
309 Debug( LDAP_DEBUG_ANY,
310 "%s: line %d: depth must be positive (ignored)\n",
314 be->be_max_deref_depth = i;
318 /* set magic "root" dn for this database */
319 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
321 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
327 Debug( LDAP_DEBUG_ANY,
328 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
331 be->be_root_dn = ch_strdup( cargv[1] );
332 be->be_root_ndn = ch_strdup( cargv[1] );
334 if( dn_normalize_case( be->be_root_ndn ) == NULL ) {
335 free( be->be_root_dn );
336 free( be->be_root_ndn );
337 Debug( LDAP_DEBUG_ANY,
338 "%s: line %d: rootdn DN is invalid\n",
344 /* set super-secret magic database password */
345 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
347 Debug( LDAP_DEBUG_ANY,
348 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
353 Debug( LDAP_DEBUG_ANY,
354 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
357 be->be_root_pw = ch_strdup( cargv[1] );
360 /* make this database read-only */
361 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
363 Debug( LDAP_DEBUG_ANY,
364 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
369 Debug( LDAP_DEBUG_ANY,
370 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
373 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
380 /* where to send clients when we don't hold it */
381 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
383 Debug( LDAP_DEBUG_ANY,
384 "%s: line %d: missing URL in \"referral <URL>\" line\n",
389 vals[0]->bv_val = cargv[1];
390 vals[0]->bv_len = strlen( vals[0]->bv_val );
391 value_add( &default_referral, vals );
394 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
398 Debug( LDAP_DEBUG_ANY,
399 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
404 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
405 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
406 : ch_strdup( cargv[1] ) );
408 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
409 Debug( LDAP_DEBUG_ANY,
411 ? "%s: line %d: bad locale \"%s\"\n"
412 : "%s: line %d: bad locale\n"),
413 fname, lineno, locale );
417 Debug( LDAP_DEBUG_ANY,
418 "%s: line %d: \"locale\" unsupported\n",
422 /* specify an Object Identifier macro */
423 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
424 parse_oidm( fname, lineno, cargc, cargv );
425 /* specify an objectclass */
426 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
427 if ( *cargv[1] == '(' ) {
429 p = strchr(saveline,'(');
430 parse_oc( fname, lineno, p, cargv );
432 parse_oc_old( be, fname, lineno, cargc, cargv );
435 /* specify an attribute */
436 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
437 if ( *cargv[1] == '(' ) {
439 p = strchr(saveline,'(');
440 parse_at( fname, lineno, p, cargv );
442 attr_syntax_config( fname, lineno, cargc - 1,
446 /* turn on/off schema checking */
447 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
449 Debug( LDAP_DEBUG_ANY,
450 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
454 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
455 global_schemacheck = 0;
457 global_schemacheck = 1;
460 /* specify access control info */
461 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
462 parse_acl( be, fname, lineno, cargc, cargv );
464 /* specify default access control info */
465 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
467 Debug( LDAP_DEBUG_ANY,
468 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
473 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
474 str2access(cargv[1]))) )
476 Debug( LDAP_DEBUG_ANY,
477 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
478 fname, lineno, cargv[1] );
482 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
483 str2access(cargv[1]))) )
485 Debug( LDAP_DEBUG_ANY,
486 "%s: line %d: bad access \"%s\", "
487 "expecting [self]{none|auth|compare|search|read|write}\n",
488 fname, lineno, cargv[1] );
493 /* debug level to log things to syslog */
494 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
496 Debug( LDAP_DEBUG_ANY,
497 "%s: line %d: missing level in \"loglevel <level>\" line\n",
501 ldap_syslog = atoi( cargv[1] );
503 /* list of replicas of the data in this backend (master only) */
504 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
506 Debug( LDAP_DEBUG_ANY,
507 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
512 Debug( LDAP_DEBUG_ANY,
513 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
516 for ( i = 1; i < cargc; i++ ) {
517 if ( strncasecmp( cargv[i], "host=", 5 )
519 charray_add( &be->be_replica,
525 Debug( LDAP_DEBUG_ANY,
526 "%s: line %d: missing host in \"replica\" line (ignored)\n",
531 /* dn of master entity allowed to write to replica */
532 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
534 Debug( LDAP_DEBUG_ANY,
535 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
540 Debug( LDAP_DEBUG_ANY,
541 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
544 be->be_update_ndn = ch_strdup( cargv[1] );
545 if( dn_normalize_case( be->be_update_ndn ) == NULL ) {
546 Debug( LDAP_DEBUG_ANY,
547 "%s: line %d: updatedn DN is invalid\n",
553 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
555 Debug( LDAP_DEBUG_ANY,
556 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
561 Debug( LDAP_DEBUG_ANY,
562 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
564 } else if ( be->be_update_ndn == NULL ) {
565 Debug( LDAP_DEBUG_ANY,
566 "%s: line %d: updateref line must after updatedn (ignored)\n",
569 vals[0]->bv_val = cargv[1];
570 vals[0]->bv_len = strlen( vals[0]->bv_val );
571 value_add( &be->be_update_refs, vals );
574 /* replication log file to which changes are appended */
575 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
577 Debug( LDAP_DEBUG_ANY,
578 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
583 be->be_replogfile = ch_strdup( cargv[1] );
585 replogfile = ch_strdup( cargv[1] );
588 /* maintain lastmodified{by,time} attributes */
589 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
591 Debug( LDAP_DEBUG_ANY,
592 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
596 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
603 be->be_lastmod = OFF;
605 global_lastmod = OFF;
608 /* set idle timeout value */
609 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
612 Debug( LDAP_DEBUG_ANY,
613 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
618 i = atoi( cargv[1] );
621 Debug( LDAP_DEBUG_ANY,
622 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
627 global_idletimeout = i;
629 /* include another config file */
630 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
632 Debug( LDAP_DEBUG_ANY,
633 "%s: line %d: missing filename in \"include <filename>\" line\n",
637 savefname = ch_strdup( cargv[1] );
640 if ( read_config( savefname ) != 0 ) {
645 lineno = savelineno - 1;
647 /* location of kerberos srvtab file */
648 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
650 Debug( LDAP_DEBUG_ANY,
651 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
655 ldap_srvtab = ch_strdup( cargv[1] );
658 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
660 Debug( LDAP_DEBUG_ANY,
661 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
663 exit( EXIT_FAILURE );
665 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
666 Debug( LDAP_DEBUG_ANY,
667 "%s: line %d: failed to load or initialize module %s\n",
668 fname, lineno, cargv[1]);
669 exit( EXIT_FAILURE );
671 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
673 Debug( LDAP_DEBUG_ANY,
674 "%s: line %d: missing path in \"modulepath <path>\" line\n",
676 exit( EXIT_FAILURE );
678 if (module_path( cargv[1] )) {
679 Debug( LDAP_DEBUG_ANY,
680 "%s: line %d: failed to set module search path to %s\n",
681 fname, lineno, cargv[1]);
682 exit( EXIT_FAILURE );
685 #endif /*SLAPD_MODULES*/
688 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
689 rc = ldap_pvt_tls_set_option( NULL,
690 LDAP_OPT_X_TLS_PROTOCOL,
695 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
696 rc = ldap_pvt_tls_set_option( NULL,
697 LDAP_OPT_X_TLS_CIPHER_SUITE,
702 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
703 rc = ldap_pvt_tls_set_option( NULL,
704 LDAP_OPT_X_TLS_CERTFILE,
709 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
710 rc = ldap_pvt_tls_set_option( NULL,
711 LDAP_OPT_X_TLS_KEYFILE,
716 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
717 rc = ldap_pvt_tls_set_option( NULL,
718 LDAP_OPT_X_TLS_CACERTDIR,
723 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
724 rc = ldap_pvt_tls_set_option( NULL,
725 LDAP_OPT_X_TLS_CACERTFILE,
729 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
730 rc = ldap_pvt_tls_set_option( NULL,
731 LDAP_OPT_X_TLS_REQUIRE_CERT,
738 /* pass anything else to the current backend info/db config routine */
741 if ( bi->bi_config == 0 ) {
742 Debug( LDAP_DEBUG_ANY,
743 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
744 fname, lineno, cargv[0] );
746 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
752 } else if ( be != NULL ) {
753 if ( be->be_config == 0 ) {
754 Debug( LDAP_DEBUG_ANY,
755 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
756 fname, lineno, cargv[0] );
758 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
765 Debug( LDAP_DEBUG_ANY,
766 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
767 fname, lineno, cargv[0] );
786 for ( token = strtok_quote( line, " \t" ); token != NULL;
787 token = strtok_quote( NULL, " \t" ) ) {
788 if ( *argcp == MAXARGS ) {
789 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
793 argv[(*argcp)++] = token;
800 strtok_quote( char *line, char *sep )
806 if ( line != NULL ) {
809 while ( *next && strchr( sep, *next ) ) {
813 if ( *next == '\0' ) {
819 for ( inquote = 0; *next; ) {
827 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
833 next + 1, strlen( next + 1 ) + 1 );
834 next++; /* dont parse the escaped character */
839 if ( strchr( sep, *next ) != NULL ) {
852 static char buf[BUFSIZ];
854 static int lmax, lcur;
856 #define CATLINE( buf ) { \
858 len = strlen( buf ); \
859 while ( lcur + len + 1 > lmax ) { \
861 line = (char *) ch_realloc( line, lmax ); \
863 strcpy( line + lcur, buf ); \
868 fp_getline( FILE *fp, int *lineno )
876 /* hack attack - keeps us from having to keep a stack of bufs... */
877 if ( strncasecmp( line, "include", 7 ) == 0 ) {
882 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
883 if ( (p = strchr( buf, '\n' )) != NULL ) {
886 if ( ! isspace( (unsigned char) buf[0] ) ) {
895 return( line[0] ? line : NULL );
899 fp_getline_init( int *lineno )