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