]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
ef8c52247248b860631a3c5d9a0f3347bca99202
[openldap] / servers / slapd / config.c
1 /* config.c - configuration file handling routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/ctype.h>
33 #include <ac/signal.h>
34 #include <ac/socket.h>
35 #include <ac/errno.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40
41 #include "slap.h"
42 #ifdef LDAP_SLAPI
43 #include "slapi/slapi.h"
44 #endif
45 #include "lutil.h"
46 #include "config.h"
47
48 #ifdef HAVE_TLS
49 #include <openssl/ssl.h>
50 #endif
51
52 #define ARGS_STEP       512
53
54 /*
55  * defaults for various global variables
56  */
57 slap_mask_t             global_allows = 0;
58 slap_mask_t             global_disallows = 0;
59 int             global_gentlehup = 0;
60 int             global_idletimeout = 0;
61 char    *global_host = NULL;
62 char    *global_realm = NULL;
63 char            *ldap_srvtab = "";
64 char            **default_passwd_hash = NULL;
65 struct berval default_search_base = BER_BVNULL;
66 struct berval default_search_nbase = BER_BVNULL;
67
68 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
69 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
70
71 int     slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
72 int     slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
73
74 char   *slapd_pid_file  = NULL;
75 char   *slapd_args_file = NULL;
76
77 int use_reverse_lookup = 0;
78
79 #ifdef LDAP_SLAPI
80 int slapi_plugins_used = 0;
81 #endif
82
83 static int fp_getline(FILE *fp, ConfigArgs *c);
84 static void fp_getline_init(ConfigArgs *c);
85 static int fp_parse_line(ConfigArgs *c);
86
87 static char     *strtok_quote(char *line, char *sep, char **quote_ptr);
88 static char *strtok_quote_ldif(char **line);
89
90 ConfigArgs *
91 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
92 {
93         ConfigArgs *c;
94         c = ch_calloc( 1, sizeof( ConfigArgs ) );
95         if ( c == NULL ) return(NULL);
96         c->be     = be; 
97         c->fname  = fname;
98         c->argc   = argc;
99         c->argv   = argv; 
100         c->lineno = lineno;
101         snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
102         return(c);
103 }
104
105 void
106 init_config_argv( ConfigArgs *c )
107 {
108         c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
109         c->argv_size = ARGS_STEP + 1;
110 }
111
112 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
113         int i;
114
115         for(i = 0; Conf[i].name; i++)
116                 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
117                         (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
118         if ( !Conf[i].name ) return NULL;
119         return Conf+i;
120 }
121
122 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
123         int rc, arg_user, arg_type, arg_syn, iarg;
124         long larg;
125         ber_len_t barg;
126         
127         if(Conf->arg_type == ARG_IGNORED) {
128                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
129                         c->log, Conf->name, 0);
130                 return(0);
131         }
132         arg_type = Conf->arg_type & ARGS_TYPES;
133         arg_user = Conf->arg_type & ARGS_USERLAND;
134         arg_syn = Conf->arg_type & ARGS_SYNTAX;
135
136         if((arg_type == ARG_DN) && c->argc == 1) {
137                 c->argc = 2;
138                 c->argv[1] = "";
139         }
140         if(Conf->min_args && (c->argc < Conf->min_args)) {
141                 snprintf( c->msg, sizeof( c->msg ), "<%s> missing <%s> argument",
142                         c->argv[0], Conf->what );
143                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n", c->log, c->msg, 0 );
144                 return(ARG_BAD_CONF);
145         }
146         if(Conf->max_args && (c->argc > Conf->max_args)) {
147                 char    *ignored = " ignored";
148
149                 snprintf( c->msg, sizeof( c->msg ), "<%s> extra cruft after <%s>",
150                         c->argv[0], Conf->what );
151
152 #ifdef LDAP_DEVEL
153                 ignored = "";
154 #endif /* LDAP_DEVEL */
155                 Debug(LDAP_DEBUG_CONFIG, "%s: %s%s.\n",
156                                 c->log, c->msg, ignored );
157 #ifdef LDAP_DEVEL
158                 return(ARG_BAD_CONF);
159 #endif /* LDAP_DEVEL */
160         }
161         if((arg_syn & ARG_DB) && !c->be) {
162                 snprintf( c->msg, sizeof( c->msg ), "<%s> only allowed within database declaration",
163                         c->argv[0] );
164                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
165                         c->log, c->msg, 0);
166                 return(ARG_BAD_CONF);
167         }
168         if((arg_syn & ARG_PRE_BI) && c->bi) {
169                 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any backend %sdeclaration",
170                         c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
171                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
172                         c->log, c->msg, 0 );
173                 return(ARG_BAD_CONF);
174         }
175         if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
176                 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any database declaration",
177                         c->argv[0] );
178                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
179                         c->log, c->msg, 0);
180                 return(ARG_BAD_CONF);
181         }
182         if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
183                 snprintf( c->msg, sizeof( c->msg ), "<%s> old format not supported", c->argv[0] );
184                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
185                         c->log, c->msg, 0);
186                 return(ARG_BAD_CONF);
187         }
188         if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
189                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid config_table, arg_item is NULL",
190                         c->argv[0] );
191                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
192                         c->log, c->msg, 0);
193                 return(ARG_BAD_CONF);
194         }
195         c->type = arg_user;
196         memset(&c->values, 0, sizeof(c->values));
197         if(arg_type == ARG_STRING) {
198                 if ( !check_only )
199                         c->value_string = ch_strdup(c->argv[1]);
200         } else if(arg_type == ARG_BERVAL) {
201                 if ( !check_only )
202                         ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
203         } else if(arg_type == ARG_DN) {
204                 struct berval bv;
205                 ber_str2bv( c->argv[1], 0, 0, &bv );
206                 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
207                 if ( rc != LDAP_SUCCESS ) {
208                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid DN %d (%s)",
209                                 c->argv[0], rc, ldap_err2string( rc ));
210                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
211                         return(ARG_BAD_CONF);
212                 }
213                 if ( check_only ) {
214                         ch_free( c->value_ndn.bv_val );
215                         ch_free( c->value_dn.bv_val );
216                 }
217         } else {        /* all numeric */
218                 int j;
219                 iarg = 0; larg = 0; barg = 0;
220                 switch(arg_type) {
221                         case ARG_INT:
222                                 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
223                                         snprintf( c->msg, sizeof( c->msg ),
224                                                 "<%s> unable to parse \"%s\" as int",
225                                                 c->argv[0], c->argv[1] );
226                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
227                                                 c->log, c->msg, 0);
228                                         return(ARG_BAD_CONF);
229                                 }
230                                 break;
231                         case ARG_LONG:
232                                 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
233                                         snprintf( c->msg, sizeof( c->msg ),
234                                                 "<%s> unable to parse \"%s\" as long",
235                                                 c->argv[0], c->argv[1] );
236                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
237                                                 c->log, c->msg, 0);
238                                         return(ARG_BAD_CONF);
239                                 }
240                                 break;
241                         case ARG_BER_LEN_T: {
242                                 unsigned long   l;
243                                 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
244                                         snprintf( c->msg, sizeof( c->msg ),
245                                                 "<%s> unable to parse \"%s\" as ber_len_t",
246                                                 c->argv[0], c->argv[1] );
247                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
248                                                 c->log, c->msg, 0);
249                                         return(ARG_BAD_CONF);
250                                 }
251                                 barg = (ber_len_t)l;
252                                 } break;
253                         case ARG_ON_OFF:
254                                 if (c->argc == 1) {
255                                         iarg = 1;
256                                 } else if ( !strcasecmp(c->argv[1], "on") ||
257                                         !strcasecmp(c->argv[1], "true") ||
258                                         !strcasecmp(c->argv[1], "yes") )
259                                 {
260                                         iarg = 1;
261                                 } else if ( !strcasecmp(c->argv[1], "off") ||
262                                         !strcasecmp(c->argv[1], "false") ||
263                                         !strcasecmp(c->argv[1], "no") )
264                                 {
265                                         iarg = 0;
266                                 } else {
267                                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
268                                                 c->argv[0] );
269                                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
270                                                 c->log, c->msg, 0 );
271                                         return(ARG_BAD_CONF);
272                                 }
273                                 break;
274                 }
275                 j = (arg_type & ARG_NONZERO) ? 1 : 0;
276                 if(iarg < j && larg < j && barg < j ) {
277                         larg = larg ? larg : (barg ? barg : iarg);
278                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
279                                 c->argv[0] );
280                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
281                                 c->log, c->msg, 0 );
282                         return(ARG_BAD_CONF);
283                 }
284                 switch(arg_type) {
285                         case ARG_ON_OFF:
286                         case ARG_INT:           c->value_int = iarg;            break;
287                         case ARG_LONG:          c->value_long = larg;           break;
288                         case ARG_BER_LEN_T:     c->value_ber_t = barg;          break;
289                 }
290         }
291         return 0;
292 }
293
294 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
295         int rc, arg_type;
296         void *ptr = NULL;
297
298         arg_type = Conf->arg_type;
299         if(arg_type & ARG_MAGIC) {
300                 if(!c->be) c->be = frontendDB;
301                 c->msg[0] = '\0';
302                 rc = (*((ConfigDriver*)Conf->arg_item))(c);
303 #if 0
304                 if(c->be == frontendDB) c->be = NULL;
305 #endif
306                 if(rc) {
307                         if ( !c->msg[0] ) {
308                                 snprintf( c->msg, sizeof( c->msg ), "<%s> handler exited with %d",
309                                         c->argv[0], rc );
310                                 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
311                                         c->log, c->msg, 0 );
312                         }
313                         return(ARG_BAD_CONF);
314                 }
315                 return(0);
316         }
317         if(arg_type & ARG_OFFSET) {
318                 if (c->be)
319                         ptr = c->be->be_private;
320                 else if (c->bi)
321                         ptr = c->bi->bi_private;
322                 else {
323                         snprintf( c->msg, sizeof( c->msg ), "<%s> offset is missing base pointer",
324                                 c->argv[0] );
325                         Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
326                                 c->log, c->msg, 0);
327                         return(ARG_BAD_CONF);
328                 }
329                 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
330         } else if (arg_type & ARGS_TYPES) {
331                 ptr = Conf->arg_item;
332         }
333         if(arg_type & ARGS_TYPES)
334                 switch(arg_type & ARGS_TYPES) {
335                         case ARG_ON_OFF:
336                         case ARG_INT:           *(int*)ptr = c->value_int;                      break;
337                         case ARG_LONG:          *(long*)ptr = c->value_long;                    break;
338                         case ARG_BER_LEN_T:     *(ber_len_t*)ptr = c->value_ber_t;                      break;
339                         case ARG_STRING: {
340                                 char *cc = *(char**)ptr;
341                                 if(cc) {
342                                         if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
343                                                 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
344                                                         c->log, Conf->name, 0 );
345                                                 return(ARG_BAD_CONF);
346                                         }
347                                         ch_free(cc);
348                                 }
349                                 *(char **)ptr = c->value_string;
350                                 break;
351                                 }
352                         case ARG_BERVAL:
353                                 *(struct berval *)ptr = c->value_bv;
354                                 break;
355                 }
356         return(0);
357 }
358
359 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
360         int rc, arg_type;
361
362         arg_type = Conf->arg_type;
363         if(arg_type == ARG_IGNORED) {
364                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
365                         c->log, Conf->name, 0);
366                 return(0);
367         }
368         rc = config_check_vals( Conf, c, 0 );
369         if ( rc ) return rc;
370         return config_set_vals( Conf, c );
371 }
372
373 int
374 config_del_vals(ConfigTable *cf, ConfigArgs *c)
375 {
376         int rc = 0;
377
378         /* If there is no handler, just ignore it */
379         if ( cf->arg_type & ARG_MAGIC ) {
380                 c->op = LDAP_MOD_DELETE;
381                 c->type = cf->arg_type & ARGS_USERLAND;
382                 rc = (*((ConfigDriver*)cf->arg_item))(c);
383         }
384         return rc;
385 }
386
387 int
388 config_get_vals(ConfigTable *cf, ConfigArgs *c)
389 {
390         int rc = 0;
391         struct berval bv;
392         void *ptr;
393
394         if ( cf->arg_type & ARG_IGNORED ) {
395                 return 1;
396         }
397
398         memset(&c->values, 0, sizeof(c->values));
399         c->rvalue_vals = NULL;
400         c->rvalue_nvals = NULL;
401         c->op = SLAP_CONFIG_EMIT;
402         c->type = cf->arg_type & ARGS_USERLAND;
403
404         if ( cf->arg_type & ARG_MAGIC ) {
405                 rc = (*((ConfigDriver*)cf->arg_item))(c);
406                 if ( rc ) return rc;
407         } else {
408                 if ( cf->arg_type & ARG_OFFSET ) {
409                         if ( c->be )
410                                 ptr = c->be->be_private;
411                         else if ( c->bi )
412                                 ptr = c->bi->bi_private;
413                         else
414                                 return 1;
415                         ptr = (void *)((char *)ptr + (long)cf->arg_item);
416                 } else {
417                         ptr = cf->arg_item;
418                 }
419                 
420                 switch(cf->arg_type & ARGS_TYPES) {
421                 case ARG_ON_OFF:
422                 case ARG_INT:   c->value_int = *(int *)ptr; break;
423                 case ARG_LONG:  c->value_long = *(long *)ptr; break;
424                 case ARG_BER_LEN_T:     c->value_ber_t = *(ber_len_t *)ptr; break;
425                 case ARG_STRING:
426                         if ( *(char **)ptr )
427                                 c->value_string = ch_strdup(*(char **)ptr);
428                         break;
429                 case ARG_BERVAL:
430                         ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
431                 }
432         }
433         if ( cf->arg_type & ARGS_TYPES) {
434                 bv.bv_len = 0;
435                 bv.bv_val = c->log;
436                 switch(cf->arg_type & ARGS_TYPES) {
437                 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
438                 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
439                 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
440                 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
441                         c->value_int ? "TRUE" : "FALSE"); break;
442                 case ARG_STRING:
443                         if ( c->value_string && c->value_string[0]) {
444                                 ber_str2bv( c->value_string, 0, 0, &bv);
445                         } else {
446                                 return 1;
447                         }
448                         break;
449                 case ARG_BERVAL:
450                         if ( !BER_BVISEMPTY( &c->value_bv )) {
451                                 bv = c->value_bv;
452                         } else {
453                                 return 1;
454                         }
455                         break;
456                 }
457                 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
458                         return 1;
459                 }
460                 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING )
461                         ber_bvarray_add(&c->rvalue_vals, &bv);
462                 else
463                         value_add_one(&c->rvalue_vals, &bv);
464         }
465         return rc;
466 }
467
468 int
469 init_config_attrs(ConfigTable *ct) {
470         LDAPAttributeType *at;
471         int i, code;
472         const char *err;
473
474         for (i=0; ct[i].name; i++ ) {
475                 int             freeit = 0;
476
477                 if ( !ct[i].attribute ) continue;
478                 at = ldap_str2attributetype( ct[i].attribute,
479                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
480                 if ( !at ) {
481                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
482                                 ct[i].attribute, ldap_scherr2str(code), err );
483                         return code;
484                 }
485
486                 code = at_add( at, 0, NULL, &err );
487                 if ( code ) {
488                         if ( code == SLAP_SCHERR_ATTR_DUP ) {
489                                 freeit = 1;
490
491                         } else {
492                                 ldap_attributetype_free( at );
493                                 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
494                                         ct[i].attribute, scherr2str(code), err );
495                                 return code;
496                         }
497                 }
498                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
499                 if ( freeit || code ) {
500                         ldap_attributetype_free( at );
501                 } else {
502                         ldap_memfree( at );
503                 }
504                 if ( code ) {
505                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
506                                 ct[i].attribute, err );
507                         return code;
508                 }
509         }
510
511         return 0;
512 }
513
514 int
515 init_config_ocs( ConfigOCs *ocs ) {
516         int i;
517
518         for (i=0;ocs[i].co_def;i++) {
519                 LDAPObjectClass *oc;
520                 int code;
521                 const char *err;
522
523                 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
524                         LDAP_SCHEMA_ALLOW_ALL );
525                 if ( !oc ) {
526                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
527                                 ocs[i].co_def, ldap_scherr2str(code), err );
528                         return code;
529                 }
530                 code = oc_add(oc,0,NULL,&err);
531                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
532                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
533                                 ocs[i].co_def, scherr2str(code), err );
534                         ldap_objectclass_free(oc);
535                         return code;
536                 }
537                 ocs[i].co_oc = oc_find(oc->oc_names[0]);
538                 if ( code )
539                         ldap_objectclass_free(oc);
540                 else
541                         ldap_memfree(oc);
542         }
543         return 0;
544 }
545
546 /* Split an LDIF line into space-separated tokens. Words may be grouped
547  * by quotes. A quoted string may begin in the middle of a word, but must
548  * end at the end of the word (be followed by whitespace or EOS). Any other
549  * quotes are passed through unchanged. All other characters are passed
550  * through unchanged.
551  */
552 static char *
553 strtok_quote_ldif( char **line )
554 {
555         char *beg, *ptr, *quote=NULL;
556         int inquote=0;
557
558         ptr = *line;
559
560         if ( !ptr || !*ptr )
561                 return NULL;
562
563         while( isspace( (unsigned char) *ptr )) ptr++;
564
565         if ( *ptr == '"' ) {
566                 inquote = 1;
567                 ptr++;
568         }
569
570         beg = ptr;
571
572         for (;*ptr;ptr++) {
573                 if ( *ptr == '"' ) {
574                         if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
575                                 *ptr++ = '\0';
576                                 break;
577                         }
578                         inquote = 1;
579                         quote = ptr;
580                         continue;
581                 }
582                 if ( inquote )
583                         continue;
584                 if ( isspace( (unsigned char) *ptr )) {
585                         *ptr++ = '\0';
586                         break;
587                 }
588         }
589         if ( quote ) {
590                 while ( quote < ptr ) {
591                         *quote = quote[1];
592                         quote++;
593                 }
594         }
595         if ( !*ptr ) {
596                 *line = NULL;
597         } else {
598                 while ( isspace( (unsigned char) *ptr )) ptr++;
599                 *line = ptr;
600         }
601         return beg;
602 }
603
604 static void
605 config_parse_ldif( ConfigArgs *c )
606 {
607         char *next;
608         c->tline = ch_strdup(c->line);
609         next = c->tline;
610
611         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
612                 c->argc++;
613                 if ( c->argc >= c->argv_size ) {
614                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
615                                 sizeof( *c->argv ));
616                         c->argv = tmp;
617                         c->argv_size += ARGS_STEP;
618                 }
619         }
620         c->argv[c->argc] = NULL;
621 }
622
623 int
624 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
625 {
626         int     rc = 0;
627
628         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
629                 ct->ad->ad_cname.bv_val, valx );
630         c->argc = 1;
631         c->argv[0] = ct->ad->ad_cname.bv_val;
632
633         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
634                 c->argv[c->argc] = c->line;
635                 c->argc++;
636                 c->argv[c->argc] = NULL;
637                 c->tline = NULL;
638         } else {
639                 config_parse_ldif( c );
640         }
641         rc = config_check_vals( ct, c, 1 );
642         ch_free( c->tline );
643         c->tline = NULL;
644
645         if ( rc )
646                 rc = LDAP_CONSTRAINT_VIOLATION;
647
648         return rc;
649 }
650
651 int
652 config_parse_add(ConfigTable *ct, ConfigArgs *c)
653 {
654         int     rc = 0;
655
656         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
657                 ct->ad->ad_cname.bv_val, c->valx );
658         c->argc = 1;
659         c->argv[0] = ct->ad->ad_cname.bv_val;
660
661         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
662                 c->argv[c->argc] = c->line;
663                 c->argc++;
664                 c->argv[c->argc] = NULL;
665                 c->tline = NULL;
666         } else {
667                 config_parse_ldif( c );
668         }
669         c->op = LDAP_MOD_ADD;
670         rc = config_add_vals( ct, c );
671         ch_free( c->tline );
672
673         return rc;
674 }
675
676 int
677 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
678 {
679         FILE *fp;
680         ConfigTable *ct;
681         ConfigArgs *c;
682         int rc;
683         struct stat s;
684
685         c = ch_calloc( 1, sizeof( ConfigArgs ) );
686         if ( c == NULL ) {
687                 return 1;
688         }
689
690         if ( depth ) {
691                 memcpy( c, cf, sizeof( ConfigArgs ) );
692         } else {
693                 c->depth = depth; /* XXX */
694                 c->bi = NULL;
695                 c->be = NULL;
696         }
697
698         c->valx = -1;
699         c->fname = fname;
700         init_config_argv( c );
701
702         if ( stat( fname, &s ) != 0 ) {
703                 ldap_syslog = 1;
704                 Debug(LDAP_DEBUG_ANY,
705                     "could not stat config file \"%s\": %s (%d)\n",
706                     fname, strerror(errno), errno);
707                 ch_free( c );
708                 return(1);
709         }
710
711         if ( !S_ISREG( s.st_mode ) ) {
712                 ldap_syslog = 1;
713                 Debug(LDAP_DEBUG_ANY,
714                     "regular file expected, got \"%s\"\n",
715                     fname, 0, 0 );
716                 ch_free( c );
717                 return(1);
718         }
719
720         fp = fopen( fname, "r" );
721         if ( fp == NULL ) {
722                 ldap_syslog = 1;
723                 Debug(LDAP_DEBUG_ANY,
724                     "could not open config file \"%s\": %s (%d)\n",
725                     fname, strerror(errno), errno);
726                 ch_free( c );
727                 return(1);
728         }
729
730         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
731
732         fp_getline_init(c);
733
734         c->tline = NULL;
735
736         while ( fp_getline( fp, c ) ) {
737                 /* skip comments and blank lines */
738                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
739                         continue;
740                 }
741
742                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
743                                 c->fname, c->lineno );
744
745                 c->argc = 0;
746                 ch_free( c->tline );
747                 if ( fp_parse_line( c ) ) {
748                         rc = 1;
749                         goto done;
750                 }
751
752                 if ( c->argc < 1 ) {
753                         Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
754                                 c->log, 0, 0);
755                         rc = 1;
756                         goto done;
757                 }
758
759                 c->op = SLAP_CONFIG_ADD;
760
761                 ct = config_find_keyword( cft, c );
762                 if ( ct ) {
763                         rc = config_add_vals( ct, c );
764                         if ( !rc ) continue;
765
766                         if ( rc & ARGS_USERLAND ) {
767                                 /* XXX a usertype would be opaque here */
768                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
769                                         c->log, c->argv[0], 0);
770                                 rc = 1;
771                                 goto done;
772
773                         } else if ( rc == ARG_BAD_CONF ) {
774                                 rc = 1;
775                                 goto done;
776                         }
777                         
778                 } else if ( c->bi && !c->be ) {
779                         rc = SLAP_CONF_UNKNOWN;
780                         if ( c->bi->bi_cf_ocs ) {
781                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
782                                 if ( ct ) {
783                                         rc = config_add_vals( ct, c );
784                                 }
785                         }
786                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
787                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
788                                         c->argc, c->argv);
789                         }
790                         if ( rc ) {
791                                 switch(rc) {
792                                 case SLAP_CONF_UNKNOWN:
793                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
794                                                 "<%s> inside backend info definition.\n",
795                                                 c->log, *c->argv, 0);
796                                 default:
797                                         rc = 1;
798                                         goto done;
799                                 }
800                         }
801
802                 } else if ( c->be && c->be != frontendDB ) {
803                         rc = SLAP_CONF_UNKNOWN;
804                         if ( c->be->be_cf_ocs ) {
805                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
806                                 if ( ct ) {
807                                         rc = config_add_vals( ct, c );
808                                 }
809                         }
810                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
811                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
812                                         c->argc, c->argv);
813                         }
814                         if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
815                         {
816                                 /* global overlays may need 
817                                  * definitions inside other databases...
818                                  */
819                                 rc = (*frontendDB->be_config)( frontendDB,
820                                         c->fname, (int)c->lineno, c->argc, c->argv );
821                         }
822
823                         switch ( rc ) {
824                         case 0:
825                                 break;
826
827                         case SLAP_CONF_UNKNOWN:
828                                 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
829                                         "<%s> inside backend database definition.\n",
830                                         c->log, *c->argv, 0);
831                                 
832                         default:
833                                 rc = 1;
834                                 goto done;
835                         }
836
837                 } else if ( frontendDB->be_config ) {
838                         rc = (*frontendDB->be_config)( frontendDB,
839                                 c->fname, (int)c->lineno, c->argc, c->argv);
840                         if ( rc ) {
841                                 switch(rc) {
842                                 case SLAP_CONF_UNKNOWN:
843                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
844                                                 "<%s> inside global database definition.\n",
845                                                 c->log, *c->argv, 0);
846
847                                 default:
848                                         rc = 1;
849                                         goto done;
850                                 }
851                         }
852                         
853                 } else {
854                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
855                                 "<%s> outside backend info and database definitions.\n",
856                                 c->log, *c->argv, 0);
857                         rc = 1;
858                         goto done;
859                 }
860         }
861
862         rc = 0;
863
864 done:
865         ch_free(c->tline);
866         fclose(fp);
867         ch_free(c->argv);
868         ch_free(c);
869         return(rc);
870 }
871
872 /* restrictops, allows, disallows, requires, loglevel */
873
874 int
875 verb_to_mask(const char *word, slap_verbmasks *v) {
876         int i;
877         for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
878                 if(!strcasecmp(word, v[i].word.bv_val)) break;
879         }
880         return(i);
881 }
882
883 int
884 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
885         int i, j;
886         for(i = 1; i < argc; i++) {
887                 j = verb_to_mask(argv[i], v);
888                 if(BER_BVISNULL(&v[j].word)) return i;
889                 while (!v[j].mask) j--;
890                 *m |= v[j].mask;
891         }
892         return(0);
893 }
894
895 /* Mask keywords that represent multiple bits should occur before single
896  * bit keywords in the verbmasks array.
897  */
898 int
899 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
900         int i, rc = 1;
901
902         if (m) {
903                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
904                         if (!v[i].mask) continue;
905                         if (( m & v[i].mask ) == v[i].mask ) {
906                                 value_add_one( bva, &v[i].word );
907                                 rc = 0;
908                                 m ^= v[i].mask;
909                                 if ( !m ) break;
910                         }
911                 }
912         }
913         return rc;
914 }
915
916 int
917 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
918 {
919         int             i;
920
921         assert( *vp == NULL );
922
923         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
924
925         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
926
927         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
928                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
929                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
930         }
931
932         BER_BVZERO( &(*vp)[ i ].word );
933
934         return 0;               
935 }
936
937 int
938 slap_verbmasks_destroy( slap_verbmasks *v )
939 {
940         int             i;
941
942         assert( v != NULL );
943
944         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
945                 ch_free( v[ i ].word.bv_val );
946         }
947
948         ch_free( v );
949
950         return 0;
951 }
952
953 int
954 slap_verbmasks_append(
955         slap_verbmasks  **vp,
956         slap_mask_t     m,
957         struct berval   *v,
958         slap_mask_t     *ignore )
959 {
960         int     i;
961
962         if ( !m ) {
963                 return LDAP_OPERATIONS_ERROR;
964         }
965
966         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
967                 if ( !(*vp)[ i ].mask ) continue;
968
969                 if ( ignore != NULL ) {
970                         int     j;
971
972                         for ( j = 0; ignore[ j ] != 0; j++ ) {
973                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
974                                         goto check_next;
975                                 }
976                         }
977                 }
978
979                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
980                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
981                                 /* already set; ignore */
982                                 return LDAP_SUCCESS;
983                         }
984                         /* conflicts */
985                         return LDAP_TYPE_OR_VALUE_EXISTS;
986                 }
987
988                 if ( m & (*vp)[ i ].mask ) {
989                         /* conflicts */
990                         return LDAP_CONSTRAINT_VIOLATION;
991                 }
992 check_next:;
993         }
994
995         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
996         ber_dupbv( &(*vp)[ i ].word, v );
997         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
998         BER_BVZERO( &(*vp)[ i + 1 ].word );
999
1000         return LDAP_SUCCESS;
1001 }
1002
1003 int
1004 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1005         int i;
1006
1007         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1008                 if ( m == v[i].mask ) {
1009                         if ( bv != NULL ) {
1010                                 *bv = v[i].word;
1011                         }
1012                         return i;
1013                 }
1014         }
1015         return -1;
1016 }
1017
1018 #ifdef HAVE_TLS
1019 static slap_verbmasks tlskey[] = {
1020         { BER_BVC("no"),        SB_TLS_OFF },
1021         { BER_BVC("yes"),       SB_TLS_ON },
1022         { BER_BVC("critical"),  SB_TLS_CRITICAL },
1023         { BER_BVNULL, 0 }
1024 };
1025 #endif
1026
1027 static slap_verbmasks methkey[] = {
1028         { BER_BVC("none"),      LDAP_AUTH_NONE },
1029         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
1030 #ifdef HAVE_CYRUS_SASL
1031         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
1032 #endif
1033         { BER_BVNULL, 0 }
1034 };
1035
1036 static slap_cf_aux_table bindkey[] = {
1037         { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1038         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
1039         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
1040         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1041         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1042         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1043         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1044         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1045         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1046 #ifdef HAVE_TLS
1047         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
1048
1049 #define aux_TLS (bindkey+10)    /* beginning of TLS keywords */
1050
1051         { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1052         { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1053         { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1054         { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1055         { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1056         { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1057 #ifdef HAVE_OPENSSL_CRL
1058         { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1059 #endif
1060 #endif
1061         { BER_BVNULL, 0, 0, 0, NULL }
1062 };
1063
1064 int
1065 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1066 {
1067         int rc = SLAP_CONF_UNKNOWN;
1068         slap_cf_aux_table *tab;
1069
1070         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1071                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1072                         char **cptr;
1073                         int *iptr, j;
1074                         unsigned *uptr;
1075                         long *lptr;
1076                         unsigned long *ulptr;
1077                         struct berval *bptr;
1078                         const char *val = word + tab->key.bv_len;
1079
1080                         switch ( tab->type ) {
1081                         case 's':
1082                                 cptr = (char **)((char *)dst + tab->off);
1083                                 *cptr = ch_strdup( val );
1084                                 rc = 0;
1085                                 break;
1086
1087                         case 'b':
1088                                 bptr = (struct berval *)((char *)dst + tab->off);
1089                                 ber_str2bv( val, 0, 1, bptr );
1090                                 rc = 0;
1091                                 break;
1092
1093                         case 'd':
1094                                 assert( tab->aux != NULL );
1095                                 iptr = (int *)((char *)dst + tab->off);
1096
1097                                 rc = 1;
1098                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1099                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1100                                                 *iptr = tab->aux[j].mask;
1101                                                 rc = 0;
1102                                         }
1103                                 }
1104                                 break;
1105
1106                         case 'i':
1107                                 iptr = (int *)((char *)dst + tab->off);
1108
1109                                 rc = lutil_atoix( iptr, val, 0 );
1110                                 break;
1111
1112                         case 'u':
1113                                 uptr = (unsigned *)((char *)dst + tab->off);
1114
1115                                 rc = lutil_atoux( uptr, val, 0 );
1116                                 break;
1117
1118                         case 'I':
1119                                 lptr = (long *)((char *)dst + tab->off);
1120
1121                                 rc = lutil_atolx( lptr, val, 0 );
1122                                 break;
1123
1124                         case 'U':
1125                                 ulptr = (unsigned long *)((char *)dst + tab->off);
1126
1127                                 rc = lutil_atoulx( ulptr, val, 0 );
1128                                 break;
1129                         }
1130
1131                         if ( rc ) {
1132                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1133                                         tabmsg, word, 0 );
1134                         }
1135                         
1136                         return rc;
1137                 }
1138         }
1139
1140         return rc;
1141 }
1142
1143 int
1144 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1145 {
1146         char buf[BUFSIZ], *ptr;
1147         slap_cf_aux_table *tab;
1148         struct berval tmp;
1149
1150         ptr = buf;
1151         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1152                 char **cptr;
1153                 int *iptr, i;
1154                 unsigned *uptr;
1155                 long *lptr;
1156                 unsigned long *ulptr;
1157                 struct berval *bptr;
1158
1159                 cptr = (char **)((char *)src + tab->off);
1160
1161                 switch ( tab->type ) {
1162                 case 'b':
1163                         bptr = (struct berval *)((char *)src + tab->off);
1164                         cptr = &bptr->bv_val;
1165                 case 's':
1166                         if ( *cptr ) {
1167                                 *ptr++ = ' ';
1168                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1169                                 if ( tab->quote ) *ptr++ = '"';
1170                                 ptr = lutil_strcopy( ptr, *cptr );
1171                                 if ( tab->quote ) *ptr++ = '"';
1172                         }
1173                         break;
1174
1175                 case 'd':
1176                         assert( tab->aux != NULL );
1177                         iptr = (int *)((char *)src + tab->off);
1178                 
1179                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1180                                 if ( *iptr == tab->aux[i].mask ) {
1181                                         *ptr++ = ' ';
1182                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1183                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1184                                         break;
1185                                 }
1186                         }
1187                         break;
1188
1189                 case 'i':
1190                         iptr = (int *)((char *)src + tab->off);
1191                         *ptr++ = ' ';
1192                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1193                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1194                         break;
1195
1196                 case 'u':
1197                         uptr = (unsigned *)((char *)src + tab->off);
1198                         *ptr++ = ' ';
1199                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1200                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1201                         break;
1202
1203                 case 'I':
1204                         lptr = (long *)((char *)src + tab->off);
1205                         *ptr++ = ' ';
1206                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1207                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1208                         break;
1209
1210                 case 'U':
1211                         ulptr = (unsigned long *)((char *)src + tab->off);
1212                         *ptr++ = ' ';
1213                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1214                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1215                         break;
1216
1217                 default:
1218                         assert( 0 );
1219                 }
1220         }
1221         tmp.bv_val = buf;
1222         tmp.bv_len = ptr - buf;
1223         ber_dupbv( bv, &tmp );
1224         return 0;
1225 }
1226
1227 int
1228 bindconf_parse( const char *word, slap_bindconf *bc )
1229 {
1230 #ifdef HAVE_TLS
1231         /* Detect TLS config changes explicitly */
1232         if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1233                 bc->sb_tls_do_init = 1;
1234                 return 0;
1235         }
1236 #endif
1237         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1238 }
1239
1240 int
1241 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1242 {
1243         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1244 }
1245
1246 void bindconf_free( slap_bindconf *bc ) {
1247         if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1248                 ch_free( bc->sb_uri.bv_val );
1249                 BER_BVZERO( &bc->sb_uri );
1250         }
1251         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1252                 ch_free( bc->sb_binddn.bv_val );
1253                 BER_BVZERO( &bc->sb_binddn );
1254         }
1255         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1256                 ch_free( bc->sb_cred.bv_val );
1257                 BER_BVZERO( &bc->sb_cred );
1258         }
1259         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1260                 ch_free( bc->sb_saslmech.bv_val );
1261                 BER_BVZERO( &bc->sb_saslmech );
1262         }
1263         if ( bc->sb_secprops ) {
1264                 ch_free( bc->sb_secprops );
1265                 bc->sb_secprops = NULL;
1266         }
1267         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1268                 ch_free( bc->sb_realm.bv_val );
1269                 BER_BVZERO( &bc->sb_realm );
1270         }
1271         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1272                 ch_free( bc->sb_authcId.bv_val );
1273                 BER_BVZERO( &bc->sb_authcId );
1274         }
1275         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1276                 ch_free( bc->sb_authzId.bv_val );
1277                 BER_BVZERO( &bc->sb_authzId );
1278         }
1279 #ifdef HAVE_TLS
1280         if ( bc->sb_tls_ctx ) {
1281                 SSL_CTX_free( bc->sb_tls_ctx );
1282                 bc->sb_tls_ctx = NULL;
1283         }
1284         if ( bc->sb_tls_cert ) {
1285                 ch_free( bc->sb_tls_cert );
1286                 bc->sb_tls_cert = NULL;
1287         }
1288         if ( bc->sb_tls_key ) {
1289                 ch_free( bc->sb_tls_key );
1290                 bc->sb_tls_key = NULL;
1291         }
1292         if ( bc->sb_tls_cacert ) {
1293                 ch_free( bc->sb_tls_cacert );
1294                 bc->sb_tls_cacert = NULL;
1295         }
1296         if ( bc->sb_tls_cacertdir ) {
1297                 ch_free( bc->sb_tls_cacertdir );
1298                 bc->sb_tls_cacertdir = NULL;
1299         }
1300         if ( bc->sb_tls_reqcert ) {
1301                 ch_free( bc->sb_tls_reqcert );
1302                 bc->sb_tls_reqcert = NULL;
1303         }
1304         if ( bc->sb_tls_cipher_suite ) {
1305                 ch_free( bc->sb_tls_cipher_suite );
1306                 bc->sb_tls_cipher_suite = NULL;
1307         }
1308 #ifdef HAVE_OPENSSL_CRL
1309         if ( bc->sb_tls_crlcheck ) {
1310                 ch_free( bc->sb_tls_crlcheck );
1311                 bc->sb_tls_crlcheck = NULL;
1312         }
1313 #endif
1314 #endif
1315 }
1316
1317 #ifdef HAVE_TLS
1318 static struct {
1319         const char *key;
1320         size_t offset;
1321         int opt;
1322 } bindtlsopts[] = {
1323         { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1324         { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1325         { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1326         { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1327         { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1328         {0, 0}
1329 };
1330
1331 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1332 {
1333         int i, rc, newctx = 0, res = 0;
1334         char *ptr = (char *)bc, **word;
1335
1336         bc->sb_tls_do_init = 0;
1337
1338         for (i=0; bindtlsopts[i].opt; i++) {
1339                 word = (char **)(ptr + bindtlsopts[i].offset);
1340                 if ( *word ) {
1341                         rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1342                         if ( rc ) {
1343                                 Debug( LDAP_DEBUG_ANY,
1344                                         "bindconf_tls_set: failed to set %s to %s\n",
1345                                                 bindtlsopts[i].key, *word, 0 );
1346                                 res = -1;
1347                         } else
1348                                 newctx = 1;
1349                 }
1350         }
1351         if ( bc->sb_tls_reqcert ) {
1352                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1353                         bc->sb_tls_reqcert );
1354                 if ( rc ) {
1355                         Debug( LDAP_DEBUG_ANY,
1356                                 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1357                                         bc->sb_tls_reqcert, 0, 0 );
1358                         res = -1;
1359                 } else
1360                         newctx = 1;
1361         }
1362 #ifdef HAVE_OPENSSL_CRL
1363         if ( bc->sb_tls_crlcheck ) {
1364                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1365                         bc->sb_tls_crlcheck );
1366                 if ( rc ) {
1367                         Debug( LDAP_DEBUG_ANY,
1368                                 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1369                                         bc->sb_tls_crlcheck, 0, 0 );
1370                         res = -1;
1371                 } else
1372                         newctx = 1;
1373         }
1374 #endif
1375         if ( newctx ) {
1376                 int opt = 0;
1377
1378                 if ( bc->sb_tls_ctx ) {
1379                         SSL_CTX_free( bc->sb_tls_ctx );
1380                         bc->sb_tls_ctx = NULL;
1381                 }
1382                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1383                 if ( rc )
1384                         res = rc;
1385                 else
1386                         ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1387         }
1388         
1389         return res;
1390 }
1391 #endif
1392
1393 /* -------------------------------------- */
1394
1395
1396 static char *
1397 strtok_quote( char *line, char *sep, char **quote_ptr )
1398 {
1399         int             inquote;
1400         char            *tmp;
1401         static char     *next;
1402
1403         *quote_ptr = NULL;
1404         if ( line != NULL ) {
1405                 next = line;
1406         }
1407         while ( *next && strchr( sep, *next ) ) {
1408                 next++;
1409         }
1410
1411         if ( *next == '\0' ) {
1412                 next = NULL;
1413                 return( NULL );
1414         }
1415         tmp = next;
1416
1417         for ( inquote = 0; *next; ) {
1418                 switch ( *next ) {
1419                 case '"':
1420                         if ( inquote ) {
1421                                 inquote = 0;
1422                         } else {
1423                                 inquote = 1;
1424                         }
1425                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1426                         break;
1427
1428                 case '\\':
1429                         if ( next[1] )
1430                                 AC_MEMCPY( next,
1431                                             next + 1, strlen( next + 1 ) + 1 );
1432                         next++;         /* dont parse the escaped character */
1433                         break;
1434
1435                 default:
1436                         if ( ! inquote ) {
1437                                 if ( strchr( sep, *next ) != NULL ) {
1438                                         *quote_ptr = next;
1439                                         *next++ = '\0';
1440                                         return( tmp );
1441                                 }
1442                         }
1443                         next++;
1444                         break;
1445                 }
1446         }
1447
1448         return( tmp );
1449 }
1450
1451 static char     buf[BUFSIZ];
1452 static char     *line;
1453 static size_t lmax, lcur;
1454
1455 #define CATLINE( buf ) \
1456         do { \
1457                 size_t len = strlen( buf ); \
1458                 while ( lcur + len + 1 > lmax ) { \
1459                         lmax += BUFSIZ; \
1460                         line = (char *) ch_realloc( line, lmax ); \
1461                 } \
1462                 strcpy( line + lcur, buf ); \
1463                 lcur += len; \
1464         } while( 0 )
1465
1466 static void
1467 fp_getline_init(ConfigArgs *c) {
1468         c->lineno = -1;
1469         buf[0] = '\0';
1470 }
1471
1472 static int
1473 fp_getline( FILE *fp, ConfigArgs *c )
1474 {
1475         char    *p;
1476
1477         lcur = 0;
1478         CATLINE(buf);
1479         c->lineno++;
1480
1481         /* avoid stack of bufs */
1482         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1483                 buf[0] = '\0';
1484                 c->line = line;
1485                 return(1);
1486         }
1487
1488         while ( fgets( buf, sizeof( buf ), fp ) ) {
1489                 p = strchr( buf, '\n' );
1490                 if ( p ) {
1491                         if ( p > buf && p[-1] == '\r' ) {
1492                                 --p;
1493                         }
1494                         *p = '\0';
1495                 }
1496                 /* XXX ugly */
1497                 c->line = line;
1498                 if ( line[0]
1499                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1500                                 && p[-1] != '\\' )
1501                 {
1502                         p[0] = '\0';
1503                         lcur--;
1504                         
1505                 } else {
1506                         if ( !isspace( (unsigned char)buf[0] ) ) {
1507                                 return(1);
1508                         }
1509                         buf[0] = ' ';
1510                 }
1511                 CATLINE(buf);
1512                 c->lineno++;
1513         }
1514
1515         buf[0] = '\0';
1516         c->line = line;
1517         return(line[0] ? 1 : 0);
1518 }
1519
1520 static int
1521 fp_parse_line(ConfigArgs *c)
1522 {
1523         char *token;
1524         static char *const hide[] = {
1525                 "rootpw", "replica", "syncrepl",  /* in slapd */
1526                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1527                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1528                 "pseudorootpw",  /* in back-meta */
1529                 "dbpasswd",  /* in back-sql */
1530                 NULL
1531         };
1532         char *quote_ptr;
1533         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1534
1535         c->tline = ch_strdup(c->line);
1536         token = strtok_quote(c->tline, " \t", &quote_ptr);
1537
1538         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1539         if(quote_ptr) *quote_ptr = ' ';
1540         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1541                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1542         if(quote_ptr) *quote_ptr = '\0';
1543
1544         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1545                 if(c->argc >= c->argv_size) {
1546                         char **tmp;
1547                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1548                         if(!tmp) {
1549                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1550                                 return -1;
1551                         }
1552                         c->argv = tmp;
1553                         c->argv_size += ARGS_STEP;
1554                 }
1555                 if(token == NULL)
1556                         break;
1557                 c->argv[c->argc++] = token;
1558         }
1559         c->argv[c->argc] = NULL;
1560         return(0);
1561 }
1562
1563 void
1564 config_destroy( )
1565 {
1566         ucdata_unload( UCDATA_ALL );
1567         if ( frontendDB ) {
1568                 /* NOTE: in case of early exit, frontendDB can be NULL */
1569                 if ( frontendDB->be_schemandn.bv_val )
1570                         free( frontendDB->be_schemandn.bv_val );
1571                 if ( frontendDB->be_schemadn.bv_val )
1572                         free( frontendDB->be_schemadn.bv_val );
1573                 if ( frontendDB->be_acl )
1574                         acl_destroy( frontendDB->be_acl, NULL );
1575         }
1576         free( line );
1577         if ( slapd_args_file )
1578                 free ( slapd_args_file );
1579         if ( slapd_pid_file )
1580                 free ( slapd_pid_file );
1581         if ( default_passwd_hash )
1582                 ldap_charray_free( default_passwd_hash );
1583 }
1584
1585 char **
1586 slap_str2clist( char ***out, char *in, const char *brkstr )
1587 {
1588         char    *str;
1589         char    *s;
1590         char    *lasts;
1591         int     i, j;
1592         char    **new;
1593
1594         /* find last element in list */
1595         for (i = 0; *out && (*out)[i]; i++);
1596
1597         /* protect the input string from strtok */
1598         str = ch_strdup( in );
1599
1600         if ( *str == '\0' ) {
1601                 free( str );
1602                 return( *out );
1603         }
1604
1605         /* Count words in string */
1606         j=1;
1607         for ( s = str; *s; s++ ) {
1608                 if ( strchr( brkstr, *s ) != NULL ) {
1609                         j++;
1610                 }
1611         }
1612
1613         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1614         new = *out + i;
1615         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1616                 s != NULL;
1617                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1618         {
1619                 *new = ch_strdup( s );
1620                 new++;
1621         }
1622
1623         *new = NULL;
1624         free( str );
1625         return( *out );
1626 }
1627
1628 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1629         int argc, char **argv )
1630 {
1631         ConfigArgs c = { 0 };
1632         ConfigTable *ct;
1633         int rc;
1634
1635         c.be = be;
1636         c.fname = fname;
1637         c.lineno = lineno;
1638         c.argc = argc;
1639         c.argv = argv;
1640         c.valx = -1;
1641         c.line = line;
1642         c.op = SLAP_CONFIG_ADD;
1643         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1644
1645         rc = SLAP_CONF_UNKNOWN;
1646         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1647         if ( ct )
1648                 rc = config_add_vals( ct, &c );
1649         return rc;
1650 }