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 *ldap_srvtab = "";
31 char *slapd_pid_file = NULL;
32 char *slapd_args_file = NULL;
34 static char *fp_getline(FILE *fp, int *lineno);
35 static void fp_getline_init(int *lineno);
36 static int fp_parse_line(char *line, int *argcp, char **argv);
38 static char *strtok_quote(char *line, char *sep);
41 read_config( char *fname )
44 char *line, *savefname, *saveline;
45 int cargc, savelineno;
49 static BackendInfo *bi = NULL;
50 static BackendDB *be = NULL;
52 if ( (fp = fopen( fname, "r" )) == NULL ) {
54 Debug( LDAP_DEBUG_ANY,
55 "could not open config file \"%s\" - absolute path?\n",
61 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
63 if ( schema_init( ) != 0 ) {
64 Debug( LDAP_DEBUG_ANY,
65 "error initializing the schema\n",
70 fp_getline_init( &lineno );
72 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
73 /* skip comments and blank lines */
74 if ( line[0] == '#' || line[0] == '\0' ) {
78 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
80 /* fp_parse_line is destructive, we save a copy */
81 saveline = ch_strdup( line );
83 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
88 Debug( LDAP_DEBUG_ANY,
89 "%s: line %d: bad config line (ignored)\n",
94 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
96 Debug( LDAP_DEBUG_ANY,
97 "%s: line %d: missing type in \"backend <type>\" line\n",
103 Debug( LDAP_DEBUG_ANY,
104 "%s: line %d: backend line must appear before any database definition\n",
109 bi = backend_info( cargv[1] );
111 /* start of a new database definition */
112 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
114 Debug( LDAP_DEBUG_ANY,
115 "%s: line %d: missing type in \"database <type>\" line\n",
120 be = backend_db_init( cargv[1] );
122 /* assign a default depth limit for alias deref */
123 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
125 /* get pid file name */
126 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
128 Debug( LDAP_DEBUG_ANY,
129 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
134 slapd_pid_file = ch_strdup( cargv[1] );
136 /* get args file name */
137 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
139 Debug( LDAP_DEBUG_ANY,
140 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
145 slapd_args_file = ch_strdup( cargv[1] );
148 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
150 Debug( LDAP_DEBUG_ANY,
151 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
156 defsize = atoi( cargv[1] );
158 be->be_sizelimit = atoi( cargv[1] );
162 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
164 Debug( LDAP_DEBUG_ANY,
165 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
170 deftime = atoi( cargv[1] );
172 be->be_timelimit = atoi( cargv[1] );
175 /* set database suffix */
176 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
178 Debug( LDAP_DEBUG_ANY,
179 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
182 } else if ( cargc > 2 ) {
183 Debug( LDAP_DEBUG_ANY,
184 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
185 fname, lineno, cargv[1] );
188 Debug( LDAP_DEBUG_ANY,
189 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
192 char *dn = ch_strdup( cargv[1] );
193 (void) dn_normalize( dn );
194 charray_add( &be->be_suffix, dn );
195 (void) dn_upcase( dn );
196 charray_add( &be->be_nsuffix, dn );
200 /* set database suffixAlias */
201 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
203 Debug( LDAP_DEBUG_ANY,
204 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
207 } else if ( cargc < 3 ) {
208 Debug( LDAP_DEBUG_ANY,
209 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
212 } else if ( cargc > 3 ) {
213 Debug( LDAP_DEBUG_ANY,
214 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
218 Debug( LDAP_DEBUG_ANY,
219 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
222 char *alias, *aliased_dn;
224 alias = ch_strdup( cargv[1] );
225 (void) dn_normalize( alias );
227 aliased_dn = ch_strdup( cargv[2] );
228 (void) dn_normalize( aliased_dn );
231 if ( strcasecmp( alias, aliased_dn) == 0 ) {
232 Debug( LDAP_DEBUG_ANY,
233 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
234 fname, lineno, alias );
236 (void) dn_normalize_case( alias );
237 (void) dn_normalize_case( aliased_dn );
238 charray_add( &be->be_suffixAlias, alias );
239 charray_add( &be->be_suffixAlias, aliased_dn );
246 /* set max deref depth */
247 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
249 Debug( LDAP_DEBUG_ANY,
250 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
255 Debug( LDAP_DEBUG_ANY,
256 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
259 be->be_maxDerefDepth = atoi (cargv[1]);
263 /* set magic "root" dn for this database */
264 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
266 Debug( LDAP_DEBUG_ANY,
267 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
272 Debug( LDAP_DEBUG_ANY,
273 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
276 be->be_root_dn = ch_strdup( cargv[1] );
277 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
280 /* set super-secret magic database password */
281 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
283 Debug( LDAP_DEBUG_ANY,
284 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
289 Debug( LDAP_DEBUG_ANY,
290 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
293 be->be_root_pw = ch_strdup( cargv[1] );
296 /* make this database read-only */
297 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
299 Debug( LDAP_DEBUG_ANY,
300 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
305 Debug( LDAP_DEBUG_ANY,
306 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
309 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
316 /* where to send clients when we don't hold it */
317 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
319 Debug( LDAP_DEBUG_ANY,
320 "%s: line %d: missing URL in \"referral <URL>\" line\n",
324 default_referral = (char *) ch_malloc( strlen( cargv[1] )
325 + sizeof("Referral:\n") + 1 );
326 strcpy( default_referral, "Referral:\n" );
327 strcat( default_referral, cargv[1] );
330 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
334 Debug( LDAP_DEBUG_ANY,
335 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
340 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
341 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
342 : ch_strdup( cargv[1] ) );
344 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
345 Debug( LDAP_DEBUG_ANY,
347 ? "%s: line %d: bad locale \"%s\"\n"
348 : "%s: line %d: bad locale\n"),
349 fname, lineno, locale );
353 Debug( LDAP_DEBUG_ANY,
354 "%s: line %d: \"locale\" unsupported\n",
358 /* specify an objectclass */
359 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
360 if ( *cargv[1] == '(' ) {
362 p = strchr(saveline,'(');
363 parse_oc( fname, lineno, p );
365 parse_oc_old( be, fname, lineno, cargc, cargv );
368 /* specify an attribute */
369 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
370 if ( *cargv[1] == '(' ) {
372 p = strchr(saveline,'(');
373 parse_at( fname, lineno, p );
375 attr_syntax_config( fname, lineno, cargc - 1,
379 /* turn on/off schema checking */
380 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
382 Debug( LDAP_DEBUG_ANY,
383 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
387 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
388 global_schemacheck = 0;
390 global_schemacheck = 1;
393 /* specify access control info */
394 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
395 parse_acl( be, fname, lineno, cargc, cargv );
397 /* specify default access control info */
398 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
400 Debug( LDAP_DEBUG_ANY,
401 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
406 if ( (global_default_access =
407 str2access( cargv[1] )) == -1 ) {
408 Debug( LDAP_DEBUG_ANY,
409 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
410 fname, lineno, cargv[1] );
414 if ( (be->be_dfltaccess =
415 str2access( cargv[1] )) == -1 ) {
416 Debug( LDAP_DEBUG_ANY,
417 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
418 fname, lineno, cargv[1] );
423 /* debug level to log things to syslog */
424 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
426 Debug( LDAP_DEBUG_ANY,
427 "%s: line %d: missing level in \"loglevel <level>\" line\n",
431 ldap_syslog = atoi( cargv[1] );
433 /* list of replicas of the data in this backend (master only) */
434 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
436 Debug( LDAP_DEBUG_ANY,
437 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
442 Debug( LDAP_DEBUG_ANY,
443 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
446 for ( i = 1; i < cargc; i++ ) {
447 if ( strncasecmp( cargv[i], "host=", 5 )
449 charray_add( &be->be_replica,
455 Debug( LDAP_DEBUG_ANY,
456 "%s: line %d: missing host in \"replica\" line (ignored)\n",
461 /* dn of master entity allowed to write to replica */
462 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
464 Debug( LDAP_DEBUG_ANY,
465 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
470 Debug( LDAP_DEBUG_ANY,
471 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
474 be->be_update_ndn = ch_strdup( cargv[1] );
475 (void) dn_normalize_case( be->be_update_ndn );
478 /* replication log file to which changes are appended */
479 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
481 Debug( LDAP_DEBUG_ANY,
482 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
487 be->be_replogfile = ch_strdup( cargv[1] );
489 replogfile = ch_strdup( cargv[1] );
492 /* maintain lastmodified{by,time} attributes */
493 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
495 Debug( LDAP_DEBUG_ANY,
496 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
500 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
507 be->be_lastmod = OFF;
509 global_lastmod = OFF;
512 /* set idle timeout value */
513 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
516 Debug( LDAP_DEBUG_ANY,
517 "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
522 i = atoi( cargv[1] );
525 Debug( LDAP_DEBUG_ANY,
526 "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
531 global_idletimeout = i;
533 /* include another config file */
534 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
536 Debug( LDAP_DEBUG_ANY,
537 "%s: line %d: missing filename in \"include <filename>\" line\n",
541 savefname = ch_strdup( cargv[1] );
544 if ( read_config( savefname ) != 0 ) {
549 lineno = savelineno - 1;
551 /* location of kerberos srvtab file */
552 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
554 Debug( LDAP_DEBUG_ANY,
555 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
559 ldap_srvtab = ch_strdup( cargv[1] );
561 /* pass anything else to the current backend info/db config routine */
564 if ( bi->bi_config == 0 ) {
565 Debug( LDAP_DEBUG_ANY,
566 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
567 fname, lineno, cargv[0] );
569 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
575 } else if ( be != NULL ) {
576 if ( be->be_config == 0 ) {
577 Debug( LDAP_DEBUG_ANY,
578 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
579 fname, lineno, cargv[0] );
581 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
588 Debug( LDAP_DEBUG_ANY,
589 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
590 fname, lineno, cargv[0] );
609 for ( token = strtok_quote( line, " \t" ); token != NULL;
610 token = strtok_quote( NULL, " \t" ) ) {
611 if ( *argcp == MAXARGS ) {
612 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
616 argv[(*argcp)++] = token;
623 strtok_quote( char *line, char *sep )
629 if ( line != NULL ) {
632 while ( *next && strchr( sep, *next ) ) {
636 if ( *next == '\0' ) {
642 for ( inquote = 0; *next; ) {
650 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
656 next + 1, strlen( next + 1 ) + 1 );
657 next++; /* dont parse the escaped character */
662 if ( strchr( sep, *next ) != NULL ) {
675 static char buf[BUFSIZ];
677 static int lmax, lcur;
679 #define CATLINE( buf ) { \
681 len = strlen( buf ); \
682 while ( lcur + len + 1 > lmax ) { \
684 line = (char *) ch_realloc( line, lmax ); \
686 strcpy( line + lcur, buf ); \
691 fp_getline( FILE *fp, int *lineno )
699 /* hack attack - keeps us from having to keep a stack of bufs... */
700 if ( strncasecmp( line, "include", 7 ) == 0 ) {
705 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
706 if ( (p = strchr( buf, '\n' )) != NULL ) {
709 if ( ! isspace( (unsigned char) buf[0] ) ) {
718 return( line[0] ? line : NULL );
722 fp_getline_init( int *lineno )