1 /* config.c - configuration file handling routines */
10 #include <ac/string.h>
12 #include <ac/socket.h>
14 #include "ldapconfig.h"
20 * defaults for various global variables
22 int defsize = SLAPD_DEFAULT_SIZELIMIT;
23 int deftime = SLAPD_DEFAULT_TIMELIMIT;
24 struct acl *global_acl = NULL;
25 int global_default_access = ACL_READ;
28 char *ldap_srvtab = "";
30 char *slapd_pid_file = NULL;
31 char *slapd_args_file = NULL;
33 static char *fp_getline(FILE *fp, int *lineno);
34 static void fp_getline_init(int *lineno);
35 static int fp_parse_line(char *line, int *argcp, char **argv);
37 static char *strtok_quote(char *line, char *sep);
40 read_config( char *fname )
43 char *line, *savefname;
44 int cargc, savelineno;
48 static BackendInfo *bi = NULL;
49 static BackendDB *be = NULL;
51 if ( (fp = fopen( fname, "r" )) == NULL ) {
53 Debug( LDAP_DEBUG_ANY,
54 "could not open config file \"%s\" - absolute path?\n",
60 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
62 fp_getline_init( &lineno );
64 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
65 /* skip comments and blank lines */
66 if ( line[0] == '#' || line[0] == '\0' ) {
70 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
72 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
77 Debug( LDAP_DEBUG_ANY,
78 "%s: line %d: bad config line (ignored)\n",
83 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
85 Debug( LDAP_DEBUG_ANY,
86 "%s: line %d: missing type in \"backend <type>\" line\n",
92 Debug( LDAP_DEBUG_ANY,
93 "%s: line %d: backend line must appear before any database definition\n",
98 bi = backend_info( cargv[1] );
100 /* start of a new database definition */
101 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
103 Debug( LDAP_DEBUG_ANY,
104 "%s: line %d: missing type in \"database <type>\" line\n",
109 be = backend_db_init( cargv[1] );
111 /* assign a default depth limit for alias deref */
112 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
114 /* get pid file name */
115 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
117 Debug( LDAP_DEBUG_ANY,
118 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
123 slapd_pid_file = ch_strdup( cargv[1] );
125 /* get args file name */
126 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
128 Debug( LDAP_DEBUG_ANY,
129 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
134 slapd_args_file = ch_strdup( cargv[1] );
137 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
139 Debug( LDAP_DEBUG_ANY,
140 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
145 defsize = atoi( cargv[1] );
147 be->be_sizelimit = atoi( cargv[1] );
151 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
153 Debug( LDAP_DEBUG_ANY,
154 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
159 deftime = atoi( cargv[1] );
161 be->be_timelimit = atoi( cargv[1] );
164 /* set database suffix */
165 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
167 Debug( LDAP_DEBUG_ANY,
168 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
171 } else if ( cargc > 2 ) {
172 Debug( LDAP_DEBUG_ANY,
173 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
174 fname, lineno, cargv[1] );
177 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
181 char *dn = ch_strdup( cargv[1] );
182 (void) dn_normalize( dn );
183 charray_add( &be->be_suffix, dn );
184 (void) dn_upcase( dn );
185 charray_add( &be->be_nsuffix, dn );
189 /* set database suffixAlias */
190 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
192 Debug( LDAP_DEBUG_ANY,
193 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
196 } else if ( cargc < 3 ) {
197 Debug( LDAP_DEBUG_ANY,
198 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
201 } else if ( cargc > 3 ) {
202 Debug( LDAP_DEBUG_ANY,
203 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
207 Debug( LDAP_DEBUG_ANY,
208 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
211 char *alias, *aliased_dn;
213 alias = ch_strdup( cargv[1] );
214 (void) dn_normalize( alias );
216 aliased_dn = ch_strdup( cargv[2] );
217 (void) dn_normalize( aliased_dn );
220 if ( strcasecmp( alias, aliased_dn) == 0 ) {
221 Debug( LDAP_DEBUG_ANY,
222 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
223 fname, lineno, alias );
225 (void) dn_normalize_case( alias );
226 (void) dn_normalize_case( aliased_dn );
227 charray_add( &be->be_suffixAlias, alias );
228 charray_add( &be->be_suffixAlias, aliased_dn );
235 /* set max deref depth */
236 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
238 Debug( LDAP_DEBUG_ANY,
239 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
244 Debug( LDAP_DEBUG_ANY,
245 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
248 be->be_maxDerefDepth = atoi (cargv[1]);
252 /* set magic "root" dn for this database */
253 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
255 Debug( LDAP_DEBUG_ANY,
256 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
261 Debug( LDAP_DEBUG_ANY,
262 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
265 be->be_root_dn = ch_strdup( cargv[1] );
266 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
269 /* set super-secret magic database password */
270 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
272 Debug( LDAP_DEBUG_ANY,
273 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
278 Debug( LDAP_DEBUG_ANY,
279 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
282 be->be_root_pw = ch_strdup( cargv[1] );
285 /* make this database read-only */
286 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
294 Debug( LDAP_DEBUG_ANY,
295 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
298 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
305 /* where to send clients when we don't hold it */
306 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
308 Debug( LDAP_DEBUG_ANY,
309 "%s: line %d: missing URL in \"referral <URL>\" line\n",
313 default_referral = (char *) ch_malloc( strlen( cargv[1] )
314 + sizeof("Referral:\n") + 1 );
315 strcpy( default_referral, "Referral:\n" );
316 strcat( default_referral, cargv[1] );
319 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
323 Debug( LDAP_DEBUG_ANY,
324 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
329 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
330 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
331 : ch_strdup( cargv[1] ) );
333 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
334 Debug( LDAP_DEBUG_ANY,
336 ? "%s: line %d: bad locale \"%s\"\n"
337 : "%s: line %d: bad locale\n"),
338 fname, lineno, locale );
342 Debug( LDAP_DEBUG_ANY,
343 "%s: line %d: \"locale\" unsupported\n",
347 /* specify an objectclass */
348 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
349 parse_oc( be, fname, lineno, cargc, cargv );
351 /* specify an attribute */
352 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
353 attr_syntax_config( fname, lineno, cargc - 1,
356 /* turn on/off schema checking */
357 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
359 Debug( LDAP_DEBUG_ANY,
360 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
364 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
365 global_schemacheck = 0;
367 global_schemacheck = 1;
370 /* specify access control info */
371 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
372 parse_acl( be, fname, lineno, cargc, cargv );
374 /* specify default access control info */
375 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
377 Debug( LDAP_DEBUG_ANY,
378 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
383 if ( (global_default_access =
384 str2access( cargv[1] )) == -1 ) {
385 Debug( LDAP_DEBUG_ANY,
386 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
387 fname, lineno, cargv[1] );
391 if ( (be->be_dfltaccess =
392 str2access( cargv[1] )) == -1 ) {
393 Debug( LDAP_DEBUG_ANY,
394 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
395 fname, lineno, cargv[1] );
400 /* debug level to log things to syslog */
401 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
403 Debug( LDAP_DEBUG_ANY,
404 "%s: line %d: missing level in \"loglevel <level>\" line\n",
408 ldap_syslog = atoi( cargv[1] );
410 /* list of replicas of the data in this backend (master only) */
411 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
413 Debug( LDAP_DEBUG_ANY,
414 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
419 Debug( LDAP_DEBUG_ANY,
420 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
423 for ( i = 1; i < cargc; i++ ) {
424 if ( strncasecmp( cargv[i], "host=", 5 )
426 charray_add( &be->be_replica,
432 Debug( LDAP_DEBUG_ANY,
433 "%s: line %d: missing host in \"replica\" line (ignored)\n",
438 /* dn of master entity allowed to write to replica */
439 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
441 Debug( LDAP_DEBUG_ANY,
442 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
447 Debug( LDAP_DEBUG_ANY,
448 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
451 be->be_update_ndn = ch_strdup( cargv[1] );
452 (void) dn_normalize_case( be->be_update_ndn );
455 /* replication log file to which changes are appended */
456 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
458 Debug( LDAP_DEBUG_ANY,
459 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
464 be->be_replogfile = ch_strdup( cargv[1] );
466 replogfile = ch_strdup( cargv[1] );
469 /* maintain lastmodified{by,time} attributes */
470 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
472 Debug( LDAP_DEBUG_ANY,
473 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
477 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
484 be->be_lastmod = OFF;
486 global_lastmod = OFF;
489 /* include another config file */
490 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
492 Debug( LDAP_DEBUG_ANY,
493 "%s: line %d: missing filename in \"include <filename>\" line\n",
497 savefname = ch_strdup( cargv[1] );
500 if ( read_config( savefname ) != 0 ) {
505 lineno = savelineno - 1;
507 /* location of kerberos srvtab file */
508 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
510 Debug( LDAP_DEBUG_ANY,
511 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
515 ldap_srvtab = ch_strdup( cargv[1] );
517 /* pass anything else to the current backend info/db config routine */
520 if ( bi->bi_config == 0 ) {
521 Debug( LDAP_DEBUG_ANY,
522 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
523 fname, lineno, cargv[0] );
525 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
531 } else if ( be != NULL ) {
532 if ( be->be_config == 0 ) {
533 Debug( LDAP_DEBUG_ANY,
534 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
535 fname, lineno, cargv[0] );
537 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
544 Debug( LDAP_DEBUG_ANY,
545 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
546 fname, lineno, cargv[0] );
564 for ( token = strtok_quote( line, " \t" ); token != NULL;
565 token = strtok_quote( NULL, " \t" ) ) {
566 if ( *argcp == MAXARGS ) {
567 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
571 argv[(*argcp)++] = token;
578 strtok_quote( char *line, char *sep )
584 if ( line != NULL ) {
587 while ( *next && strchr( sep, *next ) ) {
591 if ( *next == '\0' ) {
597 for ( inquote = 0; *next; ) {
605 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
611 next + 1, strlen( next + 1 ) + 1 );
612 next++; /* dont parse the escaped character */
617 if ( strchr( sep, *next ) != NULL ) {
630 static char buf[BUFSIZ];
632 static int lmax, lcur;
634 #define CATLINE( buf ) { \
636 len = strlen( buf ); \
637 while ( lcur + len + 1 > lmax ) { \
639 line = (char *) ch_realloc( line, lmax ); \
641 strcpy( line + lcur, buf ); \
646 fp_getline( FILE *fp, int *lineno )
654 /* hack attack - keeps us from having to keep a stack of bufs... */
655 if ( strncasecmp( line, "include", 7 ) == 0 ) {
660 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
661 if ( (p = strchr( buf, '\n' )) != NULL ) {
664 if ( ! isspace( (unsigned char) buf[0] ) ) {
673 return( line[0] ? line : NULL );
677 fp_getline_init( int *lineno )