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>
18 #include "ldap_defaults.h"
24 * defaults for various global variables
26 int defsize = SLAPD_DEFAULT_SIZELIMIT;
27 int deftime = SLAPD_DEFAULT_TIMELIMIT;
28 AccessControl *global_acl = NULL;
29 int global_default_access = ACL_READ;
31 int global_lastmod = ON;
32 int global_idletimeout = 0;
33 char *global_realm = NULL;
34 char *ldap_srvtab = "";
36 char *slapd_pid_file = NULL;
37 char *slapd_args_file = NULL;
39 static char *fp_getline(FILE *fp, int *lineno);
40 static void fp_getline_init(int *lineno);
41 static int fp_parse_line(char *line, int *argcp, char **argv);
43 static char *strtok_quote(char *line, char *sep);
46 read_config( char *fname )
49 char *line, *savefname, *saveline;
50 int cargc, savelineno;
56 struct berval *vals[2];
59 static BackendInfo *bi = NULL;
60 static BackendDB *be = NULL;
65 if ( (fp = fopen( fname, "r" )) == NULL ) {
67 Debug( LDAP_DEBUG_ANY,
68 "could not open config file \"%s\" - absolute path?\n",
74 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
76 if ( schema_init( ) != 0 ) {
77 Debug( LDAP_DEBUG_ANY,
78 "error initializing the schema\n",
83 fp_getline_init( &lineno );
85 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
86 /* skip comments and blank lines */
87 if ( line[0] == '#' || line[0] == '\0' ) {
91 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
93 /* fp_parse_line is destructive, we save a copy */
94 saveline = ch_strdup( line );
96 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
101 Debug( LDAP_DEBUG_ANY,
102 "%s: line %d: bad config line (ignored)\n",
107 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
109 Debug( LDAP_DEBUG_ANY,
110 "%s: line %d: missing type in \"backend <type>\" line\n",
116 Debug( LDAP_DEBUG_ANY,
117 "%s: line %d: backend line must appear before any database definition\n",
122 bi = backend_info( cargv[1] );
124 /* start of a new database definition */
125 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
127 Debug( LDAP_DEBUG_ANY,
128 "%s: line %d: missing type in \"database <type>\" line\n",
133 be = backend_db_init( cargv[1] );
135 /* assign a default depth limit for alias deref */
136 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
138 /* get pid file name */
139 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
141 Debug( LDAP_DEBUG_ANY,
142 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
147 slapd_pid_file = ch_strdup( cargv[1] );
149 /* get args file name */
150 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
152 Debug( LDAP_DEBUG_ANY,
153 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
158 slapd_args_file = ch_strdup( cargv[1] );
160 /* set DIGEST realm */
161 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
163 Debug( LDAP_DEBUG_ANY,
164 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
169 be->be_realm = ch_strdup( cargv[1] );
171 } else if ( global_realm != NULL ) {
172 Debug( LDAP_DEBUG_ANY,
173 "%s: line %d: already set global realm!\n",
178 global_realm = ch_strdup( cargv[1] );
182 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
184 Debug( LDAP_DEBUG_ANY,
185 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
190 defsize = atoi( cargv[1] );
192 be->be_sizelimit = atoi( cargv[1] );
196 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
198 Debug( LDAP_DEBUG_ANY,
199 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
204 deftime = atoi( cargv[1] );
206 be->be_timelimit = atoi( cargv[1] );
209 /* set database suffix */
210 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
213 Debug( LDAP_DEBUG_ANY,
214 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
217 } else if ( cargc > 2 ) {
218 Debug( LDAP_DEBUG_ANY,
219 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
220 fname, lineno, cargv[1] );
223 Debug( LDAP_DEBUG_ANY,
224 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
226 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
227 Debug( LDAP_DEBUG_ANY,
228 "%s: line %d: suffix already served by this backend (ignored)\n",
230 } else if ( tmp_be != NULL ) {
231 Debug( LDAP_DEBUG_ANY,
232 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
233 fname, lineno, tmp_be->be_suffix[0] );
235 char *dn = ch_strdup( cargv[1] );
236 (void) dn_normalize( dn );
237 charray_add( &be->be_suffix, dn );
238 (void) str2upper( dn );
239 charray_add( &be->be_nsuffix, dn );
243 /* set database suffixAlias */
244 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
247 Debug( LDAP_DEBUG_ANY,
248 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
251 } else if ( cargc < 3 ) {
252 Debug( LDAP_DEBUG_ANY,
253 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
256 } else if ( cargc > 3 ) {
257 Debug( LDAP_DEBUG_ANY,
258 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
263 Debug( LDAP_DEBUG_ANY,
264 "%s: line %d: suffixAlias line"
265 " must appear inside a database definition (ignored)\n",
267 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
268 Debug( LDAP_DEBUG_ANY,
269 "%s: line %d: suffixAlias served by"
270 " a preceeding backend \"%s\" (ignored)\n",
271 fname, lineno, tmp_be->be_suffix[0] );
273 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
274 Debug( LDAP_DEBUG_ANY,
275 "%s: line %d: suffixAlias derefs to differnet backend"
276 " a preceeding backend \"%s\" (ignored)\n",
277 fname, lineno, tmp_be->be_suffix[0] );
280 char *alias, *aliased_dn;
282 alias = ch_strdup( cargv[1] );
283 (void) dn_normalize( alias );
285 aliased_dn = ch_strdup( cargv[2] );
286 (void) dn_normalize( aliased_dn );
288 (void) dn_normalize_case( alias );
289 (void) dn_normalize_case( aliased_dn );
290 charray_add( &be->be_suffixAlias, alias );
291 charray_add( &be->be_suffixAlias, aliased_dn );
297 /* set max deref depth */
298 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
301 Debug( LDAP_DEBUG_ANY,
302 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
307 Debug( LDAP_DEBUG_ANY,
308 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
310 } else if ((i = atoi(cargv[1])) < 0) {
311 Debug( LDAP_DEBUG_ANY,
312 "%s: line %d: depth must be positive (ignored)\n",
316 be->be_max_deref_depth = i;
320 /* set magic "root" dn for this database */
321 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
323 Debug( LDAP_DEBUG_ANY,
324 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
329 Debug( LDAP_DEBUG_ANY,
330 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
333 be->be_root_dn = ch_strdup( cargv[1] );
334 be->be_root_ndn = ch_strdup( cargv[1] );
336 if( dn_normalize_case( be->be_root_ndn ) == NULL ) {
337 free( be->be_root_dn );
338 free( be->be_root_ndn );
339 Debug( LDAP_DEBUG_ANY,
340 "%s: line %d: rootdn DN is invalid\n",
346 /* set super-secret magic database password */
347 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
349 Debug( LDAP_DEBUG_ANY,
350 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
355 Debug( LDAP_DEBUG_ANY,
356 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
359 be->be_root_pw = ch_strdup( cargv[1] );
362 /* make this database read-only */
363 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
365 Debug( LDAP_DEBUG_ANY,
366 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
371 Debug( LDAP_DEBUG_ANY,
372 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
375 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
382 /* where to send clients when we don't hold it */
383 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
385 Debug( LDAP_DEBUG_ANY,
386 "%s: line %d: missing URL in \"referral <URL>\" line\n",
391 vals[0]->bv_val = cargv[1];
392 vals[0]->bv_len = strlen( vals[0]->bv_val );
393 value_add( &default_referral, vals );
396 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
400 Debug( LDAP_DEBUG_ANY,
401 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
406 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
407 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
408 : ch_strdup( cargv[1] ) );
410 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
411 Debug( LDAP_DEBUG_ANY,
413 ? "%s: line %d: bad locale \"%s\"\n"
414 : "%s: line %d: bad locale\n"),
415 fname, lineno, locale );
419 Debug( LDAP_DEBUG_ANY,
420 "%s: line %d: \"locale\" unsupported\n",
424 /* specify an objectclass */
425 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
426 if ( *cargv[1] == '(' ) {
428 p = strchr(saveline,'(');
429 parse_oc( fname, lineno, p );
431 parse_oc_old( be, fname, lineno, cargc, cargv );
434 /* specify an attribute */
435 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
436 if ( *cargv[1] == '(' ) {
438 p = strchr(saveline,'(');
439 parse_at( fname, lineno, p );
441 attr_syntax_config( fname, lineno, cargc - 1,
445 /* turn on/off schema checking */
446 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
448 Debug( LDAP_DEBUG_ANY,
449 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
453 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
454 global_schemacheck = 0;
456 global_schemacheck = 1;
459 /* specify access control info */
460 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
461 parse_acl( be, fname, lineno, cargc, cargv );
463 /* specify default access control info */
464 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
466 Debug( LDAP_DEBUG_ANY,
467 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
472 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
473 str2access(cargv[1]))) )
475 Debug( LDAP_DEBUG_ANY,
476 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
477 fname, lineno, cargv[1] );
481 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
482 str2access(cargv[1]))) )
484 Debug( LDAP_DEBUG_ANY,
485 "%s: line %d: bad access \"%s\", "
486 "expecting [self]{none|auth|compare|search|read|write}\n",
487 fname, lineno, cargv[1] );
492 /* debug level to log things to syslog */
493 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
495 Debug( LDAP_DEBUG_ANY,
496 "%s: line %d: missing level in \"loglevel <level>\" line\n",
500 ldap_syslog = atoi( cargv[1] );
502 /* list of replicas of the data in this backend (master only) */
503 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
505 Debug( LDAP_DEBUG_ANY,
506 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
511 Debug( LDAP_DEBUG_ANY,
512 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
515 for ( i = 1; i < cargc; i++ ) {
516 if ( strncasecmp( cargv[i], "host=", 5 )
518 charray_add( &be->be_replica,
524 Debug( LDAP_DEBUG_ANY,
525 "%s: line %d: missing host in \"replica\" line (ignored)\n",
530 /* dn of master entity allowed to write to replica */
531 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
533 Debug( LDAP_DEBUG_ANY,
534 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
539 Debug( LDAP_DEBUG_ANY,
540 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
543 be->be_update_ndn = ch_strdup( cargv[1] );
544 if( dn_normalize_case( be->be_update_ndn ) == NULL ) {
545 Debug( LDAP_DEBUG_ANY,
546 "%s: line %d: updatedn DN is invalid\n",
552 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
554 Debug( LDAP_DEBUG_ANY,
555 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
560 Debug( LDAP_DEBUG_ANY,
561 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
563 } else if ( be->be_update_ndn == NULL ) {
564 Debug( LDAP_DEBUG_ANY,
565 "%s: line %d: updateref line must after updatedn (ignored)\n",
568 vals[0]->bv_val = cargv[1];
569 vals[0]->bv_len = strlen( vals[0]->bv_val );
570 value_add( &be->be_update_refs, vals );
573 /* replication log file to which changes are appended */
574 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
576 Debug( LDAP_DEBUG_ANY,
577 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
582 be->be_replogfile = ch_strdup( cargv[1] );
584 replogfile = ch_strdup( cargv[1] );
587 /* maintain lastmodified{by,time} attributes */
588 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
590 Debug( LDAP_DEBUG_ANY,
591 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
595 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
602 be->be_lastmod = OFF;
604 global_lastmod = OFF;
607 /* set idle timeout value */
608 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
611 Debug( LDAP_DEBUG_ANY,
612 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
617 i = atoi( cargv[1] );
620 Debug( LDAP_DEBUG_ANY,
621 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
626 global_idletimeout = i;
628 /* include another config file */
629 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
631 Debug( LDAP_DEBUG_ANY,
632 "%s: line %d: missing filename in \"include <filename>\" line\n",
636 savefname = ch_strdup( cargv[1] );
639 if ( read_config( savefname ) != 0 ) {
644 lineno = savelineno - 1;
646 /* location of kerberos srvtab file */
647 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
649 Debug( LDAP_DEBUG_ANY,
650 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
654 ldap_srvtab = ch_strdup( cargv[1] );
657 } else if (strcasecmp( cargv[0], "loadmodule") == 0 ) {
659 Debug( LDAP_DEBUG_ANY,
660 "%s: line %d: missing filename in \"loadmodule <filename>\" line\n",
662 exit( EXIT_FAILURE );
664 if (!load_module(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
665 Debug( LDAP_DEBUG_ANY,
666 "%s: line %d: failed to load or initialize module %s\n",
667 fname, lineno, cargv[1]);
668 exit( EXIT_FAILURE );
671 #endif /*SLAPD_MODULES*/
674 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
675 rc = ldap_pvt_tls_set_option( NULL,
676 LDAP_OPT_X_TLS_PROTOCOL,
681 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
682 rc = ldap_pvt_tls_set_option( NULL,
683 LDAP_OPT_X_TLS_CIPHER_SUITE,
688 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
689 rc = ldap_pvt_tls_set_option( NULL,
690 LDAP_OPT_X_TLS_CERTFILE,
695 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
696 rc = ldap_pvt_tls_set_option( NULL,
697 LDAP_OPT_X_TLS_KEYFILE,
702 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
703 rc = ldap_pvt_tls_set_option( NULL,
704 LDAP_OPT_X_TLS_CACERTDIR,
709 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
710 rc = ldap_pvt_tls_set_option( NULL,
711 LDAP_OPT_X_TLS_CACERTFILE,
715 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
716 rc = ldap_pvt_tls_set_option( NULL,
717 LDAP_OPT_X_TLS_REQUIRE_CERT,
724 /* pass anything else to the current backend info/db config routine */
727 if ( bi->bi_config == 0 ) {
728 Debug( LDAP_DEBUG_ANY,
729 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
730 fname, lineno, cargv[0] );
732 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
738 } else if ( be != NULL ) {
739 if ( be->be_config == 0 ) {
740 Debug( LDAP_DEBUG_ANY,
741 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
742 fname, lineno, cargv[0] );
744 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
751 Debug( LDAP_DEBUG_ANY,
752 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
753 fname, lineno, cargv[0] );
772 for ( token = strtok_quote( line, " \t" ); token != NULL;
773 token = strtok_quote( NULL, " \t" ) ) {
774 if ( *argcp == MAXARGS ) {
775 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
779 argv[(*argcp)++] = token;
786 strtok_quote( char *line, char *sep )
792 if ( line != NULL ) {
795 while ( *next && strchr( sep, *next ) ) {
799 if ( *next == '\0' ) {
805 for ( inquote = 0; *next; ) {
813 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
819 next + 1, strlen( next + 1 ) + 1 );
820 next++; /* dont parse the escaped character */
825 if ( strchr( sep, *next ) != NULL ) {
838 static char buf[BUFSIZ];
840 static int lmax, lcur;
842 #define CATLINE( buf ) { \
844 len = strlen( buf ); \
845 while ( lcur + len + 1 > lmax ) { \
847 line = (char *) ch_realloc( line, lmax ); \
849 strcpy( line + lcur, buf ); \
854 fp_getline( FILE *fp, int *lineno )
862 /* hack attack - keeps us from having to keep a stack of bufs... */
863 if ( strncasecmp( line, "include", 7 ) == 0 ) {
868 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
869 if ( (p = strchr( buf, '\n' )) != NULL ) {
872 if ( ! isspace( (unsigned char) buf[0] ) ) {
881 return( line[0] ? line : NULL );
885 fp_getline_init( int *lineno )