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 );
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 ) {
482 Debug( LDAP_DEBUG_ANY,
483 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
488 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
489 str2access(cargv[1]))) )
491 Debug( LDAP_DEBUG_ANY,
492 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
493 fname, lineno, cargv[1] );
497 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
498 str2access(cargv[1]))) )
500 Debug( LDAP_DEBUG_ANY,
501 "%s: line %d: bad access \"%s\", "
502 "expecting [self]{none|auth|compare|search|read|write}\n",
503 fname, lineno, cargv[1] );
508 /* debug level to log things to syslog */
509 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
511 Debug( LDAP_DEBUG_ANY,
512 "%s: line %d: missing level in \"loglevel <level>\" line\n",
516 ldap_syslog = atoi( cargv[1] );
518 /* list of replicas of the data in this backend (master only) */
519 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
521 Debug( LDAP_DEBUG_ANY,
522 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
527 Debug( LDAP_DEBUG_ANY,
528 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
531 for ( i = 1; i < cargc; i++ ) {
532 if ( strncasecmp( cargv[i], "host=", 5 )
534 charray_add( &be->be_replica,
540 Debug( LDAP_DEBUG_ANY,
541 "%s: line %d: missing host in \"replica\" line (ignored)\n",
546 /* dn of master entity allowed to write to replica */
547 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
549 Debug( LDAP_DEBUG_ANY,
550 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
555 Debug( LDAP_DEBUG_ANY,
556 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
559 be->be_update_ndn = ch_strdup( cargv[1] );
560 if( dn_normalize( be->be_update_ndn ) == NULL ) {
561 Debug( LDAP_DEBUG_ANY,
562 "%s: line %d: updatedn DN is invalid\n",
568 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
570 Debug( LDAP_DEBUG_ANY,
571 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
576 Debug( LDAP_DEBUG_ANY,
577 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
579 } else if ( be->be_update_ndn == NULL ) {
580 Debug( LDAP_DEBUG_ANY,
581 "%s: line %d: updateref line must after updatedn (ignored)\n",
584 vals[0]->bv_val = cargv[1];
585 vals[0]->bv_len = strlen( vals[0]->bv_val );
586 value_add( &be->be_update_refs, vals );
589 /* replication log file to which changes are appended */
590 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
592 Debug( LDAP_DEBUG_ANY,
593 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
598 be->be_replogfile = ch_strdup( cargv[1] );
600 replogfile = ch_strdup( cargv[1] );
603 /* maintain lastmodified{by,time} attributes */
604 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
606 Debug( LDAP_DEBUG_ANY,
607 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
611 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
618 be->be_lastmod = OFF;
620 global_lastmod = OFF;
623 /* set idle timeout value */
624 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
627 Debug( LDAP_DEBUG_ANY,
628 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
633 i = atoi( cargv[1] );
636 Debug( LDAP_DEBUG_ANY,
637 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
642 global_idletimeout = i;
644 /* include another config file */
645 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
647 Debug( LDAP_DEBUG_ANY,
648 "%s: line %d: missing filename in \"include <filename>\" line\n",
652 savefname = ch_strdup( cargv[1] );
655 if ( read_config( savefname ) != 0 ) {
660 lineno = savelineno - 1;
662 /* location of kerberos srvtab file */
663 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
665 Debug( LDAP_DEBUG_ANY,
666 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
670 ldap_srvtab = ch_strdup( cargv[1] );
673 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
675 Debug( LDAP_DEBUG_ANY,
676 "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
678 exit( EXIT_FAILURE );
680 if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
681 Debug( LDAP_DEBUG_ANY,
682 "%s: line %d: failed to load or initialize module %s\n",
683 fname, lineno, cargv[1]);
684 exit( EXIT_FAILURE );
686 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
688 Debug( LDAP_DEBUG_ANY,
689 "%s: line %d: missing path in \"modulepath <path>\" line\n",
691 exit( EXIT_FAILURE );
693 if (module_path( cargv[1] )) {
694 Debug( LDAP_DEBUG_ANY,
695 "%s: line %d: failed to set module search path to %s\n",
696 fname, lineno, cargv[1]);
697 exit( EXIT_FAILURE );
700 #endif /*SLAPD_MODULES*/
703 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
704 rc = ldap_pvt_tls_set_option( NULL,
705 LDAP_OPT_X_TLS_PROTOCOL,
710 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
711 rc = ldap_pvt_tls_set_option( NULL,
712 LDAP_OPT_X_TLS_CIPHER_SUITE,
717 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
718 rc = ldap_pvt_tls_set_option( NULL,
719 LDAP_OPT_X_TLS_CERTFILE,
724 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
725 rc = ldap_pvt_tls_set_option( NULL,
726 LDAP_OPT_X_TLS_KEYFILE,
731 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
732 rc = ldap_pvt_tls_set_option( NULL,
733 LDAP_OPT_X_TLS_CACERTDIR,
738 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
739 rc = ldap_pvt_tls_set_option( NULL,
740 LDAP_OPT_X_TLS_CACERTFILE,
744 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
745 rc = ldap_pvt_tls_set_option( NULL,
746 LDAP_OPT_X_TLS_REQUIRE_CERT,
753 /* pass anything else to the current backend info/db config routine */
756 if ( bi->bi_config == 0 ) {
757 Debug( LDAP_DEBUG_ANY,
758 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
759 fname, lineno, cargv[0] );
761 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
767 } else if ( be != NULL ) {
768 if ( be->be_config == 0 ) {
769 Debug( LDAP_DEBUG_ANY,
770 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
771 fname, lineno, cargv[0] );
773 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
780 Debug( LDAP_DEBUG_ANY,
781 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
782 fname, lineno, cargv[0] );
801 for ( token = strtok_quote( line, " \t" ); token != NULL;
802 token = strtok_quote( NULL, " \t" ) ) {
803 if ( *argcp == MAXARGS ) {
804 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
808 argv[(*argcp)++] = token;
815 strtok_quote( char *line, char *sep )
821 if ( line != NULL ) {
824 while ( *next && strchr( sep, *next ) ) {
828 if ( *next == '\0' ) {
834 for ( inquote = 0; *next; ) {
842 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
848 next + 1, strlen( next + 1 ) + 1 );
849 next++; /* dont parse the escaped character */
854 if ( strchr( sep, *next ) != NULL ) {
867 static char buf[BUFSIZ];
869 static int lmax, lcur;
871 #define CATLINE( buf ) { \
873 len = strlen( buf ); \
874 while ( lcur + len + 1 > lmax ) { \
876 line = (char *) ch_realloc( line, lmax ); \
878 strcpy( line + lcur, buf ); \
883 fp_getline( FILE *fp, int *lineno )
891 /* hack attack - keeps us from having to keep a stack of bufs... */
892 if ( strncasecmp( line, "include", 7 ) == 0 ) {
897 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
898 if ( (p = strchr( buf, '\n' )) != NULL ) {
901 if ( ! isspace( (unsigned char) buf[0] ) ) {
910 return( line[0] ? line : NULL );
914 fp_getline_init( int *lineno )