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