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, *dn;
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 dn = ch_strdup( cargv[1] );
132 (void) dn_normalize( 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 dn = ch_strdup( cargv[1] );
159 (void) dn_normalize( dn );
160 charray_add( &be->be_suffixAlias, dn );
162 dn = ch_strdup( cargv[2] );
163 (void) dn_normalize( dn );
164 charray_add( &be->be_suffixAlias, dn );
167 /* set max deref depth */
168 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
170 Debug( LDAP_DEBUG_ANY,
171 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
176 Debug( LDAP_DEBUG_ANY,
177 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
180 be->be_maxDerefDepth = atoi (cargv[1]);
184 /* set magic "root" dn for this database */
185 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
187 Debug( LDAP_DEBUG_ANY,
188 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
193 Debug( LDAP_DEBUG_ANY,
194 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
197 dn = ch_strdup( cargv[1] );
198 (void) dn_normalize( dn );
202 /* set super-secret magic database password */
203 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
205 Debug( LDAP_DEBUG_ANY,
206 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
211 Debug( LDAP_DEBUG_ANY,
212 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
215 be->be_rootpw = ch_strdup( cargv[1] );
218 /* make this database read-only */
219 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
221 Debug( LDAP_DEBUG_ANY,
222 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
227 Debug( LDAP_DEBUG_ANY,
228 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
231 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
238 /* where to send clients when we don't hold it */
239 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
241 Debug( LDAP_DEBUG_ANY,
242 "%s: line %d: missing URL in \"referral <URL>\" line\n",
246 default_referral = (char *) malloc( strlen( cargv[1] )
247 + sizeof("Referral:\n") + 1 );
248 strcpy( default_referral, "Referral:\n" );
249 strcat( default_referral, cargv[1] );
251 /* specify an objectclass */
252 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
253 parse_oc( be, fname, lineno, cargc, cargv );
255 /* specify an attribute */
256 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
257 attr_syntax_config( fname, lineno, cargc - 1,
260 /* turn on/off schema checking */
261 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
263 Debug( LDAP_DEBUG_ANY,
264 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
268 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
269 global_schemacheck = 1;
271 global_schemacheck = 0;
274 /* specify access control info */
275 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
276 parse_acl( be, fname, lineno, cargc, cargv );
278 /* specify default access control info */
279 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
281 Debug( LDAP_DEBUG_ANY,
282 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
287 if ( (global_default_access =
288 str2access( cargv[1] )) == -1 ) {
289 Debug( LDAP_DEBUG_ANY,
290 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
291 fname, lineno, cargv[1] );
295 if ( (be->be_dfltaccess =
296 str2access( cargv[1] )) == -1 ) {
297 Debug( LDAP_DEBUG_ANY,
298 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
299 fname, lineno, cargv[1] );
304 /* debug level to log things to syslog */
305 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
307 Debug( LDAP_DEBUG_ANY,
308 "%s: line %d: missing level in \"loglevel <level>\" line\n",
312 ldap_syslog = atoi( cargv[1] );
314 /* list of replicas of the data in this backend (master only) */
315 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
317 Debug( LDAP_DEBUG_ANY,
318 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
323 Debug( LDAP_DEBUG_ANY,
324 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
327 for ( i = 1; i < cargc; i++ ) {
328 if ( strncasecmp( cargv[i], "host=", 5 )
330 charray_add( &be->be_replica,
331 ch_strdup( cargv[i] + 5 ) );
336 Debug( LDAP_DEBUG_ANY,
337 "%s: line %d: missing host in \"replica\" line (ignored)\n",
342 /* dn of master entity allowed to write to replica */
343 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
345 Debug( LDAP_DEBUG_ANY,
346 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
351 Debug( LDAP_DEBUG_ANY,
352 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
355 be->be_updatedn = ch_strdup( cargv[1] );
356 (void) dn_normalize( be->be_updatedn );
359 /* replication log file to which changes are appended */
360 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
362 Debug( LDAP_DEBUG_ANY,
363 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
368 be->be_replogfile = ch_strdup( cargv[1] );
370 replogfile = ch_strdup( cargv[1] );
373 /* maintain lastmodified{by,time} attributes */
374 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
376 Debug( LDAP_DEBUG_ANY,
377 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
381 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
388 be->be_lastmod = OFF;
390 global_lastmod = OFF;
393 /* include another config file */
394 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
396 Debug( LDAP_DEBUG_ANY,
397 "%s: line %d: missing filename in \"include <filename>\" line\n",
401 savefname = ch_strdup( cargv[1] );
403 read_config( savefname, bep, NULL );
406 lineno = savelineno - 1;
408 /* location of kerberos srvtab file */
409 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
411 Debug( LDAP_DEBUG_ANY,
412 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
416 ldap_srvtab = ch_strdup( cargv[1] );
418 /* pass anything else to the current backend config routine */
421 Debug( LDAP_DEBUG_ANY,
422 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
423 fname, lineno, cargv[0] );
424 } else if ( be->be_config == NULL ) {
425 Debug( LDAP_DEBUG_ANY,
426 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
427 fname, lineno, cargv[0] );
429 (*be->be_config)( be, fname, lineno, cargc,
447 for ( token = strtok_quote( line, " \t" ); token != NULL;
448 token = strtok_quote( NULL, " \t" ) ) {
449 if ( *argcp == MAXARGS ) {
450 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
454 argv[(*argcp)++] = token;
460 strtok_quote( char *line, char *sep )
466 if ( line != NULL ) {
469 while ( *next && strchr( sep, *next ) ) {
473 if ( *next == '\0' ) {
479 for ( inquote = 0; *next; ) {
487 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
491 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
496 if ( strchr( sep, *next ) != NULL ) {
509 static char buf[BUFSIZ];
511 static int lmax, lcur;
513 #define CATLINE( buf ) { \
515 len = strlen( buf ); \
516 while ( lcur + len + 1 > lmax ) { \
518 line = (char *) ch_realloc( line, lmax ); \
520 strcpy( line + lcur, buf ); \
525 fp_getline( FILE *fp, int *lineno )
533 /* hack attack - keeps us from having to keep a stack of bufs... */
534 if ( strncasecmp( line, "include", 7 ) == 0 ) {
539 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
540 if ( (p = strchr( buf, '\n' )) != NULL ) {
543 if ( ! isspace( buf[0] ) ) {
552 return( line[0] ? line : NULL );
556 fp_getline_init( int *lineno )