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