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 int 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 = "";
38 char *slapd_pid_file = NULL;
39 char *slapd_args_file = NULL;
41 static char *fp_getline(FILE *fp, int *lineno);
42 static void fp_getline_init(int *lineno);
43 static int fp_parse_line(char *line, int *argcp, char **argv);
45 static char *strtok_quote(char *line, char *sep);
48 read_config( const char *fname )
51 char *line, *savefname, *saveline;
52 int cargc, savelineno;
58 struct berval *vals[2];
61 static BackendInfo *bi = NULL;
62 static BackendDB *be = NULL;
67 if ( (fp = fopen( fname, "r" )) == NULL ) {
69 Debug( LDAP_DEBUG_ANY,
70 "could not open config file \"%s\" - absolute path?\n",
76 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
78 if ( schema_init( ) != 0 ) {
79 Debug( LDAP_DEBUG_ANY,
80 "error initializing the schema\n",
85 fp_getline_init( &lineno );
87 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
88 /* skip comments and blank lines */
89 if ( line[0] == '#' || line[0] == '\0' ) {
93 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
95 /* fp_parse_line is destructive, we save a copy */
96 saveline = ch_strdup( line );
98 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
103 Debug( LDAP_DEBUG_ANY,
104 "%s: line %d: bad config line (ignored)\n",
109 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
111 Debug( LDAP_DEBUG_ANY,
112 "%s: line %d: missing type in \"backend <type>\" line\n",
118 Debug( LDAP_DEBUG_ANY,
119 "%s: line %d: backend line must appear before any database definition\n",
124 bi = backend_info( cargv[1] );
127 Debug( LDAP_DEBUG_ANY,
128 "backend %s initialization failed.n",
133 /* start of a new database definition */
134 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
136 Debug( LDAP_DEBUG_ANY,
137 "%s: line %d: missing type in \"database <type>\" line\n",
143 be = backend_db_init( cargv[1] );
146 Debug( LDAP_DEBUG_ANY,
147 "database %s initialization failed.n",
152 /* get pid file name */
153 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
155 Debug( LDAP_DEBUG_ANY,
156 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
161 slapd_pid_file = ch_strdup( cargv[1] );
163 /* get args file name */
164 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
166 Debug( LDAP_DEBUG_ANY,
167 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
172 slapd_args_file = ch_strdup( cargv[1] );
174 /* set DIGEST realm */
175 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
177 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
183 be->be_realm = ch_strdup( cargv[1] );
185 } else if ( global_realm != NULL ) {
186 Debug( LDAP_DEBUG_ANY,
187 "%s: line %d: already set global realm!\n",
192 global_realm = ch_strdup( cargv[1] );
196 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
198 Debug( LDAP_DEBUG_ANY,
199 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
204 defsize = atoi( cargv[1] );
206 be->be_sizelimit = atoi( cargv[1] );
210 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
212 Debug( LDAP_DEBUG_ANY,
213 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
218 deftime = atoi( cargv[1] );
220 be->be_timelimit = atoi( cargv[1] );
223 /* set database suffix */
224 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
227 Debug( LDAP_DEBUG_ANY,
228 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
231 } else if ( cargc > 2 ) {
232 Debug( LDAP_DEBUG_ANY,
233 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
234 fname, lineno, cargv[1] );
237 Debug( LDAP_DEBUG_ANY,
238 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
240 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
241 Debug( LDAP_DEBUG_ANY,
242 "%s: line %d: suffix already served by this backend (ignored)\n",
244 } else if ( tmp_be != NULL ) {
245 Debug( LDAP_DEBUG_ANY,
246 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
247 fname, lineno, tmp_be->be_suffix[0] );
249 char *dn = ch_strdup( cargv[1] );
250 (void) dn_normalize( dn );
251 charray_add( &be->be_suffix, dn );
252 (void) ldap_pvt_str2upper( dn );
253 charray_add( &be->be_nsuffix, dn );
257 /* set database suffixAlias */
258 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
261 Debug( LDAP_DEBUG_ANY,
262 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
265 } else if ( cargc < 3 ) {
266 Debug( LDAP_DEBUG_ANY,
267 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
270 } else if ( cargc > 3 ) {
271 Debug( LDAP_DEBUG_ANY,
272 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
277 Debug( LDAP_DEBUG_ANY,
278 "%s: line %d: suffixAlias line"
279 " must appear inside a database definition (ignored)\n",
281 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
282 Debug( LDAP_DEBUG_ANY,
283 "%s: line %d: suffixAlias served by"
284 " a preceeding backend \"%s\" (ignored)\n",
285 fname, lineno, tmp_be->be_suffix[0] );
287 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: suffixAlias derefs to differnet backend"
290 " a preceeding backend \"%s\" (ignored)\n",
291 fname, lineno, tmp_be->be_suffix[0] );
294 char *alias, *aliased_dn;
296 alias = ch_strdup( cargv[1] );
297 (void) dn_normalize( alias );
299 aliased_dn = ch_strdup( cargv[2] );
300 (void) dn_normalize( aliased_dn );
302 (void) dn_normalize_case( alias );
303 (void) dn_normalize_case( aliased_dn );
304 charray_add( &be->be_suffixAlias, alias );
305 charray_add( &be->be_suffixAlias, aliased_dn );
311 /* set max deref depth */
312 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
315 Debug( LDAP_DEBUG_ANY,
316 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
321 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
324 } else if ((i = atoi(cargv[1])) < 0) {
325 Debug( LDAP_DEBUG_ANY,
326 "%s: line %d: depth must be positive (ignored)\n",
330 be->be_max_deref_depth = i;
334 /* set magic "root" dn for this database */
335 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
337 Debug( LDAP_DEBUG_ANY,
338 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
343 Debug( LDAP_DEBUG_ANY,
344 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
347 be->be_root_dn = ch_strdup( cargv[1] );
348 be->be_root_ndn = ch_strdup( cargv[1] );
350 if( dn_normalize_case( be->be_root_ndn ) == NULL ) {
351 free( be->be_root_dn );
352 free( be->be_root_ndn );
353 Debug( LDAP_DEBUG_ANY,
354 "%s: line %d: rootdn DN is invalid\n",
360 /* set super-secret magic database password */
361 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
363 Debug( LDAP_DEBUG_ANY,
364 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
369 Debug( LDAP_DEBUG_ANY,
370 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
373 be->be_root_pw = ch_strdup( cargv[1] );
376 /* make this database read-only */
377 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
379 Debug( LDAP_DEBUG_ANY,
380 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
385 global_readonly = (strcasecmp( cargv[1], "on" ) == 0);
387 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
394 /* where to send clients when we don't hold it */
395 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
397 Debug( LDAP_DEBUG_ANY,
398 "%s: line %d: missing URL in \"referral <URL>\" line\n",
403 vals[0]->bv_val = cargv[1];
404 vals[0]->bv_len = strlen( vals[0]->bv_val );
405 value_add( &default_referral, vals );
408 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
412 Debug( LDAP_DEBUG_ANY,
413 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
418 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
419 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
420 : ch_strdup( cargv[1] ) );
422 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
423 Debug( LDAP_DEBUG_ANY,
425 ? "%s: line %d: bad locale \"%s\"\n"
426 : "%s: line %d: bad locale\n"),
427 fname, lineno, locale );
431 Debug( LDAP_DEBUG_ANY,
432 "%s: line %d: \"locale\" unsupported\n",
436 /* specify an Object Identifier macro */
437 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
438 parse_oidm( fname, lineno, cargc, cargv );
439 /* specify an objectclass */
440 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
441 if ( *cargv[1] == '(' ) {
443 p = strchr(saveline,'(');
444 parse_oc( fname, lineno, p, cargv );
446 parse_oc_old( be, fname, lineno, cargc, cargv );
449 /* specify an attribute */
450 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
451 if ( *cargv[1] == '(' ) {
453 p = strchr(saveline,'(');
454 parse_at( fname, lineno, p, cargv );
456 attr_syntax_config( fname, lineno, cargc - 1,
460 /* turn on/off schema checking */
461 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
463 Debug( LDAP_DEBUG_ANY,
464 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
468 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
469 global_schemacheck = 0;
471 global_schemacheck = 1;
474 /* specify access control info */
475 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
476 parse_acl( be, fname, lineno, cargc, cargv );
478 /* specify default access control info */
479 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
481 Debug( LDAP_DEBUG_ANY,
482 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
487 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
488 str2access(cargv[1]))) )
490 Debug( LDAP_DEBUG_ANY,
491 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
492 fname, lineno, cargv[1] );
496 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
497 str2access(cargv[1]))) )
499 Debug( LDAP_DEBUG_ANY,
500 "%s: line %d: bad access \"%s\", "
501 "expecting [self]{none|auth|compare|search|read|write}\n",
502 fname, lineno, cargv[1] );
507 /* debug level to log things to syslog */
508 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
510 Debug( LDAP_DEBUG_ANY,
511 "%s: line %d: missing level in \"loglevel <level>\" line\n",
515 ldap_syslog = atoi( cargv[1] );
517 /* list of replicas of the data in this backend (master only) */
518 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
520 Debug( LDAP_DEBUG_ANY,
521 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
526 Debug( LDAP_DEBUG_ANY,
527 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
530 for ( i = 1; i < cargc; i++ ) {
531 if ( strncasecmp( cargv[i], "host=", 5 )
533 charray_add( &be->be_replica,
539 Debug( LDAP_DEBUG_ANY,
540 "%s: line %d: missing host in \"replica\" line (ignored)\n",
545 /* dn of master entity allowed to write to replica */
546 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
548 Debug( LDAP_DEBUG_ANY,
549 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
554 Debug( LDAP_DEBUG_ANY,
555 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
558 be->be_update_ndn = ch_strdup( cargv[1] );
559 if( dn_normalize_case( be->be_update_ndn ) == NULL ) {
560 Debug( LDAP_DEBUG_ANY,
561 "%s: line %d: updatedn DN is invalid\n",
567 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
569 Debug( LDAP_DEBUG_ANY,
570 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
575 Debug( LDAP_DEBUG_ANY,
576 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
578 } else if ( be->be_update_ndn == NULL ) {
579 Debug( LDAP_DEBUG_ANY,
580 "%s: line %d: updateref line must after updatedn (ignored)\n",
583 vals[0]->bv_val = cargv[1];
584 vals[0]->bv_len = strlen( vals[0]->bv_val );
585 value_add( &be->be_update_refs, vals );
588 /* replication log file to which changes are appended */
589 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
591 Debug( LDAP_DEBUG_ANY,
592 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
597 be->be_replogfile = ch_strdup( cargv[1] );
599 replogfile = ch_strdup( cargv[1] );
602 /* maintain lastmodified{by,time} attributes */
603 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
605 Debug( LDAP_DEBUG_ANY,
606 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
610 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
617 be->be_lastmod = OFF;
619 global_lastmod = OFF;
622 /* set idle timeout value */
623 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
626 Debug( LDAP_DEBUG_ANY,
627 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
632 i = atoi( cargv[1] );
635 Debug( LDAP_DEBUG_ANY,
636 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
641 global_idletimeout = i;
643 /* include another config file */
644 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
646 Debug( LDAP_DEBUG_ANY,
647 "%s: line %d: missing filename in \"include <filename>\" line\n",
651 savefname = ch_strdup( cargv[1] );
654 if ( read_config( savefname ) != 0 ) {
659 lineno = savelineno - 1;
661 /* location of kerberos srvtab file */
662 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
664 Debug( LDAP_DEBUG_ANY,
665 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
669 ldap_srvtab = ch_strdup( cargv[1] );
672 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
674 Debug( LDAP_DEBUG_ANY,
675 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
677 exit( EXIT_FAILURE );
679 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
680 Debug( LDAP_DEBUG_ANY,
681 "%s: line %d: failed to load or initialize module %s\n",
682 fname, lineno, cargv[1]);
683 exit( EXIT_FAILURE );
685 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
687 Debug( LDAP_DEBUG_ANY,
688 "%s: line %d: missing path in \"modulepath <path>\" line\n",
690 exit( EXIT_FAILURE );
692 if (module_path( cargv[1] )) {
693 Debug( LDAP_DEBUG_ANY,
694 "%s: line %d: failed to set module search path to %s\n",
695 fname, lineno, cargv[1]);
696 exit( EXIT_FAILURE );
699 #endif /*SLAPD_MODULES*/
702 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
703 rc = ldap_pvt_tls_set_option( NULL,
704 LDAP_OPT_X_TLS_PROTOCOL,
709 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
710 rc = ldap_pvt_tls_set_option( NULL,
711 LDAP_OPT_X_TLS_CIPHER_SUITE,
716 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
717 rc = ldap_pvt_tls_set_option( NULL,
718 LDAP_OPT_X_TLS_CERTFILE,
723 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
724 rc = ldap_pvt_tls_set_option( NULL,
725 LDAP_OPT_X_TLS_KEYFILE,
730 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
731 rc = ldap_pvt_tls_set_option( NULL,
732 LDAP_OPT_X_TLS_CACERTDIR,
737 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
738 rc = ldap_pvt_tls_set_option( NULL,
739 LDAP_OPT_X_TLS_CACERTFILE,
743 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
744 rc = ldap_pvt_tls_set_option( NULL,
745 LDAP_OPT_X_TLS_REQUIRE_CERT,
752 /* pass anything else to the current backend info/db config routine */
755 if ( bi->bi_config == 0 ) {
756 Debug( LDAP_DEBUG_ANY,
757 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
758 fname, lineno, cargv[0] );
760 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
766 } else if ( be != NULL ) {
767 if ( be->be_config == 0 ) {
768 Debug( LDAP_DEBUG_ANY,
769 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
770 fname, lineno, cargv[0] );
772 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
779 Debug( LDAP_DEBUG_ANY,
780 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
781 fname, lineno, cargv[0] );
800 for ( token = strtok_quote( line, " \t" ); token != NULL;
801 token = strtok_quote( NULL, " \t" ) ) {
802 if ( *argcp == MAXARGS ) {
803 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
807 argv[(*argcp)++] = token;
814 strtok_quote( char *line, char *sep )
820 if ( line != NULL ) {
823 while ( *next && strchr( sep, *next ) ) {
827 if ( *next == '\0' ) {
833 for ( inquote = 0; *next; ) {
841 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
847 next + 1, strlen( next + 1 ) + 1 );
848 next++; /* dont parse the escaped character */
853 if ( strchr( sep, *next ) != NULL ) {
866 static char buf[BUFSIZ];
868 static int lmax, lcur;
870 #define CATLINE( buf ) { \
872 len = strlen( buf ); \
873 while ( lcur + len + 1 > lmax ) { \
875 line = (char *) ch_realloc( line, lmax ); \
877 strcpy( line + lcur, buf ); \
882 fp_getline( FILE *fp, int *lineno )
890 /* hack attack - keeps us from having to keep a stack of bufs... */
891 if ( strncasecmp( line, "include", 7 ) == 0 ) {
896 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
897 if ( (p = strchr( buf, '\n' )) != NULL ) {
900 if ( ! isspace( (unsigned char) buf[0] ) ) {
909 return( line[0] ? line : NULL );
913 fp_getline_init( int *lineno )