]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
1dfbdf2a98fb66f7a493e2d5f8f99dfe0c590efd
[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 ( ACL_IS_INVALID(ACL_SET(str2access(cargv[1]),
429                                         global_default_access)) ) {
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 ( ACL_IS_INVALID(ACL_SET(str2access(cargv[1]),
437                                         be->be_dfltaccess)) ) {
438                                         Debug( LDAP_DEBUG_ANY,
439                                                 "%s: line %d: bad access \"%s\", "
440                                                 "expecting [self]{none|compare|search|read|write}\n",
441                                             fname, lineno, cargv[1] );
442                                         return( 1 );
443                                 }
444                         }
445
446                 /* debug level to log things to syslog */
447                 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
448                         if ( cargc < 2 ) {
449                                 Debug( LDAP_DEBUG_ANY,
450                     "%s: line %d: missing level in \"loglevel <level>\" line\n",
451                                     fname, lineno, 0 );
452                                 return( 1 );
453                         }
454                         ldap_syslog = atoi( cargv[1] );
455
456                 /* list of replicas of the data in this backend (master only) */
457                 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
458                         if ( cargc < 2 ) {
459                                 Debug( LDAP_DEBUG_ANY,
460             "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
461                                     fname, lineno, 0 );
462                                 return( 1 );
463                         }
464                         if ( be == NULL ) {
465                                 Debug( LDAP_DEBUG_ANY,
466 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
467                                     fname, lineno, 0 );
468                         } else {
469                                 for ( i = 1; i < cargc; i++ ) {
470                                         if ( strncasecmp( cargv[i], "host=", 5 )
471                                             == 0 ) {
472                                                 charray_add( &be->be_replica,
473                                                              cargv[i] + 5 );
474                                                 break;
475                                         }
476                                 }
477                                 if ( i == cargc ) {
478                                         Debug( LDAP_DEBUG_ANY,
479                     "%s: line %d: missing host in \"replica\" line (ignored)\n",
480                                             fname, lineno, 0 );
481                                 }
482                         }
483
484                 /* dn of master entity allowed to write to replica */
485                 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
486                         if ( cargc < 2 ) {
487                                 Debug( LDAP_DEBUG_ANY,
488                     "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
489                                     fname, lineno, 0 );
490                                 return( 1 );
491                         }
492                         if ( be == NULL ) {
493                                 Debug( LDAP_DEBUG_ANY,
494 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
495                                     fname, lineno, 0 );
496                         } else {
497                                 be->be_update_ndn = ch_strdup( cargv[1] );
498                                 (void) dn_normalize_case( be->be_update_ndn );
499                         }
500
501                 /* replication log file to which changes are appended */
502                 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
503                         if ( cargc < 2 ) {
504                                 Debug( LDAP_DEBUG_ANY,
505             "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
506                                     fname, lineno, 0 );
507                                 return( 1 );
508                         }
509                         if ( be ) {
510                                 be->be_replogfile = ch_strdup( cargv[1] );
511                         } else {
512                                 replogfile = ch_strdup( cargv[1] );
513                         }
514
515                 /* maintain lastmodified{by,time} attributes */
516                 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
517                         if ( cargc < 2 ) {
518                                 Debug( LDAP_DEBUG_ANY,
519             "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
520                                     fname, lineno, 0 );
521                                 return( 1 );
522                         }
523                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
524                                 if ( be )
525                                         be->be_lastmod = ON;
526                                 else
527                                         global_lastmod = ON;
528                         } else {
529                                 if ( be )
530                                         be->be_lastmod = OFF;
531                                 else
532                                         global_lastmod = OFF;
533                         }
534
535                 /* set idle timeout value */
536                 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
537                         int i;
538                         if ( cargc < 2 ) {
539                                 Debug( LDAP_DEBUG_ANY,
540             "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
541                                     fname, lineno, 0 );
542                                 return( 1 );
543                         }
544
545                         i = atoi( cargv[1] );
546
547                         if( i < 0 ) {
548                                 Debug( LDAP_DEBUG_ANY,
549             "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
550                                     fname, lineno, i );
551                                 return( 1 );
552                         }
553
554                         global_idletimeout = i;
555
556                 /* include another config file */
557                 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
558                         if ( cargc < 2 ) {
559                                 Debug( LDAP_DEBUG_ANY,
560     "%s: line %d: missing filename in \"include <filename>\" line\n",
561                                     fname, lineno, 0 );
562                                 return( 1 );
563                         }
564                         savefname = ch_strdup( cargv[1] );
565                         savelineno = lineno;
566
567                         if ( read_config( savefname ) != 0 ) {
568                                 return( 1 );
569                         }
570
571                         free( savefname );
572                         lineno = savelineno - 1;
573
574                 /* location of kerberos srvtab file */
575                 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
576                         if ( cargc < 2 ) {
577                                 Debug( LDAP_DEBUG_ANY,
578             "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
579                                     fname, lineno, 0 );
580                                 return( 1 );
581                         }
582                         ldap_srvtab = ch_strdup( cargv[1] );
583
584 #ifdef SLAPD_MODULES
585                 } else if (strcasecmp( cargv[0], "loadmodule") == 0 ) {
586                    if ( cargc < 2 ) {
587                       Debug( LDAP_DEBUG_ANY,
588                              "%s: line %d: missing filename in \"loadmodule <filename>\" line\n",
589                              fname, lineno, 0 );
590                       exit( 1 );
591                    }
592                    if (!load_module(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
593                       Debug( LDAP_DEBUG_ANY,
594                              "%s: line %d: failed to load or initialize module %s\n",
595                              fname, lineno, cargv[1]);
596                       exit( 1 );
597                    }
598                    
599 #endif /*SLAPD_MODULES*/
600
601                 /* pass anything else to the current backend info/db config routine */
602                 } else {
603                         if ( bi != NULL ) {
604                                 if ( bi->bi_config == 0 ) {
605                                         Debug( LDAP_DEBUG_ANY,
606 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
607                                                 fname, lineno, cargv[0] );
608                                 } else {
609                                         if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
610                                                 != 0 )
611                                         {
612                                                 return( 1 );
613                                         }
614                                 }
615                         } else if ( be != NULL ) {
616                                 if ( be->be_config == 0 ) {
617                                         Debug( LDAP_DEBUG_ANY,
618 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
619                                         fname, lineno, cargv[0] );
620                                 } else {
621                                         if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
622                                                 != 0 )
623                                         {
624                                                 return( 1 );
625                                         }
626                                 }
627                         } else {
628                                 Debug( LDAP_DEBUG_ANY,
629 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
630                                     fname, lineno, cargv[0] );
631                         }
632                 }
633                 free( saveline );
634         }
635         fclose( fp );
636         return( 0 );
637 }
638
639 static int
640 fp_parse_line(
641     char        *line,
642     int         *argcp,
643     char        **argv
644 )
645 {
646         char *  token;
647
648         *argcp = 0;
649         for ( token = strtok_quote( line, " \t" ); token != NULL;
650             token = strtok_quote( NULL, " \t" ) ) {
651                 if ( *argcp == MAXARGS ) {
652                         Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
653                             MAXARGS, 0, 0 );
654                         return( 1 );
655                 }
656                 argv[(*argcp)++] = token;
657         }
658         argv[*argcp] = NULL;
659         return 0;
660 }
661
662 static char *
663 strtok_quote( char *line, char *sep )
664 {
665         int             inquote;
666         char            *tmp;
667         static char     *next;
668
669         if ( line != NULL ) {
670                 next = line;
671         }
672         while ( *next && strchr( sep, *next ) ) {
673                 next++;
674         }
675
676         if ( *next == '\0' ) {
677                 next = NULL;
678                 return( NULL );
679         }
680         tmp = next;
681
682         for ( inquote = 0; *next; ) {
683                 switch ( *next ) {
684                 case '"':
685                         if ( inquote ) {
686                                 inquote = 0;
687                         } else {
688                                 inquote = 1;
689                         }
690                         SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
691                         break;
692
693                 case '\\':
694                         if ( next[1] )
695                                 SAFEMEMCPY( next,
696                                             next + 1, strlen( next + 1 ) + 1 );
697                         next++;         /* dont parse the escaped character */
698                         break;
699
700                 default:
701                         if ( ! inquote ) {
702                                 if ( strchr( sep, *next ) != NULL ) {
703                                         *next++ = '\0';
704                                         return( tmp );
705                                 }
706                         }
707                         next++;
708                         break;
709                 }
710         }
711
712         return( tmp );
713 }
714
715 static char     buf[BUFSIZ];
716 static char     *line;
717 static int      lmax, lcur;
718
719 #define CATLINE( buf )  { \
720         int     len; \
721         len = strlen( buf ); \
722         while ( lcur + len + 1 > lmax ) { \
723                 lmax += BUFSIZ; \
724                 line = (char *) ch_realloc( line, lmax ); \
725         } \
726         strcpy( line + lcur, buf ); \
727         lcur += len; \
728 }
729
730 static char *
731 fp_getline( FILE *fp, int *lineno )
732 {
733         char            *p;
734
735         lcur = 0;
736         CATLINE( buf );
737         (*lineno)++;
738
739         /* hack attack - keeps us from having to keep a stack of bufs... */
740         if ( strncasecmp( line, "include", 7 ) == 0 ) {
741                 buf[0] = '\0';
742                 return( line );
743         }
744
745         while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
746                 if ( (p = strchr( buf, '\n' )) != NULL ) {
747                         *p = '\0';
748                 }
749                 if ( ! isspace( (unsigned char) buf[0] ) ) {
750                         return( line );
751                 }
752
753                 CATLINE( buf );
754                 (*lineno)++;
755         }
756         buf[0] = '\0';
757
758         return( line[0] ? line : NULL );
759 }
760
761 static void
762 fp_getline_init( int *lineno )
763 {
764         *lineno = -1;
765         buf[0] = '\0';
766 }