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 int 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 );
437 /* specify an objectclass */
438 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
439 if ( *cargv[1] == '(' ) {
441 p = strchr(saveline,'(');
442 parse_oc( fname, lineno, p, cargv );
444 parse_oc_old( be, fname, lineno, cargc, cargv );
447 /* specify an attribute */
448 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
449 if ( *cargv[1] == '(' ) {
451 p = strchr(saveline,'(');
452 parse_at( fname, lineno, p, cargv );
454 attr_syntax_config( fname, lineno, cargc - 1,
458 /* turn on/off schema checking */
459 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
461 Debug( LDAP_DEBUG_ANY,
462 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
466 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
467 global_schemacheck = 0;
469 global_schemacheck = 1;
472 /* specify access control info */
473 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
474 parse_acl( be, fname, lineno, cargc, cargv );
476 /* specify default access control info */
477 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
479 Debug( LDAP_DEBUG_ANY,
480 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
485 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
486 str2access(cargv[1]))) )
488 Debug( LDAP_DEBUG_ANY,
489 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
490 fname, lineno, cargv[1] );
494 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
495 str2access(cargv[1]))) )
497 Debug( LDAP_DEBUG_ANY,
498 "%s: line %d: bad access \"%s\", "
499 "expecting [self]{none|auth|compare|search|read|write}\n",
500 fname, lineno, cargv[1] );
505 /* debug level to log things to syslog */
506 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
508 Debug( LDAP_DEBUG_ANY,
509 "%s: line %d: missing level in \"loglevel <level>\" line\n",
513 ldap_syslog = atoi( cargv[1] );
515 /* list of replicas of the data in this backend (master only) */
516 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
518 Debug( LDAP_DEBUG_ANY,
519 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
524 Debug( LDAP_DEBUG_ANY,
525 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
528 for ( i = 1; i < cargc; i++ ) {
529 if ( strncasecmp( cargv[i], "host=", 5 )
531 charray_add( &be->be_replica,
537 Debug( LDAP_DEBUG_ANY,
538 "%s: line %d: missing host in \"replica\" line (ignored)\n",
543 /* dn of master entity allowed to write to replica */
544 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
546 Debug( LDAP_DEBUG_ANY,
547 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
552 Debug( LDAP_DEBUG_ANY,
553 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
556 be->be_update_ndn = ch_strdup( cargv[1] );
557 if( dn_normalize( be->be_update_ndn ) == NULL ) {
558 Debug( LDAP_DEBUG_ANY,
559 "%s: line %d: updatedn DN is invalid\n",
565 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
567 Debug( LDAP_DEBUG_ANY,
568 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
573 Debug( LDAP_DEBUG_ANY,
574 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
576 } else if ( be->be_update_ndn == NULL ) {
577 Debug( LDAP_DEBUG_ANY,
578 "%s: line %d: updateref line must after updatedn (ignored)\n",
581 vals[0]->bv_val = cargv[1];
582 vals[0]->bv_len = strlen( vals[0]->bv_val );
583 value_add( &be->be_update_refs, vals );
586 /* replication log file to which changes are appended */
587 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
589 Debug( LDAP_DEBUG_ANY,
590 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
595 be->be_replogfile = ch_strdup( cargv[1] );
597 replogfile = ch_strdup( cargv[1] );
600 /* maintain lastmodified{by,time} attributes */
601 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
603 Debug( LDAP_DEBUG_ANY,
604 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
608 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
615 be->be_lastmod = OFF;
617 global_lastmod = OFF;
620 /* set idle timeout value */
621 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
624 Debug( LDAP_DEBUG_ANY,
625 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
630 i = atoi( cargv[1] );
633 Debug( LDAP_DEBUG_ANY,
634 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
639 global_idletimeout = i;
641 /* include another config file */
642 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
644 Debug( LDAP_DEBUG_ANY,
645 "%s: line %d: missing filename in \"include <filename>\" line\n",
649 savefname = ch_strdup( cargv[1] );
652 if ( read_config( savefname ) != 0 ) {
657 lineno = savelineno - 1;
659 /* location of kerberos srvtab file */
660 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
662 Debug( LDAP_DEBUG_ANY,
663 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
667 ldap_srvtab = ch_strdup( cargv[1] );
670 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
672 Debug( LDAP_DEBUG_ANY,
673 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
675 exit( EXIT_FAILURE );
677 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
678 Debug( LDAP_DEBUG_ANY,
679 "%s: line %d: failed to load or initialize module %s\n",
680 fname, lineno, cargv[1]);
681 exit( EXIT_FAILURE );
683 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
685 Debug( LDAP_DEBUG_ANY,
686 "%s: line %d: missing path in \"modulepath <path>\" line\n",
688 exit( EXIT_FAILURE );
690 if (module_path( cargv[1] )) {
691 Debug( LDAP_DEBUG_ANY,
692 "%s: line %d: failed to set module search path to %s\n",
693 fname, lineno, cargv[1]);
694 exit( EXIT_FAILURE );
697 #endif /*SLAPD_MODULES*/
700 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
701 rc = ldap_pvt_tls_set_option( NULL,
702 LDAP_OPT_X_TLS_PROTOCOL,
707 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
708 rc = ldap_pvt_tls_set_option( NULL,
709 LDAP_OPT_X_TLS_CIPHER_SUITE,
714 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
715 rc = ldap_pvt_tls_set_option( NULL,
716 LDAP_OPT_X_TLS_CERTFILE,
721 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
722 rc = ldap_pvt_tls_set_option( NULL,
723 LDAP_OPT_X_TLS_KEYFILE,
728 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
729 rc = ldap_pvt_tls_set_option( NULL,
730 LDAP_OPT_X_TLS_CACERTDIR,
735 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
736 rc = ldap_pvt_tls_set_option( NULL,
737 LDAP_OPT_X_TLS_CACERTFILE,
741 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
742 rc = ldap_pvt_tls_set_option( NULL,
743 LDAP_OPT_X_TLS_REQUIRE_CERT,
750 /* pass anything else to the current backend info/db config routine */
753 if ( bi->bi_config == 0 ) {
754 Debug( LDAP_DEBUG_ANY,
755 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
756 fname, lineno, cargv[0] );
758 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
764 } else if ( be != NULL ) {
765 if ( be->be_config == 0 ) {
766 Debug( LDAP_DEBUG_ANY,
767 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
768 fname, lineno, cargv[0] );
770 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
777 Debug( LDAP_DEBUG_ANY,
778 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
779 fname, lineno, cargv[0] );
798 for ( token = strtok_quote( line, " \t" ); token != NULL;
799 token = strtok_quote( NULL, " \t" ) ) {
800 if ( *argcp == MAXARGS ) {
801 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
805 argv[(*argcp)++] = token;
812 strtok_quote( char *line, char *sep )
818 if ( line != NULL ) {
821 while ( *next && strchr( sep, *next ) ) {
825 if ( *next == '\0' ) {
831 for ( inquote = 0; *next; ) {
839 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
845 next + 1, strlen( next + 1 ) + 1 );
846 next++; /* dont parse the escaped character */
851 if ( strchr( sep, *next ) != NULL ) {
864 static char buf[BUFSIZ];
866 static int lmax, lcur;
868 #define CATLINE( buf ) { \
870 len = strlen( buf ); \
871 while ( lcur + len + 1 > lmax ) { \
873 line = (char *) ch_realloc( line, lmax ); \
875 strcpy( line + lcur, buf ); \
880 fp_getline( FILE *fp, int *lineno )
888 /* hack attack - keeps us from having to keep a stack of bufs... */
889 if ( strncasecmp( line, "include", 7 ) == 0 ) {
894 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
895 if ( (p = strchr( buf, '\n' )) != NULL ) {
898 if ( ! isspace( (unsigned char) buf[0] ) ) {
907 return( line[0] ? line : NULL );
911 fp_getline_init( int *lineno )