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( 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 *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 be->be_rootdn = dn_normalize_case( ch_strdup( cargv[1] ) );
200 /* set super-secret magic database password */
201 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
203 Debug( LDAP_DEBUG_ANY,
204 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
209 Debug( LDAP_DEBUG_ANY,
210 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
213 be->be_rootpw = ch_strdup( cargv[1] );
216 /* make this database read-only */
217 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
219 Debug( LDAP_DEBUG_ANY,
220 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
225 Debug( LDAP_DEBUG_ANY,
226 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
229 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
236 /* where to send clients when we don't hold it */
237 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
239 Debug( LDAP_DEBUG_ANY,
240 "%s: line %d: missing URL in \"referral <URL>\" line\n",
244 default_referral = (char *) malloc( strlen( cargv[1] )
245 + sizeof("Referral:\n") + 1 );
246 strcpy( default_referral, "Referral:\n" );
247 strcat( default_referral, cargv[1] );
249 /* specify an objectclass */
250 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
251 parse_oc( be, fname, lineno, cargc, cargv );
253 /* specify an attribute */
254 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
255 attr_syntax_config( fname, lineno, cargc - 1,
258 /* turn on/off schema checking */
259 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
261 Debug( LDAP_DEBUG_ANY,
262 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
266 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
267 global_schemacheck = 1;
269 global_schemacheck = 0;
272 /* specify access control info */
273 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
274 parse_acl( be, fname, lineno, cargc, cargv );
276 /* specify default access control info */
277 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
279 Debug( LDAP_DEBUG_ANY,
280 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
285 if ( (global_default_access =
286 str2access( cargv[1] )) == -1 ) {
287 Debug( LDAP_DEBUG_ANY,
288 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
289 fname, lineno, cargv[1] );
293 if ( (be->be_dfltaccess =
294 str2access( cargv[1] )) == -1 ) {
295 Debug( LDAP_DEBUG_ANY,
296 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
297 fname, lineno, cargv[1] );
302 /* debug level to log things to syslog */
303 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
305 Debug( LDAP_DEBUG_ANY,
306 "%s: line %d: missing level in \"loglevel <level>\" line\n",
310 ldap_syslog = atoi( cargv[1] );
312 /* list of replicas of the data in this backend (master only) */
313 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
315 Debug( LDAP_DEBUG_ANY,
316 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
321 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
325 for ( i = 1; i < cargc; i++ ) {
326 if ( strncasecmp( cargv[i], "host=", 5 )
328 charray_add( &be->be_replica,
329 ch_strdup( cargv[i] + 5 ) );
334 Debug( LDAP_DEBUG_ANY,
335 "%s: line %d: missing host in \"replica\" line (ignored)\n",
340 /* dn of master entity allowed to write to replica */
341 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
343 Debug( LDAP_DEBUG_ANY,
344 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
349 Debug( LDAP_DEBUG_ANY,
350 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
353 be->be_updatedn = ch_strdup( cargv[1] );
354 (void) dn_normalize( be->be_updatedn );
357 /* replication log file to which changes are appended */
358 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
360 Debug( LDAP_DEBUG_ANY,
361 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
366 be->be_replogfile = ch_strdup( cargv[1] );
368 replogfile = ch_strdup( cargv[1] );
371 /* maintain lastmodified{by,time} attributes */
372 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
374 Debug( LDAP_DEBUG_ANY,
375 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
379 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
386 be->be_lastmod = OFF;
388 global_lastmod = OFF;
391 /* include another config file */
392 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
394 Debug( LDAP_DEBUG_ANY,
395 "%s: line %d: missing filename in \"include <filename>\" line\n",
399 savefname = ch_strdup( cargv[1] );
401 read_config( savefname, bep, NULL );
404 lineno = savelineno - 1;
406 /* location of kerberos srvtab file */
407 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
409 Debug( LDAP_DEBUG_ANY,
410 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
414 ldap_srvtab = ch_strdup( cargv[1] );
416 /* pass anything else to the current backend config routine */
419 Debug( LDAP_DEBUG_ANY,
420 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
421 fname, lineno, cargv[0] );
422 } else if ( be->be_config == NULL ) {
423 Debug( LDAP_DEBUG_ANY,
424 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
425 fname, lineno, cargv[0] );
427 (*be->be_config)( be, fname, lineno, cargc,
445 for ( token = strtok_quote( line, " \t" ); token != NULL;
446 token = strtok_quote( NULL, " \t" ) ) {
447 if ( *argcp == MAXARGS ) {
448 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
452 argv[(*argcp)++] = token;
458 strtok_quote( char *line, char *sep )
464 if ( line != NULL ) {
467 while ( *next && strchr( sep, *next ) ) {
471 if ( *next == '\0' ) {
477 for ( inquote = 0; *next; ) {
485 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
489 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
494 if ( strchr( sep, *next ) != NULL ) {
507 static char buf[BUFSIZ];
509 static int lmax, lcur;
511 #define CATLINE( buf ) { \
513 len = strlen( buf ); \
514 while ( lcur + len + 1 > lmax ) { \
516 line = (char *) ch_realloc( line, lmax ); \
518 strcpy( line + lcur, buf ); \
523 fp_getline( FILE *fp, int *lineno )
531 /* hack attack - keeps us from having to keep a stack of bufs... */
532 if ( strncasecmp( line, "include", 7 ) == 0 ) {
537 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
538 if ( (p = strchr( buf, '\n' )) != NULL ) {
541 if ( ! isspace( buf[0] ) ) {
550 return( line[0] ? line : NULL );
554 fp_getline_init( int *lineno )