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, *saveline;
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 /* fp_parse_line is destructive, we save a copy */
73 saveline = ch_strdup( line );
75 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
80 Debug( LDAP_DEBUG_ANY,
81 "%s: line %d: bad config line (ignored)\n",
86 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
88 Debug( LDAP_DEBUG_ANY,
89 "%s: line %d: missing type in \"backend <type>\" line\n",
95 Debug( LDAP_DEBUG_ANY,
96 "%s: line %d: backend line must appear before any database definition\n",
101 bi = backend_info( cargv[1] );
103 /* start of a new database definition */
104 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
106 Debug( LDAP_DEBUG_ANY,
107 "%s: line %d: missing type in \"database <type>\" line\n",
112 be = backend_db_init( cargv[1] );
114 /* assign a default depth limit for alias deref */
115 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
117 /* get pid file name */
118 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
120 Debug( LDAP_DEBUG_ANY,
121 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
126 slapd_pid_file = ch_strdup( cargv[1] );
128 /* get args file name */
129 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
131 Debug( LDAP_DEBUG_ANY,
132 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
137 slapd_args_file = ch_strdup( cargv[1] );
140 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
142 Debug( LDAP_DEBUG_ANY,
143 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
148 defsize = atoi( cargv[1] );
150 be->be_sizelimit = atoi( cargv[1] );
154 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
156 Debug( LDAP_DEBUG_ANY,
157 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
162 deftime = atoi( cargv[1] );
164 be->be_timelimit = atoi( cargv[1] );
167 /* set database suffix */
168 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
170 Debug( LDAP_DEBUG_ANY,
171 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
174 } else if ( cargc > 2 ) {
175 Debug( LDAP_DEBUG_ANY,
176 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
177 fname, lineno, cargv[1] );
180 Debug( LDAP_DEBUG_ANY,
181 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
184 char *dn = ch_strdup( cargv[1] );
185 (void) dn_normalize( dn );
186 charray_add( &be->be_suffix, dn );
187 (void) dn_upcase( dn );
188 charray_add( &be->be_nsuffix, dn );
192 /* set database suffixAlias */
193 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
195 Debug( LDAP_DEBUG_ANY,
196 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
199 } else if ( cargc < 3 ) {
200 Debug( LDAP_DEBUG_ANY,
201 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
204 } else if ( cargc > 3 ) {
205 Debug( LDAP_DEBUG_ANY,
206 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
210 Debug( LDAP_DEBUG_ANY,
211 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
214 char *alias, *aliased_dn;
216 alias = ch_strdup( cargv[1] );
217 (void) dn_normalize( alias );
219 aliased_dn = ch_strdup( cargv[2] );
220 (void) dn_normalize( aliased_dn );
223 if ( strcasecmp( alias, aliased_dn) == 0 ) {
224 Debug( LDAP_DEBUG_ANY,
225 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
226 fname, lineno, alias );
228 (void) dn_normalize_case( alias );
229 (void) dn_normalize_case( aliased_dn );
230 charray_add( &be->be_suffixAlias, alias );
231 charray_add( &be->be_suffixAlias, aliased_dn );
238 /* set max deref depth */
239 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
241 Debug( LDAP_DEBUG_ANY,
242 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
247 Debug( LDAP_DEBUG_ANY,
248 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
251 be->be_maxDerefDepth = atoi (cargv[1]);
255 /* set magic "root" dn for this database */
256 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
258 Debug( LDAP_DEBUG_ANY,
259 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
264 Debug( LDAP_DEBUG_ANY,
265 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
268 be->be_root_dn = ch_strdup( cargv[1] );
269 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
272 /* set super-secret magic database password */
273 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
275 Debug( LDAP_DEBUG_ANY,
276 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
281 Debug( LDAP_DEBUG_ANY,
282 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
285 be->be_root_pw = ch_strdup( cargv[1] );
288 /* make this database read-only */
289 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
291 Debug( LDAP_DEBUG_ANY,
292 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
297 Debug( LDAP_DEBUG_ANY,
298 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
301 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
308 /* where to send clients when we don't hold it */
309 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
311 Debug( LDAP_DEBUG_ANY,
312 "%s: line %d: missing URL in \"referral <URL>\" line\n",
316 default_referral = (char *) ch_malloc( strlen( cargv[1] )
317 + sizeof("Referral:\n") + 1 );
318 strcpy( default_referral, "Referral:\n" );
319 strcat( default_referral, cargv[1] );
322 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
326 Debug( LDAP_DEBUG_ANY,
327 "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
332 locale = (strcasecmp( cargv[1], "on" ) == 0 ? ""
333 : strcasecmp( cargv[1], "off" ) == 0 ? "C"
334 : ch_strdup( cargv[1] ) );
336 if ( setlocale( LC_CTYPE, locale ) == 0 ) {
337 Debug( LDAP_DEBUG_ANY,
339 ? "%s: line %d: bad locale \"%s\"\n"
340 : "%s: line %d: bad locale\n"),
341 fname, lineno, locale );
345 Debug( LDAP_DEBUG_ANY,
346 "%s: line %d: \"locale\" unsupported\n",
350 /* specify an objectclass */
351 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
352 if ( *cargv[1] == '(' ) {
354 p = strchr(saveline,'(');
355 parse_oc( fname, lineno, p );
357 parse_oc_old( be, fname, lineno, cargc, cargv );
360 /* specify an attribute */
361 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
362 if ( *cargv[1] == '(' ) {
364 p = strchr(saveline,'(');
365 parse_at( fname, lineno, p );
367 attr_syntax_config( fname, lineno, cargc - 1,
371 /* turn on/off schema checking */
372 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
374 Debug( LDAP_DEBUG_ANY,
375 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
379 if ( strcasecmp( cargv[1], "off" ) == 0 ) {
380 global_schemacheck = 0;
382 global_schemacheck = 1;
385 /* specify access control info */
386 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
387 parse_acl( be, fname, lineno, cargc, cargv );
389 /* specify default access control info */
390 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
392 Debug( LDAP_DEBUG_ANY,
393 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
398 if ( (global_default_access =
399 str2access( cargv[1] )) == -1 ) {
400 Debug( LDAP_DEBUG_ANY,
401 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
402 fname, lineno, cargv[1] );
406 if ( (be->be_dfltaccess =
407 str2access( cargv[1] )) == -1 ) {
408 Debug( LDAP_DEBUG_ANY,
409 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
410 fname, lineno, cargv[1] );
415 /* debug level to log things to syslog */
416 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
418 Debug( LDAP_DEBUG_ANY,
419 "%s: line %d: missing level in \"loglevel <level>\" line\n",
423 ldap_syslog = atoi( cargv[1] );
425 /* list of replicas of the data in this backend (master only) */
426 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
428 Debug( LDAP_DEBUG_ANY,
429 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
434 Debug( LDAP_DEBUG_ANY,
435 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
438 for ( i = 1; i < cargc; i++ ) {
439 if ( strncasecmp( cargv[i], "host=", 5 )
441 charray_add( &be->be_replica,
447 Debug( LDAP_DEBUG_ANY,
448 "%s: line %d: missing host in \"replica\" line (ignored)\n",
453 /* dn of master entity allowed to write to replica */
454 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
456 Debug( LDAP_DEBUG_ANY,
457 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
462 Debug( LDAP_DEBUG_ANY,
463 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
466 be->be_update_ndn = ch_strdup( cargv[1] );
467 (void) dn_normalize_case( be->be_update_ndn );
470 /* replication log file to which changes are appended */
471 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
473 Debug( LDAP_DEBUG_ANY,
474 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
479 be->be_replogfile = ch_strdup( cargv[1] );
481 replogfile = ch_strdup( cargv[1] );
484 /* maintain lastmodified{by,time} attributes */
485 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
487 Debug( LDAP_DEBUG_ANY,
488 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
492 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
499 be->be_lastmod = OFF;
501 global_lastmod = OFF;
504 /* include another config file */
505 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
507 Debug( LDAP_DEBUG_ANY,
508 "%s: line %d: missing filename in \"include <filename>\" line\n",
512 savefname = ch_strdup( cargv[1] );
515 if ( read_config( savefname ) != 0 ) {
520 lineno = savelineno - 1;
522 /* location of kerberos srvtab file */
523 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
525 Debug( LDAP_DEBUG_ANY,
526 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
530 ldap_srvtab = ch_strdup( cargv[1] );
532 /* pass anything else to the current backend info/db config routine */
535 if ( bi->bi_config == 0 ) {
536 Debug( LDAP_DEBUG_ANY,
537 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
538 fname, lineno, cargv[0] );
540 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
546 } else if ( be != NULL ) {
547 if ( be->be_config == 0 ) {
548 Debug( LDAP_DEBUG_ANY,
549 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
550 fname, lineno, cargv[0] );
552 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
559 Debug( LDAP_DEBUG_ANY,
560 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
561 fname, lineno, cargv[0] );
580 for ( token = strtok_quote( line, " \t" ); token != NULL;
581 token = strtok_quote( NULL, " \t" ) ) {
582 if ( *argcp == MAXARGS ) {
583 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
587 argv[(*argcp)++] = token;
594 strtok_quote( char *line, char *sep )
600 if ( line != NULL ) {
603 while ( *next && strchr( sep, *next ) ) {
607 if ( *next == '\0' ) {
613 for ( inquote = 0; *next; ) {
621 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
627 next + 1, strlen( next + 1 ) + 1 );
628 next++; /* dont parse the escaped character */
633 if ( strchr( sep, *next ) != NULL ) {
646 static char buf[BUFSIZ];
648 static int lmax, lcur;
650 #define CATLINE( buf ) { \
652 len = strlen( buf ); \
653 while ( lcur + len + 1 > lmax ) { \
655 line = (char *) ch_realloc( line, lmax ); \
657 strcpy( line + lcur, buf ); \
662 fp_getline( FILE *fp, int *lineno )
670 /* hack attack - keeps us from having to keep a stack of bufs... */
671 if ( strncasecmp( line, "include", 7 ) == 0 ) {
676 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
677 if ( (p = strchr( buf, '\n' )) != NULL ) {
680 if ( ! isspace( (unsigned char) buf[0] ) ) {
689 return( line[0] ? line : NULL );
693 fp_getline_init( int *lineno )