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 struct acl *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;
50 struct berval *vals[2];
53 static BackendInfo *bi = NULL;
54 static BackendDB *be = NULL;
59 if ( (fp = fopen( fname, "r" )) == NULL ) {
61 Debug( LDAP_DEBUG_ANY,
62 "could not open config file \"%s\" - absolute path?\n",
68 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
70 if ( schema_init( ) != 0 ) {
71 Debug( LDAP_DEBUG_ANY,
72 "error initializing the schema\n",
77 fp_getline_init( &lineno );
79 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
80 /* skip comments and blank lines */
81 if ( line[0] == '#' || line[0] == '\0' ) {
85 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
87 /* fp_parse_line is destructive, we save a copy */
88 saveline = ch_strdup( line );
90 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
95 Debug( LDAP_DEBUG_ANY,
96 "%s: line %d: bad config line (ignored)\n",
101 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
103 Debug( LDAP_DEBUG_ANY,
104 "%s: line %d: missing type in \"backend <type>\" line\n",
110 Debug( LDAP_DEBUG_ANY,
111 "%s: line %d: backend line must appear before any database definition\n",
116 bi = backend_info( cargv[1] );
118 /* start of a new database definition */
119 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
121 Debug( LDAP_DEBUG_ANY,
122 "%s: line %d: missing type in \"database <type>\" line\n",
127 be = backend_db_init( cargv[1] );
129 /* assign a default depth limit for alias deref */
130 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
132 /* get pid file name */
133 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
135 Debug( LDAP_DEBUG_ANY,
136 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
141 slapd_pid_file = ch_strdup( cargv[1] );
143 /* get args file name */
144 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
146 Debug( LDAP_DEBUG_ANY,
147 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
152 slapd_args_file = ch_strdup( cargv[1] );
154 /* set DIGEST realm */
155 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
157 Debug( LDAP_DEBUG_ANY,
158 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
163 be->be_realm = ch_strdup( cargv[1] );
165 } else if ( global_realm != NULL ) {
166 Debug( LDAP_DEBUG_ANY,
167 "%s: line %d: already set global realm!\n",
172 global_realm = ch_strdup( cargv[1] );
176 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
178 Debug( LDAP_DEBUG_ANY,
179 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
184 defsize = atoi( cargv[1] );
186 be->be_sizelimit = atoi( cargv[1] );
190 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
192 Debug( LDAP_DEBUG_ANY,
193 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
198 deftime = atoi( cargv[1] );
200 be->be_timelimit = atoi( cargv[1] );
203 /* set database suffix */
204 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
206 Debug( LDAP_DEBUG_ANY,
207 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
210 } else if ( cargc > 2 ) {
211 Debug( LDAP_DEBUG_ANY,
212 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
213 fname, lineno, cargv[1] );
216 Debug( LDAP_DEBUG_ANY,
217 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
220 char *dn = ch_strdup( cargv[1] );
221 (void) dn_normalize( dn );
222 charray_add( &be->be_suffix, dn );
223 (void) dn_upcase( dn );
224 charray_add( &be->be_nsuffix, dn );
228 /* set max deref depth */
229 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
232 Debug( LDAP_DEBUG_ANY,
233 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
238 Debug( LDAP_DEBUG_ANY,
239 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
241 } else if ((i = atoi(cargv[i])) < 0) {
242 Debug( LDAP_DEBUG_ANY,
243 "%s: line %d: depth must be positive (ignored)\n",
247 be->be_max_deref_depth = i;
251 /* set magic "root" dn for this database */
252 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
254 Debug( LDAP_DEBUG_ANY,
255 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
260 Debug( LDAP_DEBUG_ANY,
261 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
264 be->be_root_dn = ch_strdup( cargv[1] );
265 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
268 /* set super-secret magic database password */
269 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
271 Debug( LDAP_DEBUG_ANY,
272 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
277 Debug( LDAP_DEBUG_ANY,
278 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
281 be->be_root_pw = ch_strdup( cargv[1] );
284 /* make this database read-only */
285 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
287 Debug( LDAP_DEBUG_ANY,
288 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
293 Debug( LDAP_DEBUG_ANY,
294 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
297 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
304 /* where to send clients when we don't hold it */
305 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
307 Debug( LDAP_DEBUG_ANY,
308 "%s: line %d: missing URL in \"referral <URL>\" line\n",
313 vals[0]->bv_val = cargv[1];
314 vals[0]->bv_len = strlen( vals[0]->bv_val );
315 value_add( &default_referral, vals );
318 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
322 Debug( LDAP_DEBUG_ANY,
323 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
328 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
329 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
330 : ch_strdup( cargv[1] ) );
332 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
333 Debug( LDAP_DEBUG_ANY,
335 ? "%s: line %d: bad locale \"%s\"\n"
336 : "%s: line %d: bad locale\n"),
337 fname, lineno, locale );
341 Debug( LDAP_DEBUG_ANY,
342 "%s: line %d: \"locale\" unsupported\n",
346 /* specify an objectclass */
347 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
348 if ( *cargv[1] == '(' ) {
350 p = strchr(saveline,'(');
351 parse_oc( fname, lineno, p );
353 parse_oc_old( be, fname, lineno, cargc, cargv );
356 /* specify an attribute */
357 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
358 if ( *cargv[1] == '(' ) {
360 p = strchr(saveline,'(');
361 parse_at( fname, lineno, p );
363 attr_syntax_config( fname, lineno, cargc - 1,
367 /* turn on/off schema checking */
368 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
370 Debug( LDAP_DEBUG_ANY,
371 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
375 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
376 global_schemacheck = 0;
378 global_schemacheck = 1;
381 /* specify access control info */
382 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
383 parse_acl( be, fname, lineno, cargc, cargv );
385 /* specify default access control info */
386 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
388 Debug( LDAP_DEBUG_ANY,
389 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
394 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
395 str2access(cargv[1]))) )
397 Debug( LDAP_DEBUG_ANY,
398 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
399 fname, lineno, cargv[1] );
403 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
404 str2access(cargv[1]))) )
406 Debug( LDAP_DEBUG_ANY,
407 "%s: line %d: bad access \"%s\", "
408 "expecting [self]{none|compare|search|read|write}\n",
409 fname, lineno, cargv[1] );
414 /* debug level to log things to syslog */
415 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
417 Debug( LDAP_DEBUG_ANY,
418 "%s: line %d: missing level in \"loglevel <level>\" line\n",
422 ldap_syslog = atoi( cargv[1] );
424 /* list of replicas of the data in this backend (master only) */
425 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
427 Debug( LDAP_DEBUG_ANY,
428 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
433 Debug( LDAP_DEBUG_ANY,
434 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
437 for ( i = 1; i < cargc; i++ ) {
438 if ( strncasecmp( cargv[i], "host=", 5 )
440 charray_add( &be->be_replica,
446 Debug( LDAP_DEBUG_ANY,
447 "%s: line %d: missing host in \"replica\" line (ignored)\n",
452 /* dn of master entity allowed to write to replica */
453 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
455 Debug( LDAP_DEBUG_ANY,
456 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
461 Debug( LDAP_DEBUG_ANY,
462 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
465 be->be_update_ndn = ch_strdup( cargv[1] );
466 (void) dn_normalize_case( be->be_update_ndn );
469 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
471 Debug( LDAP_DEBUG_ANY,
472 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
477 Debug( LDAP_DEBUG_ANY,
478 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
480 } else if ( be->be_update_ndn == NULL ) {
481 Debug( LDAP_DEBUG_ANY,
482 "%s: line %d: updateref line must after updatedn (ignored)\n",
485 vals[0]->bv_val = cargv[1];
486 vals[0]->bv_len = strlen( vals[0]->bv_val );
487 value_add( &be->be_update_refs, vals );
490 /* replication log file to which changes are appended */
491 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
493 Debug( LDAP_DEBUG_ANY,
494 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
499 be->be_replogfile = ch_strdup( cargv[1] );
501 replogfile = ch_strdup( cargv[1] );
504 /* maintain lastmodified{by,time} attributes */
505 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
507 Debug( LDAP_DEBUG_ANY,
508 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
512 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
519 be->be_lastmod = OFF;
521 global_lastmod = OFF;
524 /* set idle timeout value */
525 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
528 Debug( LDAP_DEBUG_ANY,
529 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
534 i = atoi( cargv[1] );
537 Debug( LDAP_DEBUG_ANY,
538 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
543 global_idletimeout = i;
545 /* include another config file */
546 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
548 Debug( LDAP_DEBUG_ANY,
549 "%s: line %d: missing filename in \"include <filename>\" line\n",
553 savefname = ch_strdup( cargv[1] );
556 if ( read_config( savefname ) != 0 ) {
561 lineno = savelineno - 1;
563 /* location of kerberos srvtab file */
564 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
566 Debug( LDAP_DEBUG_ANY,
567 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
571 ldap_srvtab = ch_strdup( cargv[1] );
574 } else if (strcasecmp( cargv[0], "loadmodule") == 0 ) {
576 Debug( LDAP_DEBUG_ANY,
577 "%s: line %d: missing filename in \"loadmodule <filename>\" line\n",
581 if (!load_module(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
582 Debug( LDAP_DEBUG_ANY,
583 "%s: line %d: failed to load or initialize module %s\n",
584 fname, lineno, cargv[1]);
588 #endif /*SLAPD_MODULES*/
591 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
592 rc = ldap_pvt_tls_set_option( NULL,
593 LDAP_OPT_X_TLS_PROTOCOL,
598 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
599 rc = ldap_pvt_tls_set_option( NULL,
600 LDAP_OPT_X_TLS_CIPHER_SUITE,
605 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
606 rc = ldap_pvt_tls_set_option( NULL,
607 LDAP_OPT_X_TLS_CERTFILE,
612 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
613 rc = ldap_pvt_tls_set_option( NULL,
614 LDAP_OPT_X_TLS_KEYFILE,
619 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
620 rc = ldap_pvt_tls_set_option( NULL,
621 LDAP_OPT_X_TLS_CACERTDIR,
626 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
627 rc = ldap_pvt_tls_set_option( NULL,
628 LDAP_OPT_X_TLS_CACERTFILE,
635 /* pass anything else to the current backend info/db config routine */
638 if ( bi->bi_config == 0 ) {
639 Debug( LDAP_DEBUG_ANY,
640 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
641 fname, lineno, cargv[0] );
643 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
649 } else if ( be != NULL ) {
650 if ( be->be_config == 0 ) {
651 Debug( LDAP_DEBUG_ANY,
652 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
653 fname, lineno, cargv[0] );
655 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
662 Debug( LDAP_DEBUG_ANY,
663 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
664 fname, lineno, cargv[0] );
683 for ( token = strtok_quote( line, " \t" ); token != NULL;
684 token = strtok_quote( NULL, " \t" ) ) {
685 if ( *argcp == MAXARGS ) {
686 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
690 argv[(*argcp)++] = token;
697 strtok_quote( char *line, char *sep )
703 if ( line != NULL ) {
706 while ( *next && strchr( sep, *next ) ) {
710 if ( *next == '\0' ) {
716 for ( inquote = 0; *next; ) {
724 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
730 next + 1, strlen( next + 1 ) + 1 );
731 next++; /* dont parse the escaped character */
736 if ( strchr( sep, *next ) != NULL ) {
749 static char buf[BUFSIZ];
751 static int lmax, lcur;
753 #define CATLINE( buf ) { \
755 len = strlen( buf ); \
756 while ( lcur + len + 1 > lmax ) { \
758 line = (char *) ch_realloc( line, lmax ); \
760 strcpy( line + lcur, buf ); \
765 fp_getline( FILE *fp, int *lineno )
773 /* hack attack - keeps us from having to keep a stack of bufs... */
774 if ( strncasecmp( line, "include", 7 ) == 0 ) {
779 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
780 if ( (p = strchr( buf, '\n' )) != NULL ) {
783 if ( ! isspace( (unsigned char) buf[0] ) ) {
792 return( line[0] ? line : NULL );
796 fp_getline_init( int *lineno )