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