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