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