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