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