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 static char *fp_getline(FILE *fp, int *lineno);
28 static void fp_getline_init(int *lineno);
29 static void fp_parse_line(char *line, int *argcp, char **argv);
31 static char *strtok_quote(char *line, char *sep);
34 read_config( char *fname, Backend **bep, FILE *pfp )
37 char *line, *savefname;
38 int cargc, savelineno;
43 if ( (fp = pfp) == NULL && (fp = fopen( fname, "r" )) == NULL ) {
45 Debug( LDAP_DEBUG_ANY,
46 "could not open config file \"%s\" - absolute path?\n",
52 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
54 fp_getline_init( &lineno );
55 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
56 /* skip comments and blank lines */
57 if ( line[0] == '#' || line[0] == '\0' ) {
61 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
63 fp_parse_line( line, &cargc, cargv );
66 Debug( LDAP_DEBUG_ANY,
67 "%s: line %d: bad config line (ignored)\n",
72 /* start of a new database definition */
73 if ( strcasecmp( cargv[0], "database" ) == 0 ) {
75 Debug( LDAP_DEBUG_ANY,
76 "%s: line %d: missing type in \"database <type>\" line\n",
80 *bep = new_backend( cargv[1] );
83 /* assign a default depth limit for alias deref */
84 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
87 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
89 Debug( LDAP_DEBUG_ANY,
90 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
95 defsize = atoi( cargv[1] );
97 be->be_sizelimit = atoi( cargv[1] );
101 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
103 Debug( LDAP_DEBUG_ANY,
104 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
109 deftime = atoi( cargv[1] );
111 be->be_timelimit = atoi( cargv[1] );
114 /* set database suffix */
115 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
117 Debug( LDAP_DEBUG_ANY,
118 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
121 } else if ( cargc > 2 ) {
122 Debug( LDAP_DEBUG_ANY,
123 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
124 fname, lineno, cargv[1] );
127 Debug( LDAP_DEBUG_ANY,
128 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
131 char *dn = ch_strdup( cargv[1] );
132 (void) dn_normalize_case( dn );
133 charray_add( &be->be_suffix, dn );
136 /* set database suffixAlias */
137 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
139 Debug( LDAP_DEBUG_ANY,
140 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
143 } else if ( cargc < 3 ) {
144 Debug( LDAP_DEBUG_ANY,
145 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
148 } else if ( cargc > 3 ) {
149 Debug( LDAP_DEBUG_ANY,
150 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
154 Debug( LDAP_DEBUG_ANY,
155 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
158 char *alias, *aliased_dn;
160 alias = ch_strdup( cargv[1] );
161 (void) dn_normalize( alias );
163 aliased_dn = ch_strdup( cargv[2] );
164 (void) dn_normalize( aliased_dn );
167 if ( strcasecmp( alias, aliased_dn) ) {
168 Debug( LDAP_DEBUG_ANY,
169 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
170 fname, lineno, alias );
172 (void) dn_normalize_case( alias );
173 (void) dn_normalize_case( aliased_dn );
174 charray_add( &be->be_suffixAlias, alias );
175 charray_add( &be->be_suffixAlias, aliased_dn );
182 /* set max deref depth */
183 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
185 Debug( LDAP_DEBUG_ANY,
186 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
191 Debug( LDAP_DEBUG_ANY,
192 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
195 be->be_maxDerefDepth = atoi (cargv[1]);
199 /* set magic "root" dn for this database */
200 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
202 Debug( LDAP_DEBUG_ANY,
203 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
208 Debug( LDAP_DEBUG_ANY,
209 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
212 be->be_root_dn = ch_strdup( cargv[1] );
213 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
216 /* set super-secret magic database password */
217 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
219 Debug( LDAP_DEBUG_ANY,
220 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
225 Debug( LDAP_DEBUG_ANY,
226 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
229 be->be_root_pw = ch_strdup( cargv[1] );
232 /* make this database read-only */
233 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
235 Debug( LDAP_DEBUG_ANY,
236 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
241 Debug( LDAP_DEBUG_ANY,
242 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
245 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
252 /* where to send clients when we don't hold it */
253 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
255 Debug( LDAP_DEBUG_ANY,
256 "%s: line %d: missing URL in \"referral <URL>\" line\n",
260 default_referral = (char *) malloc( strlen( cargv[1] )
261 + sizeof("Referral:\n") + 1 );
262 strcpy( default_referral, "Referral:\n" );
263 strcat( default_referral, cargv[1] );
265 /* specify an objectclass */
266 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
267 parse_oc( be, fname, lineno, cargc, cargv );
269 /* specify an attribute */
270 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
271 attr_syntax_config( fname, lineno, cargc - 1,
274 /* turn on/off schema checking */
275 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
277 Debug( LDAP_DEBUG_ANY,
278 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
282 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
283 global_schemacheck = 1;
285 global_schemacheck = 0;
288 /* specify access control info */
289 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
290 parse_acl( be, fname, lineno, cargc, cargv );
292 /* specify default access control info */
293 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
295 Debug( LDAP_DEBUG_ANY,
296 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
301 if ( (global_default_access =
302 str2access( cargv[1] )) == -1 ) {
303 Debug( LDAP_DEBUG_ANY,
304 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
305 fname, lineno, cargv[1] );
309 if ( (be->be_dfltaccess =
310 str2access( cargv[1] )) == -1 ) {
311 Debug( LDAP_DEBUG_ANY,
312 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
313 fname, lineno, cargv[1] );
318 /* debug level to log things to syslog */
319 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
321 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: missing level in \"loglevel <level>\" line\n",
326 ldap_syslog = atoi( cargv[1] );
328 /* list of replicas of the data in this backend (master only) */
329 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
331 Debug( LDAP_DEBUG_ANY,
332 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
337 Debug( LDAP_DEBUG_ANY,
338 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
341 for ( i = 1; i < cargc; i++ ) {
342 if ( strncasecmp( cargv[i], "host=", 5 )
344 charray_add( &be->be_replica,
345 ch_strdup( cargv[i] + 5 ) );
350 Debug( LDAP_DEBUG_ANY,
351 "%s: line %d: missing host in \"replica\" line (ignored)\n",
356 /* dn of master entity allowed to write to replica */
357 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
359 Debug( LDAP_DEBUG_ANY,
360 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
365 Debug( LDAP_DEBUG_ANY,
366 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
369 be->be_update_ndn = ch_strdup( cargv[1] );
370 (void) dn_normalize_case( be->be_update_ndn );
373 /* replication log file to which changes are appended */
374 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
376 Debug( LDAP_DEBUG_ANY,
377 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
382 be->be_replogfile = ch_strdup( cargv[1] );
384 replogfile = ch_strdup( cargv[1] );
387 /* maintain lastmodified{by,time} attributes */
388 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
390 Debug( LDAP_DEBUG_ANY,
391 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
395 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
402 be->be_lastmod = OFF;
404 global_lastmod = OFF;
407 /* include another config file */
408 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
410 Debug( LDAP_DEBUG_ANY,
411 "%s: line %d: missing filename in \"include <filename>\" line\n",
415 savefname = ch_strdup( cargv[1] );
417 read_config( savefname, bep, NULL );
420 lineno = savelineno - 1;
422 /* location of kerberos srvtab file */
423 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
425 Debug( LDAP_DEBUG_ANY,
426 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
430 ldap_srvtab = ch_strdup( cargv[1] );
432 /* pass anything else to the current backend config routine */
435 Debug( LDAP_DEBUG_ANY,
436 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
437 fname, lineno, cargv[0] );
438 } else if ( be->be_config == NULL ) {
439 Debug( LDAP_DEBUG_ANY,
440 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
441 fname, lineno, cargv[0] );
443 (*be->be_config)( be, fname, lineno, cargc,
461 for ( token = strtok_quote( line, " \t" ); token != NULL;
462 token = strtok_quote( NULL, " \t" ) ) {
463 if ( *argcp == MAXARGS ) {
464 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
468 argv[(*argcp)++] = token;
474 strtok_quote( char *line, char *sep )
480 if ( line != NULL ) {
483 while ( *next && strchr( sep, *next ) ) {
487 if ( *next == '\0' ) {
493 for ( inquote = 0; *next; ) {
501 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
505 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
510 if ( strchr( sep, *next ) != NULL ) {
523 static char buf[BUFSIZ];
525 static int lmax, lcur;
527 #define CATLINE( buf ) { \
529 len = strlen( buf ); \
530 while ( lcur + len + 1 > lmax ) { \
532 line = (char *) ch_realloc( line, lmax ); \
534 strcpy( line + lcur, buf ); \
539 fp_getline( FILE *fp, int *lineno )
547 /* hack attack - keeps us from having to keep a stack of bufs... */
548 if ( strncasecmp( line, "include", 7 ) == 0 ) {
553 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
554 if ( (p = strchr( buf, '\n' )) != NULL ) {
557 if ( ! isspace( buf[0] ) ) {
566 return( line[0] ? line : NULL );
570 fp_getline_init( int *lineno )