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 char *ldap_srvtab = "";
30 char *slapd_pid_file = NULL;
31 char *slapd_args_file = NULL;
33 static char *fp_getline(FILE *fp, int *lineno);
34 static void fp_getline_init(int *lineno);
35 static int fp_parse_line(char *line, int *argcp, char **argv);
37 static char *strtok_quote(char *line, char *sep);
40 read_config( char *fname )
43 char *line, *savefname, *saveline;
44 int cargc, savelineno;
48 static BackendInfo *bi = NULL;
49 static BackendDB *be = NULL;
51 if ( (fp = fopen( fname, "r" )) == NULL ) {
53 Debug( LDAP_DEBUG_ANY,
54 "could not open config file \"%s\" - absolute path?\n",
60 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
62 if ( schema_init( ) != 0 ) {
63 Debug( LDAP_DEBUG_ANY,
64 "error initializing the schema\n",
69 fp_getline_init( &lineno );
71 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
72 /* skip comments and blank lines */
73 if ( line[0] == '#' || line[0] == '\0' ) {
77 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
79 /* fp_parse_line is destructive, we save a copy */
80 saveline = ch_strdup( line );
82 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
87 Debug( LDAP_DEBUG_ANY,
88 "%s: line %d: bad config line (ignored)\n",
93 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
95 Debug( LDAP_DEBUG_ANY,
96 "%s: line %d: missing type in \"backend <type>\" line\n",
102 Debug( LDAP_DEBUG_ANY,
103 "%s: line %d: backend line must appear before any database definition\n",
108 bi = backend_info( cargv[1] );
110 /* start of a new database definition */
111 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
113 Debug( LDAP_DEBUG_ANY,
114 "%s: line %d: missing type in \"database <type>\" line\n",
119 be = backend_db_init( cargv[1] );
121 /* assign a default depth limit for alias deref */
122 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
124 /* get pid file name */
125 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
127 Debug( LDAP_DEBUG_ANY,
128 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
133 slapd_pid_file = ch_strdup( cargv[1] );
135 /* get args file name */
136 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
138 Debug( LDAP_DEBUG_ANY,
139 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
144 slapd_args_file = ch_strdup( cargv[1] );
147 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
149 Debug( LDAP_DEBUG_ANY,
150 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
155 defsize = atoi( cargv[1] );
157 be->be_sizelimit = atoi( cargv[1] );
161 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
163 Debug( LDAP_DEBUG_ANY,
164 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
169 deftime = atoi( cargv[1] );
171 be->be_timelimit = atoi( cargv[1] );
174 /* set database suffix */
175 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
177 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
181 } else if ( cargc > 2 ) {
182 Debug( LDAP_DEBUG_ANY,
183 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
184 fname, lineno, cargv[1] );
187 Debug( LDAP_DEBUG_ANY,
188 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
191 char *dn = ch_strdup( cargv[1] );
192 (void) dn_normalize( dn );
193 charray_add( &be->be_suffix, dn );
194 (void) dn_upcase( dn );
195 charray_add( &be->be_nsuffix, dn );
199 /* set database suffixAlias */
200 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
202 Debug( LDAP_DEBUG_ANY,
203 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
206 } else if ( cargc < 3 ) {
207 Debug( LDAP_DEBUG_ANY,
208 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
211 } else if ( cargc > 3 ) {
212 Debug( LDAP_DEBUG_ANY,
213 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
217 Debug( LDAP_DEBUG_ANY,
218 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
221 char *alias, *aliased_dn;
223 alias = ch_strdup( cargv[1] );
224 (void) dn_normalize( alias );
226 aliased_dn = ch_strdup( cargv[2] );
227 (void) dn_normalize( aliased_dn );
230 if ( strcasecmp( alias, aliased_dn) == 0 ) {
231 Debug( LDAP_DEBUG_ANY,
232 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
233 fname, lineno, alias );
235 (void) dn_normalize_case( alias );
236 (void) dn_normalize_case( aliased_dn );
237 charray_add( &be->be_suffixAlias, alias );
238 charray_add( &be->be_suffixAlias, aliased_dn );
245 /* set max deref depth */
246 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
248 Debug( LDAP_DEBUG_ANY,
249 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
254 Debug( LDAP_DEBUG_ANY,
255 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
258 be->be_maxDerefDepth = atoi (cargv[1]);
262 /* set magic "root" dn for this database */
263 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
265 Debug( LDAP_DEBUG_ANY,
266 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
271 Debug( LDAP_DEBUG_ANY,
272 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
275 be->be_root_dn = ch_strdup( cargv[1] );
276 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
279 /* set super-secret magic database password */
280 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
282 Debug( LDAP_DEBUG_ANY,
283 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
292 be->be_root_pw = ch_strdup( cargv[1] );
295 /* make this database read-only */
296 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
298 Debug( LDAP_DEBUG_ANY,
299 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
304 Debug( LDAP_DEBUG_ANY,
305 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
308 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
315 /* where to send clients when we don't hold it */
316 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
318 Debug( LDAP_DEBUG_ANY,
319 "%s: line %d: missing URL in \"referral <URL>\" line\n",
323 default_referral = (char *) ch_malloc( strlen( cargv[1] )
324 + sizeof("Referral:\n") + 1 );
325 strcpy( default_referral, "Referral:\n" );
326 strcat( default_referral, cargv[1] );
329 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
333 Debug( LDAP_DEBUG_ANY,
334 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
339 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
340 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
341 : ch_strdup( cargv[1] ) );
343 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
344 Debug( LDAP_DEBUG_ANY,
346 ? "%s: line %d: bad locale \"%s\"\n"
347 : "%s: line %d: bad locale\n"),
348 fname, lineno, locale );
352 Debug( LDAP_DEBUG_ANY,
353 "%s: line %d: \"locale\" unsupported\n",
357 /* specify an objectclass */
358 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
359 if ( *cargv[1] == '(' ) {
361 p = strchr(saveline,'(');
362 parse_oc( fname, lineno, p );
364 parse_oc_old( be, fname, lineno, cargc, cargv );
367 /* specify an attribute */
368 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
369 if ( *cargv[1] == '(' ) {
371 p = strchr(saveline,'(');
372 parse_at( fname, lineno, p );
374 attr_syntax_config( fname, lineno, cargc - 1,
378 /* turn on/off schema checking */
379 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
381 Debug( LDAP_DEBUG_ANY,
382 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
386 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
387 global_schemacheck = 0;
389 global_schemacheck = 1;
392 /* specify access control info */
393 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
394 parse_acl( be, fname, lineno, cargc, cargv );
396 /* specify default access control info */
397 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
399 Debug( LDAP_DEBUG_ANY,
400 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
405 if ( (global_default_access =
406 str2access( cargv[1] )) == -1 ) {
407 Debug( LDAP_DEBUG_ANY,
408 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
409 fname, lineno, cargv[1] );
413 if ( (be->be_dfltaccess =
414 str2access( cargv[1] )) == -1 ) {
415 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
417 fname, lineno, cargv[1] );
422 /* debug level to log things to syslog */
423 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
425 Debug( LDAP_DEBUG_ANY,
426 "%s: line %d: missing level in \"loglevel <level>\" line\n",
430 ldap_syslog = atoi( cargv[1] );
432 /* list of replicas of the data in this backend (master only) */
433 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
435 Debug( LDAP_DEBUG_ANY,
436 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
441 Debug( LDAP_DEBUG_ANY,
442 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
445 for ( i = 1; i < cargc; i++ ) {
446 if ( strncasecmp( cargv[i], "host=", 5 )
448 charray_add( &be->be_replica,
454 Debug( LDAP_DEBUG_ANY,
455 "%s: line %d: missing host in \"replica\" line (ignored)\n",
460 /* dn of master entity allowed to write to replica */
461 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
463 Debug( LDAP_DEBUG_ANY,
464 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
469 Debug( LDAP_DEBUG_ANY,
470 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
473 be->be_update_ndn = ch_strdup( cargv[1] );
474 (void) dn_normalize_case( be->be_update_ndn );
477 /* replication log file to which changes are appended */
478 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
480 Debug( LDAP_DEBUG_ANY,
481 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
486 be->be_replogfile = ch_strdup( cargv[1] );
488 replogfile = ch_strdup( cargv[1] );
491 /* maintain lastmodified{by,time} attributes */
492 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
494 Debug( LDAP_DEBUG_ANY,
495 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
499 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
506 be->be_lastmod = OFF;
508 global_lastmod = OFF;
511 /* include another config file */
512 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
514 Debug( LDAP_DEBUG_ANY,
515 "%s: line %d: missing filename in \"include <filename>\" line\n",
519 savefname = ch_strdup( cargv[1] );
522 if ( read_config( savefname ) != 0 ) {
527 lineno = savelineno - 1;
529 /* location of kerberos srvtab file */
530 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
532 Debug( LDAP_DEBUG_ANY,
533 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
537 ldap_srvtab = ch_strdup( cargv[1] );
539 /* pass anything else to the current backend info/db config routine */
542 if ( bi->bi_config == 0 ) {
543 Debug( LDAP_DEBUG_ANY,
544 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
545 fname, lineno, cargv[0] );
547 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
553 } else if ( be != NULL ) {
554 if ( be->be_config == 0 ) {
555 Debug( LDAP_DEBUG_ANY,
556 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
557 fname, lineno, cargv[0] );
559 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
566 Debug( LDAP_DEBUG_ANY,
567 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
568 fname, lineno, cargv[0] );
587 for ( token = strtok_quote( line, " \t" ); token != NULL;
588 token = strtok_quote( NULL, " \t" ) ) {
589 if ( *argcp == MAXARGS ) {
590 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
594 argv[(*argcp)++] = token;
601 strtok_quote( char *line, char *sep )
607 if ( line != NULL ) {
610 while ( *next && strchr( sep, *next ) ) {
614 if ( *next == '\0' ) {
620 for ( inquote = 0; *next; ) {
628 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
634 next + 1, strlen( next + 1 ) + 1 );
635 next++; /* dont parse the escaped character */
640 if ( strchr( sep, *next ) != NULL ) {
653 static char buf[BUFSIZ];
655 static int lmax, lcur;
657 #define CATLINE( buf ) { \
659 len = strlen( buf ); \
660 while ( lcur + len + 1 > lmax ) { \
662 line = (char *) ch_realloc( line, lmax ); \
664 strcpy( line + lcur, buf ); \
669 fp_getline( FILE *fp, int *lineno )
677 /* hack attack - keeps us from having to keep a stack of bufs... */
678 if ( strncasecmp( line, "include", 7 ) == 0 ) {
683 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
684 if ( (p = strchr( buf, '\n' )) != NULL ) {
687 if ( ! isspace( (unsigned char) buf[0] ) ) {
696 return( line[0] ? line : NULL );
700 fp_getline_init( int *lineno )