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>
24 * defaults for various global variables
26 int defsize = SLAPD_DEFAULT_SIZELIMIT;
27 int deftime = SLAPD_DEFAULT_TIMELIMIT;
28 AccessControl *global_acl = NULL;
29 int global_default_access = ACL_READ;
30 int global_readonly = 0;
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 /* get pid file name */
137 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
139 Debug( LDAP_DEBUG_ANY,
140 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
145 slapd_pid_file = ch_strdup( cargv[1] );
147 /* get args file name */
148 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
150 Debug( LDAP_DEBUG_ANY,
151 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
156 slapd_args_file = ch_strdup( cargv[1] );
158 /* set DIGEST realm */
159 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
161 Debug( LDAP_DEBUG_ANY,
162 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
167 be->be_realm = ch_strdup( cargv[1] );
169 } else if ( global_realm != NULL ) {
170 Debug( LDAP_DEBUG_ANY,
171 "%s: line %d: already set global realm!\n",
176 global_realm = ch_strdup( cargv[1] );
180 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
182 Debug( LDAP_DEBUG_ANY,
183 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
188 defsize = atoi( cargv[1] );
190 be->be_sizelimit = atoi( cargv[1] );
194 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
196 Debug( LDAP_DEBUG_ANY,
197 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
202 deftime = atoi( cargv[1] );
204 be->be_timelimit = atoi( cargv[1] );
207 /* set database suffix */
208 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
211 Debug( LDAP_DEBUG_ANY,
212 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
215 } else if ( cargc > 2 ) {
216 Debug( LDAP_DEBUG_ANY,
217 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
218 fname, lineno, cargv[1] );
221 Debug( LDAP_DEBUG_ANY,
222 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
224 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
225 Debug( LDAP_DEBUG_ANY,
226 "%s: line %d: suffix already served by this backend (ignored)\n",
228 } else if ( tmp_be != NULL ) {
229 Debug( LDAP_DEBUG_ANY,
230 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
231 fname, lineno, tmp_be->be_suffix[0] );
233 char *dn = ch_strdup( cargv[1] );
234 (void) dn_normalize( dn );
235 charray_add( &be->be_suffix, dn );
236 (void) ldap_pvt_str2upper( dn );
237 charray_add( &be->be_nsuffix, dn );
241 /* set database suffixAlias */
242 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
245 Debug( LDAP_DEBUG_ANY,
246 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
249 } else if ( cargc < 3 ) {
250 Debug( LDAP_DEBUG_ANY,
251 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
254 } else if ( cargc > 3 ) {
255 Debug( LDAP_DEBUG_ANY,
256 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
261 Debug( LDAP_DEBUG_ANY,
262 "%s: line %d: suffixAlias line"
263 " must appear inside a database definition (ignored)\n",
265 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
266 Debug( LDAP_DEBUG_ANY,
267 "%s: line %d: suffixAlias served by"
268 " a preceeding backend \"%s\" (ignored)\n",
269 fname, lineno, tmp_be->be_suffix[0] );
271 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
272 Debug( LDAP_DEBUG_ANY,
273 "%s: line %d: suffixAlias derefs to differnet backend"
274 " a preceeding backend \"%s\" (ignored)\n",
275 fname, lineno, tmp_be->be_suffix[0] );
278 char *alias, *aliased_dn;
280 alias = ch_strdup( cargv[1] );
281 (void) dn_normalize( alias );
283 aliased_dn = ch_strdup( cargv[2] );
284 (void) dn_normalize( aliased_dn );
286 (void) dn_normalize_case( alias );
287 (void) dn_normalize_case( aliased_dn );
288 charray_add( &be->be_suffixAlias, alias );
289 charray_add( &be->be_suffixAlias, aliased_dn );
295 /* set max deref depth */
296 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
299 Debug( LDAP_DEBUG_ANY,
300 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
305 Debug( LDAP_DEBUG_ANY,
306 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
308 } else if ((i = atoi(cargv[1])) < 0) {
309 Debug( LDAP_DEBUG_ANY,
310 "%s: line %d: depth must be positive (ignored)\n",
314 be->be_max_deref_depth = i;
318 /* set magic "root" dn for this database */
319 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
321 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
327 Debug( LDAP_DEBUG_ANY,
328 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
331 be->be_root_dn = ch_strdup( cargv[1] );
332 be->be_root_ndn = ch_strdup( cargv[1] );
334 if( dn_normalize_case( be->be_root_ndn ) == NULL ) {
335 free( be->be_root_dn );
336 free( be->be_root_ndn );
337 Debug( LDAP_DEBUG_ANY,
338 "%s: line %d: rootdn DN is invalid\n",
344 /* set super-secret magic database password */
345 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
347 Debug( LDAP_DEBUG_ANY,
348 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
353 Debug( LDAP_DEBUG_ANY,
354 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
357 be->be_root_pw = ch_strdup( cargv[1] );
360 /* make this database read-only */
361 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
363 Debug( LDAP_DEBUG_ANY,
364 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
369 global_readonly = (strcasecmp( cargv[1], "on" ) == 0);
371 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
378 /* where to send clients when we don't hold it */
379 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
381 Debug( LDAP_DEBUG_ANY,
382 "%s: line %d: missing URL in \"referral <URL>\" line\n",
387 vals[0]->bv_val = cargv[1];
388 vals[0]->bv_len = strlen( vals[0]->bv_val );
389 value_add( &default_referral, vals );
392 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
396 Debug( LDAP_DEBUG_ANY,
397 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
402 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
403 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
404 : ch_strdup( cargv[1] ) );
406 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
407 Debug( LDAP_DEBUG_ANY,
409 ? "%s: line %d: bad locale \"%s\"\n"
410 : "%s: line %d: bad locale\n"),
411 fname, lineno, locale );
415 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: \"locale\" unsupported\n",
420 /* specify an Object Identifier macro */
421 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
422 parse_oidm( fname, lineno, cargc, cargv );
423 /* specify an objectclass */
424 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
425 if ( *cargv[1] == '(' ) {
427 p = strchr(saveline,'(');
428 parse_oc( fname, lineno, p, cargv );
430 parse_oc_old( be, fname, lineno, cargc, cargv );
433 /* specify an attribute */
434 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
435 if ( *cargv[1] == '(' ) {
437 p = strchr(saveline,'(');
438 parse_at( fname, lineno, p, cargv );
440 attr_syntax_config( fname, lineno, cargc - 1,
444 /* turn on/off schema checking */
445 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
447 Debug( LDAP_DEBUG_ANY,
448 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
452 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
453 global_schemacheck = 0;
455 global_schemacheck = 1;
458 /* specify access control info */
459 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
460 parse_acl( be, fname, lineno, cargc, cargv );
462 /* specify default access control info */
463 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
465 Debug( LDAP_DEBUG_ANY,
466 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
471 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
472 str2access(cargv[1]))) )
474 Debug( LDAP_DEBUG_ANY,
475 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
476 fname, lineno, cargv[1] );
480 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
481 str2access(cargv[1]))) )
483 Debug( LDAP_DEBUG_ANY,
484 "%s: line %d: bad access \"%s\", "
485 "expecting [self]{none|auth|compare|search|read|write}\n",
486 fname, lineno, cargv[1] );
491 /* debug level to log things to syslog */
492 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
494 Debug( LDAP_DEBUG_ANY,
495 "%s: line %d: missing level in \"loglevel <level>\" line\n",
499 ldap_syslog = atoi( cargv[1] );
501 /* list of replicas of the data in this backend (master only) */
502 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
504 Debug( LDAP_DEBUG_ANY,
505 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
510 Debug( LDAP_DEBUG_ANY,
511 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
514 for ( i = 1; i < cargc; i++ ) {
515 if ( strncasecmp( cargv[i], "host=", 5 )
517 charray_add( &be->be_replica,
523 Debug( LDAP_DEBUG_ANY,
524 "%s: line %d: missing host in \"replica\" line (ignored)\n",
529 /* dn of master entity allowed to write to replica */
530 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
532 Debug( LDAP_DEBUG_ANY,
533 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
538 Debug( LDAP_DEBUG_ANY,
539 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
542 be->be_update_ndn = ch_strdup( cargv[1] );
543 if( dn_normalize_case( be->be_update_ndn ) == NULL ) {
544 Debug( LDAP_DEBUG_ANY,
545 "%s: line %d: updatedn DN is invalid\n",
551 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
553 Debug( LDAP_DEBUG_ANY,
554 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
559 Debug( LDAP_DEBUG_ANY,
560 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
562 } else if ( be->be_update_ndn == NULL ) {
563 Debug( LDAP_DEBUG_ANY,
564 "%s: line %d: updateref line must after updatedn (ignored)\n",
567 vals[0]->bv_val = cargv[1];
568 vals[0]->bv_len = strlen( vals[0]->bv_val );
569 value_add( &be->be_update_refs, vals );
572 /* replication log file to which changes are appended */
573 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
575 Debug( LDAP_DEBUG_ANY,
576 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
581 be->be_replogfile = ch_strdup( cargv[1] );
583 replogfile = ch_strdup( cargv[1] );
586 /* maintain lastmodified{by,time} attributes */
587 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
589 Debug( LDAP_DEBUG_ANY,
590 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
594 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
601 be->be_lastmod = OFF;
603 global_lastmod = OFF;
606 /* set idle timeout value */
607 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
610 Debug( LDAP_DEBUG_ANY,
611 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
616 i = atoi( cargv[1] );
619 Debug( LDAP_DEBUG_ANY,
620 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
625 global_idletimeout = i;
627 /* include another config file */
628 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
630 Debug( LDAP_DEBUG_ANY,
631 "%s: line %d: missing filename in \"include <filename>\" line\n",
635 savefname = ch_strdup( cargv[1] );
638 if ( read_config( savefname ) != 0 ) {
643 lineno = savelineno - 1;
645 /* location of kerberos srvtab file */
646 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
648 Debug( LDAP_DEBUG_ANY,
649 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
653 ldap_srvtab = ch_strdup( cargv[1] );
656 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
658 Debug( LDAP_DEBUG_ANY,
659 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
661 exit( EXIT_FAILURE );
663 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
664 Debug( LDAP_DEBUG_ANY,
665 "%s: line %d: failed to load or initialize module %s\n",
666 fname, lineno, cargv[1]);
667 exit( EXIT_FAILURE );
669 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
671 Debug( LDAP_DEBUG_ANY,
672 "%s: line %d: missing path in \"modulepath <path>\" line\n",
674 exit( EXIT_FAILURE );
676 if (module_path( cargv[1] )) {
677 Debug( LDAP_DEBUG_ANY,
678 "%s: line %d: failed to set module search path to %s\n",
679 fname, lineno, cargv[1]);
680 exit( EXIT_FAILURE );
683 #endif /*SLAPD_MODULES*/
686 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
687 rc = ldap_pvt_tls_set_option( NULL,
688 LDAP_OPT_X_TLS_PROTOCOL,
693 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
694 rc = ldap_pvt_tls_set_option( NULL,
695 LDAP_OPT_X_TLS_CIPHER_SUITE,
700 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
701 rc = ldap_pvt_tls_set_option( NULL,
702 LDAP_OPT_X_TLS_CERTFILE,
707 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
708 rc = ldap_pvt_tls_set_option( NULL,
709 LDAP_OPT_X_TLS_KEYFILE,
714 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
715 rc = ldap_pvt_tls_set_option( NULL,
716 LDAP_OPT_X_TLS_CACERTDIR,
721 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
722 rc = ldap_pvt_tls_set_option( NULL,
723 LDAP_OPT_X_TLS_CACERTFILE,
727 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
728 rc = ldap_pvt_tls_set_option( NULL,
729 LDAP_OPT_X_TLS_REQUIRE_CERT,
736 /* pass anything else to the current backend info/db config routine */
739 if ( bi->bi_config == 0 ) {
740 Debug( LDAP_DEBUG_ANY,
741 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
742 fname, lineno, cargv[0] );
744 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
750 } else if ( be != NULL ) {
751 if ( be->be_config == 0 ) {
752 Debug( LDAP_DEBUG_ANY,
753 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
754 fname, lineno, cargv[0] );
756 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
763 Debug( LDAP_DEBUG_ANY,
764 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
765 fname, lineno, cargv[0] );
784 for ( token = strtok_quote( line, " \t" ); token != NULL;
785 token = strtok_quote( NULL, " \t" ) ) {
786 if ( *argcp == MAXARGS ) {
787 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
791 argv[(*argcp)++] = token;
798 strtok_quote( char *line, char *sep )
804 if ( line != NULL ) {
807 while ( *next && strchr( sep, *next ) ) {
811 if ( *next == '\0' ) {
817 for ( inquote = 0; *next; ) {
825 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
831 next + 1, strlen( next + 1 ) + 1 );
832 next++; /* dont parse the escaped character */
837 if ( strchr( sep, *next ) != NULL ) {
850 static char buf[BUFSIZ];
852 static int lmax, lcur;
854 #define CATLINE( buf ) { \
856 len = strlen( buf ); \
857 while ( lcur + len + 1 > lmax ) { \
859 line = (char *) ch_realloc( line, lmax ); \
861 strcpy( line + lcur, buf ); \
866 fp_getline( FILE *fp, int *lineno )
874 /* hack attack - keeps us from having to keep a stack of bufs... */
875 if ( strncasecmp( line, "include", 7 ) == 0 ) {
880 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
881 if ( (p = strchr( buf, '\n' )) != NULL ) {
884 if ( ! isspace( (unsigned char) buf[0] ) ) {
893 return( line[0] ? line : NULL );
897 fp_getline_init( int *lineno )