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