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 static BackendInfo *bi = NULL;
51 static BackendDB *be = NULL;
53 if ( (fp = fopen( fname, "r" )) == NULL ) {
55 Debug( LDAP_DEBUG_ANY,
56 "could not open config file \"%s\" - absolute path?\n",
62 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
64 if ( schema_init( ) != 0 ) {
65 Debug( LDAP_DEBUG_ANY,
66 "error initializing the schema\n",
71 fp_getline_init( &lineno );
73 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
74 /* skip comments and blank lines */
75 if ( line[0] == '#' || line[0] == '\0' ) {
79 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
81 /* fp_parse_line is destructive, we save a copy */
82 saveline = ch_strdup( line );
84 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
89 Debug( LDAP_DEBUG_ANY,
90 "%s: line %d: bad config line (ignored)\n",
95 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
97 Debug( LDAP_DEBUG_ANY,
98 "%s: line %d: missing type in \"backend <type>\" line\n",
104 Debug( LDAP_DEBUG_ANY,
105 "%s: line %d: backend line must appear before any database definition\n",
110 bi = backend_info( cargv[1] );
112 /* start of a new database definition */
113 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
115 Debug( LDAP_DEBUG_ANY,
116 "%s: line %d: missing type in \"database <type>\" line\n",
121 be = backend_db_init( cargv[1] );
123 /* assign a default depth limit for alias deref */
124 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
126 /* get pid file name */
127 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
129 Debug( LDAP_DEBUG_ANY,
130 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
135 slapd_pid_file = ch_strdup( cargv[1] );
137 /* get args file name */
138 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
140 Debug( LDAP_DEBUG_ANY,
141 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
146 slapd_args_file = ch_strdup( cargv[1] );
148 /* set DIGEST realm */
149 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
151 Debug( LDAP_DEBUG_ANY,
152 "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
157 be->be_realm = ch_strdup( cargv[1] );
159 } else if ( global_realm != NULL ) {
160 Debug( LDAP_DEBUG_ANY,
161 "%s: line %d: already set global realm!\n",
166 global_realm = ch_strdup( cargv[1] );
170 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
172 Debug( LDAP_DEBUG_ANY,
173 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
178 defsize = atoi( cargv[1] );
180 be->be_sizelimit = atoi( cargv[1] );
184 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
186 Debug( LDAP_DEBUG_ANY,
187 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
192 deftime = atoi( cargv[1] );
194 be->be_timelimit = atoi( cargv[1] );
197 /* set database suffix */
198 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
200 Debug( LDAP_DEBUG_ANY,
201 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
204 } else if ( cargc > 2 ) {
205 Debug( LDAP_DEBUG_ANY,
206 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
207 fname, lineno, cargv[1] );
210 Debug( LDAP_DEBUG_ANY,
211 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
214 char *dn = ch_strdup( cargv[1] );
215 (void) dn_normalize( dn );
216 charray_add( &be->be_suffix, dn );
217 (void) dn_upcase( dn );
218 charray_add( &be->be_nsuffix, dn );
222 /* set database suffixAlias */
223 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
225 Debug( LDAP_DEBUG_ANY,
226 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
229 } else if ( cargc < 3 ) {
230 Debug( LDAP_DEBUG_ANY,
231 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
234 } else if ( cargc > 3 ) {
235 Debug( LDAP_DEBUG_ANY,
236 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
240 Debug( LDAP_DEBUG_ANY,
241 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
244 char *alias, *aliased_dn;
246 alias = ch_strdup( cargv[1] );
247 (void) dn_normalize( alias );
249 aliased_dn = ch_strdup( cargv[2] );
250 (void) dn_normalize( aliased_dn );
253 if ( strcasecmp( alias, aliased_dn) == 0 ) {
254 Debug( LDAP_DEBUG_ANY,
255 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
256 fname, lineno, alias );
258 (void) dn_normalize_case( alias );
259 (void) dn_normalize_case( aliased_dn );
260 charray_add( &be->be_suffixAlias, alias );
261 charray_add( &be->be_suffixAlias, aliased_dn );
268 /* set max deref depth */
269 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
271 Debug( LDAP_DEBUG_ANY,
272 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
277 Debug( LDAP_DEBUG_ANY,
278 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
281 be->be_maxDerefDepth = atoi (cargv[1]);
285 /* set magic "root" dn for this database */
286 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
294 Debug( LDAP_DEBUG_ANY,
295 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
298 be->be_root_dn = ch_strdup( cargv[1] );
299 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
302 /* set super-secret magic database password */
303 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
305 Debug( LDAP_DEBUG_ANY,
306 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
311 Debug( LDAP_DEBUG_ANY,
312 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
315 be->be_root_pw = ch_strdup( cargv[1] );
318 /* make this database read-only */
319 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
321 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
327 Debug( LDAP_DEBUG_ANY,
328 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
331 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
338 /* where to send clients when we don't hold it */
339 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
341 Debug( LDAP_DEBUG_ANY,
342 "%s: line %d: missing URL in \"referral <URL>\" line\n",
346 default_referral = (char *) ch_malloc( strlen( cargv[1] )
347 + sizeof("Referral:\n") + 1 );
348 strcpy( default_referral, "Referral:\n" );
349 strcat( default_referral, cargv[1] );
352 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
356 Debug( LDAP_DEBUG_ANY,
357 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
362 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
363 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
364 : ch_strdup( cargv[1] ) );
366 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
367 Debug( LDAP_DEBUG_ANY,
369 ? "%s: line %d: bad locale \"%s\"\n"
370 : "%s: line %d: bad locale\n"),
371 fname, lineno, locale );
375 Debug( LDAP_DEBUG_ANY,
376 "%s: line %d: \"locale\" unsupported\n",
380 /* specify an objectclass */
381 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
382 if ( *cargv[1] == '(' ) {
384 p = strchr(saveline,'(');
385 parse_oc( fname, lineno, p );
387 parse_oc_old( be, fname, lineno, cargc, cargv );
390 /* specify an attribute */
391 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
392 if ( *cargv[1] == '(' ) {
394 p = strchr(saveline,'(');
395 parse_at( fname, lineno, p );
397 attr_syntax_config( fname, lineno, cargc - 1,
401 /* turn on/off schema checking */
402 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
404 Debug( LDAP_DEBUG_ANY,
405 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
409 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
410 global_schemacheck = 0;
412 global_schemacheck = 1;
415 /* specify access control info */
416 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
417 parse_acl( be, fname, lineno, cargc, cargv );
419 /* specify default access control info */
420 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
422 Debug( LDAP_DEBUG_ANY,
423 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
428 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
429 str2access(cargv[1]))) )
431 Debug( LDAP_DEBUG_ANY,
432 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
433 fname, lineno, cargv[1] );
437 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
438 str2access(cargv[1]))) )
440 Debug( LDAP_DEBUG_ANY,
441 "%s: line %d: bad access \"%s\", "
442 "expecting [self]{none|compare|search|read|write}\n",
443 fname, lineno, cargv[1] );
448 /* debug level to log things to syslog */
449 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
451 Debug( LDAP_DEBUG_ANY,
452 "%s: line %d: missing level in \"loglevel <level>\" line\n",
456 ldap_syslog = atoi( cargv[1] );
458 /* list of replicas of the data in this backend (master only) */
459 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
461 Debug( LDAP_DEBUG_ANY,
462 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
467 Debug( LDAP_DEBUG_ANY,
468 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
471 for ( i = 1; i < cargc; i++ ) {
472 if ( strncasecmp( cargv[i], "host=", 5 )
474 charray_add( &be->be_replica,
480 Debug( LDAP_DEBUG_ANY,
481 "%s: line %d: missing host in \"replica\" line (ignored)\n",
486 /* dn of master entity allowed to write to replica */
487 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
489 Debug( LDAP_DEBUG_ANY,
490 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
495 Debug( LDAP_DEBUG_ANY,
496 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
499 be->be_update_ndn = ch_strdup( cargv[1] );
500 (void) dn_normalize_case( be->be_update_ndn );
503 /* replication log file to which changes are appended */
504 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
506 Debug( LDAP_DEBUG_ANY,
507 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
512 be->be_replogfile = ch_strdup( cargv[1] );
514 replogfile = ch_strdup( cargv[1] );
517 /* maintain lastmodified{by,time} attributes */
518 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
520 Debug( LDAP_DEBUG_ANY,
521 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
525 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
532 be->be_lastmod = OFF;
534 global_lastmod = OFF;
537 /* set idle timeout value */
538 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
541 Debug( LDAP_DEBUG_ANY,
542 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
547 i = atoi( cargv[1] );
550 Debug( LDAP_DEBUG_ANY,
551 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
556 global_idletimeout = i;
558 /* include another config file */
559 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
561 Debug( LDAP_DEBUG_ANY,
562 "%s: line %d: missing filename in \"include <filename>\" line\n",
566 savefname = ch_strdup( cargv[1] );
569 if ( read_config( savefname ) != 0 ) {
574 lineno = savelineno - 1;
576 /* location of kerberos srvtab file */
577 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
579 Debug( LDAP_DEBUG_ANY,
580 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
584 ldap_srvtab = ch_strdup( cargv[1] );
587 } else if (strcasecmp( cargv[0], "loadmodule") == 0 ) {
589 Debug( LDAP_DEBUG_ANY,
590 "%s: line %d: missing filename in \"loadmodule <filename>\" line\n",
594 if (!load_module(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
595 Debug( LDAP_DEBUG_ANY,
596 "%s: line %d: failed to load or initialize module %s\n",
597 fname, lineno, cargv[1]);
601 #endif /*SLAPD_MODULES*/
604 } else if ( !strcasecmp( cargv[0], "SSLCertificateFile" ) ) {
605 rc = ldap_pvt_tls_set_option( NULL,
606 LDAP_OPT_X_TLS_CERTFILE,
611 } else if ( !strcasecmp( cargv[0], "SSLCertificateKeyFile" ) ) {
612 rc = ldap_pvt_tls_set_option( NULL,
613 LDAP_OPT_X_TLS_KEYFILE,
619 /* pass anything else to the current backend info/db config routine */
622 if ( bi->bi_config == 0 ) {
623 Debug( LDAP_DEBUG_ANY,
624 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
625 fname, lineno, cargv[0] );
627 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
633 } else if ( be != NULL ) {
634 if ( be->be_config == 0 ) {
635 Debug( LDAP_DEBUG_ANY,
636 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
637 fname, lineno, cargv[0] );
639 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
646 Debug( LDAP_DEBUG_ANY,
647 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
648 fname, lineno, cargv[0] );
667 for ( token = strtok_quote( line, " \t" ); token != NULL;
668 token = strtok_quote( NULL, " \t" ) ) {
669 if ( *argcp == MAXARGS ) {
670 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
674 argv[(*argcp)++] = token;
681 strtok_quote( char *line, char *sep )
687 if ( line != NULL ) {
690 while ( *next && strchr( sep, *next ) ) {
694 if ( *next == '\0' ) {
700 for ( inquote = 0; *next; ) {
708 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
714 next + 1, strlen( next + 1 ) + 1 );
715 next++; /* dont parse the escaped character */
720 if ( strchr( sep, *next ) != NULL ) {
733 static char buf[BUFSIZ];
735 static int lmax, lcur;
737 #define CATLINE( buf ) { \
739 len = strlen( buf ); \
740 while ( lcur + len + 1 > lmax ) { \
742 line = (char *) ch_realloc( line, lmax ); \
744 strcpy( line + lcur, buf ); \
749 fp_getline( FILE *fp, int *lineno )
757 /* hack attack - keeps us from having to keep a stack of bufs... */
758 if ( strncasecmp( line, "include", 7 ) == 0 ) {
763 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
764 if ( (p = strchr( buf, '\n' )) != NULL ) {
767 if ( ! isspace( (unsigned char) buf[0] ) ) {
776 return( line[0] ? line : NULL );
780 fp_getline_init( int *lineno )