1 /* config.c - configuration file handling routines */
8 #include <sys/socket.h>
10 #include "ldapconfig.h"
14 extern Backend *new_backend();
15 extern char *default_referral;
16 extern int ldap_syslog;
17 extern int global_schemacheck;
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 static char *fp_getline();
31 static void fp_getline_init();
32 static void fp_parse_line();
34 static char *strtok_quote();
37 read_config( char *fname, Backend **bep, FILE *pfp )
40 char *line, *savefname, *dn;
41 int cargc, savelineno;
46 if ( (fp = pfp) == NULL && (fp = fopen( fname, "r" )) == NULL ) {
48 Debug( LDAP_DEBUG_ANY,
49 "could not open config file \"%s\" - absolute path?\n",
55 Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
57 fp_getline_init( &lineno );
58 while ( (line = fp_getline( fp, &lineno )) != NULL ) {
59 /* skip comments and blank lines */
60 if ( line[0] == '#' || line[0] == '\0' ) {
64 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
66 fp_parse_line( line, &cargc, cargv );
69 Debug( LDAP_DEBUG_ANY,
70 "%s: line %d: bad config line (ignored)\n",
75 /* start of a new database definition */
76 if ( strcasecmp( cargv[0], "database" ) == 0 ) {
78 Debug( LDAP_DEBUG_ANY,
79 "%s: line %d: missing type in \"database <type>\" line\n",
83 *bep = new_backend( cargv[1] );
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 dn = strdup( cargv[1] );
132 (void) dn_normalize( dn );
133 charray_add( &be->be_suffix, dn );
136 /* set magic "root" dn for this database */
137 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
139 Debug( LDAP_DEBUG_ANY,
140 "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
145 Debug( LDAP_DEBUG_ANY,
146 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
149 dn = strdup( cargv[1] );
150 (void) dn_normalize( dn );
154 /* set super-secret magic database password */
155 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
157 Debug( LDAP_DEBUG_ANY,
158 "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
163 Debug( LDAP_DEBUG_ANY,
164 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
167 be->be_rootpw = strdup( cargv[1] );
170 /* make this database read-only */
171 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
173 Debug( LDAP_DEBUG_ANY,
174 "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
179 Debug( LDAP_DEBUG_ANY,
180 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
183 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
190 /* where to send clients when we don't hold it */
191 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
193 Debug( LDAP_DEBUG_ANY,
194 "%s: line %d: missing URL in \"referral <URL>\" line\n",
198 default_referral = (char *) malloc( strlen( cargv[1] )
199 + sizeof("Referral:\n") + 1 );
200 strcpy( default_referral, "Referral:\n" );
201 strcat( default_referral, cargv[1] );
203 /* specify an objectclass */
204 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
205 parse_oc( be, fname, lineno, cargc, cargv );
207 /* specify an attribute */
208 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
209 attr_syntax_config( fname, lineno, cargc - 1,
212 /* turn on/off schema checking */
213 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
215 Debug( LDAP_DEBUG_ANY,
216 "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
220 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
221 global_schemacheck = 1;
223 global_schemacheck = 0;
226 /* specify access control info */
227 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
228 parse_acl( be, fname, lineno, cargc, cargv );
230 /* specify default access control info */
231 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
233 Debug( LDAP_DEBUG_ANY,
234 "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
239 if ( (global_default_access =
240 str2access( cargv[1] )) == -1 ) {
241 Debug( LDAP_DEBUG_ANY,
242 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
243 fname, lineno, cargv[1] );
247 if ( (be->be_dfltaccess =
248 str2access( cargv[1] )) == -1 ) {
249 Debug( LDAP_DEBUG_ANY,
250 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
251 fname, lineno, cargv[1] );
256 /* debug level to log things to syslog */
257 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
259 Debug( LDAP_DEBUG_ANY,
260 "%s: line %d: missing level in \"loglevel <level>\" line\n",
264 ldap_syslog = atoi( cargv[1] );
266 /* list of replicas of the data in this backend (master only) */
267 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
269 Debug( LDAP_DEBUG_ANY,
270 "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
275 Debug( LDAP_DEBUG_ANY,
276 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
279 for ( i = 1; i < cargc; i++ ) {
280 if ( strncasecmp( cargv[i], "host=", 5 )
282 charray_add( &be->be_replica,
283 strdup( cargv[i] + 5 ) );
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: missing host in \"replica\" line (ignored)\n",
294 /* dn of master entity allowed to write to replica */
295 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
297 Debug( LDAP_DEBUG_ANY,
298 "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
303 Debug( LDAP_DEBUG_ANY,
304 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
307 be->be_updatedn = strdup( cargv[1] );
308 (void) dn_normalize( be->be_updatedn );
311 /* replication log file to which changes are appended */
312 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
314 Debug( LDAP_DEBUG_ANY,
315 "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
320 be->be_replogfile = strdup( cargv[1] );
322 replogfile = strdup( cargv[1] );
325 /* maintain lastmodified{by,time} attributes */
326 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
328 Debug( LDAP_DEBUG_ANY,
329 "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
333 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
340 be->be_lastmod = OFF;
342 global_lastmod = OFF;
345 /* include another config file */
346 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
348 Debug( LDAP_DEBUG_ANY,
349 "%s: line %d: missing filename in \"include <filename>\" line\n",
353 savefname = strdup( cargv[1] );
355 read_config( savefname, bep, NULL );
358 lineno = savelineno - 1;
360 /* location of kerberos srvtab file */
361 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
363 Debug( LDAP_DEBUG_ANY,
364 "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
368 ldap_srvtab = strdup( cargv[1] );
370 /* pass anything else to the current backend config routine */
373 Debug( LDAP_DEBUG_ANY,
374 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
375 fname, lineno, cargv[0] );
376 } else if ( be->be_config == NULL ) {
377 Debug( LDAP_DEBUG_ANY,
378 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
379 fname, lineno, cargv[0] );
381 (*be->be_config)( be, fname, lineno, cargc,
399 for ( token = strtok_quote( line, " \t" ); token != NULL;
400 token = strtok_quote( NULL, " \t" ) ) {
401 if ( *argcp == MAXARGS ) {
402 Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
406 argv[(*argcp)++] = token;
412 strtok_quote( char *line, char *sep )
418 if ( line != NULL ) {
421 while ( *next && strchr( sep, *next ) ) {
425 if ( *next == '\0' ) {
431 for ( inquote = 0; *next; ) {
439 strcpy( next, next + 1 );
443 strcpy( next, next + 1 );
448 if ( strchr( sep, *next ) != NULL ) {
461 static char buf[BUFSIZ];
463 static int lmax, lcur;
465 #define CATLINE( buf ) { \
467 len = strlen( buf ); \
468 while ( lcur + len + 1 > lmax ) { \
470 line = (char *) ch_realloc( line, lmax ); \
472 strcpy( line + lcur, buf ); \
477 fp_getline( FILE *fp, int *lineno )
485 /* hack attack - keeps us from having to keep a stack of bufs... */
486 if ( strncasecmp( line, "include", 7 ) == 0 ) {
491 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
492 if ( (p = strchr( buf, '\n' )) != NULL ) {
495 if ( ! isspace( buf[0] ) ) {
504 return( line[0] ? line : NULL );
508 fp_getline_init( int *lineno )