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