]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
4912fe71a05d09db806968fc72c2f4f76ef1248b
[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 slap_access_t           global_default_access = ACL_READ;
31 int             global_readonly = 0;
32 char            *replogfile;
33 int             global_lastmod = ON;
34 int             global_idletimeout = 0;
35 char    *global_realm = NULL;
36 char            *ldap_srvtab = "";
37
38 char   *slapd_pid_file  = NULL;
39 char   *slapd_args_file = NULL;
40
41 static char     *fp_getline(FILE *fp, int *lineno);
42 static void     fp_getline_init(int *lineno);
43 static int      fp_parse_line(char *line, int *argcp, char **argv);
44
45 static char     *strtok_quote(char *line, char *sep);
46
47 int
48 read_config( const char *fname )
49 {
50         FILE    *fp;
51         char    *line, *savefname, *saveline;
52         int     cargc, savelineno;
53         char    *cargv[MAXARGS];
54         int     lineno, i;
55 #ifdef HAVE_TLS
56         int rc;
57 #endif
58         struct berval *vals[2];
59         struct berval val;
60
61         static BackendInfo *bi = NULL;
62         static BackendDB        *be = NULL;
63
64         vals[0] = &val;
65         vals[1] = NULL;
66
67         if ( (fp = fopen( fname, "r" )) == NULL ) {
68                 ldap_syslog = 1;
69                 Debug( LDAP_DEBUG_ANY,
70                     "could not open config file \"%s\" - absolute path?\n",
71                     fname, 0, 0 );
72                 perror( fname );
73                 return 1;
74         }
75
76         Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
77
78         if ( schema_init( ) != 0 ) {
79                 Debug( LDAP_DEBUG_ANY,
80                     "error initializing the schema\n",
81                     0, 0, 0 );
82                 return( 1 );
83         }
84
85         fp_getline_init( &lineno );
86
87         while ( (line = fp_getline( fp, &lineno )) != NULL ) {
88                 /* skip comments and blank lines */
89                 if ( line[0] == '#' || line[0] == '\0' ) {
90                         continue;
91                 }
92
93                 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
94
95                 /* fp_parse_line is destructive, we save a copy */
96                 saveline = ch_strdup( line );
97
98                 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
99                         return( 1 );
100                 }
101
102                 if ( cargc < 1 ) {
103                         Debug( LDAP_DEBUG_ANY,
104                             "%s: line %d: bad config line (ignored)\n",
105                             fname, lineno, 0 );
106                         continue;
107                 }
108
109                 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
110                         if ( cargc < 2 ) {
111                                 Debug( LDAP_DEBUG_ANY,
112                 "%s: line %d: missing type in \"backend <type>\" line\n",
113                                     fname, lineno, 0 );
114                                 return( 1 );
115                         }
116
117                         if( be != NULL ) {
118                                 Debug( LDAP_DEBUG_ANY,
119 "%s: line %d: backend line must appear before any database definition\n",
120                                     fname, lineno, 0 );
121                                 return( 1 );
122                         }
123
124                         bi = backend_info( cargv[1] );
125
126                         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                         slap_access_t access;
482
483                         if ( cargc < 2 ) {
484                                 Debug( LDAP_DEBUG_ANY,
485             "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
486                                     fname, lineno, 0 );
487                                 return( 1 );
488                         }
489
490                         access = str2access( cargv[1] );
491
492                         if ( access == ACL_INVALID_ACCESS ) {
493                                 Debug( LDAP_DEBUG_ANY,
494                                         "%s: line %d: bad access level \"%s\", "
495                                         "expecting none|auth|compare|search|read|write\n",
496                                     fname, lineno, cargv[1] );
497                                 return( 1 );
498                         }
499
500                         if ( be == NULL ) {
501                                 global_default_access = access;
502                         } else {
503                                 be->be_dfltaccess = access;
504                         }
505
506                 /* debug level to log things to syslog */
507                 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
508                         if ( cargc < 2 ) {
509                                 Debug( LDAP_DEBUG_ANY,
510                     "%s: line %d: missing level in \"loglevel <level>\" line\n",
511                                     fname, lineno, 0 );
512                                 return( 1 );
513                         }
514                         ldap_syslog = atoi( cargv[1] );
515
516                 /* list of replicas of the data in this backend (master only) */
517                 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
518                         if ( cargc < 2 ) {
519                                 Debug( LDAP_DEBUG_ANY,
520             "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
521                                     fname, lineno, 0 );
522                                 return( 1 );
523                         }
524                         if ( be == NULL ) {
525                                 Debug( LDAP_DEBUG_ANY,
526 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
527                                     fname, lineno, 0 );
528                         } else {
529                                 for ( i = 1; i < cargc; i++ ) {
530                                         if ( strncasecmp( cargv[i], "host=", 5 )
531                                             == 0 ) {
532                                                 charray_add( &be->be_replica,
533                                                              cargv[i] + 5 );
534                                                 break;
535                                         }
536                                 }
537                                 if ( i == cargc ) {
538                                         Debug( LDAP_DEBUG_ANY,
539                     "%s: line %d: missing host in \"replica\" line (ignored)\n",
540                                             fname, lineno, 0 );
541                                 }
542                         }
543
544                 /* dn of master entity allowed to write to replica */
545                 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
546                         if ( cargc < 2 ) {
547                                 Debug( LDAP_DEBUG_ANY,
548                     "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
549                                     fname, lineno, 0 );
550                                 return( 1 );
551                         }
552                         if ( be == NULL ) {
553                                 Debug( LDAP_DEBUG_ANY,
554 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
555                                     fname, lineno, 0 );
556                         } else {
557                                 be->be_update_ndn = ch_strdup( cargv[1] );
558                                 if( dn_normalize( be->be_update_ndn ) == NULL ) {
559                                         Debug( LDAP_DEBUG_ANY,
560 "%s: line %d: updatedn DN is invalid\n",
561                                             fname, lineno, 0 );
562                                         return 1;
563                                 }
564                         }
565
566                 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
567                         if ( cargc < 2 ) {
568                                 Debug( LDAP_DEBUG_ANY,
569                     "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
570                                     fname, lineno, 0 );
571                                 return( 1 );
572                         }
573                         if ( be == NULL ) {
574                                 Debug( LDAP_DEBUG_ANY,
575 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
576                                     fname, lineno, 0 );
577                         } else if ( be->be_update_ndn == NULL ) {
578                                 Debug( LDAP_DEBUG_ANY,
579 "%s: line %d: updateref line must after updatedn (ignored)\n",
580                                     fname, lineno, 0 );
581                         } else {
582                                 vals[0]->bv_val = cargv[1];
583                                 vals[0]->bv_len = strlen( vals[0]->bv_val );
584                                 value_add( &be->be_update_refs, vals );
585                         }
586
587                 /* replication log file to which changes are appended */
588                 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
589                         if ( cargc < 2 ) {
590                                 Debug( LDAP_DEBUG_ANY,
591             "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
592                                     fname, lineno, 0 );
593                                 return( 1 );
594                         }
595                         if ( be ) {
596                                 be->be_replogfile = ch_strdup( cargv[1] );
597                         } else {
598                                 replogfile = ch_strdup( cargv[1] );
599                         }
600
601                 /* maintain lastmodified{by,time} attributes */
602                 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
603                         if ( cargc < 2 ) {
604                                 Debug( LDAP_DEBUG_ANY,
605             "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
606                                     fname, lineno, 0 );
607                                 return( 1 );
608                         }
609                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
610                                 if ( be )
611                                         be->be_lastmod = ON;
612                                 else
613                                         global_lastmod = ON;
614                         } else {
615                                 if ( be )
616                                         be->be_lastmod = OFF;
617                                 else
618                                         global_lastmod = OFF;
619                         }
620
621                 /* set idle timeout value */
622                 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
623                         int i;
624                         if ( cargc < 2 ) {
625                                 Debug( LDAP_DEBUG_ANY,
626             "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
627                                     fname, lineno, 0 );
628                                 return( 1 );
629                         }
630
631                         i = atoi( cargv[1] );
632
633                         if( i < 0 ) {
634                                 Debug( LDAP_DEBUG_ANY,
635             "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
636                                     fname, lineno, i );
637                                 return( 1 );
638                         }
639
640                         global_idletimeout = i;
641
642                 /* include another config file */
643                 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
644                         if ( cargc < 2 ) {
645                                 Debug( LDAP_DEBUG_ANY,
646     "%s: line %d: missing filename in \"include <filename>\" line\n",
647                                     fname, lineno, 0 );
648                                 return( 1 );
649                         }
650                         savefname = ch_strdup( cargv[1] );
651                         savelineno = lineno;
652
653                         if ( read_config( savefname ) != 0 ) {
654                                 return( 1 );
655                         }
656
657                         free( savefname );
658                         lineno = savelineno - 1;
659
660                 /* location of kerberos srvtab file */
661                 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
662                         if ( cargc < 2 ) {
663                                 Debug( LDAP_DEBUG_ANY,
664             "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
665                                     fname, lineno, 0 );
666                                 return( 1 );
667                         }
668                         ldap_srvtab = ch_strdup( cargv[1] );
669
670 #ifdef SLAPD_MODULES
671                 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
672                    if ( cargc < 2 ) {
673                       Debug( LDAP_DEBUG_ANY,
674                              "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
675                              fname, lineno, 0 );
676                       exit( EXIT_FAILURE );
677                    }
678                    if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
679                       Debug( LDAP_DEBUG_ANY,
680                              "%s: line %d: failed to load or initialize module %s\n",
681                              fname, lineno, cargv[1]);
682                       exit( EXIT_FAILURE );
683                    }
684                 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
685                    if ( cargc != 2 ) {
686                       Debug( LDAP_DEBUG_ANY,
687                              "%s: line %d: missing path in \"modulepath <path>\" line\n",
688                              fname, lineno, 0 );
689                       exit( EXIT_FAILURE );
690                    }
691                    if (module_path( cargv[1] )) {
692                       Debug( LDAP_DEBUG_ANY,
693                              "%s: line %d: failed to set module search path to %s\n",
694                              fname, lineno, cargv[1]);
695                       exit( EXIT_FAILURE );
696                    }
697                    
698 #endif /*SLAPD_MODULES*/
699
700 #ifdef HAVE_TLS
701                 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
702                         rc = ldap_pvt_tls_set_option( NULL,
703                                                       LDAP_OPT_X_TLS_PROTOCOL,
704                                                       cargv[1] );
705                         if ( rc )
706                                 return rc;
707
708                 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
709                         rc = ldap_pvt_tls_set_option( NULL,
710                                                       LDAP_OPT_X_TLS_CIPHER_SUITE,
711                                                       cargv[1] );
712                         if ( rc )
713                                 return rc;
714
715                 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
716                         rc = ldap_pvt_tls_set_option( NULL,
717                                                       LDAP_OPT_X_TLS_CERTFILE,
718                                                       cargv[1] );
719                         if ( rc )
720                                 return rc;
721
722                 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
723                         rc = ldap_pvt_tls_set_option( NULL,
724                                                       LDAP_OPT_X_TLS_KEYFILE,
725                                                       cargv[1] );
726                         if ( rc )
727                                 return rc;
728
729                 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
730                         rc = ldap_pvt_tls_set_option( NULL,
731                                                       LDAP_OPT_X_TLS_CACERTDIR,
732                                                       cargv[1] );
733                         if ( rc )
734                                 return rc;
735
736                 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
737                         rc = ldap_pvt_tls_set_option( NULL,
738                                                       LDAP_OPT_X_TLS_CACERTFILE,
739                                                       cargv[1] );
740                         if ( rc )
741                                 return rc;
742                 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
743                         rc = ldap_pvt_tls_set_option( NULL,
744                                                       LDAP_OPT_X_TLS_REQUIRE_CERT,
745                                                       cargv[1] );
746                         if ( rc )
747                                 return rc;
748
749 #endif
750
751 #ifdef SLAPD_EXTERNAL_EXTENSIONS
752                 } else if ( !strcasecmp( cargv[0], "extension" ) ) {
753                         if ( cargc < 2 ) {
754                                 Debug( LDAP_DEBUG_ANY,
755                                                 "%s: line %d: missing oid in \"extension <oid> <libpath>\" line\n",
756                                                 fname, lineno, 0 );
757                         } else if ( cargc < 3 ) {
758                                 Debug( LDAP_DEBUG_ANY,
759                                                 "%s: line %d: missing libpath in \"extension <oid> <libpath>\" line\n",
760                                                 fname, lineno, 0 );
761                         } else {
762                                 rc = load_extension(cargv[1], cargv[2], cargc - 3, (cargc > 3) ? cargv + 3 : NULL);
763                                 if (rc != 0) {
764                                         Debug( LDAP_DEBUG_ANY,
765                                                         "%s: line %d: failed to load or initialize extension library %s\n",
766                                                         fname, lineno, cargv[2]);
767                                         return rc;
768                                 }
769                         }
770 #endif
771
772                 /* pass anything else to the current backend info/db config routine */
773                 } else {
774                         if ( bi != NULL ) {
775                                 if ( bi->bi_config == 0 ) {
776                                         Debug( LDAP_DEBUG_ANY,
777 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
778                                                 fname, lineno, cargv[0] );
779                                 } else {
780                                         if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
781                                                 != 0 )
782                                         {
783                                                 return( 1 );
784                                         }
785                                 }
786                         } else if ( be != NULL ) {
787                                 if ( be->be_config == 0 ) {
788                                         Debug( LDAP_DEBUG_ANY,
789 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
790                                         fname, lineno, cargv[0] );
791                                 } else {
792                                         if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
793                                                 != 0 )
794                                         {
795                                                 return( 1 );
796                                         }
797                                 }
798                         } else {
799                                 Debug( LDAP_DEBUG_ANY,
800 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
801                                     fname, lineno, cargv[0] );
802                         }
803                 }
804                 free( saveline );
805         }
806         fclose( fp );
807         return( 0 );
808 }
809
810 static int
811 fp_parse_line(
812     char        *line,
813     int         *argcp,
814     char        **argv
815 )
816 {
817         char *  token;
818
819         *argcp = 0;
820         for ( token = strtok_quote( line, " \t" ); token != NULL;
821             token = strtok_quote( NULL, " \t" ) ) {
822                 if ( *argcp == MAXARGS ) {
823                         Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
824                             MAXARGS, 0, 0 );
825                         return( 1 );
826                 }
827                 argv[(*argcp)++] = token;
828         }
829         argv[*argcp] = NULL;
830         return 0;
831 }
832
833 static char *
834 strtok_quote( char *line, char *sep )
835 {
836         int             inquote;
837         char            *tmp;
838         static char     *next;
839
840         if ( line != NULL ) {
841                 next = line;
842         }
843         while ( *next && strchr( sep, *next ) ) {
844                 next++;
845         }
846
847         if ( *next == '\0' ) {
848                 next = NULL;
849                 return( NULL );
850         }
851         tmp = next;
852
853         for ( inquote = 0; *next; ) {
854                 switch ( *next ) {
855                 case '"':
856                         if ( inquote ) {
857                                 inquote = 0;
858                         } else {
859                                 inquote = 1;
860                         }
861                         SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
862                         break;
863
864                 case '\\':
865                         if ( next[1] )
866                                 SAFEMEMCPY( next,
867                                             next + 1, strlen( next + 1 ) + 1 );
868                         next++;         /* dont parse the escaped character */
869                         break;
870
871                 default:
872                         if ( ! inquote ) {
873                                 if ( strchr( sep, *next ) != NULL ) {
874                                         *next++ = '\0';
875                                         return( tmp );
876                                 }
877                         }
878                         next++;
879                         break;
880                 }
881         }
882
883         return( tmp );
884 }
885
886 static char     buf[BUFSIZ];
887 static char     *line;
888 static int      lmax, lcur;
889
890 #define CATLINE( buf )  { \
891         int     len; \
892         len = strlen( buf ); \
893         while ( lcur + len + 1 > lmax ) { \
894                 lmax += BUFSIZ; \
895                 line = (char *) ch_realloc( line, lmax ); \
896         } \
897         strcpy( line + lcur, buf ); \
898         lcur += len; \
899 }
900
901 static char *
902 fp_getline( FILE *fp, int *lineno )
903 {
904         char            *p;
905
906         lcur = 0;
907         CATLINE( buf );
908         (*lineno)++;
909
910         /* hack attack - keeps us from having to keep a stack of bufs... */
911         if ( strncasecmp( line, "include", 7 ) == 0 ) {
912                 buf[0] = '\0';
913                 return( line );
914         }
915
916         while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
917                 if ( (p = strchr( buf, '\n' )) != NULL ) {
918                         *p = '\0';
919                 }
920                 if ( ! isspace( (unsigned char) buf[0] ) ) {
921                         return( line );
922                 }
923
924                 CATLINE( buf );
925                 (*lineno)++;
926         }
927         buf[0] = '\0';
928
929         return( line[0] ? line : NULL );
930 }
931
932 static void
933 fp_getline_init( int *lineno )
934 {
935         *lineno = -1;
936         buf[0] = '\0';
937 }