1 /* config.c - configuration file handling routines */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
14 #include <ac/socket.h>
22 * defaults for various global variables
24 int defsize = SLAPD_DEFAULT_SIZELIMIT;
25 int deftime = SLAPD_DEFAULT_TIMELIMIT;
26 AccessControl *global_acl = NULL;
27 slap_access_t global_default_access = ACL_READ;
28 int global_readonly = 0;
30 int global_lastmod = ON;
31 int global_idletimeout = 0;
32 char *global_realm = NULL;
33 char *ldap_srvtab = "";
34 char *default_passwd_hash;
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( const 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 fp_getline_init( &lineno );
78 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
79 /* skip comments and blank lines */
80 if ( line[0] == '#' || line[0] == '\0' ) {
84 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
86 /* fp_parse_line is destructive, we save a copy */
87 saveline = ch_strdup( line );
89 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
94 Debug( LDAP_DEBUG_ANY,
95 "%s: line %d: bad config line (ignored)\n",
100 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
102 Debug( LDAP_DEBUG_ANY,
103 "%s: line %d: missing type in \"backend <type>\" line\n",
109 Debug( LDAP_DEBUG_ANY,
110 "%s: line %d: backend line must appear before any database definition\n",
115 bi = backend_info( cargv[1] );
118 Debug( LDAP_DEBUG_ANY,
119 "backend %s initialization failed.n",
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",
134 be = backend_db_init( cargv[1] );
137 Debug( LDAP_DEBUG_ANY,
138 "database %s initialization failed.n",
143 /* set thread concurrency */
144 } else if ( strcasecmp( cargv[0], "concurrency" ) == 0 ) {
147 Debug( LDAP_DEBUG_ANY,
148 "%s: line %d: missing level in \"concurrency <level>\" line\n",
153 c = atoi( cargv[1] );
156 Debug( LDAP_DEBUG_ANY,
157 "%s: line %d: invalid level (%d) in \"concurrency <level>\" line\n",
162 ldap_pvt_thread_set_concurrency( c );
164 /* get pid file name */
165 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
167 Debug( LDAP_DEBUG_ANY,
168 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
173 slapd_pid_file = ch_strdup( cargv[1] );
175 /* get args file name */
176 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
178 Debug( LDAP_DEBUG_ANY,
179 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
184 slapd_args_file = ch_strdup( cargv[1] );
186 /* default password hash */
187 } else if ( strcasecmp( cargv[0], "password-hash" ) == 0 ) {
189 Debug( LDAP_DEBUG_ANY,
190 "%s: line %d: missing realm in \"password-hash <hash>\" line\n",
194 if ( default_passwd_hash != NULL ) {
195 Debug( LDAP_DEBUG_ANY,
196 "%s: line %d: already set default password_hash!\n",
201 default_passwd_hash = ch_strdup( cargv[1] );
204 /* set DIGEST realm */
205 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
207 Debug( LDAP_DEBUG_ANY,
208 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
213 be->be_realm = ch_strdup( cargv[1] );
215 } else if ( global_realm != NULL ) {
216 Debug( LDAP_DEBUG_ANY,
217 "%s: line %d: already set global realm!\n",
222 global_realm = ch_strdup( cargv[1] );
226 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
228 Debug( LDAP_DEBUG_ANY,
229 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
234 defsize = atoi( cargv[1] );
236 be->be_sizelimit = atoi( cargv[1] );
240 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
242 Debug( LDAP_DEBUG_ANY,
243 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
248 deftime = atoi( cargv[1] );
250 be->be_timelimit = atoi( cargv[1] );
253 /* set database suffix */
254 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
257 Debug( LDAP_DEBUG_ANY,
258 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
261 } else if ( cargc > 2 ) {
262 Debug( LDAP_DEBUG_ANY,
263 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
264 fname, lineno, cargv[1] );
267 Debug( LDAP_DEBUG_ANY,
268 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
270 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
271 Debug( LDAP_DEBUG_ANY,
272 "%s: line %d: suffix already served by this backend (ignored)\n",
274 } else if ( tmp_be != NULL ) {
275 Debug( LDAP_DEBUG_ANY,
276 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
277 fname, lineno, tmp_be->be_suffix[0] );
279 char *dn = ch_strdup( cargv[1] );
280 (void) dn_validate( dn );
281 charray_add( &be->be_suffix, dn );
282 (void) ldap_pvt_str2upper( dn );
283 charray_add( &be->be_nsuffix, dn );
287 /* set database suffixAlias */
288 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
291 Debug( LDAP_DEBUG_ANY,
292 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
295 } else if ( cargc < 3 ) {
296 Debug( LDAP_DEBUG_ANY,
297 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
300 } else if ( cargc > 3 ) {
301 Debug( LDAP_DEBUG_ANY,
302 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
307 Debug( LDAP_DEBUG_ANY,
308 "%s: line %d: suffixAlias line"
309 " must appear inside a database definition (ignored)\n",
311 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
312 Debug( LDAP_DEBUG_ANY,
313 "%s: line %d: suffixAlias served by"
314 " a preceeding backend \"%s\" (ignored)\n",
315 fname, lineno, tmp_be->be_suffix[0] );
317 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
318 Debug( LDAP_DEBUG_ANY,
319 "%s: line %d: suffixAlias derefs to differnet backend"
320 " a preceeding backend \"%s\" (ignored)\n",
321 fname, lineno, tmp_be->be_suffix[0] );
324 char *alias, *aliased_dn;
326 alias = ch_strdup( cargv[1] );
327 (void) dn_normalize( alias );
329 aliased_dn = ch_strdup( cargv[2] );
330 (void) dn_normalize( aliased_dn );
332 charray_add( &be->be_suffixAlias, alias );
333 charray_add( &be->be_suffixAlias, aliased_dn );
339 /* set max deref depth */
340 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
343 Debug( LDAP_DEBUG_ANY,
344 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
349 Debug( LDAP_DEBUG_ANY,
350 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
352 } else if ((i = atoi(cargv[1])) < 0) {
353 Debug( LDAP_DEBUG_ANY,
354 "%s: line %d: depth must be positive (ignored)\n",
358 be->be_max_deref_depth = i;
362 /* set magic "root" dn for this database */
363 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
365 Debug( LDAP_DEBUG_ANY,
366 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
371 Debug( LDAP_DEBUG_ANY,
372 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
375 be->be_root_dn = ch_strdup( cargv[1] );
376 be->be_root_ndn = ch_strdup( cargv[1] );
378 if( dn_normalize( be->be_root_ndn ) == NULL ) {
379 free( be->be_root_dn );
380 free( be->be_root_ndn );
381 Debug( LDAP_DEBUG_ANY,
382 "%s: line %d: rootdn DN is invalid\n",
388 /* set super-secret magic database password */
389 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
391 Debug( LDAP_DEBUG_ANY,
392 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
397 Debug( LDAP_DEBUG_ANY,
398 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
401 be->be_root_pw.bv_val = ch_strdup( cargv[1] );
402 be->be_root_pw.bv_len = strlen( be->be_root_pw.bv_val );
405 /* make this database read-only */
406 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
408 Debug( LDAP_DEBUG_ANY,
409 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
414 global_readonly = (strcasecmp( cargv[1], "on" ) == 0);
416 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
423 /* where to send clients when we don't hold it */
424 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
426 Debug( LDAP_DEBUG_ANY,
427 "%s: line %d: missing URL in \"referral <URL>\" line\n",
432 vals[0]->bv_val = cargv[1];
433 vals[0]->bv_len = strlen( vals[0]->bv_val );
434 value_add( &default_referral, vals );
436 /* specify an Object Identifier macro */
437 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
438 parse_oidm( fname, lineno, cargc, cargv );
440 /* specify an objectclass */
441 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
442 if ( *cargv[1] == '(' ) {
444 p = strchr(saveline,'(');
445 parse_oc( fname, lineno, p, cargv );
447 Debug( LDAP_DEBUG_ANY,
448 "%s: line %d: old objectclass format not supported.\n",
452 /* specify an attribute type */
453 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
454 || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
456 if ( *cargv[1] == '(' ) {
458 p = strchr(saveline,'(');
459 parse_at( fname, lineno, p, cargv );
461 Debug( LDAP_DEBUG_ANY,
462 "%s: line %d: old attribute type format not supported.\n",
466 /* turn on/off schema checking */
467 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
469 Debug( LDAP_DEBUG_ANY,
470 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
474 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
475 global_schemacheck = 0;
477 global_schemacheck = 1;
480 /* specify access control info */
481 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
482 parse_acl( be, fname, lineno, cargc, cargv );
484 /* specify default access control info */
485 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
486 slap_access_t access;
489 Debug( LDAP_DEBUG_ANY,
490 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
495 access = str2access( cargv[1] );
497 if ( access == ACL_INVALID_ACCESS ) {
498 Debug( LDAP_DEBUG_ANY,
499 "%s: line %d: bad access level \"%s\", "
500 "expecting none|auth|compare|search|read|write\n",
501 fname, lineno, cargv[1] );
506 global_default_access = access;
508 be->be_dfltaccess = access;
511 /* debug level to log things to syslog */
512 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
514 Debug( LDAP_DEBUG_ANY,
515 "%s: line %d: missing level in \"loglevel <level>\" line\n",
519 ldap_syslog = atoi( cargv[1] );
521 /* list of replicas of the data in this backend (master only) */
522 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
524 Debug( LDAP_DEBUG_ANY,
525 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
530 Debug( LDAP_DEBUG_ANY,
531 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
534 for ( i = 1; i < cargc; i++ ) {
535 if ( strncasecmp( cargv[i], "host=", 5 )
537 charray_add( &be->be_replica,
543 Debug( LDAP_DEBUG_ANY,
544 "%s: line %d: missing host in \"replica\" line (ignored)\n",
549 /* dn of master entity allowed to write to replica */
550 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
552 Debug( LDAP_DEBUG_ANY,
553 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
558 Debug( LDAP_DEBUG_ANY,
559 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
562 be->be_update_ndn = ch_strdup( cargv[1] );
563 if( dn_normalize( be->be_update_ndn ) == NULL ) {
564 Debug( LDAP_DEBUG_ANY,
565 "%s: line %d: updatedn DN is invalid\n",
571 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
573 Debug( LDAP_DEBUG_ANY,
574 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
579 Debug( LDAP_DEBUG_ANY,
580 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
582 } else if ( be->be_update_ndn == NULL ) {
583 Debug( LDAP_DEBUG_ANY,
584 "%s: line %d: updateref line must after updatedn (ignored)\n",
587 vals[0]->bv_val = cargv[1];
588 vals[0]->bv_len = strlen( vals[0]->bv_val );
589 value_add( &be->be_update_refs, vals );
592 /* replication log file to which changes are appended */
593 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
595 Debug( LDAP_DEBUG_ANY,
596 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
601 be->be_replogfile = ch_strdup( cargv[1] );
603 replogfile = ch_strdup( cargv[1] );
606 /* maintain lastmodified{by,time} attributes */
607 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
609 Debug( LDAP_DEBUG_ANY,
610 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
614 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
621 be->be_lastmod = OFF;
623 global_lastmod = OFF;
626 /* set idle timeout value */
627 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
630 Debug( LDAP_DEBUG_ANY,
631 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
636 i = atoi( cargv[1] );
639 Debug( LDAP_DEBUG_ANY,
640 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
645 global_idletimeout = i;
647 /* include another config file */
648 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
650 Debug( LDAP_DEBUG_ANY,
651 "%s: line %d: missing filename in \"include <filename>\" line\n",
655 savefname = ch_strdup( cargv[1] );
658 if ( read_config( savefname ) != 0 ) {
663 lineno = savelineno - 1;
665 /* location of kerberos srvtab file */
666 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
668 Debug( LDAP_DEBUG_ANY,
669 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
673 ldap_srvtab = ch_strdup( cargv[1] );
676 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
678 Debug( LDAP_DEBUG_ANY,
679 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
681 exit( EXIT_FAILURE );
683 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
684 Debug( LDAP_DEBUG_ANY,
685 "%s: line %d: failed to load or initialize module %s\n",
686 fname, lineno, cargv[1]);
687 exit( EXIT_FAILURE );
689 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
691 Debug( LDAP_DEBUG_ANY,
692 "%s: line %d: missing path in \"modulepath <path>\" line\n",
694 exit( EXIT_FAILURE );
696 if (module_path( cargv[1] )) {
697 Debug( LDAP_DEBUG_ANY,
698 "%s: line %d: failed to set module search path to %s\n",
699 fname, lineno, cargv[1]);
700 exit( EXIT_FAILURE );
703 #endif /*SLAPD_MODULES*/
706 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
707 rc = ldap_pvt_tls_set_option( NULL,
708 LDAP_OPT_X_TLS_PROTOCOL,
713 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
714 rc = ldap_pvt_tls_set_option( NULL,
715 LDAP_OPT_X_TLS_CIPHER_SUITE,
720 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
721 rc = ldap_pvt_tls_set_option( NULL,
722 LDAP_OPT_X_TLS_CERTFILE,
727 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
728 rc = ldap_pvt_tls_set_option( NULL,
729 LDAP_OPT_X_TLS_KEYFILE,
734 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
735 rc = ldap_pvt_tls_set_option( NULL,
736 LDAP_OPT_X_TLS_CACERTDIR,
741 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
742 rc = ldap_pvt_tls_set_option( NULL,
743 LDAP_OPT_X_TLS_CACERTFILE,
747 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
748 rc = ldap_pvt_tls_set_option( NULL,
749 LDAP_OPT_X_TLS_REQUIRE_CERT,
756 /* pass anything else to the current backend info/db config routine */
759 if ( bi->bi_config == 0 ) {
760 Debug( LDAP_DEBUG_ANY,
761 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
762 fname, lineno, cargv[0] );
764 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
770 } else if ( be != NULL ) {
771 if ( be->be_config == 0 ) {
772 Debug( LDAP_DEBUG_ANY,
773 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
774 fname, lineno, cargv[0] );
776 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
783 Debug( LDAP_DEBUG_ANY,
784 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
785 fname, lineno, cargv[0] );
804 for ( token = strtok_quote( line, " \t" ); token != NULL;
805 token = strtok_quote( NULL, " \t" ) ) {
806 if ( *argcp == MAXARGS ) {
807 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
811 argv[(*argcp)++] = token;
818 strtok_quote( char *line, char *sep )
824 if ( line != NULL ) {
827 while ( *next && strchr( sep, *next ) ) {
831 if ( *next == '\0' ) {
837 for ( inquote = 0; *next; ) {
845 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
851 next + 1, strlen( next + 1 ) + 1 );
852 next++; /* dont parse the escaped character */
857 if ( strchr( sep, *next ) != NULL ) {
870 static char buf[BUFSIZ];
872 static int lmax, lcur;
874 #define CATLINE( buf ) { \
876 len = strlen( buf ); \
877 while ( lcur + len + 1 > lmax ) { \
879 line = (char *) ch_realloc( line, lmax ); \
881 strcpy( line + lcur, buf ); \
886 fp_getline( FILE *fp, int *lineno )
894 /* hack attack - keeps us from having to keep a stack of bufs... */
895 if ( strncasecmp( line, "include", 7 ) == 0 ) {
900 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
901 if ( (p = strchr( buf, '\n' )) != NULL ) {
904 if ( ! isspace( (unsigned char) buf[0] ) ) {
913 return( line[0] ? line : NULL );
917 fp_getline_init( int *lineno )