1 /* config.c - configuration file handling routines */
10 #include <ac/string.h>
12 #include <ac/socket.h>
14 #include "ldap_defaults.h"
20 * defaults for various global variables
22 int defsize = SLAPD_DEFAULT_SIZELIMIT;
23 int deftime = SLAPD_DEFAULT_TIMELIMIT;
24 AccessControl *global_acl = NULL;
25 int global_default_access = ACL_READ;
28 int global_idletimeout = 0;
29 char *global_realm = NULL;
30 char *ldap_srvtab = "";
32 char *slapd_pid_file = NULL;
33 char *slapd_args_file = NULL;
35 static char *fp_getline(FILE *fp, int *lineno);
36 static void fp_getline_init(int *lineno);
37 static int fp_parse_line(char *line, int *argcp, char **argv);
39 static char *strtok_quote(char *line, char *sep);
42 read_config( char *fname )
45 char *line, *savefname, *saveline;
46 int cargc, savelineno;
52 struct berval *vals[2];
55 static BackendInfo *bi = NULL;
56 static BackendDB *be = NULL;
61 if ( (fp = fopen( fname, "r" )) == NULL ) {
63 Debug( LDAP_DEBUG_ANY,
64 "could not open config file \"%s\" - absolute path?\n",
70 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
72 if ( schema_init( ) != 0 ) {
73 Debug( LDAP_DEBUG_ANY,
74 "error initializing the schema\n",
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] );
120 /* start of a new database definition */
121 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
123 Debug( LDAP_DEBUG_ANY,
124 "%s: line %d: missing type in \"database <type>\" line\n",
129 be = backend_db_init( cargv[1] );
131 /* assign a default depth limit for alias deref */
132 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
134 /* get pid file name */
135 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
137 Debug( LDAP_DEBUG_ANY,
138 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
143 slapd_pid_file = ch_strdup( cargv[1] );
145 /* get args file name */
146 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
148 Debug( LDAP_DEBUG_ANY,
149 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
154 slapd_args_file = ch_strdup( cargv[1] );
156 /* set DIGEST realm */
157 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
159 Debug( LDAP_DEBUG_ANY,
160 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
165 be->be_realm = ch_strdup( cargv[1] );
167 } else if ( global_realm != NULL ) {
168 Debug( LDAP_DEBUG_ANY,
169 "%s: line %d: already set global realm!\n",
174 global_realm = ch_strdup( cargv[1] );
178 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
180 Debug( LDAP_DEBUG_ANY,
181 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
186 defsize = atoi( cargv[1] );
188 be->be_sizelimit = atoi( cargv[1] );
192 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
194 Debug( LDAP_DEBUG_ANY,
195 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
200 deftime = atoi( cargv[1] );
202 be->be_timelimit = atoi( cargv[1] );
205 /* set database suffix */
206 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
208 Debug( LDAP_DEBUG_ANY,
209 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
212 } else if ( cargc > 2 ) {
213 Debug( LDAP_DEBUG_ANY,
214 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
215 fname, lineno, cargv[1] );
218 Debug( LDAP_DEBUG_ANY,
219 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
222 char *dn = ch_strdup( cargv[1] );
223 (void) dn_normalize( dn );
224 charray_add( &be->be_suffix, dn );
225 (void) str2upper( dn );
226 charray_add( &be->be_nsuffix, dn );
230 /* set max deref depth */
231 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
234 Debug( LDAP_DEBUG_ANY,
235 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
240 Debug( LDAP_DEBUG_ANY,
241 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
243 } else if ((i = atoi(cargv[i])) < 0) {
244 Debug( LDAP_DEBUG_ANY,
245 "%s: line %d: depth must be positive (ignored)\n",
249 be->be_max_deref_depth = i;
253 /* set magic "root" dn for this database */
254 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
256 Debug( LDAP_DEBUG_ANY,
257 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
262 Debug( LDAP_DEBUG_ANY,
263 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
266 be->be_root_dn = ch_strdup( cargv[1] );
267 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
270 /* set super-secret magic database password */
271 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
273 Debug( LDAP_DEBUG_ANY,
274 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
279 Debug( LDAP_DEBUG_ANY,
280 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
283 be->be_root_pw = ch_strdup( cargv[1] );
286 /* make this database read-only */
287 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
289 Debug( LDAP_DEBUG_ANY,
290 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
295 Debug( LDAP_DEBUG_ANY,
296 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
299 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
306 /* where to send clients when we don't hold it */
307 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
309 Debug( LDAP_DEBUG_ANY,
310 "%s: line %d: missing URL in \"referral <URL>\" line\n",
315 vals[0]->bv_val = cargv[1];
316 vals[0]->bv_len = strlen( vals[0]->bv_val );
317 value_add( &default_referral, vals );
320 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
324 Debug( LDAP_DEBUG_ANY,
325 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
330 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
331 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
332 : ch_strdup( cargv[1] ) );
334 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
335 Debug( LDAP_DEBUG_ANY,
337 ? "%s: line %d: bad locale \"%s\"\n"
338 : "%s: line %d: bad locale\n"),
339 fname, lineno, locale );
343 Debug( LDAP_DEBUG_ANY,
344 "%s: line %d: \"locale\" unsupported\n",
348 /* specify an objectclass */
349 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
350 if ( *cargv[1] == '(' ) {
352 p = strchr(saveline,'(');
353 parse_oc( fname, lineno, p );
355 parse_oc_old( be, fname, lineno, cargc, cargv );
358 /* specify an attribute */
359 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
360 if ( *cargv[1] == '(' ) {
362 p = strchr(saveline,'(');
363 parse_at( fname, lineno, p );
365 attr_syntax_config( fname, lineno, cargc - 1,
369 /* turn on/off schema checking */
370 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
372 Debug( LDAP_DEBUG_ANY,
373 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
377 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
378 global_schemacheck = 0;
380 global_schemacheck = 1;
383 /* specify access control info */
384 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
385 parse_acl( be, fname, lineno, cargc, cargv );
387 /* specify default access control info */
388 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
390 Debug( LDAP_DEBUG_ANY,
391 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
396 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
397 str2access(cargv[1]))) )
399 Debug( LDAP_DEBUG_ANY,
400 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
401 fname, lineno, cargv[1] );
405 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
406 str2access(cargv[1]))) )
408 Debug( LDAP_DEBUG_ANY,
409 "%s: line %d: bad access \"%s\", "
410 "expecting [self]{none|compare|search|read|write}\n",
411 fname, lineno, cargv[1] );
416 /* debug level to log things to syslog */
417 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
419 Debug( LDAP_DEBUG_ANY,
420 "%s: line %d: missing level in \"loglevel <level>\" line\n",
424 ldap_syslog = atoi( cargv[1] );
426 /* list of replicas of the data in this backend (master only) */
427 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
429 Debug( LDAP_DEBUG_ANY,
430 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
435 Debug( LDAP_DEBUG_ANY,
436 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
439 for ( i = 1; i < cargc; i++ ) {
440 if ( strncasecmp( cargv[i], "host=", 5 )
442 charray_add( &be->be_replica,
448 Debug( LDAP_DEBUG_ANY,
449 "%s: line %d: missing host in \"replica\" line (ignored)\n",
454 /* dn of master entity allowed to write to replica */
455 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
457 Debug( LDAP_DEBUG_ANY,
458 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
463 Debug( LDAP_DEBUG_ANY,
464 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
467 be->be_update_ndn = ch_strdup( cargv[1] );
468 (void) dn_normalize_case( be->be_update_ndn );
471 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
473 Debug( LDAP_DEBUG_ANY,
474 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
479 Debug( LDAP_DEBUG_ANY,
480 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
482 } else if ( be->be_update_ndn == NULL ) {
483 Debug( LDAP_DEBUG_ANY,
484 "%s: line %d: updateref line must after updatedn (ignored)\n",
487 vals[0]->bv_val = cargv[1];
488 vals[0]->bv_len = strlen( vals[0]->bv_val );
489 value_add( &be->be_update_refs, vals );
492 /* replication log file to which changes are appended */
493 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
495 Debug( LDAP_DEBUG_ANY,
496 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
501 be->be_replogfile = ch_strdup( cargv[1] );
503 replogfile = ch_strdup( cargv[1] );
506 /* maintain lastmodified{by,time} attributes */
507 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
509 Debug( LDAP_DEBUG_ANY,
510 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
514 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
521 be->be_lastmod = OFF;
523 global_lastmod = OFF;
526 /* set idle timeout value */
527 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
530 Debug( LDAP_DEBUG_ANY,
531 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
536 i = atoi( cargv[1] );
539 Debug( LDAP_DEBUG_ANY,
540 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
545 global_idletimeout = i;
547 /* include another config file */
548 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
550 Debug( LDAP_DEBUG_ANY,
551 "%s: line %d: missing filename in \"include <filename>\" line\n",
555 savefname = ch_strdup( cargv[1] );
558 if ( read_config( savefname ) != 0 ) {
563 lineno = savelineno - 1;
565 /* location of kerberos srvtab file */
566 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
568 Debug( LDAP_DEBUG_ANY,
569 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
573 ldap_srvtab = ch_strdup( cargv[1] );
576 } else if (strcasecmp( cargv[0], "loadmodule") == 0 ) {
578 Debug( LDAP_DEBUG_ANY,
579 "%s: line %d: missing filename in \"loadmodule <filename>\" line\n",
583 if (!load_module(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
584 Debug( LDAP_DEBUG_ANY,
585 "%s: line %d: failed to load or initialize module %s\n",
586 fname, lineno, cargv[1]);
590 #endif /*SLAPD_MODULES*/
593 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
594 rc = ldap_pvt_tls_set_option( NULL,
595 LDAP_OPT_X_TLS_PROTOCOL,
600 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
601 rc = ldap_pvt_tls_set_option( NULL,
602 LDAP_OPT_X_TLS_CIPHER_SUITE,
607 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
608 rc = ldap_pvt_tls_set_option( NULL,
609 LDAP_OPT_X_TLS_CERTFILE,
614 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
615 rc = ldap_pvt_tls_set_option( NULL,
616 LDAP_OPT_X_TLS_KEYFILE,
621 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
622 rc = ldap_pvt_tls_set_option( NULL,
623 LDAP_OPT_X_TLS_CACERTDIR,
628 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
629 rc = ldap_pvt_tls_set_option( NULL,
630 LDAP_OPT_X_TLS_CACERTFILE,
634 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
635 rc = ldap_pvt_tls_set_option( NULL,
636 LDAP_OPT_X_TLS_REQUIRE_CERT,
643 /* pass anything else to the current backend info/db config routine */
646 if ( bi->bi_config == 0 ) {
647 Debug( LDAP_DEBUG_ANY,
648 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
649 fname, lineno, cargv[0] );
651 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
657 } else if ( be != NULL ) {
658 if ( be->be_config == 0 ) {
659 Debug( LDAP_DEBUG_ANY,
660 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
661 fname, lineno, cargv[0] );
663 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
670 Debug( LDAP_DEBUG_ANY,
671 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
672 fname, lineno, cargv[0] );
691 for ( token = strtok_quote( line, " \t" ); token != NULL;
692 token = strtok_quote( NULL, " \t" ) ) {
693 if ( *argcp == MAXARGS ) {
694 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
698 argv[(*argcp)++] = token;
705 strtok_quote( char *line, char *sep )
711 if ( line != NULL ) {
714 while ( *next && strchr( sep, *next ) ) {
718 if ( *next == '\0' ) {
724 for ( inquote = 0; *next; ) {
732 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
738 next + 1, strlen( next + 1 ) + 1 );
739 next++; /* dont parse the escaped character */
744 if ( strchr( sep, *next ) != NULL ) {
757 static char buf[BUFSIZ];
759 static int lmax, lcur;
761 #define CATLINE( buf ) { \
763 len = strlen( buf ); \
764 while ( lcur + len + 1 > lmax ) { \
766 line = (char *) ch_realloc( line, lmax ); \
768 strcpy( line + lcur, buf ); \
773 fp_getline( FILE *fp, int *lineno )
781 /* hack attack - keeps us from having to keep a stack of bufs... */
782 if ( strncasecmp( line, "include", 7 ) == 0 ) {
787 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
788 if ( (p = strchr( buf, '\n' )) != NULL ) {
791 if ( ! isspace( (unsigned char) buf[0] ) ) {
800 return( line[0] ? line : NULL );
804 fp_getline_init( int *lineno )