]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
Further trim_refs_url fixes from Hallvard. Need additional testing.
[openldap] / servers / slapd / config.c
1 /* config.c - configuration file handling routines */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10 #ifdef HAVE_LOCALE_H
11 #include <locale.h>
12 #endif
13
14 #include <ac/string.h>
15 #include <ac/ctype.h>
16 #include <ac/socket.h>
17
18 #include "ldap_pvt.h"
19 #include "slap.h"
20
21 #define MAXARGS 100
22
23 /*
24  * defaults for various global variables
25  */
26 int             defsize = SLAPD_DEFAULT_SIZELIMIT;
27 int             deftime = SLAPD_DEFAULT_TIMELIMIT;
28 AccessControl   *global_acl = NULL;
29 int             global_default_access = ACL_READ;
30 int             global_readonly = 0;
31 char            *replogfile;
32 int             global_lastmod = ON;
33 int             global_idletimeout = 0;
34 char    *global_realm = NULL;
35 char            *ldap_srvtab = "";
36
37 char   *slapd_pid_file  = NULL;
38 char   *slapd_args_file = NULL;
39
40 static char     *fp_getline(FILE *fp, int *lineno);
41 static void     fp_getline_init(int *lineno);
42 static int      fp_parse_line(char *line, int *argcp, char **argv);
43
44 static char     *strtok_quote(char *line, char *sep);
45
46 int
47 read_config( const char *fname )
48 {
49         FILE    *fp;
50         char    *line, *savefname, *saveline;
51         int     cargc, savelineno;
52         char    *cargv[MAXARGS];
53         int     lineno, i;
54 #ifdef HAVE_TLS
55         int rc;
56 #endif
57         struct berval *vals[2];
58         struct berval val;
59
60         static BackendInfo *bi = NULL;
61         static BackendDB        *be = NULL;
62
63         vals[0] = &val;
64         vals[1] = NULL;
65
66         if ( (fp = fopen( fname, "r" )) == NULL ) {
67                 ldap_syslog = 1;
68                 Debug( LDAP_DEBUG_ANY,
69                     "could not open config file \"%s\" - absolute path?\n",
70                     fname, 0, 0 );
71                 perror( fname );
72                 return 1;
73         }
74
75         Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
76
77         if ( schema_init( ) != 0 ) {
78                 Debug( LDAP_DEBUG_ANY,
79                     "error initializing the schema\n",
80                     0, 0, 0 );
81                 return( 1 );
82         }
83
84         fp_getline_init( &lineno );
85
86         while ( (line = fp_getline( fp, &lineno )) != NULL ) {
87                 /* skip comments and blank lines */
88                 if ( line[0] == '#' || line[0] == '\0' ) {
89                         continue;
90                 }
91
92                 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
93
94                 /* fp_parse_line is destructive, we save a copy */
95                 saveline = ch_strdup( line );
96
97                 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
98                         return( 1 );
99                 }
100
101                 if ( cargc < 1 ) {
102                         Debug( LDAP_DEBUG_ANY,
103                             "%s: line %d: bad config line (ignored)\n",
104                             fname, lineno, 0 );
105                         continue;
106                 }
107
108                 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
109                         if ( cargc < 2 ) {
110                                 Debug( LDAP_DEBUG_ANY,
111                 "%s: line %d: missing type in \"backend <type>\" line\n",
112                                     fname, lineno, 0 );
113                                 return( 1 );
114                         }
115
116                         if( be != NULL ) {
117                                 Debug( LDAP_DEBUG_ANY,
118 "%s: line %d: backend line must appear before any database definition\n",
119                                     fname, lineno, 0 );
120                                 return( 1 );
121                         }
122
123                         bi = backend_info( cargv[1] );
124
125                 /* start of a new database definition */
126                 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
127                         if ( cargc < 2 ) {
128                                 Debug( LDAP_DEBUG_ANY,
129                 "%s: line %d: missing type in \"database <type>\" line\n",
130                                     fname, lineno, 0 );
131                                 return( 1 );
132                         }
133                         bi = NULL;
134                         be = backend_db_init( cargv[1] );
135
136                 /* get pid file name */
137                 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
138                         if ( cargc < 2 ) {
139                                 Debug( LDAP_DEBUG_ANY,
140             "%s: line %d: missing file name in \"pidfile <file>\" line\n",
141                                     fname, lineno, 0 );
142                                 return( 1 );
143                         }
144
145                         slapd_pid_file = ch_strdup( cargv[1] );
146
147                 /* get args file name */
148                 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
149                         if ( cargc < 2 ) {
150                                 Debug( LDAP_DEBUG_ANY,
151             "%s: line %d: missing file name in \"argsfile <file>\" line\n",
152                                     fname, lineno, 0 );
153                                 return( 1 );
154                         }
155
156                         slapd_args_file = ch_strdup( cargv[1] );
157
158                 /* set DIGEST realm */
159                 } else if ( strcasecmp( cargv[0], "digest-realm" ) == 0 ) {
160                         if ( cargc < 2 ) {
161                                 Debug( LDAP_DEBUG_ANY,
162             "%s: line %d: missing realm in \"digest-realm <realm>\" line\n",
163                                     fname, lineno, 0 );
164                                 return( 1 );
165                         }
166                         if ( be != NULL ) {
167                                 be->be_realm = ch_strdup( cargv[1] );
168
169                         } else if ( global_realm != NULL ) {
170                                 Debug( LDAP_DEBUG_ANY,
171                                         "%s: line %d: already set global realm!\n",
172                                         fname, lineno, 0 );
173                                 return 1;
174
175                         } else {
176                                 global_realm = ch_strdup( cargv[1] );
177                         }
178
179                 /* set time limit */
180                 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
181                         if ( cargc < 2 ) {
182                                 Debug( LDAP_DEBUG_ANY,
183             "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
184                                     fname, lineno, 0 );
185                                 return( 1 );
186                         }
187                         if ( be == NULL ) {
188                                 defsize = atoi( cargv[1] );
189                         } else {
190                                 be->be_sizelimit = atoi( cargv[1] );
191                         }
192
193                 /* set time limit */
194                 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
195                         if ( cargc < 2 ) {
196                                 Debug( LDAP_DEBUG_ANY,
197             "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
198                                     fname, lineno, 0 );
199                                 return( 1 );
200                         }
201                         if ( be == NULL ) {
202                                 deftime = atoi( cargv[1] );
203                         } else {
204                                 be->be_timelimit = atoi( cargv[1] );
205                         }
206
207                 /* set database suffix */
208                 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
209                         Backend *tmp_be;
210                         if ( cargc < 2 ) {
211                                 Debug( LDAP_DEBUG_ANY,
212                     "%s: line %d: missing dn in \"suffix <dn>\" line\n",
213                                     fname, lineno, 0 );
214                                 return( 1 );
215                         } else if ( cargc > 2 ) {
216                                 Debug( LDAP_DEBUG_ANY,
217     "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
218                                     fname, lineno, cargv[1] );
219                         }
220                         if ( be == NULL ) {
221                                 Debug( LDAP_DEBUG_ANY,
222 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
223                                     fname, lineno, 0 );
224                         } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
225                                 Debug( LDAP_DEBUG_ANY,
226 "%s: line %d: suffix already served by this backend (ignored)\n",
227                                     fname, lineno, 0 );
228                         } else if ( tmp_be  != NULL ) {
229                                 Debug( LDAP_DEBUG_ANY,
230 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
231                                     fname, lineno, tmp_be->be_suffix[0] );
232                         } else {
233                                 char *dn = ch_strdup( cargv[1] );
234                                 (void) dn_normalize( dn );
235                                 charray_add( &be->be_suffix, dn );
236                                 (void) ldap_pvt_str2upper( dn );
237                                 charray_add( &be->be_nsuffix, dn );
238                                 free( dn );
239                         }
240
241                 /* set database suffixAlias */
242                 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
243                         Backend *tmp_be;
244                         if ( cargc < 2 ) {
245                                 Debug( LDAP_DEBUG_ANY,
246 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
247                                         fname, lineno, 0 );
248                                 return( 1 );
249                         } else if ( cargc < 3 ) {
250                                 Debug( LDAP_DEBUG_ANY,
251 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
252                                 fname, lineno, 0 );
253                                 return( 1 );
254                         } else if ( cargc > 3 ) {
255                                 Debug( LDAP_DEBUG_ANY,
256                                         "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
257                                 fname, lineno, 0 );
258                         }
259
260                         if ( be == NULL ) {
261                                 Debug( LDAP_DEBUG_ANY,
262                                         "%s: line %d: suffixAlias line"
263                                         " must appear inside a database definition (ignored)\n",
264                                         fname, lineno, 0 );
265                         } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
266                                 Debug( LDAP_DEBUG_ANY,
267                                         "%s: line %d: suffixAlias served by"
268                                         "  a preceeding backend \"%s\" (ignored)\n",
269                                         fname, lineno, tmp_be->be_suffix[0] );
270
271                         } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
272                                 Debug( LDAP_DEBUG_ANY,
273                                         "%s: line %d: suffixAlias derefs to differnet backend"
274                                         "  a preceeding backend \"%s\" (ignored)\n",
275                                         fname, lineno, tmp_be->be_suffix[0] );
276
277                         } else {
278                                 char *alias, *aliased_dn;
279
280                                 alias = ch_strdup( cargv[1] );
281                                 (void) dn_normalize( alias );
282
283                                 aliased_dn = ch_strdup( cargv[2] );
284                                 (void) dn_normalize( aliased_dn );
285
286                                 (void) dn_normalize_case( alias );
287                                 (void) dn_normalize_case( aliased_dn );
288                                 charray_add( &be->be_suffixAlias, alias );
289                                 charray_add( &be->be_suffixAlias, aliased_dn );
290
291                                 free(alias);
292                                 free(aliased_dn);
293                         }
294
295                /* set max deref depth */
296                } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
297                                         int i;
298                        if ( cargc < 2 ) {
299                                Debug( LDAP_DEBUG_ANY,
300                    "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
301                                    fname, lineno, 0 );
302                                return( 1 );
303                        }
304                        if ( be == NULL ) {
305                                Debug( LDAP_DEBUG_ANY,
306 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
307                                    fname, lineno, 0 );
308                        } else if ((i = atoi(cargv[1])) < 0) {
309                                Debug( LDAP_DEBUG_ANY,
310 "%s: line %d: depth must be positive (ignored)\n",
311                                    fname, lineno, 0 );
312
313                        } else {
314                            be->be_max_deref_depth = i;
315                                            }
316
317
318                 /* set magic "root" dn for this database */
319                 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
320                         if ( cargc < 2 ) {
321                                 Debug( LDAP_DEBUG_ANY,
322                     "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
323                                     fname, lineno, 0 );
324                                 return( 1 );
325                         }
326                         if ( be == NULL ) {
327                                 Debug( LDAP_DEBUG_ANY,
328 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
329                                     fname, lineno, 0 );
330                         } else {
331                                 be->be_root_dn = ch_strdup( cargv[1] );
332                                 be->be_root_ndn = ch_strdup( cargv[1] );
333
334                                 if( dn_normalize_case( be->be_root_ndn ) == NULL ) {
335                                         free( be->be_root_dn );
336                                         free( be->be_root_ndn );
337                                         Debug( LDAP_DEBUG_ANY,
338 "%s: line %d: rootdn DN is invalid\n",
339                                            fname, lineno, 0 );
340                                         return( 1 );
341                                 }
342                         }
343
344                 /* set super-secret magic database password */
345                 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
346                         if ( cargc < 2 ) {
347                                 Debug( LDAP_DEBUG_ANY,
348             "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
349                                     fname, lineno, 0 );
350                                 return( 1 );
351                         }
352                         if ( be == NULL ) {
353                                 Debug( LDAP_DEBUG_ANY,
354 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
355                                     fname, lineno, 0 );
356                         } else {
357                                 be->be_root_pw = ch_strdup( cargv[1] );
358                         }
359
360                 /* make this database read-only */
361                 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
362                         if ( cargc < 2 ) {
363                                 Debug( LDAP_DEBUG_ANY,
364             "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
365                                     fname, lineno, 0 );
366                                 return( 1 );
367                         }
368                         if ( be == NULL ) {
369                                 global_readonly = (strcasecmp( cargv[1], "on" ) == 0);
370                         } else {
371                                 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
372                                         be->be_readonly = 1;
373                                 } else {
374                                         be->be_readonly = 0;
375                                 }
376                         }
377
378                 /* where to send clients when we don't hold it */
379                 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
380                         if ( cargc < 2 ) {
381                                 Debug( LDAP_DEBUG_ANY,
382                     "%s: line %d: missing URL in \"referral <URL>\" line\n",
383                                     fname, lineno, 0 );
384                                 return( 1 );
385                         }
386
387                         vals[0]->bv_val = cargv[1];
388                         vals[0]->bv_len = strlen( vals[0]->bv_val );
389                         value_add( &default_referral, vals );
390
391                 /* specify locale */
392                 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
393 #ifdef HAVE_LOCALE_H
394                         char *locale;
395                         if ( cargc < 2 ) {
396                                 Debug( LDAP_DEBUG_ANY,
397         "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
398                                        fname, lineno, 0 );
399                                 return( 1 );
400                         }
401
402                         locale = (strcasecmp(   cargv[1], "on"  ) == 0 ? ""
403                                   : strcasecmp( cargv[1], "off" ) == 0 ? "C"
404                                   : ch_strdup( cargv[1] )                    );
405
406                         if ( setlocale( LC_CTYPE, locale ) == 0 ) {
407                                 Debug( LDAP_DEBUG_ANY,
408                                        (*locale
409                                         ? "%s: line %d: bad locale \"%s\"\n"
410                                         : "%s: line %d: bad locale\n"),
411                                        fname, lineno, locale );
412                                 return( 1 );
413                         }
414 #else
415                         Debug( LDAP_DEBUG_ANY,
416                                "%s: line %d: \"locale\" unsupported\n",
417                                fname, lineno, 0 );
418                         return( 1 );
419 #endif
420                 /* specify an Object Identifier macro */
421                 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
422                         parse_oidm( fname, lineno, cargc, cargv );
423                 /* specify an objectclass */
424                 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
425                         if ( *cargv[1] == '(' ) {
426                                 char * p;
427                                 p = strchr(saveline,'(');
428                                 parse_oc( fname, lineno, p, cargv );
429                         } else {
430                                 parse_oc_old( be, fname, lineno, cargc, cargv );
431                         }
432
433                 /* specify an attribute */
434                 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
435                         if ( *cargv[1] == '(' ) {
436                                 char * p;
437                                 p = strchr(saveline,'(');
438                                 parse_at( fname, lineno, p, cargv );
439                         } else {
440                                 attr_syntax_config( fname, lineno, cargc - 1,
441                                     &cargv[1] );
442                         }
443
444                 /* turn on/off schema checking */
445                 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
446                         if ( cargc < 2 ) {
447                                 Debug( LDAP_DEBUG_ANY,
448     "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
449                                     fname, lineno, 0 );
450                                 return( 1 );
451                         }
452                         if ( strcasecmp( cargv[1], "off" ) == 0 ) {
453                                 global_schemacheck = 0;
454                         } else {
455                                 global_schemacheck = 1;
456                         }
457
458                 /* specify access control info */
459                 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
460                         parse_acl( be, fname, lineno, cargc, cargv );
461
462                 /* specify default access control info */
463                 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
464                         if ( cargc < 2 ) {
465                                 Debug( LDAP_DEBUG_ANY,
466             "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
467                                     fname, lineno, 0 );
468                                 return( 1 );
469                         }
470                         if ( be == NULL ) {
471                                 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
472                                                 str2access(cargv[1]))) )
473                                 {
474                                         Debug( LDAP_DEBUG_ANY,
475 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
476                                             fname, lineno, cargv[1] );
477                                         return( 1 );
478                                 }
479                         } else {
480                                 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
481                                                 str2access(cargv[1]))) )
482                                 {
483                                         Debug( LDAP_DEBUG_ANY,
484                                                 "%s: line %d: bad access \"%s\", "
485                                                 "expecting [self]{none|auth|compare|search|read|write}\n",
486                                             fname, lineno, cargv[1] );
487                                         return( 1 );
488                                 }
489                         }
490
491                 /* debug level to log things to syslog */
492                 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
493                         if ( cargc < 2 ) {
494                                 Debug( LDAP_DEBUG_ANY,
495                     "%s: line %d: missing level in \"loglevel <level>\" line\n",
496                                     fname, lineno, 0 );
497                                 return( 1 );
498                         }
499                         ldap_syslog = atoi( cargv[1] );
500
501                 /* list of replicas of the data in this backend (master only) */
502                 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
503                         if ( cargc < 2 ) {
504                                 Debug( LDAP_DEBUG_ANY,
505             "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
506                                     fname, lineno, 0 );
507                                 return( 1 );
508                         }
509                         if ( be == NULL ) {
510                                 Debug( LDAP_DEBUG_ANY,
511 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
512                                     fname, lineno, 0 );
513                         } else {
514                                 for ( i = 1; i < cargc; i++ ) {
515                                         if ( strncasecmp( cargv[i], "host=", 5 )
516                                             == 0 ) {
517                                                 charray_add( &be->be_replica,
518                                                              cargv[i] + 5 );
519                                                 break;
520                                         }
521                                 }
522                                 if ( i == cargc ) {
523                                         Debug( LDAP_DEBUG_ANY,
524                     "%s: line %d: missing host in \"replica\" line (ignored)\n",
525                                             fname, lineno, 0 );
526                                 }
527                         }
528
529                 /* dn of master entity allowed to write to replica */
530                 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
531                         if ( cargc < 2 ) {
532                                 Debug( LDAP_DEBUG_ANY,
533                     "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
534                                     fname, lineno, 0 );
535                                 return( 1 );
536                         }
537                         if ( be == NULL ) {
538                                 Debug( LDAP_DEBUG_ANY,
539 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
540                                     fname, lineno, 0 );
541                         } else {
542                                 be->be_update_ndn = ch_strdup( cargv[1] );
543                                 if( dn_normalize_case( be->be_update_ndn ) == NULL ) {
544                                         Debug( LDAP_DEBUG_ANY,
545 "%s: line %d: updatedn DN is invalid\n",
546                                             fname, lineno, 0 );
547                                         return 1;
548                                 }
549                         }
550
551                 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
552                         if ( cargc < 2 ) {
553                                 Debug( LDAP_DEBUG_ANY,
554                     "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
555                                     fname, lineno, 0 );
556                                 return( 1 );
557                         }
558                         if ( be == NULL ) {
559                                 Debug( LDAP_DEBUG_ANY,
560 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
561                                     fname, lineno, 0 );
562                         } else if ( be->be_update_ndn == NULL ) {
563                                 Debug( LDAP_DEBUG_ANY,
564 "%s: line %d: updateref line must after updatedn (ignored)\n",
565                                     fname, lineno, 0 );
566                         } else {
567                                 vals[0]->bv_val = cargv[1];
568                                 vals[0]->bv_len = strlen( vals[0]->bv_val );
569                                 value_add( &be->be_update_refs, vals );
570                         }
571
572                 /* replication log file to which changes are appended */
573                 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
574                         if ( cargc < 2 ) {
575                                 Debug( LDAP_DEBUG_ANY,
576             "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
577                                     fname, lineno, 0 );
578                                 return( 1 );
579                         }
580                         if ( be ) {
581                                 be->be_replogfile = ch_strdup( cargv[1] );
582                         } else {
583                                 replogfile = ch_strdup( cargv[1] );
584                         }
585
586                 /* maintain lastmodified{by,time} attributes */
587                 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
588                         if ( cargc < 2 ) {
589                                 Debug( LDAP_DEBUG_ANY,
590             "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
591                                     fname, lineno, 0 );
592                                 return( 1 );
593                         }
594                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
595                                 if ( be )
596                                         be->be_lastmod = ON;
597                                 else
598                                         global_lastmod = ON;
599                         } else {
600                                 if ( be )
601                                         be->be_lastmod = OFF;
602                                 else
603                                         global_lastmod = OFF;
604                         }
605
606                 /* set idle timeout value */
607                 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
608                         int i;
609                         if ( cargc < 2 ) {
610                                 Debug( LDAP_DEBUG_ANY,
611             "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
612                                     fname, lineno, 0 );
613                                 return( 1 );
614                         }
615
616                         i = atoi( cargv[1] );
617
618                         if( i < 0 ) {
619                                 Debug( LDAP_DEBUG_ANY,
620             "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
621                                     fname, lineno, i );
622                                 return( 1 );
623                         }
624
625                         global_idletimeout = i;
626
627                 /* include another config file */
628                 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
629                         if ( cargc < 2 ) {
630                                 Debug( LDAP_DEBUG_ANY,
631     "%s: line %d: missing filename in \"include <filename>\" line\n",
632                                     fname, lineno, 0 );
633                                 return( 1 );
634                         }
635                         savefname = ch_strdup( cargv[1] );
636                         savelineno = lineno;
637
638                         if ( read_config( savefname ) != 0 ) {
639                                 return( 1 );
640                         }
641
642                         free( savefname );
643                         lineno = savelineno - 1;
644
645                 /* location of kerberos srvtab file */
646                 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
647                         if ( cargc < 2 ) {
648                                 Debug( LDAP_DEBUG_ANY,
649             "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
650                                     fname, lineno, 0 );
651                                 return( 1 );
652                         }
653                         ldap_srvtab = ch_strdup( cargv[1] );
654
655 #ifdef SLAPD_MODULES
656                 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
657                    if ( cargc < 2 ) {
658                       Debug( LDAP_DEBUG_ANY,
659                              "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
660                              fname, lineno, 0 );
661                       exit( EXIT_FAILURE );
662                    }
663                    if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
664                       Debug( LDAP_DEBUG_ANY,
665                              "%s: line %d: failed to load or initialize module %s\n",
666                              fname, lineno, cargv[1]);
667                       exit( EXIT_FAILURE );
668                    }
669                 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
670                    if ( cargc != 2 ) {
671                       Debug( LDAP_DEBUG_ANY,
672                              "%s: line %d: missing path in \"modulepath <path>\" line\n",
673                              fname, lineno, 0 );
674                       exit( EXIT_FAILURE );
675                    }
676                    if (module_path( cargv[1] )) {
677                       Debug( LDAP_DEBUG_ANY,
678                              "%s: line %d: failed to set module search path to %s\n",
679                              fname, lineno, cargv[1]);
680                       exit( EXIT_FAILURE );
681                    }
682                    
683 #endif /*SLAPD_MODULES*/
684
685 #ifdef HAVE_TLS
686                 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
687                         rc = ldap_pvt_tls_set_option( NULL,
688                                                       LDAP_OPT_X_TLS_PROTOCOL,
689                                                       cargv[1] );
690                         if ( rc )
691                                 return rc;
692
693                 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
694                         rc = ldap_pvt_tls_set_option( NULL,
695                                                       LDAP_OPT_X_TLS_CIPHER_SUITE,
696                                                       cargv[1] );
697                         if ( rc )
698                                 return rc;
699
700                 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
701                         rc = ldap_pvt_tls_set_option( NULL,
702                                                       LDAP_OPT_X_TLS_CERTFILE,
703                                                       cargv[1] );
704                         if ( rc )
705                                 return rc;
706
707                 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
708                         rc = ldap_pvt_tls_set_option( NULL,
709                                                       LDAP_OPT_X_TLS_KEYFILE,
710                                                       cargv[1] );
711                         if ( rc )
712                                 return rc;
713
714                 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
715                         rc = ldap_pvt_tls_set_option( NULL,
716                                                       LDAP_OPT_X_TLS_CACERTDIR,
717                                                       cargv[1] );
718                         if ( rc )
719                                 return rc;
720
721                 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
722                         rc = ldap_pvt_tls_set_option( NULL,
723                                                       LDAP_OPT_X_TLS_CACERTFILE,
724                                                       cargv[1] );
725                         if ( rc )
726                                 return rc;
727                 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
728                         rc = ldap_pvt_tls_set_option( NULL,
729                                                       LDAP_OPT_X_TLS_REQUIRE_CERT,
730                                                       cargv[1] );
731                         if ( rc )
732                                 return rc;
733
734 #endif
735
736                 /* pass anything else to the current backend info/db config routine */
737                 } else {
738                         if ( bi != NULL ) {
739                                 if ( bi->bi_config == 0 ) {
740                                         Debug( LDAP_DEBUG_ANY,
741 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
742                                                 fname, lineno, cargv[0] );
743                                 } else {
744                                         if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
745                                                 != 0 )
746                                         {
747                                                 return( 1 );
748                                         }
749                                 }
750                         } else if ( be != NULL ) {
751                                 if ( be->be_config == 0 ) {
752                                         Debug( LDAP_DEBUG_ANY,
753 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
754                                         fname, lineno, cargv[0] );
755                                 } else {
756                                         if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
757                                                 != 0 )
758                                         {
759                                                 return( 1 );
760                                         }
761                                 }
762                         } else {
763                                 Debug( LDAP_DEBUG_ANY,
764 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
765                                     fname, lineno, cargv[0] );
766                         }
767                 }
768                 free( saveline );
769         }
770         fclose( fp );
771         return( 0 );
772 }
773
774 static int
775 fp_parse_line(
776     char        *line,
777     int         *argcp,
778     char        **argv
779 )
780 {
781         char *  token;
782
783         *argcp = 0;
784         for ( token = strtok_quote( line, " \t" ); token != NULL;
785             token = strtok_quote( NULL, " \t" ) ) {
786                 if ( *argcp == MAXARGS ) {
787                         Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
788                             MAXARGS, 0, 0 );
789                         return( 1 );
790                 }
791                 argv[(*argcp)++] = token;
792         }
793         argv[*argcp] = NULL;
794         return 0;
795 }
796
797 static char *
798 strtok_quote( char *line, char *sep )
799 {
800         int             inquote;
801         char            *tmp;
802         static char     *next;
803
804         if ( line != NULL ) {
805                 next = line;
806         }
807         while ( *next && strchr( sep, *next ) ) {
808                 next++;
809         }
810
811         if ( *next == '\0' ) {
812                 next = NULL;
813                 return( NULL );
814         }
815         tmp = next;
816
817         for ( inquote = 0; *next; ) {
818                 switch ( *next ) {
819                 case '"':
820                         if ( inquote ) {
821                                 inquote = 0;
822                         } else {
823                                 inquote = 1;
824                         }
825                         SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
826                         break;
827
828                 case '\\':
829                         if ( next[1] )
830                                 SAFEMEMCPY( next,
831                                             next + 1, strlen( next + 1 ) + 1 );
832                         next++;         /* dont parse the escaped character */
833                         break;
834
835                 default:
836                         if ( ! inquote ) {
837                                 if ( strchr( sep, *next ) != NULL ) {
838                                         *next++ = '\0';
839                                         return( tmp );
840                                 }
841                         }
842                         next++;
843                         break;
844                 }
845         }
846
847         return( tmp );
848 }
849
850 static char     buf[BUFSIZ];
851 static char     *line;
852 static int      lmax, lcur;
853
854 #define CATLINE( buf )  { \
855         int     len; \
856         len = strlen( buf ); \
857         while ( lcur + len + 1 > lmax ) { \
858                 lmax += BUFSIZ; \
859                 line = (char *) ch_realloc( line, lmax ); \
860         } \
861         strcpy( line + lcur, buf ); \
862         lcur += len; \
863 }
864
865 static char *
866 fp_getline( FILE *fp, int *lineno )
867 {
868         char            *p;
869
870         lcur = 0;
871         CATLINE( buf );
872         (*lineno)++;
873
874         /* hack attack - keeps us from having to keep a stack of bufs... */
875         if ( strncasecmp( line, "include", 7 ) == 0 ) {
876                 buf[0] = '\0';
877                 return( line );
878         }
879
880         while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
881                 if ( (p = strchr( buf, '\n' )) != NULL ) {
882                         *p = '\0';
883                 }
884                 if ( ! isspace( (unsigned char) buf[0] ) ) {
885                         return( line );
886                 }
887
888                 CATLINE( buf );
889                 (*lineno)++;
890         }
891         buf[0] = '\0';
892
893         return( line[0] ? line : NULL );
894 }
895
896 static void
897 fp_getline_init( int *lineno )
898 {
899         *lineno = -1;
900         buf[0] = '\0';
901 }