]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
56246ad5acafe6406b5e06d12081380c4e2457a5
[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 200
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 slap_mask_t             global_restrictops = 0;
29 slap_mask_t             global_allows = 0;
30 slap_mask_t             global_disallows = 0;
31 slap_mask_t             global_requires = 0;
32 slap_ssf_set_t  global_ssf_set;
33 char            *replogfile;
34 int             global_lastmod = ON;
35 int             global_idletimeout = 0;
36 char    *global_host = NULL;
37 char    *global_realm = NULL;
38 char    *global_ucdata_path = NULL;
39 char            *ldap_srvtab = "";
40 char            *default_passwd_hash;
41 char            *default_search_base = NULL;
42 char            *default_search_nbase = NULL;
43
44 char   *slapd_pid_file  = NULL;
45 char   *slapd_args_file = NULL;
46
47 int nSaslRegexp = 0;
48 SaslRegexp_t *SaslRegexp = NULL;
49
50 static char     *fp_getline(FILE *fp, int *lineno);
51 static void     fp_getline_init(int *lineno);
52 static int      fp_parse_line(char *line, int *argcp, char **argv);
53
54 static char     *strtok_quote(char *line, char *sep);
55
56 int
57 read_config( const char *fname )
58 {
59         FILE    *fp;
60         char    *line, *savefname, *saveline;
61         int     cargc, savelineno;
62         char    *cargv[MAXARGS+1];
63         int     lineno, i;
64 #ifdef HAVE_TLS
65         int rc;
66 #endif
67         struct berval *vals[2];
68         struct berval val;
69
70         static BackendInfo *bi = NULL;
71         static BackendDB        *be = NULL;
72
73         vals[0] = &val;
74         vals[1] = NULL;
75
76         if ( (fp = fopen( fname, "r" )) == NULL ) {
77                 ldap_syslog = 1;
78                 Debug( LDAP_DEBUG_ANY,
79                     "could not open config file \"%s\" - absolute path?\n",
80                     fname, 0, 0 );
81                 perror( fname );
82                 return 1;
83         }
84
85         Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
86
87         fp_getline_init( &lineno );
88
89         while ( (line = fp_getline( fp, &lineno )) != NULL ) {
90                 /* skip comments and blank lines */
91                 if ( line[0] == '#' || line[0] == '\0' ) {
92                         continue;
93                 }
94
95                 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
96
97                 /* fp_parse_line is destructive, we save a copy */
98                 saveline = ch_strdup( line );
99
100                 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
101                         return( 1 );
102                 }
103
104                 if ( cargc < 1 ) {
105                         Debug( LDAP_DEBUG_ANY,
106                             "%s: line %d: bad config line (ignored)\n",
107                             fname, lineno, 0 );
108                         continue;
109                 }
110
111                 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
112                         if ( cargc < 2 ) {
113                                 Debug( LDAP_DEBUG_ANY,
114                 "%s: line %d: missing type in \"backend <type>\" line\n",
115                                     fname, lineno, 0 );
116                                 return( 1 );
117                         }
118
119                         if( be != NULL ) {
120                                 Debug( LDAP_DEBUG_ANY,
121 "%s: line %d: backend line must appear before any database definition\n",
122                                     fname, lineno, 0 );
123                                 return( 1 );
124                         }
125
126                         bi = backend_info( cargv[1] );
127
128                         if( bi == NULL ) {
129                                 Debug( LDAP_DEBUG_ANY,
130                                         "backend %s initialization failed.\n",
131                                     cargv[1], 0, 0 );
132                                 return( 1 );
133                         }
134
135                 /* start of a new database definition */
136                 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
137                         if ( cargc < 2 ) {
138                                 Debug( LDAP_DEBUG_ANY,
139                 "%s: line %d: missing type in \"database <type>\" line\n",
140                                     fname, lineno, 0 );
141                                 return( 1 );
142                         }
143
144                         bi = NULL;
145                         be = backend_db_init( cargv[1] );
146
147                         if( be == NULL ) {
148                                 Debug( LDAP_DEBUG_ANY,
149                                         "database %s initialization failed.\n",
150                                     cargv[1], 0, 0 );
151                                 return( 1 );
152                         }
153
154                 /* set thread concurrency */
155                 } else if ( strcasecmp( cargv[0], "concurrency" ) == 0 ) {
156                         int c;
157                         if ( cargc < 2 ) {
158                                 Debug( LDAP_DEBUG_ANY,
159             "%s: line %d: missing level in \"concurrency <level>\" line\n",
160                                     fname, lineno, 0 );
161                                 return( 1 );
162                         }
163
164                         c = atoi( cargv[1] );
165
166                         if( c < 1 ) {
167                                 Debug( LDAP_DEBUG_ANY,
168             "%s: line %d: invalid level (%d) in \"concurrency <level>\" line\n",
169                                     fname, lineno, c );
170                                 return( 1 );
171                         }
172
173                         ldap_pvt_thread_set_concurrency( c );
174
175                 /* default search base */
176                 } else if ( strcasecmp( cargv[0], "defaultSearchBase" ) == 0 ) {
177                         if ( cargc < 2 ) {
178                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
179                                         "missing dn in \"defaultSearchBase <dn>\" line\n",
180                                         fname, lineno, 0 );
181                                 return 1;
182
183                         } else if ( cargc > 2 ) {
184                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
185                                         "extra cruft after <dn> in \"defaultSearchBase %s\", "
186                                         "line (ignored)\n",
187                                         fname, lineno, cargv[1] );
188                         }
189
190                         if ( bi != NULL || be != NULL ) {
191                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
192                                         "defaultSearchBaase line must appear prior to "
193                                         "any backend or database definition\n",
194                                     fname, lineno, 0 );
195                                 return 1;
196                         }
197
198                         if ( default_search_nbase != NULL ) {
199                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
200                                         "default search base \"%s\" already defined "
201                                         "(discarding old)\n",
202                                         fname, lineno, default_search_base );
203                                 free( default_search_base );
204                                 free( default_search_nbase );
205                         }
206
207                         default_search_base = ch_strdup( cargv[1] );
208                         default_search_nbase = ch_strdup( cargv[1] );
209
210                         if( dn_normalize( default_search_nbase ) == NULL ) {
211                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
212                                         "invalid default search base \"%s\"\n",
213                                         fname, lineno, default_search_base );
214                                 return 1;
215                         }
216                
217                 /* set maximum threads in thread pool */
218                 } else if ( strcasecmp( cargv[0], "threads" ) == 0 ) {
219                         int c;
220                         if ( cargc < 2 ) {
221                                 Debug( LDAP_DEBUG_ANY,
222             "%s: line %d: missing count in \"threads <count>\" line\n",
223                                     fname, lineno, 0 );
224                                 return( 1 );
225                         }
226
227                         c = atoi( cargv[1] );
228
229                         if( c < 0 ) {
230                                 Debug( LDAP_DEBUG_ANY,
231             "%s: line %d: invalid level (%d) in \"threads <count>\" line\n",
232                                     fname, lineno, c );
233                                 return( 1 );
234                         }
235
236                         ldap_pvt_thread_pool_maxthreads( &connection_pool, c );
237
238                 /* get pid file name */
239                 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
240                         if ( cargc < 2 ) {
241                                 Debug( LDAP_DEBUG_ANY,
242             "%s: line %d: missing file name in \"pidfile <file>\" line\n",
243                                     fname, lineno, 0 );
244                                 return( 1 );
245                         }
246
247                         slapd_pid_file = ch_strdup( cargv[1] );
248
249                 /* get args file name */
250                 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
251                         if ( cargc < 2 ) {
252                                 Debug( LDAP_DEBUG_ANY,
253             "%s: line %d: missing file name in \"argsfile <file>\" line\n",
254                                     fname, lineno, 0 );
255                                 return( 1 );
256                         }
257
258                         slapd_args_file = ch_strdup( cargv[1] );
259
260                 /* default password hash */
261                 } else if ( strcasecmp( cargv[0], "password-hash" ) == 0 ) {
262                         if ( cargc < 2 ) {
263                                 Debug( LDAP_DEBUG_ANY,
264             "%s: line %d: missing hash in \"password-hash <hash>\" line\n",
265                                     fname, lineno, 0 );
266                                 return( 1 );
267                         }
268                         if ( default_passwd_hash != NULL ) {
269                                 Debug( LDAP_DEBUG_ANY,
270                                         "%s: line %d: already set default password_hash!\n",
271                                         fname, lineno, 0 );
272                                 return 1;
273
274                         } else {
275                                 default_passwd_hash = ch_strdup( cargv[1] );
276                         }
277
278                 /* set SASL host */
279                 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
280                         if ( cargc < 2 ) {
281                                 Debug( LDAP_DEBUG_ANY,
282             "%s: line %d: missing host in \"sasl-host <host>\" line\n",
283                                     fname, lineno, 0 );
284                                 return( 1 );
285                         }
286
287                         if ( global_host != NULL ) {
288                                 Debug( LDAP_DEBUG_ANY,
289                                         "%s: line %d: already set sasl-host!\n",
290                                         fname, lineno, 0 );
291                                 return 1;
292
293                         } else {
294                                 global_host = ch_strdup( cargv[1] );
295                         }
296
297                 /* set SASL realm */
298                 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
299                         if ( cargc < 2 ) {
300                                 Debug( LDAP_DEBUG_ANY,
301             "%s: line %d: missing realm in \"sasl-realm <realm>\" line\n",
302                                     fname, lineno, 0 );
303                                 return( 1 );
304                         }
305
306                         if ( global_realm != NULL ) {
307                                 Debug( LDAP_DEBUG_ANY,
308                                         "%s: line %d: already set sasl-realm!\n",
309                                         fname, lineno, 0 );
310                                 return 1;
311
312                         } else {
313                                 global_realm = ch_strdup( cargv[1] );
314                         }
315
316                 } else if ( !strcasecmp( cargv[0], "saslregexp" ) ) {
317                         if ( cargc != 3 ) {
318                                 Debug( LDAP_DEBUG_ANY, 
319                                 "%s: line %d: need 2 args in \"saslregexp <match> <replace>\"\n",
320                                     fname, lineno, 0 );
321                                 return( 1 );
322                         }
323                         rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
324                         if ( rc )
325                                 return rc;
326
327                 /* SASL security properties */
328                 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
329                         char *txt;
330
331                         if ( cargc < 2 ) {
332                                 Debug( LDAP_DEBUG_ANY,
333             "%s: line %d: missing flags in \"sasl-secprops <properties>\" line\n",
334                                     fname, lineno, 0 );
335                                 return 1;
336                         }
337
338                         txt = slap_sasl_secprops( cargv[1] );
339                         if ( txt != NULL ) {
340                                 Debug( LDAP_DEBUG_ANY,
341             "%s: line %d: sasl-secprops: %s\n",
342                                     fname, lineno, txt );
343                                 return 1;
344                         }
345
346                 /* set UCDATA path */
347                 } else if ( strcasecmp( cargv[0], "ucdata-path" ) == 0 ) {
348                         if ( cargc < 2 ) {
349                                 Debug( LDAP_DEBUG_ANY,
350             "%s: line %d: missing path in \"ucdata-path <path>\" line\n",
351                                     fname, lineno, 0 );
352                                 return( 1 );
353                         }
354
355                         if ( global_ucdata_path != NULL ) {
356                                 Debug( LDAP_DEBUG_ANY,
357                                         "%s: line %d: already set ucdata-path!\n",
358                                         fname, lineno, 0 );
359                                 return 1;
360
361                         } else {
362                                 global_ucdata_path = ch_strdup( cargv[1] );
363                         }
364
365                 /* set time limit */
366                 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
367                         if ( cargc < 2 ) {
368                                 Debug( LDAP_DEBUG_ANY,
369             "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
370                                     fname, lineno, 0 );
371                                 return( 1 );
372                         }
373                         if ( be == NULL ) {
374                                 defsize = atoi( cargv[1] );
375                         } else {
376                                 be->be_sizelimit = atoi( cargv[1] );
377                         }
378
379                 /* set time limit */
380                 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
381                         if ( cargc < 2 ) {
382                                 Debug( LDAP_DEBUG_ANY,
383             "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
384                                     fname, lineno, 0 );
385                                 return( 1 );
386                         }
387                         if ( be == NULL ) {
388                                 deftime = atoi( cargv[1] );
389                         } else {
390                                 be->be_timelimit = atoi( cargv[1] );
391                         }
392
393                 /* set database suffix */
394                 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
395                         Backend *tmp_be;
396                         if ( cargc < 2 ) {
397                                 Debug( LDAP_DEBUG_ANY,
398                     "%s: line %d: missing dn in \"suffix <dn>\" line\n",
399                                     fname, lineno, 0 );
400                                 return( 1 );
401                         } else if ( cargc > 2 ) {
402                                 Debug( LDAP_DEBUG_ANY,
403     "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
404                                     fname, lineno, cargv[1] );
405                         }
406                         if ( be == NULL ) {
407                                 Debug( LDAP_DEBUG_ANY,
408 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
409                                     fname, lineno, 0 );
410                         } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
411                                 Debug( LDAP_DEBUG_ANY,
412 "%s: line %d: suffix already served by this backend (ignored)\n",
413                                     fname, lineno, 0 );
414                         } else if ( tmp_be  != NULL ) {
415                                 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
417                                     fname, lineno, tmp_be->be_suffix[0] );
418                         } else {
419                                 char *dn = ch_strdup( cargv[1] );
420                                 if( dn_validate( dn ) == NULL ) {
421                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
422                                                 "suffix DN invalid \"%s\"\n",
423                                         fname, lineno, cargv[1] );
424                                         return 1;
425
426                                 } else if( *dn == '\0' && default_search_nbase != NULL ) {
427                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
428                                                 "suffix DN empty and default "
429                                                 "search base provided \"%s\" (assuming okay)\n",
430                                         fname, lineno, default_search_base );
431                                 }
432                                 charray_add( &be->be_suffix, dn );
433                                 (void) ldap_pvt_str2upper( dn );
434                                 charray_add( &be->be_nsuffix, dn );
435                                 free( dn );
436                         }
437
438                 /* set database suffixAlias */
439                 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
440                         Backend *tmp_be;
441                         if ( cargc < 2 ) {
442                                 Debug( LDAP_DEBUG_ANY,
443 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
444                                         fname, lineno, 0 );
445                                 return( 1 );
446                         } else if ( cargc < 3 ) {
447                                 Debug( LDAP_DEBUG_ANY,
448 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
449                                 fname, lineno, 0 );
450                                 return( 1 );
451                         } else if ( cargc > 3 ) {
452                                 Debug( LDAP_DEBUG_ANY,
453                                         "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
454                                 fname, lineno, 0 );
455                         }
456
457                         if ( be == NULL ) {
458                                 Debug( LDAP_DEBUG_ANY,
459                                         "%s: line %d: suffixAlias line"
460                                         " must appear inside a database definition (ignored)\n",
461                                         fname, lineno, 0 );
462                         } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
463                                 Debug( LDAP_DEBUG_ANY,
464                                         "%s: line %d: suffixAlias served by"
465                                         "  a preceeding backend \"%s\" (ignored)\n",
466                                         fname, lineno, tmp_be->be_suffix[0] );
467
468                         } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
469                                 Debug( LDAP_DEBUG_ANY,
470                                         "%s: line %d: suffixAlias derefs to differnet backend"
471                                         "  a preceeding backend \"%s\" (ignored)\n",
472                                         fname, lineno, tmp_be->be_suffix[0] );
473
474                         } else {
475                                 char *alias, *aliased_dn;
476
477                                 alias = ch_strdup( cargv[1] );
478                                 (void) dn_normalize( alias );
479
480                                 aliased_dn = ch_strdup( cargv[2] );
481                                 (void) dn_normalize( aliased_dn );
482
483                                 charray_add( &be->be_suffixAlias, alias );
484                                 charray_add( &be->be_suffixAlias, aliased_dn );
485
486                                 free(alias);
487                                 free(aliased_dn);
488                         }
489
490                /* set max deref depth */
491                } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
492                                         int i;
493                        if ( cargc < 2 ) {
494                                Debug( LDAP_DEBUG_ANY,
495                    "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
496                                    fname, lineno, 0 );
497                                return( 1 );
498                        }
499                        if ( be == NULL ) {
500                                Debug( LDAP_DEBUG_ANY,
501 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
502                                    fname, lineno, 0 );
503                        } else if ((i = atoi(cargv[1])) < 0) {
504                                Debug( LDAP_DEBUG_ANY,
505 "%s: line %d: depth must be positive (ignored)\n",
506                                    fname, lineno, 0 );
507
508                        } else {
509                            be->be_max_deref_depth = i;
510                                            }
511
512
513                 /* set magic "root" dn for this database */
514                 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
515                         if ( cargc < 2 ) {
516                                 Debug( LDAP_DEBUG_ANY,
517                     "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
518                                     fname, lineno, 0 );
519                                 return( 1 );
520                         }
521                         if ( be == NULL ) {
522                                 Debug( LDAP_DEBUG_ANY,
523 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
524                                     fname, lineno, 0 );
525                         } else {
526                                 be->be_root_dn = ch_strdup( cargv[1] );
527                                 be->be_root_ndn = ch_strdup( cargv[1] );
528
529                                 if( dn_normalize( be->be_root_ndn ) == NULL ) {
530                                         free( be->be_root_dn );
531                                         free( be->be_root_ndn );
532                                         Debug( LDAP_DEBUG_ANY,
533 "%s: line %d: rootdn DN is invalid\n",
534                                            fname, lineno, 0 );
535                                         return( 1 );
536                                 }
537                         }
538
539                 /* set super-secret magic database password */
540                 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
541                         if ( cargc < 2 ) {
542                                 Debug( LDAP_DEBUG_ANY,
543             "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
544                                     fname, lineno, 0 );
545                                 return( 1 );
546                         }
547                         if ( be == NULL ) {
548                                 Debug( LDAP_DEBUG_ANY,
549 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
550                                     fname, lineno, 0 );
551                         } else {
552                                 be->be_root_pw.bv_val = ch_strdup( cargv[1] );
553                                 be->be_root_pw.bv_len = strlen( be->be_root_pw.bv_val );
554                         }
555
556                 /* make this database read-only */
557                 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
558                         if ( cargc < 2 ) {
559                                 Debug( LDAP_DEBUG_ANY,
560             "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
561                                     fname, lineno, 0 );
562                                 return( 1 );
563                         }
564                         if ( be == NULL ) {
565                                 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
566                                         global_restrictops |= SLAP_RESTRICT_OP_WRITES;
567                                 } else {
568                                         global_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
569                                 }
570                         } else {
571                                 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
572                                         be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
573                                 } else {
574                                         be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
575                                 }
576                         }
577
578
579                 /* allow these features */
580                 } else if ( strcasecmp( cargv[0], "allows" ) == 0 ||
581                         strcasecmp( cargv[0], "allow" ) == 0 )
582                 {
583                         slap_mask_t     allows;
584
585                         if ( be != NULL ) {
586                                 Debug( LDAP_DEBUG_ANY,
587 "%s: line %d: allow line must appear prior to database definitions\n",
588                                     fname, lineno, 0 );
589                         }
590
591                         if ( cargc < 2 ) {
592                                 Debug( LDAP_DEBUG_ANY,
593             "%s: line %d: missing feature(s) in \"allow <features>\" line\n",
594                                     fname, lineno, 0 );
595                                 return( 1 );
596                         }
597
598                         allows = 0;
599
600                         for( i=1; i < cargc; i++ ) {
601                                 if( strcasecmp( cargv[i], "tls_2_anon" ) == 0 ) {
602                                         allows |= SLAP_ALLOW_TLS_2_ANON;
603
604                                 } else if( strcasecmp( cargv[i], "none" ) != 0 ) {
605                                         Debug( LDAP_DEBUG_ANY,
606                     "%s: line %d: unknown feature %s in \"allow <features>\" line\n",
607                                             fname, lineno, cargv[i] );
608                                         return( 1 );
609                                 }
610                         }
611
612                         global_allows = allows;
613
614                 /* disallow these features */
615                 } else if ( strcasecmp( cargv[0], "disallows" ) == 0 ||
616                         strcasecmp( cargv[0], "disallow" ) == 0 )
617                 {
618                         slap_mask_t     disallows;
619
620                         if ( be != NULL ) {
621                                 Debug( LDAP_DEBUG_ANY,
622 "%s: line %d: disallow line must appear prior to database definitions\n",
623                                     fname, lineno, 0 );
624                         }
625
626                         if ( cargc < 2 ) {
627                                 Debug( LDAP_DEBUG_ANY,
628             "%s: line %d: missing feature(s) in \"disallow <features>\" line\n",
629                                     fname, lineno, 0 );
630                                 return( 1 );
631                         }
632
633                         disallows = 0;
634
635                         for( i=1; i < cargc; i++ ) {
636                                 if( strcasecmp( cargv[i], "bind_v2" ) == 0 ) {
637                                         disallows |= SLAP_DISALLOW_BIND_V2;
638
639                                 } else if( strcasecmp( cargv[i], "bind_anon" ) == 0 ) {
640                                         disallows |= SLAP_DISALLOW_BIND_ANON;
641
642                                 } else if( strcasecmp( cargv[i], "bind_anon_cred" ) == 0 ) {
643                                         disallows |= SLAP_DISALLOW_BIND_ANON_CRED;
644
645                                 } else if( strcasecmp( cargv[i], "bind_anon_dn" ) == 0 ) {
646                                         disallows |= SLAP_DISALLOW_BIND_ANON_DN;
647
648                                 } else if( strcasecmp( cargv[i], "bind_simple" ) == 0 ) {
649                                         disallows |= SLAP_DISALLOW_BIND_SIMPLE;
650
651                                 } else if( strcasecmp( cargv[i], "bind_krbv4" ) == 0 ) {
652                                         disallows |= SLAP_DISALLOW_BIND_KRBV4;
653
654                                 } else if( strcasecmp( cargv[i], "tls_authc" ) == 0 ) {
655                                         disallows |= SLAP_DISALLOW_TLS_AUTHC;
656
657                                 } else if( strcasecmp( cargv[i], "none" ) != 0 ) {
658                                         Debug( LDAP_DEBUG_ANY,
659                     "%s: line %d: unknown feature %s in \"disallow <features>\" line\n",
660                                             fname, lineno, cargv[i] );
661                                         return( 1 );
662                                 }
663                         }
664
665                         global_disallows = disallows;
666
667                 /* require these features */
668                 } else if ( strcasecmp( cargv[0], "requires" ) == 0 ||
669                         strcasecmp( cargv[0], "require" ) == 0 )
670                 {
671                         slap_mask_t     requires;
672
673                         if ( cargc < 2 ) {
674                                 Debug( LDAP_DEBUG_ANY,
675             "%s: line %d: missing feature(s) in \"require <features>\" line\n",
676                                     fname, lineno, 0 );
677                                 return( 1 );
678                         }
679
680                         requires = 0;
681
682                         for( i=1; i < cargc; i++ ) {
683                                 if( strcasecmp( cargv[i], "bind" ) == 0 ) {
684                                         requires |= SLAP_REQUIRE_BIND;
685
686                                 } else if( strcasecmp( cargv[i], "LDAPv3" ) == 0 ) {
687                                         requires |= SLAP_REQUIRE_LDAP_V3;
688
689                                 } else if( strcasecmp( cargv[i], "authc" ) == 0 ) {
690                                         requires |= SLAP_REQUIRE_AUTHC;
691
692                                 } else if( strcasecmp( cargv[i], "SASL" ) == 0 ) {
693                                         requires |= SLAP_REQUIRE_SASL;
694
695                                 } else if( strcasecmp( cargv[i], "strong" ) == 0 ) {
696                                         requires |= SLAP_REQUIRE_STRONG;
697
698                                 } else if( strcasecmp( cargv[i], "none" ) != 0 ) {
699                                         Debug( LDAP_DEBUG_ANY,
700                     "%s: line %d: unknown feature %s in \"require <features>\" line\n",
701                                             fname, lineno, cargv[i] );
702                                         return( 1 );
703                                 }
704                         }
705
706                         if ( be == NULL ) {
707                                 global_requires = requires;
708                         } else {
709                                 be->be_requires = requires;
710                         }
711
712                 /* required security factors */
713                 } else if ( strcasecmp( cargv[0], "security" ) == 0 ) {
714                         slap_ssf_set_t *set;
715
716                         if ( cargc < 2 ) {
717                                 Debug( LDAP_DEBUG_ANY,
718             "%s: line %d: missing factor(s) in \"security <factors>\" line\n",
719                                     fname, lineno, 0 );
720                                 return( 1 );
721                         }
722
723                         if ( be == NULL ) {
724                                 set = &global_ssf_set;
725                         } else {
726                                 set = &be->be_ssf_set;
727                         }
728
729                         for( i=1; i < cargc; i++ ) {
730                                 if( strncasecmp( cargv[i], "ssf=",
731                                         sizeof("ssf") ) == 0 )
732                                 {
733                                         set->sss_ssf =
734                                                 atoi( &cargv[i][sizeof("ssf")] );
735
736                                 } else if( strncasecmp( cargv[i], "transport=",
737                                         sizeof("transport") ) == 0 )
738                                 {
739                                         set->sss_transport =
740                                                 atoi( &cargv[i][sizeof("transport")] );
741
742                                 } else if( strncasecmp( cargv[i], "tls=",
743                                         sizeof("tls") ) == 0 )
744                                 {
745                                         set->sss_tls =
746                                                 atoi( &cargv[i][sizeof("tls")] );
747
748                                 } else if( strncasecmp( cargv[i], "sasl=",
749                                         sizeof("sasl") ) == 0 )
750                                 {
751                                         set->sss_sasl =
752                                                 atoi( &cargv[i][sizeof("sasl")] );
753
754                                 } else if( strncasecmp( cargv[i], "update_ssf=",
755                                         sizeof("update_ssf") ) == 0 )
756                                 {
757                                         set->sss_update_ssf =
758                                                 atoi( &cargv[i][sizeof("update_ssf")] );
759
760                                 } else if( strncasecmp( cargv[i], "update_transport=",
761                                         sizeof("update_transport") ) == 0 )
762                                 {
763                                         set->sss_update_transport =
764                                                 atoi( &cargv[i][sizeof("update_transport")] );
765
766                                 } else if( strncasecmp( cargv[i], "update_tls=",
767                                         sizeof("update_tls") ) == 0 )
768                                 {
769                                         set->sss_update_tls =
770                                                 atoi( &cargv[i][sizeof("update_tls")] );
771
772                                 } else if( strncasecmp( cargv[i], "update_sasl=",
773                                         sizeof("update_sasl") ) == 0 )
774                                 {
775                                         set->sss_update_sasl =
776                                                 atoi( &cargv[i][sizeof("update_sasl")] );
777
778                                 } else {
779                                         Debug( LDAP_DEBUG_ANY,
780                     "%s: line %d: unknown factor %s in \"security <factors>\" line\n",
781                                             fname, lineno, cargv[i] );
782                                         return( 1 );
783                                 }
784                         }
785
786                 
787                 /* where to send clients when we don't hold it */
788                 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
789                         if ( cargc < 2 ) {
790                                 Debug( LDAP_DEBUG_ANY,
791                     "%s: line %d: missing URL in \"referral <URL>\" line\n",
792                                     fname, lineno, 0 );
793                                 return( 1 );
794                         }
795
796                         vals[0]->bv_val = cargv[1];
797                         vals[0]->bv_len = strlen( vals[0]->bv_val );
798                         value_add( &default_referral, vals );
799
800                 /* specify an Object Identifier macro */
801                 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
802                         parse_oidm( fname, lineno, cargc, cargv );
803
804                 /* specify an objectclass */
805                 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
806                         if ( *cargv[1] == '(' ) {
807                                 char * p;
808                                 p = strchr(saveline,'(');
809                                 parse_oc( fname, lineno, p, cargv );
810                         } else {
811                                 Debug( LDAP_DEBUG_ANY,
812     "%s: line %d: old objectclass format not supported.\n",
813                                     fname, lineno, 0 );
814                         }
815
816                 /* specify an attribute type */
817                 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
818                         || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
819                 {
820                         if ( *cargv[1] == '(' ) {
821                                 char * p;
822                                 p = strchr(saveline,'(');
823                                 parse_at( fname, lineno, p, cargv );
824                         } else {
825                                 Debug( LDAP_DEBUG_ANY,
826     "%s: line %d: old attribute type format not supported.\n",
827                                     fname, lineno, 0 );
828                         }
829
830                 /* turn on/off schema checking */
831                 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
832                         if ( cargc < 2 ) {
833                                 Debug( LDAP_DEBUG_ANY,
834     "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
835                                     fname, lineno, 0 );
836                                 return( 1 );
837                         }
838                         if ( strcasecmp( cargv[1], "off" ) == 0 ) {
839                                 global_schemacheck = 0;
840                         } else {
841                                 global_schemacheck = 1;
842                         }
843
844                 /* specify access control info */
845                 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
846                         parse_acl( be, fname, lineno, cargc, cargv );
847
848                 /* specify default access control info */
849                 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
850                         slap_access_t access;
851
852                         if ( cargc < 2 ) {
853                                 Debug( LDAP_DEBUG_ANY,
854             "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
855                                     fname, lineno, 0 );
856                                 return( 1 );
857                         }
858
859                         access = str2access( cargv[1] );
860
861                         if ( access == ACL_INVALID_ACCESS ) {
862                                 Debug( LDAP_DEBUG_ANY,
863                                         "%s: line %d: bad access level \"%s\", "
864                                         "expecting none|auth|compare|search|read|write\n",
865                                     fname, lineno, cargv[1] );
866                                 return( 1 );
867                         }
868
869                         if ( be == NULL ) {
870                                 global_default_access = access;
871                         } else {
872                                 be->be_dfltaccess = access;
873                         }
874
875                 /* debug level to log things to syslog */
876                 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
877                         if ( cargc < 2 ) {
878                                 Debug( LDAP_DEBUG_ANY,
879                     "%s: line %d: missing level in \"loglevel <level>\" line\n",
880                                     fname, lineno, 0 );
881                                 return( 1 );
882                         }
883
884                         ldap_syslog = 0;
885
886                         for( i=1; i < cargc; i++ ) {
887                                 ldap_syslog += atoi( cargv[1] );
888                         }
889
890                 /* list of replicas of the data in this backend (master only) */
891                 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
892                         if ( cargc < 2 ) {
893                                 Debug( LDAP_DEBUG_ANY,
894             "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
895                                     fname, lineno, 0 );
896                                 return( 1 );
897                         }
898                         if ( be == NULL ) {
899                                 Debug( LDAP_DEBUG_ANY,
900 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
901                                     fname, lineno, 0 );
902                         } else {
903                                 for ( i = 1; i < cargc; i++ ) {
904                                         if ( strncasecmp( cargv[i], "host=", 5 )
905                                             == 0 ) {
906                                                 charray_add( &be->be_replica,
907                                                              cargv[i] + 5 );
908                                                 break;
909                                         }
910                                 }
911                                 if ( i == cargc ) {
912                                         Debug( LDAP_DEBUG_ANY,
913                     "%s: line %d: missing host in \"replica\" line (ignored)\n",
914                                             fname, lineno, 0 );
915                                 }
916                         }
917
918                 /* dn of master entity allowed to write to replica */
919                 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
920                         if ( cargc < 2 ) {
921                                 Debug( LDAP_DEBUG_ANY,
922                     "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
923                                     fname, lineno, 0 );
924                                 return( 1 );
925                         }
926                         if ( be == NULL ) {
927                                 Debug( LDAP_DEBUG_ANY,
928 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
929                                     fname, lineno, 0 );
930                         } else {
931                                 be->be_update_ndn = ch_strdup( cargv[1] );
932                                 if( dn_normalize( be->be_update_ndn ) == NULL ) {
933                                         Debug( LDAP_DEBUG_ANY,
934 "%s: line %d: updatedn DN is invalid\n",
935                                             fname, lineno, 0 );
936                                         return 1;
937                                 }
938                         }
939
940                 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
941                         if ( cargc < 2 ) {
942                                 Debug( LDAP_DEBUG_ANY,
943                     "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
944                                     fname, lineno, 0 );
945                                 return( 1 );
946                         }
947                         if ( be == NULL ) {
948                                 Debug( LDAP_DEBUG_ANY,
949 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
950                                     fname, lineno, 0 );
951                         } else if ( be->be_update_ndn == NULL ) {
952                                 Debug( LDAP_DEBUG_ANY,
953 "%s: line %d: updateref line must after updatedn (ignored)\n",
954                                     fname, lineno, 0 );
955                         } else {
956                                 vals[0]->bv_val = cargv[1];
957                                 vals[0]->bv_len = strlen( vals[0]->bv_val );
958                                 value_add( &be->be_update_refs, vals );
959                         }
960
961                 /* replication log file to which changes are appended */
962                 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
963                         if ( cargc < 2 ) {
964                                 Debug( LDAP_DEBUG_ANY,
965             "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
966                                     fname, lineno, 0 );
967                                 return( 1 );
968                         }
969                         if ( be ) {
970                                 be->be_replogfile = ch_strdup( cargv[1] );
971                         } else {
972                                 replogfile = ch_strdup( cargv[1] );
973                         }
974
975                 /* maintain lastmodified{by,time} attributes */
976                 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
977                         if ( cargc < 2 ) {
978                                 Debug( LDAP_DEBUG_ANY,
979             "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
980                                     fname, lineno, 0 );
981                                 return( 1 );
982                         }
983                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
984                                 if ( be )
985                                         be->be_lastmod = ON;
986                                 else
987                                         global_lastmod = ON;
988                         } else {
989                                 if ( be )
990                                         be->be_lastmod = OFF;
991                                 else
992                                         global_lastmod = OFF;
993                         }
994
995                 /* set idle timeout value */
996                 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
997                         int i;
998                         if ( cargc < 2 ) {
999                                 Debug( LDAP_DEBUG_ANY,
1000             "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
1001                                     fname, lineno, 0 );
1002                                 return( 1 );
1003                         }
1004
1005                         i = atoi( cargv[1] );
1006
1007                         if( i < 0 ) {
1008                                 Debug( LDAP_DEBUG_ANY,
1009             "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
1010                                     fname, lineno, i );
1011                                 return( 1 );
1012                         }
1013
1014                         global_idletimeout = i;
1015
1016                 /* include another config file */
1017                 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
1018                         if ( cargc < 2 ) {
1019                                 Debug( LDAP_DEBUG_ANY,
1020     "%s: line %d: missing filename in \"include <filename>\" line\n",
1021                                     fname, lineno, 0 );
1022                                 return( 1 );
1023                         }
1024                         savefname = ch_strdup( cargv[1] );
1025                         savelineno = lineno;
1026
1027                         if ( read_config( savefname ) != 0 ) {
1028                                 return( 1 );
1029                         }
1030
1031                         free( savefname );
1032                         lineno = savelineno - 1;
1033
1034                 /* location of kerberos srvtab file */
1035                 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
1036                         if ( cargc < 2 ) {
1037                                 Debug( LDAP_DEBUG_ANY,
1038             "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
1039                                     fname, lineno, 0 );
1040                                 return( 1 );
1041                         }
1042                         ldap_srvtab = ch_strdup( cargv[1] );
1043
1044 #ifdef SLAPD_MODULES
1045                 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
1046                    if ( cargc < 2 ) {
1047                       Debug( LDAP_DEBUG_ANY,
1048                              "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
1049                              fname, lineno, 0 );
1050                       exit( EXIT_FAILURE );
1051                    }
1052                    if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
1053                       Debug( LDAP_DEBUG_ANY,
1054                              "%s: line %d: failed to load or initialize module %s\n",
1055                              fname, lineno, cargv[1]);
1056                       exit( EXIT_FAILURE );
1057                    }
1058                 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
1059                    if ( cargc != 2 ) {
1060                       Debug( LDAP_DEBUG_ANY,
1061                              "%s: line %d: missing path in \"modulepath <path>\" line\n",
1062                              fname, lineno, 0 );
1063                       exit( EXIT_FAILURE );
1064                    }
1065                    if (module_path( cargv[1] )) {
1066                       Debug( LDAP_DEBUG_ANY,
1067                              "%s: line %d: failed to set module search path to %s\n",
1068                              fname, lineno, cargv[1]);
1069                       exit( EXIT_FAILURE );
1070                    }
1071                    
1072 #endif /*SLAPD_MODULES*/
1073
1074 #ifdef HAVE_TLS
1075                 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
1076                         rc = ldap_pvt_tls_set_option( NULL,
1077                                                       LDAP_OPT_X_TLS_PROTOCOL,
1078                                                       cargv[1] );
1079                         if ( rc )
1080                                 return rc;
1081
1082                 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
1083                         rc = ldap_pvt_tls_set_option( NULL,
1084                                                       LDAP_OPT_X_TLS_CIPHER_SUITE,
1085                                                       cargv[1] );
1086                         if ( rc )
1087                                 return rc;
1088
1089                 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
1090                         rc = ldap_pvt_tls_set_option( NULL,
1091                                                       LDAP_OPT_X_TLS_CERTFILE,
1092                                                       cargv[1] );
1093                         if ( rc )
1094                                 return rc;
1095
1096                 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
1097                         rc = ldap_pvt_tls_set_option( NULL,
1098                                                       LDAP_OPT_X_TLS_KEYFILE,
1099                                                       cargv[1] );
1100                         if ( rc )
1101                                 return rc;
1102
1103                 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
1104                         rc = ldap_pvt_tls_set_option( NULL,
1105                                                       LDAP_OPT_X_TLS_CACERTDIR,
1106                                                       cargv[1] );
1107                         if ( rc )
1108                                 return rc;
1109
1110                 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
1111                         rc = ldap_pvt_tls_set_option( NULL,
1112                                                       LDAP_OPT_X_TLS_CACERTFILE,
1113                                                       cargv[1] );
1114                         if ( rc )
1115                                 return rc;
1116                 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
1117                         rc = ldap_pvt_tls_set_option( NULL,
1118                                                       LDAP_OPT_X_TLS_REQUIRE_CERT,
1119                                                       cargv[1] );
1120                         if ( rc )
1121                                 return rc;
1122
1123 #endif
1124
1125                 /* pass anything else to the current backend info/db config routine */
1126                 } else {
1127                         if ( bi != NULL ) {
1128                                 if ( bi->bi_config == 0 ) {
1129                                         Debug( LDAP_DEBUG_ANY,
1130 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
1131                                                 fname, lineno, cargv[0] );
1132                                 } else {
1133                                         if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
1134                                                 != 0 )
1135                                         {
1136                                                 return( 1 );
1137                                         }
1138                                 }
1139                         } else if ( be != NULL ) {
1140                                 if ( be->be_config == 0 ) {
1141                                         Debug( LDAP_DEBUG_ANY,
1142 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
1143                                         fname, lineno, cargv[0] );
1144                                 } else {
1145                                         if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
1146                                                 != 0 )
1147                                         {
1148                                                 return( 1 );
1149                                         }
1150                                 }
1151                         } else {
1152                                 Debug( LDAP_DEBUG_ANY,
1153 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
1154                                     fname, lineno, cargv[0] );
1155                         }
1156                 }
1157                 free( saveline );
1158         }
1159         fclose( fp );
1160         return( 0 );
1161 }
1162
1163 static int
1164 fp_parse_line(
1165     char        *line,
1166     int         *argcp,
1167     char        **argv
1168 )
1169 {
1170         char *  token;
1171
1172         *argcp = 0;
1173         for ( token = strtok_quote( line, " \t" ); token != NULL;
1174             token = strtok_quote( NULL, " \t" ) ) {
1175                 if ( *argcp == MAXARGS ) {
1176                         Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
1177                             MAXARGS, 0, 0 );
1178                         return( 1 );
1179                 }
1180                 argv[(*argcp)++] = token;
1181         }
1182         argv[*argcp] = NULL;
1183         return 0;
1184 }
1185
1186 static char *
1187 strtok_quote( char *line, char *sep )
1188 {
1189         int             inquote;
1190         char            *tmp;
1191         static char     *next;
1192
1193         if ( line != NULL ) {
1194                 next = line;
1195         }
1196         while ( *next && strchr( sep, *next ) ) {
1197                 next++;
1198         }
1199
1200         if ( *next == '\0' ) {
1201                 next = NULL;
1202                 return( NULL );
1203         }
1204         tmp = next;
1205
1206         for ( inquote = 0; *next; ) {
1207                 switch ( *next ) {
1208                 case '"':
1209                         if ( inquote ) {
1210                                 inquote = 0;
1211                         } else {
1212                                 inquote = 1;
1213                         }
1214                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1215                         break;
1216
1217                 case '\\':
1218                         if ( next[1] )
1219                                 AC_MEMCPY( next,
1220                                             next + 1, strlen( next + 1 ) + 1 );
1221                         next++;         /* dont parse the escaped character */
1222                         break;
1223
1224                 default:
1225                         if ( ! inquote ) {
1226                                 if ( strchr( sep, *next ) != NULL ) {
1227                                         *next++ = '\0';
1228                                         return( tmp );
1229                                 }
1230                         }
1231                         next++;
1232                         break;
1233                 }
1234         }
1235
1236         return( tmp );
1237 }
1238
1239 static char     buf[BUFSIZ];
1240 static char     *line;
1241 static int      lmax, lcur;
1242
1243 #define CATLINE( buf )  { \
1244         int     len; \
1245         len = strlen( buf ); \
1246         while ( lcur + len + 1 > lmax ) { \
1247                 lmax += BUFSIZ; \
1248                 line = (char *) ch_realloc( line, lmax ); \
1249         } \
1250         strcpy( line + lcur, buf ); \
1251         lcur += len; \
1252 }
1253
1254 static char *
1255 fp_getline( FILE *fp, int *lineno )
1256 {
1257         char            *p;
1258
1259         lcur = 0;
1260         CATLINE( buf );
1261         (*lineno)++;
1262
1263         /* hack attack - keeps us from having to keep a stack of bufs... */
1264         if ( strncasecmp( line, "include", 7 ) == 0 ) {
1265                 buf[0] = '\0';
1266                 return( line );
1267         }
1268
1269         while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
1270                 if ( (p = strchr( buf, '\n' )) != NULL ) {
1271                         *p = '\0';
1272                 }
1273                 if ( ! isspace( (unsigned char) buf[0] ) ) {
1274                         return( line );
1275                 }
1276
1277                 CATLINE( buf );
1278                 (*lineno)++;
1279         }
1280         buf[0] = '\0';
1281
1282         return( line[0] ? line : NULL );
1283 }
1284
1285 static void
1286 fp_getline_init( int *lineno )
1287 {
1288         *lineno = -1;
1289         buf[0] = '\0';
1290 }