]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
Use NULL pointer to indicate values need not be freed instead of
[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 "ldap_defaults.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 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                                 Debug( LDAP_DEBUG_ANY,
370 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
371                                     fname, lineno, 0 );
372                         } else {
373                                 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
374                                         be->be_readonly = 1;
375                                 } else {
376                                         be->be_readonly = 0;
377                                 }
378                         }
379
380                 /* where to send clients when we don't hold it */
381                 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
382                         if ( cargc < 2 ) {
383                                 Debug( LDAP_DEBUG_ANY,
384                     "%s: line %d: missing URL in \"referral <URL>\" line\n",
385                                     fname, lineno, 0 );
386                                 return( 1 );
387                         }
388
389                         vals[0]->bv_val = cargv[1];
390                         vals[0]->bv_len = strlen( vals[0]->bv_val );
391                         value_add( &default_referral, vals );
392
393                 /* specify locale */
394                 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
395 #ifdef HAVE_LOCALE_H
396                         char *locale;
397                         if ( cargc < 2 ) {
398                                 Debug( LDAP_DEBUG_ANY,
399         "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
400                                        fname, lineno, 0 );
401                                 return( 1 );
402                         }
403
404                         locale = (strcasecmp(   cargv[1], "on"  ) == 0 ? ""
405                                   : strcasecmp( cargv[1], "off" ) == 0 ? "C"
406                                   : ch_strdup( cargv[1] )                    );
407
408                         if ( setlocale( LC_CTYPE, locale ) == 0 ) {
409                                 Debug( LDAP_DEBUG_ANY,
410                                        (*locale
411                                         ? "%s: line %d: bad locale \"%s\"\n"
412                                         : "%s: line %d: bad locale\n"),
413                                        fname, lineno, locale );
414                                 return( 1 );
415                         }
416 #else
417                         Debug( LDAP_DEBUG_ANY,
418                                "%s: line %d: \"locale\" unsupported\n",
419                                fname, lineno, 0 );
420                         return( 1 );
421 #endif
422                 /* specify an Object Identifier macro */
423                 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
424                         parse_oidm( fname, lineno, cargc, cargv );
425                 /* specify an objectclass */
426                 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
427                         if ( *cargv[1] == '(' ) {
428                                 char * p;
429                                 p = strchr(saveline,'(');
430                                 parse_oc( fname, lineno, p, cargv );
431                         } else {
432                                 parse_oc_old( be, fname, lineno, cargc, cargv );
433                         }
434
435                 /* specify an attribute */
436                 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
437                         if ( *cargv[1] == '(' ) {
438                                 char * p;
439                                 p = strchr(saveline,'(');
440                                 parse_at( fname, lineno, p, cargv );
441                         } else {
442                                 attr_syntax_config( fname, lineno, cargc - 1,
443                                     &cargv[1] );
444                         }
445
446                 /* turn on/off schema checking */
447                 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
448                         if ( cargc < 2 ) {
449                                 Debug( LDAP_DEBUG_ANY,
450     "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
451                                     fname, lineno, 0 );
452                                 return( 1 );
453                         }
454                         if ( strcasecmp( cargv[1], "off" ) == 0 ) {
455                                 global_schemacheck = 0;
456                         } else {
457                                 global_schemacheck = 1;
458                         }
459
460                 /* specify access control info */
461                 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
462                         parse_acl( be, fname, lineno, cargc, cargv );
463
464                 /* specify default access control info */
465                 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
466                         if ( cargc < 2 ) {
467                                 Debug( LDAP_DEBUG_ANY,
468             "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
469                                     fname, lineno, 0 );
470                                 return( 1 );
471                         }
472                         if ( be == NULL ) {
473                                 if ( ACL_IS_INVALID(ACL_SET(global_default_access,
474                                                 str2access(cargv[1]))) )
475                                 {
476                                         Debug( LDAP_DEBUG_ANY,
477 "%s: line %d: bad access \"%s\" expecting [self]{none|auth|compare|search|read|write}\n",
478                                             fname, lineno, cargv[1] );
479                                         return( 1 );
480                                 }
481                         } else {
482                                 if ( ACL_IS_INVALID(ACL_SET(be->be_dfltaccess,
483                                                 str2access(cargv[1]))) )
484                                 {
485                                         Debug( LDAP_DEBUG_ANY,
486                                                 "%s: line %d: bad access \"%s\", "
487                                                 "expecting [self]{none|auth|compare|search|read|write}\n",
488                                             fname, lineno, cargv[1] );
489                                         return( 1 );
490                                 }
491                         }
492
493                 /* debug level to log things to syslog */
494                 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
495                         if ( cargc < 2 ) {
496                                 Debug( LDAP_DEBUG_ANY,
497                     "%s: line %d: missing level in \"loglevel <level>\" line\n",
498                                     fname, lineno, 0 );
499                                 return( 1 );
500                         }
501                         ldap_syslog = atoi( cargv[1] );
502
503                 /* list of replicas of the data in this backend (master only) */
504                 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
505                         if ( cargc < 2 ) {
506                                 Debug( LDAP_DEBUG_ANY,
507             "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
508                                     fname, lineno, 0 );
509                                 return( 1 );
510                         }
511                         if ( be == NULL ) {
512                                 Debug( LDAP_DEBUG_ANY,
513 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
514                                     fname, lineno, 0 );
515                         } else {
516                                 for ( i = 1; i < cargc; i++ ) {
517                                         if ( strncasecmp( cargv[i], "host=", 5 )
518                                             == 0 ) {
519                                                 charray_add( &be->be_replica,
520                                                              cargv[i] + 5 );
521                                                 break;
522                                         }
523                                 }
524                                 if ( i == cargc ) {
525                                         Debug( LDAP_DEBUG_ANY,
526                     "%s: line %d: missing host in \"replica\" line (ignored)\n",
527                                             fname, lineno, 0 );
528                                 }
529                         }
530
531                 /* dn of master entity allowed to write to replica */
532                 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
533                         if ( cargc < 2 ) {
534                                 Debug( LDAP_DEBUG_ANY,
535                     "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
536                                     fname, lineno, 0 );
537                                 return( 1 );
538                         }
539                         if ( be == NULL ) {
540                                 Debug( LDAP_DEBUG_ANY,
541 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
542                                     fname, lineno, 0 );
543                         } else {
544                                 be->be_update_ndn = ch_strdup( cargv[1] );
545                                 if( dn_normalize_case( be->be_update_ndn ) == NULL ) {
546                                         Debug( LDAP_DEBUG_ANY,
547 "%s: line %d: updatedn DN is invalid\n",
548                                             fname, lineno, 0 );
549                                         return 1;
550                                 }
551                         }
552
553                 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
554                         if ( cargc < 2 ) {
555                                 Debug( LDAP_DEBUG_ANY,
556                     "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
557                                     fname, lineno, 0 );
558                                 return( 1 );
559                         }
560                         if ( be == NULL ) {
561                                 Debug( LDAP_DEBUG_ANY,
562 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
563                                     fname, lineno, 0 );
564                         } else if ( be->be_update_ndn == NULL ) {
565                                 Debug( LDAP_DEBUG_ANY,
566 "%s: line %d: updateref line must after updatedn (ignored)\n",
567                                     fname, lineno, 0 );
568                         } else {
569                                 vals[0]->bv_val = cargv[1];
570                                 vals[0]->bv_len = strlen( vals[0]->bv_val );
571                                 value_add( &be->be_update_refs, vals );
572                         }
573
574                 /* replication log file to which changes are appended */
575                 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
576                         if ( cargc < 2 ) {
577                                 Debug( LDAP_DEBUG_ANY,
578             "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
579                                     fname, lineno, 0 );
580                                 return( 1 );
581                         }
582                         if ( be ) {
583                                 be->be_replogfile = ch_strdup( cargv[1] );
584                         } else {
585                                 replogfile = ch_strdup( cargv[1] );
586                         }
587
588                 /* maintain lastmodified{by,time} attributes */
589                 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
590                         if ( cargc < 2 ) {
591                                 Debug( LDAP_DEBUG_ANY,
592             "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
593                                     fname, lineno, 0 );
594                                 return( 1 );
595                         }
596                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
597                                 if ( be )
598                                         be->be_lastmod = ON;
599                                 else
600                                         global_lastmod = ON;
601                         } else {
602                                 if ( be )
603                                         be->be_lastmod = OFF;
604                                 else
605                                         global_lastmod = OFF;
606                         }
607
608                 /* set idle timeout value */
609                 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
610                         int i;
611                         if ( cargc < 2 ) {
612                                 Debug( LDAP_DEBUG_ANY,
613             "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
614                                     fname, lineno, 0 );
615                                 return( 1 );
616                         }
617
618                         i = atoi( cargv[1] );
619
620                         if( i < 0 ) {
621                                 Debug( LDAP_DEBUG_ANY,
622             "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
623                                     fname, lineno, i );
624                                 return( 1 );
625                         }
626
627                         global_idletimeout = i;
628
629                 /* include another config file */
630                 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
631                         if ( cargc < 2 ) {
632                                 Debug( LDAP_DEBUG_ANY,
633     "%s: line %d: missing filename in \"include <filename>\" line\n",
634                                     fname, lineno, 0 );
635                                 return( 1 );
636                         }
637                         savefname = ch_strdup( cargv[1] );
638                         savelineno = lineno;
639
640                         if ( read_config( savefname ) != 0 ) {
641                                 return( 1 );
642                         }
643
644                         free( savefname );
645                         lineno = savelineno - 1;
646
647                 /* location of kerberos srvtab file */
648                 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
649                         if ( cargc < 2 ) {
650                                 Debug( LDAP_DEBUG_ANY,
651             "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
652                                     fname, lineno, 0 );
653                                 return( 1 );
654                         }
655                         ldap_srvtab = ch_strdup( cargv[1] );
656
657 #ifdef SLAPD_MODULES
658                 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
659                    if ( cargc < 2 ) {
660                       Debug( LDAP_DEBUG_ANY,
661                              "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
662                              fname, lineno, 0 );
663                       exit( EXIT_FAILURE );
664                    }
665                    if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
666                       Debug( LDAP_DEBUG_ANY,
667                              "%s: line %d: failed to load or initialize module %s\n",
668                              fname, lineno, cargv[1]);
669                       exit( EXIT_FAILURE );
670                    }
671                 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
672                    if ( cargc != 2 ) {
673                       Debug( LDAP_DEBUG_ANY,
674                              "%s: line %d: missing path in \"modulepath <path>\" line\n",
675                              fname, lineno, 0 );
676                       exit( EXIT_FAILURE );
677                    }
678                    if (module_path( cargv[1] )) {
679                       Debug( LDAP_DEBUG_ANY,
680                              "%s: line %d: failed to set module search path to %s\n",
681                              fname, lineno, cargv[1]);
682                       exit( EXIT_FAILURE );
683                    }
684                    
685 #endif /*SLAPD_MODULES*/
686
687 #ifdef HAVE_TLS
688                 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
689                         rc = ldap_pvt_tls_set_option( NULL,
690                                                       LDAP_OPT_X_TLS_PROTOCOL,
691                                                       cargv[1] );
692                         if ( rc )
693                                 return rc;
694
695                 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
696                         rc = ldap_pvt_tls_set_option( NULL,
697                                                       LDAP_OPT_X_TLS_CIPHER_SUITE,
698                                                       cargv[1] );
699                         if ( rc )
700                                 return rc;
701
702                 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
703                         rc = ldap_pvt_tls_set_option( NULL,
704                                                       LDAP_OPT_X_TLS_CERTFILE,
705                                                       cargv[1] );
706                         if ( rc )
707                                 return rc;
708
709                 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
710                         rc = ldap_pvt_tls_set_option( NULL,
711                                                       LDAP_OPT_X_TLS_KEYFILE,
712                                                       cargv[1] );
713                         if ( rc )
714                                 return rc;
715
716                 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
717                         rc = ldap_pvt_tls_set_option( NULL,
718                                                       LDAP_OPT_X_TLS_CACERTDIR,
719                                                       cargv[1] );
720                         if ( rc )
721                                 return rc;
722
723                 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
724                         rc = ldap_pvt_tls_set_option( NULL,
725                                                       LDAP_OPT_X_TLS_CACERTFILE,
726                                                       cargv[1] );
727                         if ( rc )
728                                 return rc;
729                 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
730                         rc = ldap_pvt_tls_set_option( NULL,
731                                                       LDAP_OPT_X_TLS_REQUIRE_CERT,
732                                                       cargv[1] );
733                         if ( rc )
734                                 return rc;
735
736 #endif
737
738                 /* pass anything else to the current backend info/db config routine */
739                 } else {
740                         if ( bi != NULL ) {
741                                 if ( bi->bi_config == 0 ) {
742                                         Debug( LDAP_DEBUG_ANY,
743 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
744                                                 fname, lineno, cargv[0] );
745                                 } else {
746                                         if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
747                                                 != 0 )
748                                         {
749                                                 return( 1 );
750                                         }
751                                 }
752                         } else if ( be != NULL ) {
753                                 if ( be->be_config == 0 ) {
754                                         Debug( LDAP_DEBUG_ANY,
755 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
756                                         fname, lineno, cargv[0] );
757                                 } else {
758                                         if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
759                                                 != 0 )
760                                         {
761                                                 return( 1 );
762                                         }
763                                 }
764                         } else {
765                                 Debug( LDAP_DEBUG_ANY,
766 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
767                                     fname, lineno, cargv[0] );
768                         }
769                 }
770                 free( saveline );
771         }
772         fclose( fp );
773         return( 0 );
774 }
775
776 static int
777 fp_parse_line(
778     char        *line,
779     int         *argcp,
780     char        **argv
781 )
782 {
783         char *  token;
784
785         *argcp = 0;
786         for ( token = strtok_quote( line, " \t" ); token != NULL;
787             token = strtok_quote( NULL, " \t" ) ) {
788                 if ( *argcp == MAXARGS ) {
789                         Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
790                             MAXARGS, 0, 0 );
791                         return( 1 );
792                 }
793                 argv[(*argcp)++] = token;
794         }
795         argv[*argcp] = NULL;
796         return 0;
797 }
798
799 static char *
800 strtok_quote( char *line, char *sep )
801 {
802         int             inquote;
803         char            *tmp;
804         static char     *next;
805
806         if ( line != NULL ) {
807                 next = line;
808         }
809         while ( *next && strchr( sep, *next ) ) {
810                 next++;
811         }
812
813         if ( *next == '\0' ) {
814                 next = NULL;
815                 return( NULL );
816         }
817         tmp = next;
818
819         for ( inquote = 0; *next; ) {
820                 switch ( *next ) {
821                 case '"':
822                         if ( inquote ) {
823                                 inquote = 0;
824                         } else {
825                                 inquote = 1;
826                         }
827                         SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
828                         break;
829
830                 case '\\':
831                         if ( next[1] )
832                                 SAFEMEMCPY( next,
833                                             next + 1, strlen( next + 1 ) + 1 );
834                         next++;         /* dont parse the escaped character */
835                         break;
836
837                 default:
838                         if ( ! inquote ) {
839                                 if ( strchr( sep, *next ) != NULL ) {
840                                         *next++ = '\0';
841                                         return( tmp );
842                                 }
843                         }
844                         next++;
845                         break;
846                 }
847         }
848
849         return( tmp );
850 }
851
852 static char     buf[BUFSIZ];
853 static char     *line;
854 static int      lmax, lcur;
855
856 #define CATLINE( buf )  { \
857         int     len; \
858         len = strlen( buf ); \
859         while ( lcur + len + 1 > lmax ) { \
860                 lmax += BUFSIZ; \
861                 line = (char *) ch_realloc( line, lmax ); \
862         } \
863         strcpy( line + lcur, buf ); \
864         lcur += len; \
865 }
866
867 static char *
868 fp_getline( FILE *fp, int *lineno )
869 {
870         char            *p;
871
872         lcur = 0;
873         CATLINE( buf );
874         (*lineno)++;
875
876         /* hack attack - keeps us from having to keep a stack of bufs... */
877         if ( strncasecmp( line, "include", 7 ) == 0 ) {
878                 buf[0] = '\0';
879                 return( line );
880         }
881
882         while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
883                 if ( (p = strchr( buf, '\n' )) != NULL ) {
884                         *p = '\0';
885                 }
886                 if ( ! isspace( (unsigned char) buf[0] ) ) {
887                         return( line );
888                 }
889
890                 CATLINE( buf );
891                 (*lineno)++;
892         }
893         buf[0] = '\0';
894
895         return( line[0] ? line : NULL );
896 }
897
898 static void
899 fp_getline_init( int *lineno )
900 {
901         *lineno = -1;
902         buf[0] = '\0';
903 }