1 /* config.c - configuration file handling routines */
11 #include "ldapconfig.h"
17 * defaults for various global variables
19 int defsize = SLAPD_DEFAULT_SIZELIMIT;
20 int deftime = SLAPD_DEFAULT_TIMELIMIT;
21 struct acl *global_acl = NULL;
22 int global_default_access = ACL_READ;
25 char *ldap_srvtab = "";
27 char *slapd_pid_file = NULL;
28 char *slapd_args_file = NULL;
30 static char *fp_getline(FILE *fp, int *lineno);
31 static void fp_getline_init(int *lineno);
32 static void fp_parse_line(char *line, int *argcp, char **argv);
34 static char *strtok_quote(char *line, char *sep);
36 /* the old interface for tools */
38 read_config( char *fname, Backend **bep, FILE *pfp )
40 read_config_env( fname, bep, pfp, 0 );
43 /* the new interface for slapd */
45 read_config_env( char *fname, Backend **bep, FILE *pfp, int startup )
48 char *line, *savefname;
49 int cargc, savelineno;
54 if ( (fp = pfp) == NULL && (fp = fopen( fname, "r" )) == NULL ) {
56 Debug( LDAP_DEBUG_ANY,
57 "could not open config file \"%s\" - absolute path?\n",
63 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
65 fp_getline_init( &lineno );
66 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
67 /* skip comments and blank lines */
68 if ( line[0] == '#' || line[0] == '\0' ) {
72 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
74 fp_parse_line( line, &cargc, cargv );
77 Debug( LDAP_DEBUG_ANY,
78 "%s: line %d: bad config line (ignored)\n",
83 /* start of a new database definition */
84 if ( strcasecmp( cargv[0], "database" ) == 0 ) {
86 Debug( LDAP_DEBUG_ANY,
87 "%s: line %d: missing type in \"database <type>\" line\n",
91 *bep = new_backend( cargv[1] );
94 /* assign a default depth limit for alias deref */
95 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
97 /* get pid file name */
98 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
100 Debug( LDAP_DEBUG_ANY,
101 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
106 slapd_pid_file = ch_strdup( cargv[1] );
108 /* get args file name */
109 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
111 Debug( LDAP_DEBUG_ANY,
112 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
117 slapd_args_file = ch_strdup( cargv[1] );
120 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
122 Debug( LDAP_DEBUG_ANY,
123 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
128 defsize = atoi( cargv[1] );
130 be->be_sizelimit = atoi( cargv[1] );
134 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
136 Debug( LDAP_DEBUG_ANY,
137 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
142 deftime = atoi( cargv[1] );
144 be->be_timelimit = atoi( cargv[1] );
147 /* set database suffix */
148 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
150 Debug( LDAP_DEBUG_ANY,
151 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
154 } else if ( cargc > 2 ) {
155 Debug( LDAP_DEBUG_ANY,
156 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
157 fname, lineno, cargv[1] );
160 Debug( LDAP_DEBUG_ANY,
161 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
164 char *dn = ch_strdup( cargv[1] );
165 (void) dn_normalize_case( dn );
166 charray_add( &be->be_suffix, dn );
169 /* set database suffixAlias */
170 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
172 Debug( LDAP_DEBUG_ANY,
173 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
176 } else if ( cargc < 3 ) {
177 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
181 } else if ( cargc > 3 ) {
182 Debug( LDAP_DEBUG_ANY,
183 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
187 Debug( LDAP_DEBUG_ANY,
188 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
191 char *alias, *aliased_dn;
193 alias = ch_strdup( cargv[1] );
194 (void) dn_normalize( alias );
196 aliased_dn = ch_strdup( cargv[2] );
197 (void) dn_normalize( aliased_dn );
200 if ( strcasecmp( alias, aliased_dn) == 0 ) {
201 Debug( LDAP_DEBUG_ANY,
202 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
203 fname, lineno, alias );
205 (void) dn_normalize_case( alias );
206 (void) dn_normalize_case( aliased_dn );
207 charray_add( &be->be_suffixAlias, alias );
208 charray_add( &be->be_suffixAlias, aliased_dn );
215 /* set max deref depth */
216 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
218 Debug( LDAP_DEBUG_ANY,
219 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
224 Debug( LDAP_DEBUG_ANY,
225 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
228 be->be_maxDerefDepth = atoi (cargv[1]);
232 /* set magic "root" dn for this database */
233 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
235 Debug( LDAP_DEBUG_ANY,
236 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
241 Debug( LDAP_DEBUG_ANY,
242 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
245 be->be_root_dn = ch_strdup( cargv[1] );
246 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
249 /* set super-secret magic database password */
250 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
252 Debug( LDAP_DEBUG_ANY,
253 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
258 Debug( LDAP_DEBUG_ANY,
259 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
262 be->be_root_pw = ch_strdup( cargv[1] );
265 /* make this database read-only */
266 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
268 Debug( LDAP_DEBUG_ANY,
269 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
274 Debug( LDAP_DEBUG_ANY,
275 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
278 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
285 /* where to send clients when we don't hold it */
286 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: missing URL in \"referral <URL>\" line\n",
293 default_referral = (char *) ch_malloc( strlen( cargv[1] )
294 + sizeof("Referral:\n") + 1 );
295 strcpy( default_referral, "Referral:\n" );
296 strcat( default_referral, cargv[1] );
298 /* specify an objectclass */
299 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
300 parse_oc( be, fname, lineno, cargc, cargv );
302 /* specify an attribute */
303 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
304 attr_syntax_config( fname, lineno, cargc - 1,
307 /* turn on/off schema checking */
308 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
310 Debug( LDAP_DEBUG_ANY,
311 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
315 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
316 global_schemacheck = 1;
318 global_schemacheck = 0;
321 /* specify access control info */
322 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
323 parse_acl( be, fname, lineno, cargc, cargv );
325 /* specify default access control info */
326 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
328 Debug( LDAP_DEBUG_ANY,
329 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
334 if ( (global_default_access =
335 str2access( cargv[1] )) == -1 ) {
336 Debug( LDAP_DEBUG_ANY,
337 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
338 fname, lineno, cargv[1] );
342 if ( (be->be_dfltaccess =
343 str2access( cargv[1] )) == -1 ) {
344 Debug( LDAP_DEBUG_ANY,
345 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
346 fname, lineno, cargv[1] );
351 /* debug level to log things to syslog */
352 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
354 Debug( LDAP_DEBUG_ANY,
355 "%s: line %d: missing level in \"loglevel <level>\" line\n",
359 ldap_syslog = atoi( cargv[1] );
361 /* list of replicas of the data in this backend (master only) */
362 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
364 Debug( LDAP_DEBUG_ANY,
365 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
370 Debug( LDAP_DEBUG_ANY,
371 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
374 for ( i = 1; i < cargc; i++ ) {
375 if ( strncasecmp( cargv[i], "host=", 5 )
377 charray_add( &be->be_replica,
378 ch_strdup( cargv[i] + 5 ) );
383 Debug( LDAP_DEBUG_ANY,
384 "%s: line %d: missing host in \"replica\" line (ignored)\n",
389 /* dn of master entity allowed to write to replica */
390 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
392 Debug( LDAP_DEBUG_ANY,
393 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
398 Debug( LDAP_DEBUG_ANY,
399 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
402 be->be_update_ndn = ch_strdup( cargv[1] );
403 (void) dn_normalize_case( be->be_update_ndn );
406 /* replication log file to which changes are appended */
407 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
409 Debug( LDAP_DEBUG_ANY,
410 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
415 be->be_replogfile = ch_strdup( cargv[1] );
417 replogfile = ch_strdup( cargv[1] );
420 /* maintain lastmodified{by,time} attributes */
421 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
423 Debug( LDAP_DEBUG_ANY,
424 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
428 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
435 be->be_lastmod = OFF;
437 global_lastmod = OFF;
440 /* include another config file */
441 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
443 Debug( LDAP_DEBUG_ANY,
444 "%s: line %d: missing filename in \"include <filename>\" line\n",
448 savefname = ch_strdup( cargv[1] );
450 read_config( savefname, bep, NULL );
453 lineno = savelineno - 1;
455 /* location of kerberos srvtab file */
456 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
458 Debug( LDAP_DEBUG_ANY,
459 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
463 ldap_srvtab = ch_strdup( cargv[1] );
465 /* pass anything else to the current backend config routine */
468 Debug( LDAP_DEBUG_ANY,
469 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
470 fname, lineno, cargv[0] );
471 } else if ( be->be_config == NULL ) {
472 Debug( LDAP_DEBUG_ANY,
473 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
474 fname, lineno, cargv[0] );
476 (*be->be_config)( be, fname, lineno, cargc,
483 if ( startup ) be_startup();
496 for ( token = strtok_quote( line, " \t" ); token != NULL;
497 token = strtok_quote( NULL, " \t" ) ) {
498 if ( *argcp == MAXARGS ) {
499 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
503 argv[(*argcp)++] = token;
509 strtok_quote( char *line, char *sep )
515 if ( line != NULL ) {
518 while ( *next && strchr( sep, *next ) ) {
522 if ( *next == '\0' ) {
528 for ( inquote = 0; *next; ) {
536 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
540 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
545 if ( strchr( sep, *next ) != NULL ) {
558 static char buf[BUFSIZ];
560 static int lmax, lcur;
562 #define CATLINE( buf ) { \
564 len = strlen( buf ); \
565 while ( lcur + len + 1 > lmax ) { \
567 line = (char *) ch_realloc( line, lmax ); \
569 strcpy( line + lcur, buf ); \
574 fp_getline( FILE *fp, int *lineno )
582 /* hack attack - keeps us from having to keep a stack of bufs... */
583 if ( strncasecmp( line, "include", 7 ) == 0 ) {
588 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
589 if ( (p = strchr( buf, '\n' )) != NULL ) {
592 if ( ! isspace( buf[0] ) ) {
601 return( line[0] ? line : NULL );
605 fp_getline_init( int *lineno )