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