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);
37 read_config( char *fname, Backend **bep, FILE *pfp )
40 char *line, *savefname;
41 int cargc, savelineno;
46 if ( (fp = pfp) == NULL && (fp = fopen( fname, "r" )) == NULL ) {
48 Debug( LDAP_DEBUG_ANY,
49 "could not open config file \"%s\" - absolute path?\n",
55 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
57 fp_getline_init( &lineno );
58 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
59 /* skip comments and blank lines */
60 if ( line[0] == '#' || line[0] == '\0' ) {
64 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
66 fp_parse_line( line, &cargc, cargv );
69 Debug( LDAP_DEBUG_ANY,
70 "%s: line %d: bad config line (ignored)\n",
75 /* start of a new database definition */
76 if ( strcasecmp( cargv[0], "database" ) == 0 ) {
78 Debug( LDAP_DEBUG_ANY,
79 "%s: line %d: missing type in \"database <type>\" line\n",
83 *bep = new_backend( cargv[1] );
86 /* assign a default depth limit for alias deref */
87 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
89 /* get pid file name */
90 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
92 Debug( LDAP_DEBUG_ANY,
93 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
98 slapd_pid_file = ch_strdup( cargv[1] );
100 /* get args file name */
101 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
103 Debug( LDAP_DEBUG_ANY,
104 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
109 slapd_args_file = ch_strdup( cargv[1] );
112 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
114 Debug( LDAP_DEBUG_ANY,
115 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
120 defsize = atoi( cargv[1] );
122 be->be_sizelimit = atoi( cargv[1] );
126 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
128 Debug( LDAP_DEBUG_ANY,
129 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
134 deftime = atoi( cargv[1] );
136 be->be_timelimit = atoi( cargv[1] );
139 /* set database suffix */
140 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
142 Debug( LDAP_DEBUG_ANY,
143 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
146 } else if ( cargc > 2 ) {
147 Debug( LDAP_DEBUG_ANY,
148 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
149 fname, lineno, cargv[1] );
152 Debug( LDAP_DEBUG_ANY,
153 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
156 char *dn = ch_strdup( cargv[1] );
157 (void) dn_normalize_case( dn );
158 charray_add( &be->be_suffix, dn );
161 /* set database suffixAlias */
162 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
164 Debug( LDAP_DEBUG_ANY,
165 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
168 } else if ( cargc < 3 ) {
169 Debug( LDAP_DEBUG_ANY,
170 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
173 } else if ( cargc > 3 ) {
174 Debug( LDAP_DEBUG_ANY,
175 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
179 Debug( LDAP_DEBUG_ANY,
180 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
183 char *alias, *aliased_dn;
185 alias = ch_strdup( cargv[1] );
186 (void) dn_normalize( alias );
188 aliased_dn = ch_strdup( cargv[2] );
189 (void) dn_normalize( aliased_dn );
192 if ( strcasecmp( alias, aliased_dn) ) {
193 Debug( LDAP_DEBUG_ANY,
194 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
195 fname, lineno, alias );
197 (void) dn_normalize_case( alias );
198 (void) dn_normalize_case( aliased_dn );
199 charray_add( &be->be_suffixAlias, alias );
200 charray_add( &be->be_suffixAlias, aliased_dn );
207 /* set max deref depth */
208 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
210 Debug( LDAP_DEBUG_ANY,
211 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
216 Debug( LDAP_DEBUG_ANY,
217 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
220 be->be_maxDerefDepth = atoi (cargv[1]);
224 /* set magic "root" dn for this database */
225 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
227 Debug( LDAP_DEBUG_ANY,
228 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
233 Debug( LDAP_DEBUG_ANY,
234 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
237 be->be_root_dn = ch_strdup( cargv[1] );
238 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
241 /* set super-secret magic database password */
242 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
244 Debug( LDAP_DEBUG_ANY,
245 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
250 Debug( LDAP_DEBUG_ANY,
251 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
254 be->be_root_pw = ch_strdup( cargv[1] );
257 /* make this database read-only */
258 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
260 Debug( LDAP_DEBUG_ANY,
261 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
266 Debug( LDAP_DEBUG_ANY,
267 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
270 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
277 /* where to send clients when we don't hold it */
278 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
280 Debug( LDAP_DEBUG_ANY,
281 "%s: line %d: missing URL in \"referral <URL>\" line\n",
285 default_referral = (char *) malloc( strlen( cargv[1] )
286 + sizeof("Referral:\n") + 1 );
287 strcpy( default_referral, "Referral:\n" );
288 strcat( default_referral, cargv[1] );
290 /* specify an objectclass */
291 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
292 parse_oc( be, fname, lineno, cargc, cargv );
294 /* specify an attribute */
295 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
296 attr_syntax_config( fname, lineno, cargc - 1,
299 /* turn on/off schema checking */
300 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
302 Debug( LDAP_DEBUG_ANY,
303 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
307 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
308 global_schemacheck = 1;
310 global_schemacheck = 0;
313 /* specify access control info */
314 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
315 parse_acl( be, fname, lineno, cargc, cargv );
317 /* specify default access control info */
318 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
320 Debug( LDAP_DEBUG_ANY,
321 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
326 if ( (global_default_access =
327 str2access( cargv[1] )) == -1 ) {
328 Debug( LDAP_DEBUG_ANY,
329 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
330 fname, lineno, cargv[1] );
334 if ( (be->be_dfltaccess =
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] );
343 /* debug level to log things to syslog */
344 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
346 Debug( LDAP_DEBUG_ANY,
347 "%s: line %d: missing level in \"loglevel <level>\" line\n",
351 ldap_syslog = atoi( cargv[1] );
353 /* list of replicas of the data in this backend (master only) */
354 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
356 Debug( LDAP_DEBUG_ANY,
357 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
362 Debug( LDAP_DEBUG_ANY,
363 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
366 for ( i = 1; i < cargc; i++ ) {
367 if ( strncasecmp( cargv[i], "host=", 5 )
369 charray_add( &be->be_replica,
370 ch_strdup( cargv[i] + 5 ) );
375 Debug( LDAP_DEBUG_ANY,
376 "%s: line %d: missing host in \"replica\" line (ignored)\n",
381 /* dn of master entity allowed to write to replica */
382 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
384 Debug( LDAP_DEBUG_ANY,
385 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
390 Debug( LDAP_DEBUG_ANY,
391 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
394 be->be_update_ndn = ch_strdup( cargv[1] );
395 (void) dn_normalize_case( be->be_update_ndn );
398 /* replication log file to which changes are appended */
399 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
401 Debug( LDAP_DEBUG_ANY,
402 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
407 be->be_replogfile = ch_strdup( cargv[1] );
409 replogfile = ch_strdup( cargv[1] );
412 /* maintain lastmodified{by,time} attributes */
413 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
415 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
420 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
427 be->be_lastmod = OFF;
429 global_lastmod = OFF;
432 /* include another config file */
433 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
435 Debug( LDAP_DEBUG_ANY,
436 "%s: line %d: missing filename in \"include <filename>\" line\n",
440 savefname = ch_strdup( cargv[1] );
442 read_config( savefname, bep, NULL );
445 lineno = savelineno - 1;
447 /* location of kerberos srvtab file */
448 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
450 Debug( LDAP_DEBUG_ANY,
451 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
455 ldap_srvtab = ch_strdup( cargv[1] );
457 /* pass anything else to the current backend config routine */
460 Debug( LDAP_DEBUG_ANY,
461 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
462 fname, lineno, cargv[0] );
463 } else if ( be->be_config == NULL ) {
464 Debug( LDAP_DEBUG_ANY,
465 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
466 fname, lineno, cargv[0] );
468 (*be->be_config)( be, fname, lineno, cargc,
486 for ( token = strtok_quote( line, " \t" ); token != NULL;
487 token = strtok_quote( NULL, " \t" ) ) {
488 if ( *argcp == MAXARGS ) {
489 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
493 argv[(*argcp)++] = token;
499 strtok_quote( char *line, char *sep )
505 if ( line != NULL ) {
508 while ( *next && strchr( sep, *next ) ) {
512 if ( *next == '\0' ) {
518 for ( inquote = 0; *next; ) {
526 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
530 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
535 if ( strchr( sep, *next ) != NULL ) {
548 static char buf[BUFSIZ];
550 static int lmax, lcur;
552 #define CATLINE( buf ) { \
554 len = strlen( buf ); \
555 while ( lcur + len + 1 > lmax ) { \
557 line = (char *) ch_realloc( line, lmax ); \
559 strcpy( line + lcur, buf ); \
564 fp_getline( FILE *fp, int *lineno )
572 /* hack attack - keeps us from having to keep a stack of bufs... */
573 if ( strncasecmp( line, "include", 7 ) == 0 ) {
578 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
579 if ( (p = strchr( buf, '\n' )) != NULL ) {
582 if ( ! isspace( buf[0] ) ) {
591 return( line[0] ? line : NULL );
595 fp_getline_init( int *lineno )