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