1 /* config.c - configuration file handling routines */
4 * Copyright 1998-1999 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 = "";
38 char *slapd_pid_file = NULL;
39 char *slapd_args_file = NULL;
41 static char *fp_getline(FILE *fp, int *lineno);
42 static void fp_getline_init(int *lineno);
43 static int fp_parse_line(char *line, int *argcp, char **argv);
45 static char *strtok_quote(char *line, char *sep);
48 read_config( const char *fname )
51 char *line, *savefname, *saveline;
52 int cargc, savelineno;
58 struct berval *vals[2];
61 static BackendInfo *bi = NULL;
62 static BackendDB *be = NULL;
67 if ( (fp = fopen( fname, "r" )) == NULL ) {
69 Debug( LDAP_DEBUG_ANY,
70 "could not open config file \"%s\" - absolute path?\n",
76 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
78 if ( schema_init( ) != 0 ) {
79 Debug( LDAP_DEBUG_ANY,
80 "error initializing the schema\n",
85 fp_getline_init( &lineno );
87 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
88 /* skip comments and blank lines */
89 if ( line[0] == '#' || line[0] == '\0' ) {
93 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
95 /* fp_parse_line is destructive, we save a copy */
96 saveline = ch_strdup( line );
98 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
103 Debug( LDAP_DEBUG_ANY,
104 "%s: line %d: bad config line (ignored)\n",
109 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
111 Debug( LDAP_DEBUG_ANY,
112 "%s: line %d: missing type in \"backend <type>\" line\n",
118 Debug( LDAP_DEBUG_ANY,
119 "%s: line %d: backend line must appear before any database definition\n",
124 bi = backend_info( cargv[1] );
127 Debug( LDAP_DEBUG_ANY,
128 "backend %s initialization failed.n",
133 /* start of a new database definition */
134 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
136 Debug( LDAP_DEBUG_ANY,
137 "%s: line %d: missing type in \"database <type>\" line\n",
143 be = backend_db_init( cargv[1] );
146 Debug( LDAP_DEBUG_ANY,
147 "database %s initialization failed.n",
152 /* get pid file name */
153 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
155 Debug( LDAP_DEBUG_ANY,
156 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
161 slapd_pid_file = ch_strdup( cargv[1] );
163 /* get args file name */
164 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
166 Debug( LDAP_DEBUG_ANY,
167 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
172 slapd_args_file = ch_strdup( cargv[1] );
174 /* set DIGEST realm */
175 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
177 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
183 be->be_realm = ch_strdup( cargv[1] );
185 } else if ( global_realm != NULL ) {
186 Debug( LDAP_DEBUG_ANY,
187 "%s: line %d: already set global realm!\n",
192 global_realm = ch_strdup( cargv[1] );
196 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
198 Debug( LDAP_DEBUG_ANY,
199 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
204 defsize = atoi( cargv[1] );
206 be->be_sizelimit = atoi( cargv[1] );
210 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
212 Debug( LDAP_DEBUG_ANY,
213 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
218 deftime = atoi( cargv[1] );
220 be->be_timelimit = atoi( cargv[1] );
223 /* set database suffix */
224 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
227 Debug( LDAP_DEBUG_ANY,
228 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
231 } else if ( cargc > 2 ) {
232 Debug( LDAP_DEBUG_ANY,
233 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
234 fname, lineno, cargv[1] );
237 Debug( LDAP_DEBUG_ANY,
238 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
240 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
241 Debug( LDAP_DEBUG_ANY,
242 "%s: line %d: suffix already served by this backend (ignored)\n",
244 } else if ( tmp_be != NULL ) {
245 Debug( LDAP_DEBUG_ANY,
246 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
247 fname, lineno, tmp_be->be_suffix[0] );
249 char *dn = ch_strdup( cargv[1] );
250 (void) dn_validate( dn );
251 charray_add( &be->be_suffix, dn );
252 (void) ldap_pvt_str2upper( dn );
253 charray_add( &be->be_nsuffix, dn );
257 /* set database suffixAlias */
258 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
261 Debug( LDAP_DEBUG_ANY,
262 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
265 } else if ( cargc < 3 ) {
266 Debug( LDAP_DEBUG_ANY,
267 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
270 } else if ( cargc > 3 ) {
271 Debug( LDAP_DEBUG_ANY,
272 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
277 Debug( LDAP_DEBUG_ANY,
278 "%s: line %d: suffixAlias line"
279 " must appear inside a database definition (ignored)\n",
281 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
282 Debug( LDAP_DEBUG_ANY,
283 "%s: line %d: suffixAlias served by"
284 " a preceeding backend \"%s\" (ignored)\n",
285 fname, lineno, tmp_be->be_suffix[0] );
287 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: suffixAlias derefs to differnet backend"
290 " a preceeding backend \"%s\" (ignored)\n",
291 fname, lineno, tmp_be->be_suffix[0] );
294 char *alias, *aliased_dn;
296 alias = ch_strdup( cargv[1] );
297 (void) dn_normalize( alias );
299 aliased_dn = ch_strdup( cargv[2] );
300 (void) dn_normalize( aliased_dn );
302 charray_add( &be->be_suffixAlias, alias );
303 charray_add( &be->be_suffixAlias, aliased_dn );
309 /* set max deref depth */
310 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
313 Debug( LDAP_DEBUG_ANY,
314 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
319 Debug( LDAP_DEBUG_ANY,
320 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
322 } else if ((i = atoi(cargv[1])) < 0) {
323 Debug( LDAP_DEBUG_ANY,
324 "%s: line %d: depth must be positive (ignored)\n",
328 be->be_max_deref_depth = i;
332 /* set magic "root" dn for this database */
333 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
335 Debug( LDAP_DEBUG_ANY,
336 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
341 Debug( LDAP_DEBUG_ANY,
342 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
345 be->be_root_dn = ch_strdup( cargv[1] );
346 be->be_root_ndn = ch_strdup( cargv[1] );
348 if( dn_normalize( be->be_root_ndn ) == NULL ) {
349 free( be->be_root_dn );
350 free( be->be_root_ndn );
351 Debug( LDAP_DEBUG_ANY,
352 "%s: line %d: rootdn DN is invalid\n",
358 /* set super-secret magic database password */
359 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
361 Debug( LDAP_DEBUG_ANY,
362 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
367 Debug( LDAP_DEBUG_ANY,
368 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
371 be->be_root_pw = ch_strdup( cargv[1] );
374 /* make this database read-only */
375 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
377 Debug( LDAP_DEBUG_ANY,
378 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
383 global_readonly = (strcasecmp( cargv[1], "on" ) == 0);
385 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
392 /* where to send clients when we don't hold it */
393 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
395 Debug( LDAP_DEBUG_ANY,
396 "%s: line %d: missing URL in \"referral <URL>\" line\n",
401 vals[0]->bv_val = cargv[1];
402 vals[0]->bv_len = strlen( vals[0]->bv_val );
403 value_add( &default_referral, vals );
406 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
410 Debug( LDAP_DEBUG_ANY,
411 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
416 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
417 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
418 : ch_strdup( cargv[1] ) );
420 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
421 Debug( LDAP_DEBUG_ANY,
423 ? "%s: line %d: bad locale \"%s\"\n"
424 : "%s: line %d: bad locale\n"),
425 fname, lineno, locale );
429 Debug( LDAP_DEBUG_ANY,
430 "%s: line %d: \"locale\" unsupported\n",
434 /* specify an Object Identifier macro */
435 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
436 parse_oidm( fname, lineno, cargc, cargv );
438 /* specify an objectclass */
439 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
440 if ( *cargv[1] == '(' ) {
442 p = strchr(saveline,'(');
443 parse_oc( fname, lineno, p, cargv );
445 parse_oc_old( be, fname, lineno, cargc, cargv );
448 /* specify an attribute type */
449 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
450 || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
452 if ( *cargv[1] == '(' ) {
454 p = strchr(saveline,'(');
455 parse_at( fname, lineno, p, cargv );
457 attr_syntax_config( fname, lineno, cargc - 1,
461 /* turn on/off schema checking */
462 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
464 Debug( LDAP_DEBUG_ANY,
465 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
469 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
470 global_schemacheck = 0;
472 global_schemacheck = 1;
475 /* specify access control info */
476 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
477 parse_acl( be, fname, lineno, cargc, cargv );
479 /* specify default access control info */
480 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
481 slap_access_t access;
484 Debug( LDAP_DEBUG_ANY,
485 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
490 access = str2access( cargv[1] );
492 if ( access == ACL_INVALID_ACCESS ) {
493 Debug( LDAP_DEBUG_ANY,
494 "%s: line %d: bad access level \"%s\", "
495 "expecting none|auth|compare|search|read|write\n",
496 fname, lineno, cargv[1] );
501 global_default_access = access;
503 be->be_dfltaccess = access;
506 /* debug level to log things to syslog */
507 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
509 Debug( LDAP_DEBUG_ANY,
510 "%s: line %d: missing level in \"loglevel <level>\" line\n",
514 ldap_syslog = atoi( cargv[1] );
516 /* list of replicas of the data in this backend (master only) */
517 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
519 Debug( LDAP_DEBUG_ANY,
520 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
525 Debug( LDAP_DEBUG_ANY,
526 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
529 for ( i = 1; i < cargc; i++ ) {
530 if ( strncasecmp( cargv[i], "host=", 5 )
532 charray_add( &be->be_replica,
538 Debug( LDAP_DEBUG_ANY,
539 "%s: line %d: missing host in \"replica\" line (ignored)\n",
544 /* dn of master entity allowed to write to replica */
545 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
547 Debug( LDAP_DEBUG_ANY,
548 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
553 Debug( LDAP_DEBUG_ANY,
554 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
557 be->be_update_ndn = ch_strdup( cargv[1] );
558 if( dn_normalize( be->be_update_ndn ) == NULL ) {
559 Debug( LDAP_DEBUG_ANY,
560 "%s: line %d: updatedn DN is invalid\n",
566 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
568 Debug( LDAP_DEBUG_ANY,
569 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
574 Debug( LDAP_DEBUG_ANY,
575 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
577 } else if ( be->be_update_ndn == NULL ) {
578 Debug( LDAP_DEBUG_ANY,
579 "%s: line %d: updateref line must after updatedn (ignored)\n",
582 vals[0]->bv_val = cargv[1];
583 vals[0]->bv_len = strlen( vals[0]->bv_val );
584 value_add( &be->be_update_refs, vals );
587 /* replication log file to which changes are appended */
588 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
590 Debug( LDAP_DEBUG_ANY,
591 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
596 be->be_replogfile = ch_strdup( cargv[1] );
598 replogfile = ch_strdup( cargv[1] );
601 /* maintain lastmodified{by,time} attributes */
602 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
604 Debug( LDAP_DEBUG_ANY,
605 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
609 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
616 be->be_lastmod = OFF;
618 global_lastmod = OFF;
621 /* set idle timeout value */
622 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
625 Debug( LDAP_DEBUG_ANY,
626 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
631 i = atoi( cargv[1] );
634 Debug( LDAP_DEBUG_ANY,
635 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
640 global_idletimeout = i;
642 /* include another config file */
643 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
645 Debug( LDAP_DEBUG_ANY,
646 "%s: line %d: missing filename in \"include <filename>\" line\n",
650 savefname = ch_strdup( cargv[1] );
653 if ( read_config( savefname ) != 0 ) {
658 lineno = savelineno - 1;
660 /* location of kerberos srvtab file */
661 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
663 Debug( LDAP_DEBUG_ANY,
664 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
668 ldap_srvtab = ch_strdup( cargv[1] );
671 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
673 Debug( LDAP_DEBUG_ANY,
674 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
676 exit( EXIT_FAILURE );
678 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
679 Debug( LDAP_DEBUG_ANY,
680 "%s: line %d: failed to load or initialize module %s\n",
681 fname, lineno, cargv[1]);
682 exit( EXIT_FAILURE );
684 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
686 Debug( LDAP_DEBUG_ANY,
687 "%s: line %d: missing path in \"modulepath <path>\" line\n",
689 exit( EXIT_FAILURE );
691 if (module_path( cargv[1] )) {
692 Debug( LDAP_DEBUG_ANY,
693 "%s: line %d: failed to set module search path to %s\n",
694 fname, lineno, cargv[1]);
695 exit( EXIT_FAILURE );
698 #endif /*SLAPD_MODULES*/
701 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
702 rc = ldap_pvt_tls_set_option( NULL,
703 LDAP_OPT_X_TLS_PROTOCOL,
708 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
709 rc = ldap_pvt_tls_set_option( NULL,
710 LDAP_OPT_X_TLS_CIPHER_SUITE,
715 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
716 rc = ldap_pvt_tls_set_option( NULL,
717 LDAP_OPT_X_TLS_CERTFILE,
722 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
723 rc = ldap_pvt_tls_set_option( NULL,
724 LDAP_OPT_X_TLS_KEYFILE,
729 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
730 rc = ldap_pvt_tls_set_option( NULL,
731 LDAP_OPT_X_TLS_CACERTDIR,
736 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
737 rc = ldap_pvt_tls_set_option( NULL,
738 LDAP_OPT_X_TLS_CACERTFILE,
742 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
743 rc = ldap_pvt_tls_set_option( NULL,
744 LDAP_OPT_X_TLS_REQUIRE_CERT,
751 /* pass anything else to the current backend info/db config routine */
754 if ( bi->bi_config == 0 ) {
755 Debug( LDAP_DEBUG_ANY,
756 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
757 fname, lineno, cargv[0] );
759 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
765 } else if ( be != NULL ) {
766 if ( be->be_config == 0 ) {
767 Debug( LDAP_DEBUG_ANY,
768 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
769 fname, lineno, cargv[0] );
771 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
778 Debug( LDAP_DEBUG_ANY,
779 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
780 fname, lineno, cargv[0] );
799 for ( token = strtok_quote( line, " \t" ); token != NULL;
800 token = strtok_quote( NULL, " \t" ) ) {
801 if ( *argcp == MAXARGS ) {
802 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
806 argv[(*argcp)++] = token;
813 strtok_quote( char *line, char *sep )
819 if ( line != NULL ) {
822 while ( *next && strchr( sep, *next ) ) {
826 if ( *next == '\0' ) {
832 for ( inquote = 0; *next; ) {
840 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
846 next + 1, strlen( next + 1 ) + 1 );
847 next++; /* dont parse the escaped character */
852 if ( strchr( sep, *next ) != NULL ) {
865 static char buf[BUFSIZ];
867 static int lmax, lcur;
869 #define CATLINE( buf ) { \
871 len = strlen( buf ); \
872 while ( lcur + len + 1 > lmax ) { \
874 line = (char *) ch_realloc( line, lmax ); \
876 strcpy( line + lcur, buf ); \
881 fp_getline( FILE *fp, int *lineno )
889 /* hack attack - keeps us from having to keep a stack of bufs... */
890 if ( strncasecmp( line, "include", 7 ) == 0 ) {
895 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
896 if ( (p = strchr( buf, '\n' )) != NULL ) {
899 if ( ! isspace( (unsigned char) buf[0] ) ) {
908 return( line[0] ? line : NULL );
912 fp_getline_init( int *lineno )