1 /* config.c - configuration file handling routines */
6 #include <sys/socket.h>
8 #include "ldapconfig.h"
12 extern Backend *new_backend();
13 extern char *default_referral;
14 extern int ldap_syslog;
15 extern int global_schemacheck;
18 * defaults for various global variables
20 int defsize = SLAPD_DEFAULT_SIZELIMIT;
21 int deftime = SLAPD_DEFAULT_TIMELIMIT;
22 struct acl *global_acl = NULL;
23 int global_default_access = ACL_READ;
26 char *ldap_srvtab = "";
28 static char *fp_getline();
29 static void fp_getline_init();
30 static void fp_parse_line();
32 static char *strtok_quote();
35 read_config( char *fname, Backend **bep, FILE *pfp )
38 char *line, *savefname, *dn;
39 int cargc, savelineno;
44 if ( (fp = pfp) == NULL && (fp = fopen( fname, "r" )) == NULL ) {
46 Debug( LDAP_DEBUG_ANY,
47 "could not open config file \"%s\" - absolute path?\n",
53 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
55 fp_getline_init( &lineno );
56 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
57 /* skip comments and blank lines */
58 if ( line[0] == '#' || line[0] == '\0' ) {
62 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
64 fp_parse_line( line, &cargc, cargv );
67 Debug( LDAP_DEBUG_ANY,
68 "%s: line %d: bad config line (ignored)\n",
73 /* start of a new database definition */
74 if ( strcasecmp( cargv[0], "database" ) == 0 ) {
76 Debug( LDAP_DEBUG_ANY,
77 "%s: line %d: missing type in \"database <type>\" line\n",
81 *bep = new_backend( cargv[1] );
84 /* assign a default depth limit for alias deref */
85 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
88 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
90 Debug( LDAP_DEBUG_ANY,
91 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
96 defsize = atoi( cargv[1] );
98 be->be_sizelimit = atoi( cargv[1] );
102 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
104 Debug( LDAP_DEBUG_ANY,
105 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
110 deftime = atoi( cargv[1] );
112 be->be_timelimit = atoi( cargv[1] );
115 /* set database suffix */
116 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
118 Debug( LDAP_DEBUG_ANY,
119 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
122 } else if ( cargc > 2 ) {
123 Debug( LDAP_DEBUG_ANY,
124 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
125 fname, lineno, cargv[1] );
128 Debug( LDAP_DEBUG_ANY,
129 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
132 dn = strdup( cargv[1] );
133 (void) dn_normalize( dn );
134 charray_add( &be->be_suffix, dn );
137 /* set database suffixAlias */
138 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
140 Debug( LDAP_DEBUG_ANY,
141 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
144 } else if ( cargc < 3 ) {
145 Debug( LDAP_DEBUG_ANY,
146 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
149 } else if ( cargc > 3 ) {
150 Debug( LDAP_DEBUG_ANY,
151 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
155 Debug( LDAP_DEBUG_ANY,
156 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
159 dn = strdup( cargv[1] );
160 (void) dn_normalize( dn );
161 charray_add( &be->be_suffixAlias, dn );
163 dn = strdup( cargv[2] );
164 (void) dn_normalize( dn );
165 charray_add( &be->be_suffixAlias, dn );
168 /* set max deref depth */
169 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
171 Debug( LDAP_DEBUG_ANY,
172 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
177 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
181 be->be_maxDerefDepth = atoi (cargv[1]);
185 /* set magic "root" dn for this database */
186 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
188 Debug( LDAP_DEBUG_ANY,
189 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
194 Debug( LDAP_DEBUG_ANY,
195 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
198 dn = strdup( cargv[1] );
199 (void) dn_normalize( dn );
203 /* set super-secret magic database password */
204 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
206 Debug( LDAP_DEBUG_ANY,
207 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
212 Debug( LDAP_DEBUG_ANY,
213 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
216 be->be_rootpw = strdup( cargv[1] );
219 /* make this database read-only */
220 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
222 Debug( LDAP_DEBUG_ANY,
223 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
228 Debug( LDAP_DEBUG_ANY,
229 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
232 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
239 /* where to send clients when we don't hold it */
240 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
242 Debug( LDAP_DEBUG_ANY,
243 "%s: line %d: missing URL in \"referral <URL>\" line\n",
247 default_referral = (char *) malloc( strlen( cargv[1] )
248 + sizeof("Referral:\n") + 1 );
249 strcpy( default_referral, "Referral:\n" );
250 strcat( default_referral, cargv[1] );
252 /* specify an objectclass */
253 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
254 parse_oc( be, fname, lineno, cargc, cargv );
256 /* specify an attribute */
257 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
258 attr_syntax_config( fname, lineno, cargc - 1,
261 /* turn on/off schema checking */
262 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
264 Debug( LDAP_DEBUG_ANY,
265 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
269 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
270 global_schemacheck = 1;
272 global_schemacheck = 0;
275 /* specify access control info */
276 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
277 parse_acl( be, fname, lineno, cargc, cargv );
279 /* specify default access control info */
280 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
282 Debug( LDAP_DEBUG_ANY,
283 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
288 if ( (global_default_access =
289 str2access( cargv[1] )) == -1 ) {
290 Debug( LDAP_DEBUG_ANY,
291 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
292 fname, lineno, cargv[1] );
296 if ( (be->be_dfltaccess =
297 str2access( cargv[1] )) == -1 ) {
298 Debug( LDAP_DEBUG_ANY,
299 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
300 fname, lineno, cargv[1] );
305 /* debug level to log things to syslog */
306 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
308 Debug( LDAP_DEBUG_ANY,
309 "%s: line %d: missing level in \"loglevel <level>\" line\n",
313 ldap_syslog = atoi( cargv[1] );
315 /* list of replicas of the data in this backend (master only) */
316 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
318 Debug( LDAP_DEBUG_ANY,
319 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
324 Debug( LDAP_DEBUG_ANY,
325 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
328 for ( i = 1; i < cargc; i++ ) {
329 if ( strncasecmp( cargv[i], "host=", 5 )
331 charray_add( &be->be_replica,
332 strdup( cargv[i] + 5 ) );
337 Debug( LDAP_DEBUG_ANY,
338 "%s: line %d: missing host in \"replica\" line (ignored)\n",
343 /* dn of master entity allowed to write to replica */
344 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
346 Debug( LDAP_DEBUG_ANY,
347 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
352 Debug( LDAP_DEBUG_ANY,
353 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
356 be->be_updatedn = strdup( cargv[1] );
357 (void) dn_normalize( be->be_updatedn );
360 /* replication log file to which changes are appended */
361 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
363 Debug( LDAP_DEBUG_ANY,
364 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
369 be->be_replogfile = strdup( cargv[1] );
371 replogfile = strdup( cargv[1] );
374 /* maintain lastmodified{by,time} attributes */
375 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
377 Debug( LDAP_DEBUG_ANY,
378 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
382 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
389 be->be_lastmod = OFF;
391 global_lastmod = OFF;
394 /* include another config file */
395 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
397 Debug( LDAP_DEBUG_ANY,
398 "%s: line %d: missing filename in \"include <filename>\" line\n",
402 savefname = strdup( cargv[1] );
404 read_config( savefname, bep, NULL );
407 lineno = savelineno - 1;
409 /* location of kerberos srvtab file */
410 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
412 Debug( LDAP_DEBUG_ANY,
413 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
417 ldap_srvtab = strdup( cargv[1] );
419 /* pass anything else to the current backend config routine */
422 Debug( LDAP_DEBUG_ANY,
423 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
424 fname, lineno, cargv[0] );
425 } else if ( be->be_config == NULL ) {
426 Debug( LDAP_DEBUG_ANY,
427 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
428 fname, lineno, cargv[0] );
430 (*be->be_config)( be, fname, lineno, cargc,
448 for ( token = strtok_quote( line, " \t" ); token != NULL;
449 token = strtok_quote( NULL, " \t" ) ) {
450 if ( *argcp == MAXARGS ) {
451 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
455 argv[(*argcp)++] = token;
461 strtok_quote( char *line, char *sep )
467 if ( line != NULL ) {
470 while ( *next && strchr( sep, *next ) ) {
474 if ( *next == '\0' ) {
480 for ( inquote = 0; *next; ) {
488 strcpy( next, next + 1 );
492 strcpy( next, next + 1 );
497 if ( strchr( sep, *next ) != NULL ) {
510 static char buf[BUFSIZ];
512 static int lmax, lcur;
514 #define CATLINE( buf ) { \
516 len = strlen( buf ); \
517 while ( lcur + len + 1 > lmax ) { \
519 line = (char *) ch_realloc( line, lmax ); \
521 strcpy( line + lcur, buf ); \
526 fp_getline( FILE *fp, int *lineno )
534 /* hack attack - keeps us from having to keep a stack of bufs... */
535 if ( strncasecmp( line, "include", 7 ) == 0 ) {
540 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
541 if ( (p = strchr( buf, '\n' )) != NULL ) {
544 if ( ! isspace( buf[0] ) ) {
553 return( line[0] ? line : NULL );
557 fp_getline_init( int *lineno )