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