1 /* config.c - configuration file handling routines */
10 #include <ac/string.h>
12 #include <ac/socket.h>
14 #include "ldap_defaults.h"
20 * defaults for various global variables
22 int defsize = SLAPD_DEFAULT_SIZELIMIT;
23 int deftime = SLAPD_DEFAULT_TIMELIMIT;
24 AccessControl *global_acl = NULL;
25 int global_default_access = ACL_READ;
28 int global_idletimeout = 0;
29 char *global_realm = NULL;
30 char *ldap_srvtab = "";
32 char *slapd_pid_file = NULL;
33 char *slapd_args_file = NULL;
35 static char *fp_getline(FILE *fp, int *lineno);
36 static void fp_getline_init(int *lineno);
37 static int fp_parse_line(char *line, int *argcp, char **argv);
39 static char *strtok_quote(char *line, char *sep);
42 read_config( char *fname )
45 char *line, *savefname, *saveline;
46 int cargc, savelineno;
52 struct berval *vals[2];
55 static BackendInfo *bi = NULL;
56 static BackendDB *be = NULL;
61 if ( (fp = fopen( fname, "r" )) == NULL ) {
63 Debug( LDAP_DEBUG_ANY,
64 "could not open config file \"%s\" - absolute path?\n",
70 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
72 if ( schema_init( ) != 0 ) {
73 Debug( LDAP_DEBUG_ANY,
74 "error initializing the schema\n",
79 fp_getline_init( &lineno );
81 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
82 /* skip comments and blank lines */
83 if ( line[0] == '#' || line[0] == '\0' ) {
87 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
89 /* fp_parse_line is destructive, we save a copy */
90 saveline = ch_strdup( line );
92 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
97 Debug( LDAP_DEBUG_ANY,
98 "%s: line %d: bad config line (ignored)\n",
103 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
105 Debug( LDAP_DEBUG_ANY,
106 "%s: line %d: missing type in \"backend <type>\" line\n",
112 Debug( LDAP_DEBUG_ANY,
113 "%s: line %d: backend line must appear before any database definition\n",
118 bi = backend_info( cargv[1] );
120 /* start of a new database definition */
121 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
123 Debug( LDAP_DEBUG_ANY,
124 "%s: line %d: missing type in \"database <type>\" line\n",
129 be = backend_db_init( cargv[1] );
131 /* assign a default depth limit for alias deref */
132 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
134 /* get pid file name */
135 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
137 Debug( LDAP_DEBUG_ANY,
138 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
143 slapd_pid_file = ch_strdup( cargv[1] );
145 /* get args file name */
146 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
148 Debug( LDAP_DEBUG_ANY,
149 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
154 slapd_args_file = ch_strdup( cargv[1] );
156 /* set DIGEST realm */
157 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
159 Debug( LDAP_DEBUG_ANY,
160 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
165 be->be_realm = ch_strdup( cargv[1] );
167 } else if ( global_realm != NULL ) {
168 Debug( LDAP_DEBUG_ANY,
169 "%s: line %d: already set global realm!\n",
174 global_realm = ch_strdup( cargv[1] );
178 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
180 Debug( LDAP_DEBUG_ANY,
181 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
186 defsize = atoi( cargv[1] );
188 be->be_sizelimit = atoi( cargv[1] );
192 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
194 Debug( LDAP_DEBUG_ANY,
195 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
200 deftime = atoi( cargv[1] );
202 be->be_timelimit = atoi( cargv[1] );
205 /* set database suffix */
206 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
208 Debug( LDAP_DEBUG_ANY,
209 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
212 } else if ( cargc > 2 ) {
213 Debug( LDAP_DEBUG_ANY,
214 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
215 fname, lineno, cargv[1] );
218 Debug( LDAP_DEBUG_ANY,
219 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
222 char *dn = ch_strdup( cargv[1] );
223 (void) dn_normalize( dn );
224 charray_add( &be->be_suffix, dn );
225 (void) str2upper( dn );
226 charray_add( &be->be_nsuffix, dn );
230 /* set max deref depth */
231 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
234 Debug( LDAP_DEBUG_ANY,
235 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
240 Debug( LDAP_DEBUG_ANY,
241 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
243 } else if ((i = atoi(cargv[1])) < 0) {
244 Debug( LDAP_DEBUG_ANY,
245 "%s: line %d: depth must be positive (ignored)\n",
249 be->be_max_deref_depth = i;
253 /* set magic "root" dn for this database */
254 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
256 Debug( LDAP_DEBUG_ANY,
257 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
262 Debug( LDAP_DEBUG_ANY,
263 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
266 be->be_root_dn = ch_strdup( cargv[1] );
267 be->be_root_ndn = ch_strdup( cargv[1] );
269 if( dn_normalize_case( be->be_root_ndn ) == NULL ) {
270 free( be->be_root_dn );
271 free( be->be_root_ndn );
272 Debug( LDAP_DEBUG_ANY,
273 "%s: line %d: rootdn DN is invalid\n",
279 /* set super-secret magic database password */
280 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
282 Debug( LDAP_DEBUG_ANY,
283 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
292 be->be_root_pw = ch_strdup( cargv[1] );
295 /* make this database read-only */
296 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
298 Debug( LDAP_DEBUG_ANY,
299 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
304 Debug( LDAP_DEBUG_ANY,
305 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
308 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
315 /* where to send clients when we don't hold it */
316 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
318 Debug( LDAP_DEBUG_ANY,
319 "%s: line %d: missing URL in \"referral <URL>\" line\n",
324 vals[0]->bv_val = cargv[1];
325 vals[0]->bv_len = strlen( vals[0]->bv_val );
326 value_add( &default_referral, vals );
329 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
333 Debug( LDAP_DEBUG_ANY,
334 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
339 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
340 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
341 : ch_strdup( cargv[1] ) );
343 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
344 Debug( LDAP_DEBUG_ANY,
346 ? "%s: line %d: bad locale \"%s\"\n"
347 : "%s: line %d: bad locale\n"),
348 fname, lineno, locale );
352 Debug( LDAP_DEBUG_ANY,
353 "%s: line %d: \"locale\" unsupported\n",
357 /* specify an objectclass */
358 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
359 if ( *cargv[1] == '(' ) {
361 p = strchr(saveline,'(');
362 parse_oc( fname, lineno, p );
364 parse_oc_old( be, fname, lineno, cargc, cargv );
367 /* specify an attribute */
368 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
369 if ( *cargv[1] == '(' ) {
371 p = strchr(saveline,'(');
372 parse_at( fname, lineno, p );
374 attr_syntax_config( fname, lineno, cargc - 1,
378 /* turn on/off schema checking */
379 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
381 Debug( LDAP_DEBUG_ANY,
382 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
386 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
387 global_schemacheck = 0;
389 global_schemacheck = 1;
392 /* specify access control info */
393 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
394 parse_acl( be, fname, lineno, cargc, cargv );
396 /* specify default access control info */
397 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
399 Debug( LDAP_DEBUG_ANY,
400 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
405 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
406 str2access(cargv[1]))) )
408 Debug( LDAP_DEBUG_ANY,
409 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
410 fname, lineno, cargv[1] );
414 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
415 str2access(cargv[1]))) )
417 Debug( LDAP_DEBUG_ANY,
418 "%s: line %d: bad access \"%s\", "
419 "expecting [self]{none|compare|search|read|write}\n",
420 fname, lineno, cargv[1] );
425 /* debug level to log things to syslog */
426 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
428 Debug( LDAP_DEBUG_ANY,
429 "%s: line %d: missing level in \"loglevel <level>\" line\n",
433 ldap_syslog = atoi( cargv[1] );
435 /* list of replicas of the data in this backend (master only) */
436 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
438 Debug( LDAP_DEBUG_ANY,
439 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
444 Debug( LDAP_DEBUG_ANY,
445 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
448 for ( i = 1; i < cargc; i++ ) {
449 if ( strncasecmp( cargv[i], "host=", 5 )
451 charray_add( &be->be_replica,
457 Debug( LDAP_DEBUG_ANY,
458 "%s: line %d: missing host in \"replica\" line (ignored)\n",
463 /* dn of master entity allowed to write to replica */
464 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
466 Debug( LDAP_DEBUG_ANY,
467 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
472 Debug( LDAP_DEBUG_ANY,
473 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
476 be->be_update_ndn = ch_strdup( cargv[1] );
477 if( dn_normalize_case( be->be_update_ndn ) == NULL ) {
478 Debug( LDAP_DEBUG_ANY,
479 "%s: line %d: updatedn DN is invalid\n",
485 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
487 Debug( LDAP_DEBUG_ANY,
488 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
493 Debug( LDAP_DEBUG_ANY,
494 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
496 } else if ( be->be_update_ndn == NULL ) {
497 Debug( LDAP_DEBUG_ANY,
498 "%s: line %d: updateref line must after updatedn (ignored)\n",
501 vals[0]->bv_val = cargv[1];
502 vals[0]->bv_len = strlen( vals[0]->bv_val );
503 value_add( &be->be_update_refs, vals );
506 /* replication log file to which changes are appended */
507 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
509 Debug( LDAP_DEBUG_ANY,
510 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
515 be->be_replogfile = ch_strdup( cargv[1] );
517 replogfile = ch_strdup( cargv[1] );
520 /* maintain lastmodified{by,time} attributes */
521 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
523 Debug( LDAP_DEBUG_ANY,
524 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
528 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
535 be->be_lastmod = OFF;
537 global_lastmod = OFF;
540 /* set idle timeout value */
541 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
544 Debug( LDAP_DEBUG_ANY,
545 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
550 i = atoi( cargv[1] );
553 Debug( LDAP_DEBUG_ANY,
554 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
559 global_idletimeout = i;
561 /* include another config file */
562 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
564 Debug( LDAP_DEBUG_ANY,
565 "%s: line %d: missing filename in \"include <filename>\" line\n",
569 savefname = ch_strdup( cargv[1] );
572 if ( read_config( savefname ) != 0 ) {
577 lineno = savelineno - 1;
579 /* location of kerberos srvtab file */
580 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
582 Debug( LDAP_DEBUG_ANY,
583 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
587 ldap_srvtab = ch_strdup( cargv[1] );
590 } else if (strcasecmp( cargv[0], "loadmodule") == 0 ) {
592 Debug( LDAP_DEBUG_ANY,
593 "%s: line %d: missing filename in \"loadmodule <filename>\" line\n",
597 if (!load_module(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
598 Debug( LDAP_DEBUG_ANY,
599 "%s: line %d: failed to load or initialize module %s\n",
600 fname, lineno, cargv[1]);
604 #endif /*SLAPD_MODULES*/
607 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
608 rc = ldap_pvt_tls_set_option( NULL,
609 LDAP_OPT_X_TLS_PROTOCOL,
614 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
615 rc = ldap_pvt_tls_set_option( NULL,
616 LDAP_OPT_X_TLS_CIPHER_SUITE,
621 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
622 rc = ldap_pvt_tls_set_option( NULL,
623 LDAP_OPT_X_TLS_CERTFILE,
628 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
629 rc = ldap_pvt_tls_set_option( NULL,
630 LDAP_OPT_X_TLS_KEYFILE,
635 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
636 rc = ldap_pvt_tls_set_option( NULL,
637 LDAP_OPT_X_TLS_CACERTDIR,
642 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
643 rc = ldap_pvt_tls_set_option( NULL,
644 LDAP_OPT_X_TLS_CACERTFILE,
648 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
649 rc = ldap_pvt_tls_set_option( NULL,
650 LDAP_OPT_X_TLS_REQUIRE_CERT,
657 /* pass anything else to the current backend info/db config routine */
660 if ( bi->bi_config == 0 ) {
661 Debug( LDAP_DEBUG_ANY,
662 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
663 fname, lineno, cargv[0] );
665 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
671 } else if ( be != NULL ) {
672 if ( be->be_config == 0 ) {
673 Debug( LDAP_DEBUG_ANY,
674 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
675 fname, lineno, cargv[0] );
677 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
684 Debug( LDAP_DEBUG_ANY,
685 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
686 fname, lineno, cargv[0] );
705 for ( token = strtok_quote( line, " \t" ); token != NULL;
706 token = strtok_quote( NULL, " \t" ) ) {
707 if ( *argcp == MAXARGS ) {
708 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
712 argv[(*argcp)++] = token;
719 strtok_quote( char *line, char *sep )
725 if ( line != NULL ) {
728 while ( *next && strchr( sep, *next ) ) {
732 if ( *next == '\0' ) {
738 for ( inquote = 0; *next; ) {
746 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
752 next + 1, strlen( next + 1 ) + 1 );
753 next++; /* dont parse the escaped character */
758 if ( strchr( sep, *next ) != NULL ) {
771 static char buf[BUFSIZ];
773 static int lmax, lcur;
775 #define CATLINE( buf ) { \
777 len = strlen( buf ); \
778 while ( lcur + len + 1 > lmax ) { \
780 line = (char *) ch_realloc( line, lmax ); \
782 strcpy( line + lcur, buf ); \
787 fp_getline( FILE *fp, int *lineno )
795 /* hack attack - keeps us from having to keep a stack of bufs... */
796 if ( strncasecmp( line, "include", 7 ) == 0 ) {
801 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
802 if ( (p = strchr( buf, '\n' )) != NULL ) {
805 if ( ! isspace( (unsigned char) buf[0] ) ) {
814 return( line[0] ? line : NULL );
818 fp_getline_init( int *lineno )