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;
27 int global_lastmod = ON;
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 ) {
209 Debug( LDAP_DEBUG_ANY,
210 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
213 } else if ( cargc > 2 ) {
214 Debug( LDAP_DEBUG_ANY,
215 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
216 fname, lineno, cargv[1] );
219 Debug( LDAP_DEBUG_ANY,
220 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
222 } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
223 Debug( LDAP_DEBUG_ANY,
224 "%s: line %d: suffix already served by this backend (ignored)\n",
226 } else if ( tmp_be != NULL ) {
227 Debug( LDAP_DEBUG_ANY,
228 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
229 fname, lineno, tmp_be->be_suffix[0] );
231 char *dn = ch_strdup( cargv[1] );
232 (void) dn_normalize( dn );
233 charray_add( &be->be_suffix, dn );
234 (void) str2upper( dn );
235 charray_add( &be->be_nsuffix, dn );
239 /* set database suffixAlias */
240 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
243 Debug( LDAP_DEBUG_ANY,
244 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
247 } else if ( cargc < 3 ) {
248 Debug( LDAP_DEBUG_ANY,
249 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
252 } else if ( cargc > 3 ) {
253 Debug( LDAP_DEBUG_ANY,
254 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
259 Debug( LDAP_DEBUG_ANY,
260 "%s: line %d: suffixAlias line"
261 " must appear inside a database definition (ignored)\n",
263 } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
264 Debug( LDAP_DEBUG_ANY,
265 "%s: line %d: suffixAlias served by"
266 " a preceeding backend \"%s\" (ignored)\n",
267 fname, lineno, tmp_be->be_suffix[0] );
269 } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
270 Debug( LDAP_DEBUG_ANY,
271 "%s: line %d: suffixAlias derefs to differnet backend"
272 " a preceeding backend \"%s\" (ignored)\n",
273 fname, lineno, tmp_be->be_suffix[0] );
276 char *alias, *aliased_dn;
278 alias = ch_strdup( cargv[1] );
279 (void) dn_normalize( alias );
281 aliased_dn = ch_strdup( cargv[2] );
282 (void) dn_normalize( aliased_dn );
284 (void) dn_normalize_case( alias );
285 (void) dn_normalize_case( aliased_dn );
286 charray_add( &be->be_suffixAlias, alias );
287 charray_add( &be->be_suffixAlias, aliased_dn );
293 /* set max deref depth */
294 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
297 Debug( LDAP_DEBUG_ANY,
298 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
303 Debug( LDAP_DEBUG_ANY,
304 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
306 } else if ((i = atoi(cargv[1])) < 0) {
307 Debug( LDAP_DEBUG_ANY,
308 "%s: line %d: depth must be positive (ignored)\n",
312 be->be_max_deref_depth = i;
316 /* set magic "root" dn for this database */
317 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
319 Debug( LDAP_DEBUG_ANY,
320 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
325 Debug( LDAP_DEBUG_ANY,
326 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
329 be->be_root_dn = ch_strdup( cargv[1] );
330 be->be_root_ndn = ch_strdup( cargv[1] );
332 if( dn_normalize_case( be->be_root_ndn ) == NULL ) {
333 free( be->be_root_dn );
334 free( be->be_root_ndn );
335 Debug( LDAP_DEBUG_ANY,
336 "%s: line %d: rootdn DN is invalid\n",
342 /* set super-secret magic database password */
343 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
345 Debug( LDAP_DEBUG_ANY,
346 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
351 Debug( LDAP_DEBUG_ANY,
352 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
355 be->be_root_pw = ch_strdup( cargv[1] );
358 /* make this database read-only */
359 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
361 Debug( LDAP_DEBUG_ANY,
362 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
367 Debug( LDAP_DEBUG_ANY,
368 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
371 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
378 /* where to send clients when we don't hold it */
379 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
381 Debug( LDAP_DEBUG_ANY,
382 "%s: line %d: missing URL in \"referral <URL>\" line\n",
387 vals[0]->bv_val = cargv[1];
388 vals[0]->bv_len = strlen( vals[0]->bv_val );
389 value_add( &default_referral, vals );
392 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
396 Debug( LDAP_DEBUG_ANY,
397 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
402 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
403 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
404 : ch_strdup( cargv[1] ) );
406 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
407 Debug( LDAP_DEBUG_ANY,
409 ? "%s: line %d: bad locale \"%s\"\n"
410 : "%s: line %d: bad locale\n"),
411 fname, lineno, locale );
415 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: \"locale\" unsupported\n",
420 /* specify an objectclass */
421 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
422 if ( *cargv[1] == '(' ) {
424 p = strchr(saveline,'(');
425 parse_oc( fname, lineno, p );
427 parse_oc_old( be, fname, lineno, cargc, cargv );
430 /* specify an attribute */
431 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
432 if ( *cargv[1] == '(' ) {
434 p = strchr(saveline,'(');
435 parse_at( fname, lineno, p );
437 attr_syntax_config( fname, lineno, cargc - 1,
441 /* turn on/off schema checking */
442 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
444 Debug( LDAP_DEBUG_ANY,
445 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
449 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
450 global_schemacheck = 0;
452 global_schemacheck = 1;
455 /* specify access control info */
456 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
457 parse_acl( be, fname, lineno, cargc, cargv );
459 /* specify default access control info */
460 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
462 Debug( LDAP_DEBUG_ANY,
463 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
468 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
469 str2access(cargv[1]))) )
471 Debug( LDAP_DEBUG_ANY,
472 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
473 fname, lineno, cargv[1] );
477 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
478 str2access(cargv[1]))) )
480 Debug( LDAP_DEBUG_ANY,
481 "%s: line %d: bad access \"%s\", "
482 "expecting [self]{none|auth|compare|search|read|write}\n",
483 fname, lineno, cargv[1] );
488 /* debug level to log things to syslog */
489 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
491 Debug( LDAP_DEBUG_ANY,
492 "%s: line %d: missing level in \"loglevel <level>\" line\n",
496 ldap_syslog = atoi( cargv[1] );
498 /* list of replicas of the data in this backend (master only) */
499 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
501 Debug( LDAP_DEBUG_ANY,
502 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
507 Debug( LDAP_DEBUG_ANY,
508 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
511 for ( i = 1; i < cargc; i++ ) {
512 if ( strncasecmp( cargv[i], "host=", 5 )
514 charray_add( &be->be_replica,
520 Debug( LDAP_DEBUG_ANY,
521 "%s: line %d: missing host in \"replica\" line (ignored)\n",
526 /* dn of master entity allowed to write to replica */
527 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
529 Debug( LDAP_DEBUG_ANY,
530 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
535 Debug( LDAP_DEBUG_ANY,
536 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
539 be->be_update_ndn = ch_strdup( cargv[1] );
540 if( dn_normalize_case( be->be_update_ndn ) == NULL ) {
541 Debug( LDAP_DEBUG_ANY,
542 "%s: line %d: updatedn DN is invalid\n",
548 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
550 Debug( LDAP_DEBUG_ANY,
551 "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
556 Debug( LDAP_DEBUG_ANY,
557 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
559 } else if ( be->be_update_ndn == NULL ) {
560 Debug( LDAP_DEBUG_ANY,
561 "%s: line %d: updateref line must after updatedn (ignored)\n",
564 vals[0]->bv_val = cargv[1];
565 vals[0]->bv_len = strlen( vals[0]->bv_val );
566 value_add( &be->be_update_refs, vals );
569 /* replication log file to which changes are appended */
570 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
572 Debug( LDAP_DEBUG_ANY,
573 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
578 be->be_replogfile = ch_strdup( cargv[1] );
580 replogfile = ch_strdup( cargv[1] );
583 /* maintain lastmodified{by,time} attributes */
584 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
586 Debug( LDAP_DEBUG_ANY,
587 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
591 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
598 be->be_lastmod = OFF;
600 global_lastmod = OFF;
603 /* set idle timeout value */
604 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
607 Debug( LDAP_DEBUG_ANY,
608 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
613 i = atoi( cargv[1] );
616 Debug( LDAP_DEBUG_ANY,
617 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
622 global_idletimeout = i;
624 /* include another config file */
625 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
627 Debug( LDAP_DEBUG_ANY,
628 "%s: line %d: missing filename in \"include <filename>\" line\n",
632 savefname = ch_strdup( cargv[1] );
635 if ( read_config( savefname ) != 0 ) {
640 lineno = savelineno - 1;
642 /* location of kerberos srvtab file */
643 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
645 Debug( LDAP_DEBUG_ANY,
646 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
650 ldap_srvtab = ch_strdup( cargv[1] );
653 } else if (strcasecmp( cargv[0], "loadmodule") == 0 ) {
655 Debug( LDAP_DEBUG_ANY,
656 "%s: line %d: missing filename in \"loadmodule <filename>\" line\n",
658 exit( EXIT_FAILURE );
660 if (!load_module(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
661 Debug( LDAP_DEBUG_ANY,
662 "%s: line %d: failed to load or initialize module %s\n",
663 fname, lineno, cargv[1]);
664 exit( EXIT_FAILURE );
667 #endif /*SLAPD_MODULES*/
670 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
671 rc = ldap_pvt_tls_set_option( NULL,
672 LDAP_OPT_X_TLS_PROTOCOL,
677 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
678 rc = ldap_pvt_tls_set_option( NULL,
679 LDAP_OPT_X_TLS_CIPHER_SUITE,
684 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
685 rc = ldap_pvt_tls_set_option( NULL,
686 LDAP_OPT_X_TLS_CERTFILE,
691 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
692 rc = ldap_pvt_tls_set_option( NULL,
693 LDAP_OPT_X_TLS_KEYFILE,
698 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
699 rc = ldap_pvt_tls_set_option( NULL,
700 LDAP_OPT_X_TLS_CACERTDIR,
705 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
706 rc = ldap_pvt_tls_set_option( NULL,
707 LDAP_OPT_X_TLS_CACERTFILE,
711 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
712 rc = ldap_pvt_tls_set_option( NULL,
713 LDAP_OPT_X_TLS_REQUIRE_CERT,
720 /* pass anything else to the current backend info/db config routine */
723 if ( bi->bi_config == 0 ) {
724 Debug( LDAP_DEBUG_ANY,
725 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
726 fname, lineno, cargv[0] );
728 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
734 } else if ( be != NULL ) {
735 if ( be->be_config == 0 ) {
736 Debug( LDAP_DEBUG_ANY,
737 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
738 fname, lineno, cargv[0] );
740 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
747 Debug( LDAP_DEBUG_ANY,
748 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
749 fname, lineno, cargv[0] );
768 for ( token = strtok_quote( line, " \t" ); token != NULL;
769 token = strtok_quote( NULL, " \t" ) ) {
770 if ( *argcp == MAXARGS ) {
771 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
775 argv[(*argcp)++] = token;
782 strtok_quote( char *line, char *sep )
788 if ( line != NULL ) {
791 while ( *next && strchr( sep, *next ) ) {
795 if ( *next == '\0' ) {
801 for ( inquote = 0; *next; ) {
809 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
815 next + 1, strlen( next + 1 ) + 1 );
816 next++; /* dont parse the escaped character */
821 if ( strchr( sep, *next ) != NULL ) {
834 static char buf[BUFSIZ];
836 static int lmax, lcur;
838 #define CATLINE( buf ) { \
840 len = strlen( buf ); \
841 while ( lcur + len + 1 > lmax ) { \
843 line = (char *) ch_realloc( line, lmax ); \
845 strcpy( line + lcur, buf ); \
850 fp_getline( FILE *fp, int *lineno )
858 /* hack attack - keeps us from having to keep a stack of bufs... */
859 if ( strncasecmp( line, "include", 7 ) == 0 ) {
864 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
865 if ( (p = strchr( buf, '\n' )) != NULL ) {
868 if ( ! isspace( (unsigned char) buf[0] ) ) {
877 return( line[0] ? line : NULL );
881 fp_getline_init( int *lineno )