1 /* config.c - configuration file handling routines */
6 #include <sys/socket.h>
8 #include "ldapconfig.h"
12 extern Backend *new_backend();
13 extern char *default_referral;
14 extern int ldap_syslog;
15 extern int global_schemacheck;
18 * defaults for various global variables
20 int defsize = SLAPD_DEFAULT_SIZELIMIT;
21 int deftime = SLAPD_DEFAULT_TIMELIMIT;
22 struct acl *global_acl = NULL;
23 int global_default_access = ACL_READ;
26 char *ldap_srvtab = "";
28 static char *fp_getline();
29 static void fp_getline_init();
30 static void fp_parse_line();
32 static char *strtok_quote();
35 read_config( char *fname, Backend **bep, FILE *pfp )
38 char *line, *savefname, *dn;
39 int cargc, savelineno;
44 if ( (fp = pfp) == NULL && (fp = fopen( fname, "r" )) == NULL ) {
46 Debug( LDAP_DEBUG_ANY,
47 "could not open config file \"%s\" - absolute path?\n",
53 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
55 fp_getline_init( &lineno );
56 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
57 /* skip comments and blank lines */
58 if ( line[0] == '#' || line[0] == '\0' ) {
62 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
64 fp_parse_line( line, &cargc, cargv );
67 Debug( LDAP_DEBUG_ANY,
68 "%s: line %d: bad config line (ignored)\n",
73 /* start of a new database definition */
74 if ( strcasecmp( cargv[0], "database" ) == 0 ) {
76 Debug( LDAP_DEBUG_ANY,
77 "%s: line %d: missing type in \"database <type>\" line\n",
81 *bep = new_backend( cargv[1] );
85 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
87 Debug( LDAP_DEBUG_ANY,
88 "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
93 defsize = atoi( cargv[1] );
95 be->be_sizelimit = atoi( cargv[1] );
99 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
101 Debug( LDAP_DEBUG_ANY,
102 "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
107 deftime = atoi( cargv[1] );
109 be->be_timelimit = atoi( cargv[1] );
112 /* set database suffix */
113 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
115 Debug( LDAP_DEBUG_ANY,
116 "%s: line %d: missing dn in \"suffix <dn>\" line\n",
119 } else if ( cargc > 2 ) {
120 Debug( LDAP_DEBUG_ANY,
121 "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
122 fname, lineno, cargv[1] );
125 Debug( LDAP_DEBUG_ANY,
126 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
129 dn = strdup( cargv[1] );
130 (void) dn_normalize( dn );
131 charray_add( &be->be_suffix, dn );
134 /* set magic "root" dn for this database */
135 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
137 Debug( LDAP_DEBUG_ANY,
138 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
143 Debug( LDAP_DEBUG_ANY,
144 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
147 dn = strdup( cargv[1] );
148 (void) dn_normalize( dn );
152 /* set super-secret magic database password */
153 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
155 Debug( LDAP_DEBUG_ANY,
156 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
161 Debug( LDAP_DEBUG_ANY,
162 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
165 be->be_rootpw = strdup( cargv[1] );
168 /* make this database read-only */
169 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
171 Debug( LDAP_DEBUG_ANY,
172 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
177 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
181 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
188 /* where to send clients when we don't hold it */
189 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
191 Debug( LDAP_DEBUG_ANY,
192 "%s: line %d: missing URL in \"referral <URL>\" line\n",
196 default_referral = (char *) malloc( strlen( cargv[1] )
197 + sizeof("Referral:\n") + 1 );
198 strcpy( default_referral, "Referral:\n" );
199 strcat( default_referral, cargv[1] );
201 /* specify an objectclass */
202 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
203 parse_oc( be, fname, lineno, cargc, cargv );
205 /* specify an attribute */
206 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
207 attr_syntax_config( fname, lineno, cargc - 1,
210 /* turn on/off schema checking */
211 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
213 Debug( LDAP_DEBUG_ANY,
214 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
218 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
219 global_schemacheck = 1;
221 global_schemacheck = 0;
224 /* specify access control info */
225 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
226 parse_acl( be, fname, lineno, cargc, cargv );
228 /* specify default access control info */
229 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
231 Debug( LDAP_DEBUG_ANY,
232 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
237 if ( (global_default_access =
238 str2access( cargv[1] )) == -1 ) {
239 Debug( LDAP_DEBUG_ANY,
240 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
241 fname, lineno, cargv[1] );
245 if ( (be->be_dfltaccess =
246 str2access( cargv[1] )) == -1 ) {
247 Debug( LDAP_DEBUG_ANY,
248 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
249 fname, lineno, cargv[1] );
254 /* debug level to log things to syslog */
255 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
257 Debug( LDAP_DEBUG_ANY,
258 "%s: line %d: missing level in \"loglevel <level>\" line\n",
262 ldap_syslog = atoi( cargv[1] );
264 /* list of replicas of the data in this backend (master only) */
265 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
267 Debug( LDAP_DEBUG_ANY,
268 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
273 Debug( LDAP_DEBUG_ANY,
274 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
277 for ( i = 1; i < cargc; i++ ) {
278 if ( strncasecmp( cargv[i], "host=", 5 )
280 charray_add( &be->be_replica,
281 strdup( cargv[i] + 5 ) );
286 Debug( LDAP_DEBUG_ANY,
287 "%s: line %d: missing host in \"replica\" line (ignored)\n",
292 /* dn of master entity allowed to write to replica */
293 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
295 Debug( LDAP_DEBUG_ANY,
296 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
301 Debug( LDAP_DEBUG_ANY,
302 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
305 be->be_updatedn = strdup( cargv[1] );
306 (void) dn_normalize( be->be_updatedn );
309 /* replication log file to which changes are appended */
310 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
312 Debug( LDAP_DEBUG_ANY,
313 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
318 be->be_replogfile = strdup( cargv[1] );
320 replogfile = strdup( cargv[1] );
323 /* maintain lastmodified{by,time} attributes */
324 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
326 Debug( LDAP_DEBUG_ANY,
327 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
331 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
338 be->be_lastmod = OFF;
340 global_lastmod = OFF;
343 /* include another config file */
344 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
346 Debug( LDAP_DEBUG_ANY,
347 "%s: line %d: missing filename in \"include <filename>\" line\n",
351 savefname = strdup( cargv[1] );
353 read_config( savefname, bep, NULL );
356 lineno = savelineno - 1;
358 /* location of kerberos srvtab file */
359 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
361 Debug( LDAP_DEBUG_ANY,
362 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
366 ldap_srvtab = strdup( cargv[1] );
368 /* pass anything else to the current backend config routine */
371 Debug( LDAP_DEBUG_ANY,
372 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
373 fname, lineno, cargv[0] );
374 } else if ( be->be_config == NULL ) {
375 Debug( LDAP_DEBUG_ANY,
376 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
377 fname, lineno, cargv[0] );
379 (*be->be_config)( be, fname, lineno, cargc,
397 for ( token = strtok_quote( line, " \t" ); token != NULL;
398 token = strtok_quote( NULL, " \t" ) ) {
399 if ( *argcp == MAXARGS ) {
400 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
404 argv[(*argcp)++] = token;
410 strtok_quote( char *line, char *sep )
416 if ( line != NULL ) {
419 while ( *next && strchr( sep, *next ) ) {
423 if ( *next == '\0' ) {
429 for ( inquote = 0; *next; ) {
437 strcpy( next, next + 1 );
441 strcpy( next, next + 1 );
446 if ( strchr( sep, *next ) != NULL ) {
459 static char buf[BUFSIZ];
461 static int lmax, lcur;
463 #define CATLINE( buf ) { \
465 len = strlen( buf ); \
466 while ( lcur + len + 1 > lmax ) { \
468 line = (char *) ch_realloc( line, lmax ); \
470 strcpy( line + lcur, buf ); \
475 fp_getline( FILE *fp, int *lineno )
483 /* hack attack - keeps us from having to keep a stack of bufs... */
484 if ( strncasecmp( line, "include", 7 ) == 0 ) {
489 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
490 if ( (p = strchr( buf, '\n' )) != NULL ) {
493 if ( ! isspace( buf[0] ) ) {
502 return( line[0] ? line : NULL );
506 fp_getline_init( int *lineno )