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