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