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 char *slapd_pid_file = NULL;
28 char *slapd_args_file = NULL;
30 static char *fp_getline(FILE *fp, int *lineno);
31 static void fp_getline_init(int *lineno);
32 static int fp_parse_line(char *line, int *argcp, char **argv);
34 static char *strtok_quote(char *line, char *sep);
37 read_config( char *fname )
40 char *line, *savefname;
41 int cargc, savelineno;
45 static BackendInfo *bi = NULL;
46 static BackendDB *be = NULL;
48 if ( (fp = fopen( fname, "r" )) == NULL ) {
50 Debug( LDAP_DEBUG_ANY,
51 "could not open config file \"%s\" - absolute path?\n",
57 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
59 fp_getline_init( &lineno );
61 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
62 /* skip comments and blank lines */
63 if ( line[0] == '#' || line[0] == '\0' ) {
67 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
69 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
74 Debug( LDAP_DEBUG_ANY,
75 "%s: line %d: bad config line (ignored)\n",
80 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
82 Debug( LDAP_DEBUG_ANY,
83 "%s: line %d: missing type in \"backend <type>\" line\n",
89 Debug( LDAP_DEBUG_ANY,
90 "%s: line %d: backend line must appear before any database definition\n",
95 bi = backend_info( cargv[1] );
97 /* start of a new database definition */
98 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
100 Debug( LDAP_DEBUG_ANY,
101 "%s: line %d: missing type in \"database <type>\" line\n",
106 be = backend_db_init( cargv[1] );
108 /* assign a default depth limit for alias deref */
109 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH;
111 /* get pid file name */
112 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
114 Debug( LDAP_DEBUG_ANY,
115 "%s: line %d: missing file name in \"pidfile <file>\" line\n",
120 slapd_pid_file = ch_strdup( cargv[1] );
122 /* get args file name */
123 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
125 Debug( LDAP_DEBUG_ANY,
126 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
131 slapd_args_file = ch_strdup( cargv[1] );
134 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
136 Debug( LDAP_DEBUG_ANY,
137 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
142 defsize = atoi( cargv[1] );
144 be->be_sizelimit = atoi( cargv[1] );
148 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
150 Debug( LDAP_DEBUG_ANY,
151 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
156 deftime = atoi( cargv[1] );
158 be->be_timelimit = atoi( cargv[1] );
161 /* set database suffix */
162 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
164 Debug( LDAP_DEBUG_ANY,
165 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
168 } else if ( cargc > 2 ) {
169 Debug( LDAP_DEBUG_ANY,
170 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
171 fname, lineno, cargv[1] );
174 Debug( LDAP_DEBUG_ANY,
175 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
178 char *dn = ch_strdup( cargv[1] );
179 (void) dn_normalize_case( dn );
180 charray_add( &be->be_suffix, dn );
183 /* set database suffixAlias */
184 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
186 Debug( LDAP_DEBUG_ANY,
187 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
190 } else if ( cargc < 3 ) {
191 Debug( LDAP_DEBUG_ANY,
192 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
195 } else if ( cargc > 3 ) {
196 Debug( LDAP_DEBUG_ANY,
197 "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
201 Debug( LDAP_DEBUG_ANY,
202 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
205 char *alias, *aliased_dn;
207 alias = ch_strdup( cargv[1] );
208 (void) dn_normalize( alias );
210 aliased_dn = ch_strdup( cargv[2] );
211 (void) dn_normalize( aliased_dn );
214 if ( strcasecmp( alias, aliased_dn) == 0 ) {
215 Debug( LDAP_DEBUG_ANY,
216 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
217 fname, lineno, alias );
219 (void) dn_normalize_case( alias );
220 (void) dn_normalize_case( aliased_dn );
221 charray_add( &be->be_suffixAlias, alias );
222 charray_add( &be->be_suffixAlias, aliased_dn );
229 /* set max deref depth */
230 } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
232 Debug( LDAP_DEBUG_ANY,
233 "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
238 Debug( LDAP_DEBUG_ANY,
239 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
242 be->be_maxDerefDepth = atoi (cargv[1]);
246 /* set magic "root" dn for this database */
247 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
249 Debug( LDAP_DEBUG_ANY,
250 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
255 Debug( LDAP_DEBUG_ANY,
256 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
259 be->be_root_dn = ch_strdup( cargv[1] );
260 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
263 /* set super-secret magic database password */
264 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
266 Debug( LDAP_DEBUG_ANY,
267 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
272 Debug( LDAP_DEBUG_ANY,
273 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
276 be->be_root_pw = ch_strdup( cargv[1] );
279 /* make this database read-only */
280 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
282 Debug( LDAP_DEBUG_ANY,
283 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
292 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
299 /* where to send clients when we don't hold it */
300 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
302 Debug( LDAP_DEBUG_ANY,
303 "%s: line %d: missing URL in \"referral <URL>\" line\n",
307 default_referral = (char *) ch_malloc( strlen( cargv[1] )
308 + sizeof("Referral:\n") + 1 );
309 strcpy( default_referral, "Referral:\n" );
310 strcat( default_referral, cargv[1] );
312 /* specify an objectclass */
313 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
314 parse_oc( be, fname, lineno, cargc, cargv );
316 /* specify an attribute */
317 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
318 attr_syntax_config( fname, lineno, cargc - 1,
321 /* turn on/off schema checking */
322 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
324 Debug( LDAP_DEBUG_ANY,
325 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
329 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
330 global_schemacheck = 1;
332 global_schemacheck = 0;
335 /* specify access control info */
336 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
337 parse_acl( be, fname, lineno, cargc, cargv );
339 /* specify default access control info */
340 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
342 Debug( LDAP_DEBUG_ANY,
343 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
348 if ( (global_default_access =
349 str2access( cargv[1] )) == -1 ) {
350 Debug( LDAP_DEBUG_ANY,
351 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
352 fname, lineno, cargv[1] );
356 if ( (be->be_dfltaccess =
357 str2access( cargv[1] )) == -1 ) {
358 Debug( LDAP_DEBUG_ANY,
359 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
360 fname, lineno, cargv[1] );
365 /* debug level to log things to syslog */
366 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
368 Debug( LDAP_DEBUG_ANY,
369 "%s: line %d: missing level in \"loglevel <level>\" line\n",
373 ldap_syslog = atoi( cargv[1] );
375 /* list of replicas of the data in this backend (master only) */
376 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
378 Debug( LDAP_DEBUG_ANY,
379 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
384 Debug( LDAP_DEBUG_ANY,
385 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
388 for ( i = 1; i < cargc; i++ ) {
389 if ( strncasecmp( cargv[i], "host=", 5 )
391 charray_add( &be->be_replica,
392 ch_strdup( cargv[i] + 5 ) );
397 Debug( LDAP_DEBUG_ANY,
398 "%s: line %d: missing host in \"replica\" line (ignored)\n",
403 /* dn of master entity allowed to write to replica */
404 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
406 Debug( LDAP_DEBUG_ANY,
407 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
412 Debug( LDAP_DEBUG_ANY,
413 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
416 be->be_update_ndn = ch_strdup( cargv[1] );
417 (void) dn_normalize_case( be->be_update_ndn );
420 /* replication log file to which changes are appended */
421 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
423 Debug( LDAP_DEBUG_ANY,
424 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
429 be->be_replogfile = ch_strdup( cargv[1] );
431 replogfile = ch_strdup( cargv[1] );
434 /* maintain lastmodified{by,time} attributes */
435 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
437 Debug( LDAP_DEBUG_ANY,
438 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
442 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
449 be->be_lastmod = OFF;
451 global_lastmod = OFF;
454 /* include another config file */
455 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
457 Debug( LDAP_DEBUG_ANY,
458 "%s: line %d: missing filename in \"include <filename>\" line\n",
462 savefname = ch_strdup( cargv[1] );
465 if ( read_config( savefname ) != 0 ) {
470 lineno = savelineno - 1;
472 /* location of kerberos srvtab file */
473 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
475 Debug( LDAP_DEBUG_ANY,
476 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
480 ldap_srvtab = ch_strdup( cargv[1] );
482 /* pass anything else to the current backend info/db config routine */
485 if (bi->bi_config == NULL) {
486 Debug( LDAP_DEBUG_ANY,
487 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
488 fname, lineno, cargv[0] );
490 if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
496 } else if ( be != NULL ) {
497 if ( be->be_config == NULL ) {
498 Debug( LDAP_DEBUG_ANY,
499 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
500 fname, lineno, cargv[0] );
502 if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
509 Debug( LDAP_DEBUG_ANY,
510 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
511 fname, lineno, cargv[0] );
529 for ( token = strtok_quote( line, " \t" ); token != NULL;
530 token = strtok_quote( NULL, " \t" ) ) {
531 if ( *argcp == MAXARGS ) {
532 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
536 argv[(*argcp)++] = token;
543 strtok_quote( char *line, char *sep )
549 if ( line != NULL ) {
552 while ( *next && strchr( sep, *next ) ) {
556 if ( *next == '\0' ) {
562 for ( inquote = 0; *next; ) {
570 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
574 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
579 if ( strchr( sep, *next ) != NULL ) {
592 static char buf[BUFSIZ];
594 static int lmax, lcur;
596 #define CATLINE( buf ) { \
598 len = strlen( buf ); \
599 while ( lcur + len + 1 > lmax ) { \
601 line = (char *) ch_realloc( line, lmax ); \
603 strcpy( line + lcur, buf ); \
608 fp_getline( FILE *fp, int *lineno )
616 /* hack attack - keeps us from having to keep a stack of bufs... */
617 if ( strncasecmp( line, "include", 7 ) == 0 ) {
622 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
623 if ( (p = strchr( buf, '\n' )) != NULL ) {
626 if ( ! isspace( buf[0] ) ) {
635 return( line[0] ? line : NULL );
639 fp_getline_init( int *lineno )