1 /* config.c - configuration file handling routines */
4 * Copyright 1998-2000 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 slap_access_t 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 = "";
37 char *default_passwd_hash;
39 char *slapd_pid_file = NULL;
40 char *slapd_args_file = NULL;
42 static char *fp_getline(FILE *fp, int *lineno);
43 static void fp_getline_init(int *lineno);
44 static int fp_parse_line(char *line, int *argcp, char **argv);
46 static char *strtok_quote(char *line, char *sep);
49 read_config( const char *fname )
52 char *line, *savefname, *saveline;
53 int cargc, savelineno;
59 struct berval *vals[2];
62 static BackendInfo *bi = NULL;
63 static BackendDB *be = NULL;
68 if ( (fp = fopen( fname, "r" )) == NULL ) {
70 Debug( LDAP_DEBUG_ANY,
71 "could not open config file \"%s\" - absolute path?\n",
77 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
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] );
121 Debug( LDAP_DEBUG_ANY,
122 "backend %s initialization failed.n",
127 /* start of a new database definition */
128 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
130 Debug( LDAP_DEBUG_ANY,
131 "%s: line %d: missing type in \"database <type>\" line\n",
137 be = backend_db_init( cargv[1] );
140 Debug( LDAP_DEBUG_ANY,
141 "database %s initialization failed.n",
146 /* get pid file name */
147 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
149 Debug( LDAP_DEBUG_ANY,
150 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
155 slapd_pid_file = ch_strdup( cargv[1] );
157 /* get args file name */
158 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
160 Debug( LDAP_DEBUG_ANY,
161 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
166 slapd_args_file = ch_strdup( cargv[1] );
168 /* default password hash */
169 } else if ( strcasecmp( cargv[0], "password-hash" ) == 0 ) {
171 Debug( LDAP_DEBUG_ANY,
172 "%s: line %d: missing realm in \"password-hash <hash>\" line\n",
176 if ( default_passwd_hash != NULL ) {
177 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: already set default password_hash!\n",
183 default_passwd_hash = ch_strdup( cargv[1] );
186 /* set DIGEST realm */
187 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
189 Debug( LDAP_DEBUG_ANY,
190 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
195 be->be_realm = ch_strdup( cargv[1] );
197 } else if ( global_realm != NULL ) {
198 Debug( LDAP_DEBUG_ANY,
199 "%s: line %d: already set global realm!\n",
204 global_realm = ch_strdup( cargv[1] );
208 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
210 Debug( LDAP_DEBUG_ANY,
211 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
216 defsize = atoi( cargv[1] );
218 be->be_sizelimit = atoi( cargv[1] );
222 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
224 Debug( LDAP_DEBUG_ANY,
225 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
230 deftime = atoi( cargv[1] );
232 be->be_timelimit = atoi( cargv[1] );
235 /* set database suffix */
236 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
239 Debug( LDAP_DEBUG_ANY,
240 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
243 } else if ( cargc > 2 ) {
244 Debug( LDAP_DEBUG_ANY,
245 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
246 fname, lineno, cargv[1] );
249 Debug( LDAP_DEBUG_ANY,
250 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
252 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
253 Debug( LDAP_DEBUG_ANY,
254 "%s: line %d: suffix already served by this backend (ignored)\n",
256 } else if ( tmp_be != NULL ) {
257 Debug( LDAP_DEBUG_ANY,
258 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
259 fname, lineno, tmp_be->be_suffix[0] );
261 char *dn = ch_strdup( cargv[1] );
262 (void) dn_validate( dn );
263 charray_add( &be->be_suffix, dn );
264 (void) ldap_pvt_str2upper( dn );
265 charray_add( &be->be_nsuffix, dn );
269 /* set database suffixAlias */
270 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
273 Debug( LDAP_DEBUG_ANY,
274 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
277 } else if ( cargc < 3 ) {
278 Debug( LDAP_DEBUG_ANY,
279 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
282 } else if ( cargc > 3 ) {
283 Debug( LDAP_DEBUG_ANY,
284 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
289 Debug( LDAP_DEBUG_ANY,
290 "%s: line %d: suffixAlias line"
291 " must appear inside a database definition (ignored)\n",
293 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
294 Debug( LDAP_DEBUG_ANY,
295 "%s: line %d: suffixAlias served by"
296 " a preceeding backend \"%s\" (ignored)\n",
297 fname, lineno, tmp_be->be_suffix[0] );
299 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
300 Debug( LDAP_DEBUG_ANY,
301 "%s: line %d: suffixAlias derefs to differnet backend"
302 " a preceeding backend \"%s\" (ignored)\n",
303 fname, lineno, tmp_be->be_suffix[0] );
306 char *alias, *aliased_dn;
308 alias = ch_strdup( cargv[1] );
309 (void) dn_normalize( alias );
311 aliased_dn = ch_strdup( cargv[2] );
312 (void) dn_normalize( aliased_dn );
314 charray_add( &be->be_suffixAlias, alias );
315 charray_add( &be->be_suffixAlias, aliased_dn );
321 /* set max deref depth */
322 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
325 Debug( LDAP_DEBUG_ANY,
326 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
331 Debug( LDAP_DEBUG_ANY,
332 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
334 } else if ((i = atoi(cargv[1])) < 0) {
335 Debug( LDAP_DEBUG_ANY,
336 "%s: line %d: depth must be positive (ignored)\n",
340 be->be_max_deref_depth = i;
344 /* set magic "root" dn for this database */
345 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
347 Debug( LDAP_DEBUG_ANY,
348 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
353 Debug( LDAP_DEBUG_ANY,
354 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
357 be->be_root_dn = ch_strdup( cargv[1] );
358 be->be_root_ndn = ch_strdup( cargv[1] );
360 if( dn_normalize( be->be_root_ndn ) == NULL ) {
361 free( be->be_root_dn );
362 free( be->be_root_ndn );
363 Debug( LDAP_DEBUG_ANY,
364 "%s: line %d: rootdn DN is invalid\n",
370 /* set super-secret magic database password */
371 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
373 Debug( LDAP_DEBUG_ANY,
374 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
379 Debug( LDAP_DEBUG_ANY,
380 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
383 be->be_root_pw.bv_val = ch_strdup( cargv[1] );
384 be->be_root_pw.bv_len = strlen( be->be_root_pw.bv_val );
387 /* make this database read-only */
388 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
390 Debug( LDAP_DEBUG_ANY,
391 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
396 global_readonly = (strcasecmp( cargv[1], "on" ) == 0);
398 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
405 /* where to send clients when we don't hold it */
406 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
408 Debug( LDAP_DEBUG_ANY,
409 "%s: line %d: missing URL in \"referral <URL>\" line\n",
414 vals[0]->bv_val = cargv[1];
415 vals[0]->bv_len = strlen( vals[0]->bv_val );
416 value_add( &default_referral, vals );
419 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
423 Debug( LDAP_DEBUG_ANY,
424 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
429 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
430 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
431 : ch_strdup( cargv[1] ) );
433 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
434 Debug( LDAP_DEBUG_ANY,
436 ? "%s: line %d: bad locale \"%s\"\n"
437 : "%s: line %d: bad locale\n"),
438 fname, lineno, locale );
442 Debug( LDAP_DEBUG_ANY,
443 "%s: line %d: \"locale\" unsupported\n",
447 /* specify an Object Identifier macro */
448 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
449 parse_oidm( fname, lineno, cargc, cargv );
451 /* specify an objectclass */
452 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
453 if ( *cargv[1] == '(' ) {
455 p = strchr(saveline,'(');
456 parse_oc( fname, lineno, p, cargv );
458 #ifdef SLAPD_SCHEMA_NOT_COMPAT
459 Debug( LDAP_DEBUG_ANY,
460 "%s: line %d: old objectclass format not supported.\n",
463 parse_oc_old( be, fname, lineno, cargc, cargv );
467 /* specify an attribute type */
468 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
469 || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
471 if ( *cargv[1] == '(' ) {
473 p = strchr(saveline,'(');
474 parse_at( fname, lineno, p, cargv );
476 #ifdef SLAPD_SCHEMA_NOT_COMPAT
477 Debug( LDAP_DEBUG_ANY,
478 "%s: line %d: old attribute type format not supported.\n",
481 at_config( fname, lineno, cargc - 1,
486 /* turn on/off schema checking */
487 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
489 Debug( LDAP_DEBUG_ANY,
490 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
494 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
495 global_schemacheck = 0;
497 global_schemacheck = 1;
500 /* specify access control info */
501 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
502 parse_acl( be, fname, lineno, cargc, cargv );
504 /* specify default access control info */
505 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
506 slap_access_t access;
509 Debug( LDAP_DEBUG_ANY,
510 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
515 access = str2access( cargv[1] );
517 if ( access == ACL_INVALID_ACCESS ) {
518 Debug( LDAP_DEBUG_ANY,
519 "%s: line %d: bad access level \"%s\", "
520 "expecting none|auth|compare|search|read|write\n",
521 fname, lineno, cargv[1] );
526 global_default_access = access;
528 be->be_dfltaccess = access;
531 /* debug level to log things to syslog */
532 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
534 Debug( LDAP_DEBUG_ANY,
535 "%s: line %d: missing level in \"loglevel <level>\" line\n",
539 ldap_syslog = atoi( cargv[1] );
541 /* list of replicas of the data in this backend (master only) */
542 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
544 Debug( LDAP_DEBUG_ANY,
545 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
550 Debug( LDAP_DEBUG_ANY,
551 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
554 for ( i = 1; i < cargc; i++ ) {
555 if ( strncasecmp( cargv[i], "host=", 5 )
557 charray_add( &be->be_replica,
563 Debug( LDAP_DEBUG_ANY,
564 "%s: line %d: missing host in \"replica\" line (ignored)\n",
569 /* dn of master entity allowed to write to replica */
570 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
572 Debug( LDAP_DEBUG_ANY,
573 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
578 Debug( LDAP_DEBUG_ANY,
579 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
582 be->be_update_ndn = ch_strdup( cargv[1] );
583 if( dn_normalize( be->be_update_ndn ) == NULL ) {
584 Debug( LDAP_DEBUG_ANY,
585 "%s: line %d: updatedn DN is invalid\n",
591 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
593 Debug( LDAP_DEBUG_ANY,
594 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
599 Debug( LDAP_DEBUG_ANY,
600 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
602 } else if ( be->be_update_ndn == NULL ) {
603 Debug( LDAP_DEBUG_ANY,
604 "%s: line %d: updateref line must after updatedn (ignored)\n",
607 vals[0]->bv_val = cargv[1];
608 vals[0]->bv_len = strlen( vals[0]->bv_val );
609 value_add( &be->be_update_refs, vals );
612 /* replication log file to which changes are appended */
613 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
615 Debug( LDAP_DEBUG_ANY,
616 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
621 be->be_replogfile = ch_strdup( cargv[1] );
623 replogfile = ch_strdup( cargv[1] );
626 /* maintain lastmodified{by,time} attributes */
627 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
629 Debug( LDAP_DEBUG_ANY,
630 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
634 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
641 be->be_lastmod = OFF;
643 global_lastmod = OFF;
646 /* set idle timeout value */
647 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
650 Debug( LDAP_DEBUG_ANY,
651 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
656 i = atoi( cargv[1] );
659 Debug( LDAP_DEBUG_ANY,
660 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
665 global_idletimeout = i;
667 /* include another config file */
668 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
670 Debug( LDAP_DEBUG_ANY,
671 "%s: line %d: missing filename in \"include <filename>\" line\n",
675 savefname = ch_strdup( cargv[1] );
678 if ( read_config( savefname ) != 0 ) {
683 lineno = savelineno - 1;
685 /* location of kerberos srvtab file */
686 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
688 Debug( LDAP_DEBUG_ANY,
689 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
693 ldap_srvtab = ch_strdup( cargv[1] );
696 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
698 Debug( LDAP_DEBUG_ANY,
699 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
701 exit( EXIT_FAILURE );
703 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
704 Debug( LDAP_DEBUG_ANY,
705 "%s: line %d: failed to load or initialize module %s\n",
706 fname, lineno, cargv[1]);
707 exit( EXIT_FAILURE );
709 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
711 Debug( LDAP_DEBUG_ANY,
712 "%s: line %d: missing path in \"modulepath <path>\" line\n",
714 exit( EXIT_FAILURE );
716 if (module_path( cargv[1] )) {
717 Debug( LDAP_DEBUG_ANY,
718 "%s: line %d: failed to set module search path to %s\n",
719 fname, lineno, cargv[1]);
720 exit( EXIT_FAILURE );
723 #endif /*SLAPD_MODULES*/
726 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
727 rc = ldap_pvt_tls_set_option( NULL,
728 LDAP_OPT_X_TLS_PROTOCOL,
733 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
734 rc = ldap_pvt_tls_set_option( NULL,
735 LDAP_OPT_X_TLS_CIPHER_SUITE,
740 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
741 rc = ldap_pvt_tls_set_option( NULL,
742 LDAP_OPT_X_TLS_CERTFILE,
747 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
748 rc = ldap_pvt_tls_set_option( NULL,
749 LDAP_OPT_X_TLS_KEYFILE,
754 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
755 rc = ldap_pvt_tls_set_option( NULL,
756 LDAP_OPT_X_TLS_CACERTDIR,
761 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
762 rc = ldap_pvt_tls_set_option( NULL,
763 LDAP_OPT_X_TLS_CACERTFILE,
767 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
768 rc = ldap_pvt_tls_set_option( NULL,
769 LDAP_OPT_X_TLS_REQUIRE_CERT,
776 /* pass anything else to the current backend info/db config routine */
779 if ( bi->bi_config == 0 ) {
780 Debug( LDAP_DEBUG_ANY,
781 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
782 fname, lineno, cargv[0] );
784 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
790 } else if ( be != NULL ) {
791 if ( be->be_config == 0 ) {
792 Debug( LDAP_DEBUG_ANY,
793 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
794 fname, lineno, cargv[0] );
796 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
803 Debug( LDAP_DEBUG_ANY,
804 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
805 fname, lineno, cargv[0] );
824 for ( token = strtok_quote( line, " \t" ); token != NULL;
825 token = strtok_quote( NULL, " \t" ) ) {
826 if ( *argcp == MAXARGS ) {
827 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
831 argv[(*argcp)++] = token;
838 strtok_quote( char *line, char *sep )
844 if ( line != NULL ) {
847 while ( *next && strchr( sep, *next ) ) {
851 if ( *next == '\0' ) {
857 for ( inquote = 0; *next; ) {
865 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
871 next + 1, strlen( next + 1 ) + 1 );
872 next++; /* dont parse the escaped character */
877 if ( strchr( sep, *next ) != NULL ) {
890 static char buf[BUFSIZ];
892 static int lmax, lcur;
894 #define CATLINE( buf ) { \
896 len = strlen( buf ); \
897 while ( lcur + len + 1 > lmax ) { \
899 line = (char *) ch_realloc( line, lmax ); \
901 strcpy( line + lcur, buf ); \
906 fp_getline( FILE *fp, int *lineno )
914 /* hack attack - keeps us from having to keep a stack of bufs... */
915 if ( strncasecmp( line, "include", 7 ) == 0 ) {
920 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
921 if ( (p = strchr( buf, '\n' )) != NULL ) {
924 if ( ! isspace( (unsigned char) buf[0] ) ) {
933 return( line[0] ? line : NULL );
937 fp_getline_init( int *lineno )