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 Debug( LDAP_DEBUG_ANY,
459 "%s: line %d: old objectclass format not supported.\n",
463 /* specify an attribute type */
464 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
465 || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
467 if ( *cargv[1] == '(' ) {
469 p = strchr(saveline,'(');
470 parse_at( fname, lineno, p, cargv );
472 Debug( LDAP_DEBUG_ANY,
473 "%s: line %d: old attribute type format not supported.\n",
477 /* turn on/off schema checking */
478 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
480 Debug( LDAP_DEBUG_ANY,
481 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
485 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
486 global_schemacheck = 0;
488 global_schemacheck = 1;
491 /* specify access control info */
492 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
493 parse_acl( be, fname, lineno, cargc, cargv );
495 /* specify default access control info */
496 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
497 slap_access_t access;
500 Debug( LDAP_DEBUG_ANY,
501 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
506 access = str2access( cargv[1] );
508 if ( access == ACL_INVALID_ACCESS ) {
509 Debug( LDAP_DEBUG_ANY,
510 "%s: line %d: bad access level \"%s\", "
511 "expecting none|auth|compare|search|read|write\n",
512 fname, lineno, cargv[1] );
517 global_default_access = access;
519 be->be_dfltaccess = access;
522 /* debug level to log things to syslog */
523 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
525 Debug( LDAP_DEBUG_ANY,
526 "%s: line %d: missing level in \"loglevel <level>\" line\n",
530 ldap_syslog = atoi( cargv[1] );
532 /* list of replicas of the data in this backend (master only) */
533 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
535 Debug( LDAP_DEBUG_ANY,
536 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
541 Debug( LDAP_DEBUG_ANY,
542 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
545 for ( i = 1; i < cargc; i++ ) {
546 if ( strncasecmp( cargv[i], "host=", 5 )
548 charray_add( &be->be_replica,
554 Debug( LDAP_DEBUG_ANY,
555 "%s: line %d: missing host in \"replica\" line (ignored)\n",
560 /* dn of master entity allowed to write to replica */
561 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
563 Debug( LDAP_DEBUG_ANY,
564 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
569 Debug( LDAP_DEBUG_ANY,
570 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
573 be->be_update_ndn = ch_strdup( cargv[1] );
574 if( dn_normalize( be->be_update_ndn ) == NULL ) {
575 Debug( LDAP_DEBUG_ANY,
576 "%s: line %d: updatedn DN is invalid\n",
582 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
584 Debug( LDAP_DEBUG_ANY,
585 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
590 Debug( LDAP_DEBUG_ANY,
591 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
593 } else if ( be->be_update_ndn == NULL ) {
594 Debug( LDAP_DEBUG_ANY,
595 "%s: line %d: updateref line must after updatedn (ignored)\n",
598 vals[0]->bv_val = cargv[1];
599 vals[0]->bv_len = strlen( vals[0]->bv_val );
600 value_add( &be->be_update_refs, vals );
603 /* replication log file to which changes are appended */
604 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
606 Debug( LDAP_DEBUG_ANY,
607 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
612 be->be_replogfile = ch_strdup( cargv[1] );
614 replogfile = ch_strdup( cargv[1] );
617 /* maintain lastmodified{by,time} attributes */
618 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
620 Debug( LDAP_DEBUG_ANY,
621 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
625 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
632 be->be_lastmod = OFF;
634 global_lastmod = OFF;
637 /* set idle timeout value */
638 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
641 Debug( LDAP_DEBUG_ANY,
642 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
647 i = atoi( cargv[1] );
650 Debug( LDAP_DEBUG_ANY,
651 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
656 global_idletimeout = i;
658 /* include another config file */
659 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
661 Debug( LDAP_DEBUG_ANY,
662 "%s: line %d: missing filename in \"include <filename>\" line\n",
666 savefname = ch_strdup( cargv[1] );
669 if ( read_config( savefname ) != 0 ) {
674 lineno = savelineno - 1;
676 /* location of kerberos srvtab file */
677 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
679 Debug( LDAP_DEBUG_ANY,
680 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
684 ldap_srvtab = ch_strdup( cargv[1] );
687 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
689 Debug( LDAP_DEBUG_ANY,
690 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
692 exit( EXIT_FAILURE );
694 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
695 Debug( LDAP_DEBUG_ANY,
696 "%s: line %d: failed to load or initialize module %s\n",
697 fname, lineno, cargv[1]);
698 exit( EXIT_FAILURE );
700 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
702 Debug( LDAP_DEBUG_ANY,
703 "%s: line %d: missing path in \"modulepath <path>\" line\n",
705 exit( EXIT_FAILURE );
707 if (module_path( cargv[1] )) {
708 Debug( LDAP_DEBUG_ANY,
709 "%s: line %d: failed to set module search path to %s\n",
710 fname, lineno, cargv[1]);
711 exit( EXIT_FAILURE );
714 #endif /*SLAPD_MODULES*/
717 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
718 rc = ldap_pvt_tls_set_option( NULL,
719 LDAP_OPT_X_TLS_PROTOCOL,
724 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
725 rc = ldap_pvt_tls_set_option( NULL,
726 LDAP_OPT_X_TLS_CIPHER_SUITE,
731 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
732 rc = ldap_pvt_tls_set_option( NULL,
733 LDAP_OPT_X_TLS_CERTFILE,
738 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
739 rc = ldap_pvt_tls_set_option( NULL,
740 LDAP_OPT_X_TLS_KEYFILE,
745 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
746 rc = ldap_pvt_tls_set_option( NULL,
747 LDAP_OPT_X_TLS_CACERTDIR,
752 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
753 rc = ldap_pvt_tls_set_option( NULL,
754 LDAP_OPT_X_TLS_CACERTFILE,
758 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
759 rc = ldap_pvt_tls_set_option( NULL,
760 LDAP_OPT_X_TLS_REQUIRE_CERT,
767 /* pass anything else to the current backend info/db config routine */
770 if ( bi->bi_config == 0 ) {
771 Debug( LDAP_DEBUG_ANY,
772 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
773 fname, lineno, cargv[0] );
775 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
781 } else if ( be != NULL ) {
782 if ( be->be_config == 0 ) {
783 Debug( LDAP_DEBUG_ANY,
784 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
785 fname, lineno, cargv[0] );
787 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
794 Debug( LDAP_DEBUG_ANY,
795 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
796 fname, lineno, cargv[0] );
815 for ( token = strtok_quote( line, " \t" ); token != NULL;
816 token = strtok_quote( NULL, " \t" ) ) {
817 if ( *argcp == MAXARGS ) {
818 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
822 argv[(*argcp)++] = token;
829 strtok_quote( char *line, char *sep )
835 if ( line != NULL ) {
838 while ( *next && strchr( sep, *next ) ) {
842 if ( *next == '\0' ) {
848 for ( inquote = 0; *next; ) {
856 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
862 next + 1, strlen( next + 1 ) + 1 );
863 next++; /* dont parse the escaped character */
868 if ( strchr( sep, *next ) != NULL ) {
881 static char buf[BUFSIZ];
883 static int lmax, lcur;
885 #define CATLINE( buf ) { \
887 len = strlen( buf ); \
888 while ( lcur + len + 1 > lmax ) { \
890 line = (char *) ch_realloc( line, lmax ); \
892 strcpy( line + lcur, buf ); \
897 fp_getline( FILE *fp, int *lineno )
905 /* hack attack - keeps us from having to keep a stack of bufs... */
906 if ( strncasecmp( line, "include", 7 ) == 0 ) {
911 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
912 if ( (p = strchr( buf, '\n' )) != NULL ) {
915 if ( ! isspace( (unsigned char) buf[0] ) ) {
924 return( line[0] ? line : NULL );
928 fp_getline_init( int *lineno )