1 /* config.c - configuration file handling routines */
11 #include "ldapconfig.h"
15 extern Backend *new_backend();
16 extern char *default_referral;
17 extern int ldap_syslog;
18 extern int global_schemacheck;
21 * defaults for various global variables
23 int defsize = SLAPD_DEFAULT_SIZELIMIT;
24 int deftime = SLAPD_DEFAULT_TIMELIMIT;
25 struct acl *global_acl = NULL;
26 int global_default_access = ACL_READ;
29 char *ldap_srvtab = "";
31 static char *fp_getline();
32 static void fp_getline_init();
33 static void fp_parse_line();
35 static char *strtok_quote();
38 read_config( char *fname, Backend **bep, FILE *pfp )
41 char *line, *savefname, *dn;
42 int cargc, savelineno;
47 if ( (fp = pfp) == NULL && (fp = fopen( fname, "r" )) == NULL ) {
49 Debug( LDAP_DEBUG_ANY,
50 "could not open config file \"%s\" - absolute path?\n",
56 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
58 fp_getline_init( &lineno );
59 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
60 /* skip comments and blank lines */
61 if ( line[0] == '#' || line[0] == '\0' ) {
65 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
67 fp_parse_line( line, &cargc, cargv );
70 Debug( LDAP_DEBUG_ANY,
71 "%s: line %d: bad config line (ignored)\n",
76 /* start of a new database definition */
77 if ( strcasecmp( cargv[0], "database" ) == 0 ) {
79 Debug( LDAP_DEBUG_ANY,
80 "%s: line %d: missing type in \"database <type>\" line\n",
84 *bep = new_backend( cargv[1] );
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 magic "root" dn for this database */
138 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
140 Debug( LDAP_DEBUG_ANY,
141 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
146 Debug( LDAP_DEBUG_ANY,
147 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
150 dn = strdup( cargv[1] );
151 (void) dn_normalize( dn );
155 /* set super-secret magic database password */
156 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
158 Debug( LDAP_DEBUG_ANY,
159 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
164 Debug( LDAP_DEBUG_ANY,
165 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
168 be->be_rootpw = strdup( cargv[1] );
171 /* make this database read-only */
172 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
174 Debug( LDAP_DEBUG_ANY,
175 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
180 Debug( LDAP_DEBUG_ANY,
181 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
184 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
191 /* where to send clients when we don't hold it */
192 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
194 Debug( LDAP_DEBUG_ANY,
195 "%s: line %d: missing URL in \"referral <URL>\" line\n",
199 default_referral = (char *) malloc( strlen( cargv[1] )
200 + sizeof("Referral:\n") + 1 );
201 strcpy( default_referral, "Referral:\n" );
202 strcat( default_referral, cargv[1] );
204 /* specify an objectclass */
205 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
206 parse_oc( be, fname, lineno, cargc, cargv );
208 /* specify an attribute */
209 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
210 attr_syntax_config( fname, lineno, cargc - 1,
213 /* turn on/off schema checking */
214 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
216 Debug( LDAP_DEBUG_ANY,
217 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
221 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
222 global_schemacheck = 1;
224 global_schemacheck = 0;
227 /* specify access control info */
228 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
229 parse_acl( be, fname, lineno, cargc, cargv );
231 /* specify default access control info */
232 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
234 Debug( LDAP_DEBUG_ANY,
235 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
240 if ( (global_default_access =
241 str2access( cargv[1] )) == -1 ) {
242 Debug( LDAP_DEBUG_ANY,
243 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
244 fname, lineno, cargv[1] );
248 if ( (be->be_dfltaccess =
249 str2access( cargv[1] )) == -1 ) {
250 Debug( LDAP_DEBUG_ANY,
251 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
252 fname, lineno, cargv[1] );
257 /* debug level to log things to syslog */
258 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
260 Debug( LDAP_DEBUG_ANY,
261 "%s: line %d: missing level in \"loglevel <level>\" line\n",
265 ldap_syslog = atoi( cargv[1] );
267 /* list of replicas of the data in this backend (master only) */
268 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
270 Debug( LDAP_DEBUG_ANY,
271 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
276 Debug( LDAP_DEBUG_ANY,
277 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
280 for ( i = 1; i < cargc; i++ ) {
281 if ( strncasecmp( cargv[i], "host=", 5 )
283 charray_add( &be->be_replica,
284 strdup( cargv[i] + 5 ) );
289 Debug( LDAP_DEBUG_ANY,
290 "%s: line %d: missing host in \"replica\" line (ignored)\n",
295 /* dn of master entity allowed to write to replica */
296 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
298 Debug( LDAP_DEBUG_ANY,
299 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
304 Debug( LDAP_DEBUG_ANY,
305 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
308 be->be_updatedn = strdup( cargv[1] );
309 (void) dn_normalize( be->be_updatedn );
312 /* replication log file to which changes are appended */
313 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
315 Debug( LDAP_DEBUG_ANY,
316 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
321 be->be_replogfile = strdup( cargv[1] );
323 replogfile = strdup( cargv[1] );
326 /* maintain lastmodified{by,time} attributes */
327 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
329 Debug( LDAP_DEBUG_ANY,
330 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
334 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
341 be->be_lastmod = OFF;
343 global_lastmod = OFF;
346 /* include another config file */
347 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
349 Debug( LDAP_DEBUG_ANY,
350 "%s: line %d: missing filename in \"include <filename>\" line\n",
354 savefname = strdup( cargv[1] );
356 read_config( savefname, bep, NULL );
359 lineno = savelineno - 1;
361 /* location of kerberos srvtab file */
362 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
364 Debug( LDAP_DEBUG_ANY,
365 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
369 ldap_srvtab = strdup( cargv[1] );
371 /* pass anything else to the current backend config routine */
374 Debug( LDAP_DEBUG_ANY,
375 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
376 fname, lineno, cargv[0] );
377 } else if ( be->be_config == NULL ) {
378 Debug( LDAP_DEBUG_ANY,
379 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
380 fname, lineno, cargv[0] );
382 (*be->be_config)( be, fname, lineno, cargc,
400 for ( token = strtok_quote( line, " \t" ); token != NULL;
401 token = strtok_quote( NULL, " \t" ) ) {
402 if ( *argcp == MAXARGS ) {
403 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
407 argv[(*argcp)++] = token;
413 strtok_quote( char *line, char *sep )
419 if ( line != NULL ) {
422 while ( *next && strchr( sep, *next ) ) {
426 if ( *next == '\0' ) {
432 for ( inquote = 0; *next; ) {
440 strcpy( next, next + 1 );
444 strcpy( next, next + 1 );
449 if ( strchr( sep, *next ) != NULL ) {
462 static char buf[BUFSIZ];
464 static int lmax, lcur;
466 #define CATLINE( buf ) { \
468 len = strlen( buf ); \
469 while ( lcur + len + 1 > lmax ) { \
471 line = (char *) ch_realloc( line, lmax ); \
473 strcpy( line + lcur, buf ); \
478 fp_getline( FILE *fp, int *lineno )
486 /* hack attack - keeps us from having to keep a stack of bufs... */
487 if ( strncasecmp( line, "include", 7 ) == 0 ) {
492 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
493 if ( (p = strchr( buf, '\n' )) != NULL ) {
496 if ( ! isspace( buf[0] ) ) {
505 return( line[0] ? line : NULL );
509 fp_getline_init( int *lineno )