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 /* assign a default depth limit for alias deref */
137 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
139 /* get pid file name */
140 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
142 Debug( LDAP_DEBUG_ANY,
143 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
148 slapd_pid_file = ch_strdup( cargv[1] );
150 /* get args file name */
151 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
153 Debug( LDAP_DEBUG_ANY,
154 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
159 slapd_args_file = ch_strdup( cargv[1] );
161 /* set DIGEST realm */
162 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
164 Debug( LDAP_DEBUG_ANY,
165 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
170 be->be_realm = ch_strdup( cargv[1] );
172 } else if ( global_realm != NULL ) {
173 Debug( LDAP_DEBUG_ANY,
174 "%s: line %d: already set global realm!\n",
179 global_realm = ch_strdup( cargv[1] );
183 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
185 Debug( LDAP_DEBUG_ANY,
186 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
191 defsize = atoi( cargv[1] );
193 be->be_sizelimit = atoi( cargv[1] );
197 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
199 Debug( LDAP_DEBUG_ANY,
200 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
205 deftime = atoi( cargv[1] );
207 be->be_timelimit = atoi( cargv[1] );
210 /* set database suffix */
211 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
214 Debug( LDAP_DEBUG_ANY,
215 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
218 } else if ( cargc > 2 ) {
219 Debug( LDAP_DEBUG_ANY,
220 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
221 fname, lineno, cargv[1] );
224 Debug( LDAP_DEBUG_ANY,
225 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
227 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
228 Debug( LDAP_DEBUG_ANY,
229 "%s: line %d: suffix already served by this backend (ignored)\n",
231 } else if ( tmp_be != NULL ) {
232 Debug( LDAP_DEBUG_ANY,
233 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
234 fname, lineno, tmp_be->be_suffix[0] );
236 char *dn = ch_strdup( cargv[1] );
237 (void) dn_normalize( dn );
238 charray_add( &be->be_suffix, dn );
239 (void) ldap_pvt_str2upper( dn );
240 charray_add( &be->be_nsuffix, dn );
244 /* set database suffixAlias */
245 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
248 Debug( LDAP_DEBUG_ANY,
249 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
252 } else if ( cargc < 3 ) {
253 Debug( LDAP_DEBUG_ANY,
254 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
257 } else if ( cargc > 3 ) {
258 Debug( LDAP_DEBUG_ANY,
259 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
264 Debug( LDAP_DEBUG_ANY,
265 "%s: line %d: suffixAlias line"
266 " must appear inside a database definition (ignored)\n",
268 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
269 Debug( LDAP_DEBUG_ANY,
270 "%s: line %d: suffixAlias served by"
271 " a preceeding backend \"%s\" (ignored)\n",
272 fname, lineno, tmp_be->be_suffix[0] );
274 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
275 Debug( LDAP_DEBUG_ANY,
276 "%s: line %d: suffixAlias derefs to differnet backend"
277 " a preceeding backend \"%s\" (ignored)\n",
278 fname, lineno, tmp_be->be_suffix[0] );
281 char *alias, *aliased_dn;
283 alias = ch_strdup( cargv[1] );
284 (void) dn_normalize( alias );
286 aliased_dn = ch_strdup( cargv[2] );
287 (void) dn_normalize( aliased_dn );
289 (void) dn_normalize_case( alias );
290 (void) dn_normalize_case( aliased_dn );
291 charray_add( &be->be_suffixAlias, alias );
292 charray_add( &be->be_suffixAlias, aliased_dn );
298 /* set max deref depth */
299 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
302 Debug( LDAP_DEBUG_ANY,
303 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
308 Debug( LDAP_DEBUG_ANY,
309 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
311 } else if ((i = atoi(cargv[1])) < 0) {
312 Debug( LDAP_DEBUG_ANY,
313 "%s: line %d: depth must be positive (ignored)\n",
317 be->be_max_deref_depth = i;
321 /* set magic "root" dn for this database */
322 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
324 Debug( LDAP_DEBUG_ANY,
325 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
330 Debug( LDAP_DEBUG_ANY,
331 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
334 be->be_root_dn = ch_strdup( cargv[1] );
335 be->be_root_ndn = ch_strdup( cargv[1] );
337 if( dn_normalize_case( be->be_root_ndn ) == NULL ) {
338 free( be->be_root_dn );
339 free( be->be_root_ndn );
340 Debug( LDAP_DEBUG_ANY,
341 "%s: line %d: rootdn DN is invalid\n",
347 /* set super-secret magic database password */
348 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
350 Debug( LDAP_DEBUG_ANY,
351 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
356 Debug( LDAP_DEBUG_ANY,
357 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
360 be->be_root_pw = ch_strdup( cargv[1] );
363 /* make this database read-only */
364 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
366 Debug( LDAP_DEBUG_ANY,
367 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
372 Debug( LDAP_DEBUG_ANY,
373 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
376 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
383 /* where to send clients when we don't hold it */
384 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
386 Debug( LDAP_DEBUG_ANY,
387 "%s: line %d: missing URL in \"referral <URL>\" line\n",
392 vals[0]->bv_val = cargv[1];
393 vals[0]->bv_len = strlen( vals[0]->bv_val );
394 value_add( &default_referral, vals );
397 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
401 Debug( LDAP_DEBUG_ANY,
402 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
407 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
408 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
409 : ch_strdup( cargv[1] ) );
411 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
412 Debug( LDAP_DEBUG_ANY,
414 ? "%s: line %d: bad locale \"%s\"\n"
415 : "%s: line %d: bad locale\n"),
416 fname, lineno, locale );
420 Debug( LDAP_DEBUG_ANY,
421 "%s: line %d: \"locale\" unsupported\n",
425 /* specify an Object Identifier macro */
426 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
427 parse_oidm( fname, lineno, cargc, cargv );
428 /* specify an objectclass */
429 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
430 if ( *cargv[1] == '(' ) {
432 p = strchr(saveline,'(');
433 parse_oc( fname, lineno, p, cargv );
435 parse_oc_old( be, fname, lineno, cargc, cargv );
438 /* specify an attribute */
439 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
440 if ( *cargv[1] == '(' ) {
442 p = strchr(saveline,'(');
443 parse_at( fname, lineno, p, cargv );
445 attr_syntax_config( fname, lineno, cargc - 1,
449 /* turn on/off schema checking */
450 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
452 Debug( LDAP_DEBUG_ANY,
453 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
457 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
458 global_schemacheck = 0;
460 global_schemacheck = 1;
463 /* specify access control info */
464 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
465 parse_acl( be, fname, lineno, cargc, cargv );
467 /* specify default access control info */
468 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
470 Debug( LDAP_DEBUG_ANY,
471 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
476 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
477 str2access(cargv[1]))) )
479 Debug( LDAP_DEBUG_ANY,
480 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
481 fname, lineno, cargv[1] );
485 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
486 str2access(cargv[1]))) )
488 Debug( LDAP_DEBUG_ANY,
489 "%s: line %d: bad access \"%s\", "
490 "expecting [self]{none|auth|compare|search|read|write}\n",
491 fname, lineno, cargv[1] );
496 /* debug level to log things to syslog */
497 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
499 Debug( LDAP_DEBUG_ANY,
500 "%s: line %d: missing level in \"loglevel <level>\" line\n",
504 ldap_syslog = atoi( cargv[1] );
506 /* list of replicas of the data in this backend (master only) */
507 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
509 Debug( LDAP_DEBUG_ANY,
510 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
515 Debug( LDAP_DEBUG_ANY,
516 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
519 for ( i = 1; i < cargc; i++ ) {
520 if ( strncasecmp( cargv[i], "host=", 5 )
522 charray_add( &be->be_replica,
528 Debug( LDAP_DEBUG_ANY,
529 "%s: line %d: missing host in \"replica\" line (ignored)\n",
534 /* dn of master entity allowed to write to replica */
535 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
537 Debug( LDAP_DEBUG_ANY,
538 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
543 Debug( LDAP_DEBUG_ANY,
544 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
547 be->be_update_ndn = ch_strdup( cargv[1] );
548 if( dn_normalize_case( be->be_update_ndn ) == NULL ) {
549 Debug( LDAP_DEBUG_ANY,
550 "%s: line %d: updatedn DN is invalid\n",
556 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
558 Debug( LDAP_DEBUG_ANY,
559 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
564 Debug( LDAP_DEBUG_ANY,
565 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
567 } else if ( be->be_update_ndn == NULL ) {
568 Debug( LDAP_DEBUG_ANY,
569 "%s: line %d: updateref line must after updatedn (ignored)\n",
572 vals[0]->bv_val = cargv[1];
573 vals[0]->bv_len = strlen( vals[0]->bv_val );
574 value_add( &be->be_update_refs, vals );
577 /* replication log file to which changes are appended */
578 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
580 Debug( LDAP_DEBUG_ANY,
581 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
586 be->be_replogfile = ch_strdup( cargv[1] );
588 replogfile = ch_strdup( cargv[1] );
591 /* maintain lastmodified{by,time} attributes */
592 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
594 Debug( LDAP_DEBUG_ANY,
595 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
599 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
606 be->be_lastmod = OFF;
608 global_lastmod = OFF;
611 /* set idle timeout value */
612 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
615 Debug( LDAP_DEBUG_ANY,
616 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
621 i = atoi( cargv[1] );
624 Debug( LDAP_DEBUG_ANY,
625 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
630 global_idletimeout = i;
632 /* include another config file */
633 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
635 Debug( LDAP_DEBUG_ANY,
636 "%s: line %d: missing filename in \"include <filename>\" line\n",
640 savefname = ch_strdup( cargv[1] );
643 if ( read_config( savefname ) != 0 ) {
648 lineno = savelineno - 1;
650 /* location of kerberos srvtab file */
651 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
653 Debug( LDAP_DEBUG_ANY,
654 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
658 ldap_srvtab = ch_strdup( cargv[1] );
661 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
663 Debug( LDAP_DEBUG_ANY,
664 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
666 exit( EXIT_FAILURE );
668 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
669 Debug( LDAP_DEBUG_ANY,
670 "%s: line %d: failed to load or initialize module %s\n",
671 fname, lineno, cargv[1]);
672 exit( EXIT_FAILURE );
674 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
676 Debug( LDAP_DEBUG_ANY,
677 "%s: line %d: missing path in \"modulepath <path>\" line\n",
679 exit( EXIT_FAILURE );
681 if (module_path( cargv[1] )) {
682 Debug( LDAP_DEBUG_ANY,
683 "%s: line %d: failed to set module search path to %s\n",
684 fname, lineno, cargv[1]);
685 exit( EXIT_FAILURE );
688 #endif /*SLAPD_MODULES*/
691 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
692 rc = ldap_pvt_tls_set_option( NULL,
693 LDAP_OPT_X_TLS_PROTOCOL,
698 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
699 rc = ldap_pvt_tls_set_option( NULL,
700 LDAP_OPT_X_TLS_CIPHER_SUITE,
705 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
706 rc = ldap_pvt_tls_set_option( NULL,
707 LDAP_OPT_X_TLS_CERTFILE,
712 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
713 rc = ldap_pvt_tls_set_option( NULL,
714 LDAP_OPT_X_TLS_KEYFILE,
719 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
720 rc = ldap_pvt_tls_set_option( NULL,
721 LDAP_OPT_X_TLS_CACERTDIR,
726 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
727 rc = ldap_pvt_tls_set_option( NULL,
728 LDAP_OPT_X_TLS_CACERTFILE,
732 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
733 rc = ldap_pvt_tls_set_option( NULL,
734 LDAP_OPT_X_TLS_REQUIRE_CERT,
741 /* pass anything else to the current backend info/db config routine */
744 if ( bi->bi_config == 0 ) {
745 Debug( LDAP_DEBUG_ANY,
746 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
747 fname, lineno, cargv[0] );
749 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
755 } else if ( be != NULL ) {
756 if ( be->be_config == 0 ) {
757 Debug( LDAP_DEBUG_ANY,
758 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
759 fname, lineno, cargv[0] );
761 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
768 Debug( LDAP_DEBUG_ANY,
769 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
770 fname, lineno, cargv[0] );
789 for ( token = strtok_quote( line, " \t" ); token != NULL;
790 token = strtok_quote( NULL, " \t" ) ) {
791 if ( *argcp == MAXARGS ) {
792 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
796 argv[(*argcp)++] = token;
803 strtok_quote( char *line, char *sep )
809 if ( line != NULL ) {
812 while ( *next && strchr( sep, *next ) ) {
816 if ( *next == '\0' ) {
822 for ( inquote = 0; *next; ) {
830 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
836 next + 1, strlen( next + 1 ) + 1 );
837 next++; /* dont parse the escaped character */
842 if ( strchr( sep, *next ) != NULL ) {
855 static char buf[BUFSIZ];
857 static int lmax, lcur;
859 #define CATLINE( buf ) { \
861 len = strlen( buf ); \
862 while ( lcur + len + 1 > lmax ) { \
864 line = (char *) ch_realloc( line, lmax ); \
866 strcpy( line + lcur, buf ); \
871 fp_getline( FILE *fp, int *lineno )
879 /* hack attack - keeps us from having to keep a stack of bufs... */
880 if ( strncasecmp( line, "include", 7 ) == 0 ) {
885 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
886 if ( (p = strchr( buf, '\n' )) != NULL ) {
889 if ( ! isspace( (unsigned char) buf[0] ) ) {
898 return( line[0] ? line : NULL );
902 fp_getline_init( int *lineno )