]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
ITS#5276 don't stack config state for includes
[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-2007 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
40 #ifndef S_ISREG
41 #define S_ISREG(m)      (((m) & _S_IFMT) == _S_IFREG)
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 #include "slap.h"
49 #ifdef LDAP_SLAPI
50 #include "slapi/slapi.h"
51 #endif
52 #include "lutil.h"
53 #include "lutil_ldap.h"
54 #include "config.h"
55
56 #define ARGS_STEP       512
57
58 /*
59  * defaults for various global variables
60  */
61 slap_mask_t             global_allows = 0;
62 slap_mask_t             global_disallows = 0;
63 int             global_gentlehup = 0;
64 int             global_idletimeout = 0;
65 char    *global_host = NULL;
66 char    *global_realm = NULL;
67 char    *sasl_host = NULL;
68 char            **default_passwd_hash = NULL;
69 struct berval default_search_base = BER_BVNULL;
70 struct berval default_search_nbase = BER_BVNULL;
71
72 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
73 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
74
75 int     slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
76 int     slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
77
78 char   *slapd_pid_file  = NULL;
79 char   *slapd_args_file = NULL;
80
81 int use_reverse_lookup = 0;
82
83 #ifdef LDAP_SLAPI
84 int slapi_plugins_used = 0;
85 #endif
86
87 static int fp_getline(FILE *fp, ConfigArgs *c);
88 static void fp_getline_init(ConfigArgs *c);
89 static int fp_parse_line(ConfigArgs *c);
90
91 static char     *strtok_quote(char *line, char *sep, char **quote_ptr);
92 static char *strtok_quote_ldif(char **line);
93
94 ConfigArgs *
95 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
96 {
97         ConfigArgs *c;
98         c = ch_calloc( 1, sizeof( ConfigArgs ) );
99         if ( c == NULL ) return(NULL);
100         c->be     = be; 
101         c->fname  = fname;
102         c->argc   = argc;
103         c->argv   = argv; 
104         c->lineno = lineno;
105         snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
106         return(c);
107 }
108
109 void
110 init_config_argv( ConfigArgs *c )
111 {
112         c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
113         c->argv_size = ARGS_STEP + 1;
114 }
115
116 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
117         int i;
118
119         for(i = 0; Conf[i].name; i++)
120                 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
121                         (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
122         if ( !Conf[i].name ) return NULL;
123         return Conf+i;
124 }
125
126 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
127         int rc, arg_user, arg_type, arg_syn, iarg;
128         unsigned uiarg;
129         long larg;
130         ber_len_t barg;
131         
132         if(Conf->arg_type == ARG_IGNORED) {
133                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
134                         c->log, Conf->name, 0);
135                 return(0);
136         }
137         arg_type = Conf->arg_type & ARGS_TYPES;
138         arg_user = Conf->arg_type & ARGS_USERLAND;
139         arg_syn = Conf->arg_type & ARGS_SYNTAX;
140
141         if((arg_type == ARG_DN) && c->argc == 1) {
142                 c->argc = 2;
143                 c->argv[1] = "";
144         }
145         if(Conf->min_args && (c->argc < Conf->min_args)) {
146                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> missing <%s> argument",
147                         c->argv[0], Conf->what );
148                 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", c->log, c->cr_msg, 0 );
149                 return(ARG_BAD_CONF);
150         }
151         if(Conf->max_args && (c->argc > Conf->max_args)) {
152                 char    *ignored = " ignored";
153
154                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> extra cruft after <%s>",
155                         c->argv[0], Conf->what );
156
157                 ignored = "";
158                 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s%s.\n",
159                                 c->log, c->cr_msg, ignored );
160                 return(ARG_BAD_CONF);
161         }
162         if((arg_syn & ARG_DB) && !c->be) {
163                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> only allowed within database declaration",
164                         c->argv[0] );
165                 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
166                         c->log, c->cr_msg, 0);
167                 return(ARG_BAD_CONF);
168         }
169         if((arg_syn & ARG_PRE_BI) && c->bi) {
170                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any backend %sdeclaration",
171                         c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
172                 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
173                         c->log, c->cr_msg, 0 );
174                 return(ARG_BAD_CONF);
175         }
176         if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
177                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any database declaration",
178                         c->argv[0] );
179                 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
180                         c->log, c->cr_msg, 0);
181                 return(ARG_BAD_CONF);
182         }
183         if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
184                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> old format not supported", c->argv[0] );
185                 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
186                         c->log, c->cr_msg, 0);
187                 return(ARG_BAD_CONF);
188         }
189         if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
190                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid config_table, arg_item is NULL",
191                         c->argv[0] );
192                 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
193                         c->log, c->cr_msg, 0);
194                 return(ARG_BAD_CONF);
195         }
196         c->type = arg_user;
197         memset(&c->values, 0, sizeof(c->values));
198         if(arg_type == ARG_STRING) {
199                 if ( !check_only )
200                         c->value_string = ch_strdup(c->argv[1]);
201         } else if(arg_type == ARG_BERVAL) {
202                 if ( !check_only )
203                         ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
204         } else if(arg_type == ARG_DN) {
205                 struct berval bv;
206                 ber_str2bv( c->argv[1], 0, 0, &bv );
207                 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
208                 if ( rc != LDAP_SUCCESS ) {
209                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid DN %d (%s)",
210                                 c->argv[0], rc, ldap_err2string( rc ));
211                         Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
212                         return(ARG_BAD_CONF);
213                 }
214                 if ( check_only ) {
215                         ch_free( c->value_ndn.bv_val );
216                         ch_free( c->value_dn.bv_val );
217                 }
218         } else {        /* all numeric */
219                 int j;
220                 iarg = 0; larg = 0; barg = 0;
221                 switch(arg_type) {
222                         case ARG_INT:
223                                 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
224                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
225                                                 "<%s> unable to parse \"%s\" as int",
226                                                 c->argv[0], c->argv[1] );
227                                         Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
228                                                 c->log, c->cr_msg, 0);
229                                         return(ARG_BAD_CONF);
230                                 }
231                                 break;
232                         case ARG_UINT:
233                                 if ( lutil_atoux( &uiarg, c->argv[1], 0 ) != 0 ) {
234                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
235                                                 "<%s> unable to parse \"%s\" as unsigned int",
236                                                 c->argv[0], c->argv[1] );
237                                         Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
238                                                 c->log, c->cr_msg, 0);
239                                         return(ARG_BAD_CONF);
240                                 }
241                                 break;
242                         case ARG_LONG:
243                                 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
244                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
245                                                 "<%s> unable to parse \"%s\" as long",
246                                                 c->argv[0], c->argv[1] );
247                                         Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
248                                                 c->log, c->cr_msg, 0);
249                                         return(ARG_BAD_CONF);
250                                 }
251                                 break;
252                         case ARG_BER_LEN_T: {
253                                 unsigned long   l;
254                                 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
255                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
256                                                 "<%s> unable to parse \"%s\" as ber_len_t",
257                                                 c->argv[0], c->argv[1] );
258                                         Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
259                                                 c->log, c->cr_msg, 0);
260                                         return(ARG_BAD_CONF);
261                                 }
262                                 barg = (ber_len_t)l;
263                                 } break;
264                         case ARG_ON_OFF:
265                                 if (c->argc == 1) {
266                                         iarg = 1;
267                                 } else if ( !strcasecmp(c->argv[1], "on") ||
268                                         !strcasecmp(c->argv[1], "true") ||
269                                         !strcasecmp(c->argv[1], "yes") )
270                                 {
271                                         iarg = 1;
272                                 } else if ( !strcasecmp(c->argv[1], "off") ||
273                                         !strcasecmp(c->argv[1], "false") ||
274                                         !strcasecmp(c->argv[1], "no") )
275                                 {
276                                         iarg = 0;
277                                 } else {
278                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
279                                                 c->argv[0] );
280                                         Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
281                                                 c->log, c->cr_msg, 0 );
282                                         return(ARG_BAD_CONF);
283                                 }
284                                 break;
285                 }
286                 j = (arg_type & ARG_NONZERO) ? 1 : 0;
287                 if(iarg < j && larg < j && barg < j ) {
288                         larg = larg ? larg : (barg ? barg : iarg);
289                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
290                                 c->argv[0] );
291                         Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
292                                 c->log, c->cr_msg, 0 );
293                         return(ARG_BAD_CONF);
294                 }
295                 switch(arg_type) {
296                         case ARG_ON_OFF:
297                         case ARG_INT:           c->value_int = iarg;            break;
298                         case ARG_UINT:          c->value_uint = uiarg;          break;
299                         case ARG_LONG:          c->value_long = larg;           break;
300                         case ARG_BER_LEN_T:     c->value_ber_t = barg;          break;
301                 }
302         }
303         return 0;
304 }
305
306 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
307         int rc, arg_type;
308         void *ptr = NULL;
309
310         arg_type = Conf->arg_type;
311         if(arg_type & ARG_MAGIC) {
312                 if(!c->be) c->be = frontendDB;
313                 c->cr_msg[0] = '\0';
314                 rc = (*((ConfigDriver*)Conf->arg_item))(c);
315 #if 0
316                 if(c->be == frontendDB) c->be = NULL;
317 #endif
318                 if(rc) {
319                         if ( !c->cr_msg[0] ) {
320                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> handler exited with %d",
321                                         c->argv[0], rc );
322                                 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
323                                         c->log, c->cr_msg, 0 );
324                         }
325                         return(ARG_BAD_CONF);
326                 }
327                 return(0);
328         }
329         if(arg_type & ARG_OFFSET) {
330                 if (c->be && c->table == Cft_Database)
331                         ptr = c->be->be_private;
332                 else if (c->bi)
333                         ptr = c->bi->bi_private;
334                 else {
335                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> offset is missing base pointer",
336                                 c->argv[0] );
337                         Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
338                                 c->log, c->cr_msg, 0);
339                         return(ARG_BAD_CONF);
340                 }
341                 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
342         } else if (arg_type & ARGS_TYPES) {
343                 ptr = Conf->arg_item;
344         }
345         if(arg_type & ARGS_TYPES)
346                 switch(arg_type & ARGS_TYPES) {
347                         case ARG_ON_OFF:
348                         case ARG_INT:           *(int*)ptr = c->value_int;                      break;
349                         case ARG_UINT:          *(unsigned*)ptr = c->value_uint;                        break;
350                         case ARG_LONG:          *(long*)ptr = c->value_long;                    break;
351                         case ARG_BER_LEN_T:     *(ber_len_t*)ptr = c->value_ber_t;                      break;
352                         case ARG_STRING: {
353                                 char *cc = *(char**)ptr;
354                                 if(cc) {
355                                         if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
356                                                 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
357                                                         c->log, Conf->name, 0 );
358                                                 return(ARG_BAD_CONF);
359                                         }
360                                         ch_free(cc);
361                                 }
362                                 *(char **)ptr = c->value_string;
363                                 break;
364                                 }
365                         case ARG_BERVAL:
366                                 *(struct berval *)ptr = c->value_bv;
367                                 break;
368                 }
369         return(0);
370 }
371
372 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
373         int rc, arg_type;
374
375         arg_type = Conf->arg_type;
376         if(arg_type == ARG_IGNORED) {
377                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
378                         c->log, Conf->name, 0);
379                 return(0);
380         }
381         rc = config_check_vals( Conf, c, 0 );
382         if ( rc ) return rc;
383         return config_set_vals( Conf, c );
384 }
385
386 int
387 config_del_vals(ConfigTable *cf, ConfigArgs *c)
388 {
389         int rc = 0;
390
391         /* If there is no handler, just ignore it */
392         if ( cf->arg_type & ARG_MAGIC ) {
393                 c->op = LDAP_MOD_DELETE;
394                 c->type = cf->arg_type & ARGS_USERLAND;
395                 rc = (*((ConfigDriver*)cf->arg_item))(c);
396         }
397         return rc;
398 }
399
400 int
401 config_get_vals(ConfigTable *cf, ConfigArgs *c)
402 {
403         int rc = 0;
404         struct berval bv;
405         void *ptr;
406
407         if ( cf->arg_type & ARG_IGNORED ) {
408                 return 1;
409         }
410
411         memset(&c->values, 0, sizeof(c->values));
412         c->rvalue_vals = NULL;
413         c->rvalue_nvals = NULL;
414         c->op = SLAP_CONFIG_EMIT;
415         c->type = cf->arg_type & ARGS_USERLAND;
416
417         if ( cf->arg_type & ARG_MAGIC ) {
418                 rc = (*((ConfigDriver*)cf->arg_item))(c);
419                 if ( rc ) return rc;
420         } else {
421                 if ( cf->arg_type & ARG_OFFSET ) {
422                         if (c->be && c->table == Cft_Database)
423                                 ptr = c->be->be_private;
424                         else if ( c->bi )
425                                 ptr = c->bi->bi_private;
426                         else
427                                 return 1;
428                         ptr = (void *)((char *)ptr + (long)cf->arg_item);
429                 } else {
430                         ptr = cf->arg_item;
431                 }
432                 
433                 switch(cf->arg_type & ARGS_TYPES) {
434                 case ARG_ON_OFF:
435                 case ARG_INT:   c->value_int = *(int *)ptr; break;
436                 case ARG_UINT:  c->value_uint = *(unsigned *)ptr; break;
437                 case ARG_LONG:  c->value_long = *(long *)ptr; break;
438                 case ARG_BER_LEN_T:     c->value_ber_t = *(ber_len_t *)ptr; break;
439                 case ARG_STRING:
440                         if ( *(char **)ptr )
441                                 c->value_string = ch_strdup(*(char **)ptr);
442                         break;
443                 case ARG_BERVAL:
444                         ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
445                 }
446         }
447         if ( cf->arg_type & ARGS_TYPES) {
448                 bv.bv_len = 0;
449                 bv.bv_val = c->log;
450                 switch(cf->arg_type & ARGS_TYPES) {
451                 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
452                 case ARG_UINT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%u", c->value_uint); break;
453                 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
454                 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
455                 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
456                         c->value_int ? "TRUE" : "FALSE"); break;
457                 case ARG_STRING:
458                         if ( c->value_string && c->value_string[0]) {
459                                 ber_str2bv( c->value_string, 0, 0, &bv);
460                         } else {
461                                 return 1;
462                         }
463                         break;
464                 case ARG_BERVAL:
465                         if ( !BER_BVISEMPTY( &c->value_bv )) {
466                                 bv = c->value_bv;
467                         } else {
468                                 return 1;
469                         }
470                         break;
471                 default:
472                         bv.bv_val = NULL;
473                         break;
474                 }
475                 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
476                         return 1;
477                 }
478                 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
479                         ber_bvarray_add(&c->rvalue_vals, &bv);
480                 } else if ( !BER_BVISNULL( &bv ) ) {
481                         value_add_one(&c->rvalue_vals, &bv);
482                 }
483                 /* else: maybe c->rvalue_vals already set? */
484         }
485         return rc;
486 }
487
488 int
489 init_config_attrs(ConfigTable *ct) {
490         int i, code;
491
492         for (i=0; ct[i].name; i++ ) {
493                 if ( !ct[i].attribute ) continue;
494                 code = register_at( ct[i].attribute, &ct[i].ad, 1 );
495                 if ( code ) {
496                         fprintf( stderr, "init_config_attrs: register_at failed\n" );
497                         return code;
498                 }
499 #ifndef LDAP_DEVEL
500                 ct[i].ad->ad_type->sat_flags |= SLAP_AT_HIDE;
501 #endif
502         }
503
504         return 0;
505 }
506
507 int
508 init_config_ocs( ConfigOCs *ocs ) {
509         int i, code;
510
511         for (i=0;ocs[i].co_def;i++) {
512                 code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 );
513                 if ( code ) {
514                         fprintf( stderr, "init_config_ocs: register_oc failed\n" );
515                         return code;
516                 }
517 #ifndef LDAP_DEVEL
518                 ocs[i].co_oc->soc_flags |= SLAP_OC_HIDE;
519 #endif
520         }
521         return 0;
522 }
523
524 /* Split an LDIF line into space-separated tokens. Words may be grouped
525  * by quotes. A quoted string may begin in the middle of a word, but must
526  * end at the end of the word (be followed by whitespace or EOS). Any other
527  * quotes are passed through unchanged. All other characters are passed
528  * through unchanged.
529  */
530 static char *
531 strtok_quote_ldif( char **line )
532 {
533         char *beg, *ptr, *quote=NULL;
534         int inquote=0;
535
536         ptr = *line;
537
538         if ( !ptr || !*ptr )
539                 return NULL;
540
541         while( isspace( (unsigned char) *ptr )) ptr++;
542
543         if ( *ptr == '"' ) {
544                 inquote = 1;
545                 ptr++;
546         }
547
548         beg = ptr;
549
550         for (;*ptr;ptr++) {
551                 if ( *ptr == '"' ) {
552                         if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
553                                 *ptr++ = '\0';
554                                 break;
555                         }
556                         inquote = 1;
557                         quote = ptr;
558                         continue;
559                 }
560                 if ( inquote )
561                         continue;
562                 if ( isspace( (unsigned char) *ptr )) {
563                         *ptr++ = '\0';
564                         break;
565                 }
566         }
567         if ( quote ) {
568                 while ( quote < ptr ) {
569                         *quote = quote[1];
570                         quote++;
571                 }
572         }
573         if ( !*ptr ) {
574                 *line = NULL;
575         } else {
576                 while ( isspace( (unsigned char) *ptr )) ptr++;
577                 *line = ptr;
578         }
579         return beg;
580 }
581
582 static void
583 config_parse_ldif( ConfigArgs *c )
584 {
585         char *next;
586         c->tline = ch_strdup(c->line);
587         next = c->tline;
588
589         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
590                 c->argc++;
591                 if ( c->argc >= c->argv_size ) {
592                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
593                                 sizeof( *c->argv ));
594                         c->argv = tmp;
595                         c->argv_size += ARGS_STEP;
596                 }
597         }
598         c->argv[c->argc] = NULL;
599 }
600
601 int
602 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
603 {
604         int     rc = 0;
605
606         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
607                 ct->ad->ad_cname.bv_val, valx );
608         c->argc = 1;
609         c->argv[0] = ct->ad->ad_cname.bv_val;
610
611         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
612                 c->argv[c->argc] = c->line;
613                 c->argc++;
614                 c->argv[c->argc] = NULL;
615                 c->tline = NULL;
616         } else {
617                 config_parse_ldif( c );
618         }
619         rc = config_check_vals( ct, c, 1 );
620         ch_free( c->tline );
621         c->tline = NULL;
622
623         if ( rc )
624                 rc = LDAP_CONSTRAINT_VIOLATION;
625
626         return rc;
627 }
628
629 int
630 config_parse_add(ConfigTable *ct, ConfigArgs *c, int valx)
631 {
632         int     rc = 0;
633
634         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
635                 ct->ad->ad_cname.bv_val, valx );
636         c->argc = 1;
637         c->argv[0] = ct->ad->ad_cname.bv_val;
638
639         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
640                 c->argv[c->argc] = c->line;
641                 c->argc++;
642                 c->argv[c->argc] = NULL;
643                 c->tline = NULL;
644         } else {
645                 config_parse_ldif( c );
646         }
647         c->op = LDAP_MOD_ADD;
648         rc = config_add_vals( ct, c );
649         ch_free( c->tline );
650
651         return rc;
652 }
653
654 int
655 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
656 {
657         FILE *fp;
658         ConfigTable *ct;
659         ConfigArgs *c;
660         int rc;
661         struct stat s;
662
663         if ( cf ) {
664                 c = cf;
665         } else {
666                 c = ch_calloc( 1, sizeof( ConfigArgs ) );
667
668                 c->valx = -1;
669                 c->fname = fname;
670                 init_config_argv( c );
671         }
672
673         if ( stat( fname, &s ) != 0 ) {
674                 ldap_syslog = 1;
675                 Debug(LDAP_DEBUG_ANY,
676                     "could not stat config file \"%s\": %s (%d)\n",
677                     fname, strerror(errno), errno);
678                 if ( !cf )
679                         ch_free( c );
680                 return(1);
681         }
682
683         if ( !S_ISREG( s.st_mode ) ) {
684                 ldap_syslog = 1;
685                 Debug(LDAP_DEBUG_ANY,
686                     "regular file expected, got \"%s\"\n",
687                     fname, 0, 0 );
688                 if ( !cf )
689                         ch_free( c );
690                 return(1);
691         }
692
693         fp = fopen( fname, "r" );
694         if ( fp == NULL ) {
695                 ldap_syslog = 1;
696                 Debug(LDAP_DEBUG_ANY,
697                     "could not open config file \"%s\": %s (%d)\n",
698                     fname, strerror(errno), errno);
699                 if ( !cf )
700                         ch_free( c );
701                 return(1);
702         }
703
704         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
705
706         fp_getline_init(c);
707
708         c->tline = NULL;
709
710         while ( fp_getline( fp, c ) ) {
711                 /* skip comments and blank lines */
712                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
713                         continue;
714                 }
715
716                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
717                                 c->fname, c->lineno );
718
719                 c->argc = 0;
720                 ch_free( c->tline );
721                 if ( fp_parse_line( c ) ) {
722                         rc = 1;
723                         goto done;
724                 }
725
726                 if ( c->argc < 1 ) {
727                         Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
728                                 c->log, 0, 0);
729                         rc = 1;
730                         goto done;
731                 }
732
733                 c->op = SLAP_CONFIG_ADD;
734
735                 ct = config_find_keyword( cft, c );
736                 if ( ct ) {
737                         c->table = Cft_Global;
738                         rc = config_add_vals( ct, c );
739                         if ( !rc ) continue;
740
741                         if ( rc & ARGS_USERLAND ) {
742                                 /* XXX a usertype would be opaque here */
743                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
744                                         c->log, c->argv[0], 0);
745                                 rc = 1;
746                                 goto done;
747
748                         } else if ( rc == ARG_BAD_CONF ) {
749                                 rc = 1;
750                                 goto done;
751                         }
752                         
753                 } else if ( c->bi && !c->be ) {
754                         rc = SLAP_CONF_UNKNOWN;
755                         if ( c->bi->bi_cf_ocs ) {
756                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
757                                 if ( ct ) {
758                                         c->table = c->bi->bi_cf_ocs->co_type;
759                                         rc = config_add_vals( ct, c );
760                                 }
761                         }
762                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
763                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
764                                         c->argc, c->argv);
765                         }
766                         if ( rc ) {
767                                 switch(rc) {
768                                 case SLAP_CONF_UNKNOWN:
769                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
770                                                 "<%s> inside backend info definition.\n",
771                                                 c->log, *c->argv, 0);
772                                 default:
773                                         rc = 1;
774                                         goto done;
775                                 }
776                         }
777
778                 } else if ( c->be && c->be != frontendDB ) {
779                         rc = SLAP_CONF_UNKNOWN;
780                         if ( c->be->be_cf_ocs ) {
781                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
782                                 if ( ct ) {
783                                         c->table = c->be->be_cf_ocs->co_type;
784                                         rc = config_add_vals( ct, c );
785                                 }
786                         }
787                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
788                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
789                                         c->argc, c->argv);
790                         }
791                         if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
792                         {
793                                 /* global overlays may need 
794                                  * definitions inside other databases...
795                                  */
796                                 rc = (*frontendDB->be_config)( frontendDB,
797                                         c->fname, (int)c->lineno, c->argc, c->argv );
798                         }
799
800                         switch ( rc ) {
801                         case 0:
802                                 break;
803
804                         case SLAP_CONF_UNKNOWN:
805                                 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
806                                         "<%s> inside backend database definition.\n",
807                                         c->log, *c->argv, 0);
808                                 
809                         default:
810                                 rc = 1;
811                                 goto done;
812                         }
813
814                 } else if ( frontendDB->be_config ) {
815                         rc = (*frontendDB->be_config)( frontendDB,
816                                 c->fname, (int)c->lineno, c->argc, c->argv);
817                         if ( rc ) {
818                                 switch(rc) {
819                                 case SLAP_CONF_UNKNOWN:
820                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
821                                                 "<%s> inside global database definition.\n",
822                                                 c->log, *c->argv, 0);
823
824                                 default:
825                                         rc = 1;
826                                         goto done;
827                                 }
828                         }
829                         
830                 } else {
831                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
832                                 "<%s> outside backend info and database definitions.\n",
833                                 c->log, *c->argv, 0);
834                         rc = 1;
835                         goto done;
836                 }
837         }
838
839         rc = 0;
840
841 done:
842         fclose(fp);
843         if ( !cf ) {
844                 ch_free(c->tline);
845                 ch_free(c->argv);
846                 ch_free(c);
847         }
848         return(rc);
849 }
850
851 /* restrictops, allows, disallows, requires, loglevel */
852
853 int
854 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
855         int i;
856         for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
857                 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
858         }
859         return(i);
860 }
861
862 int
863 verb_to_mask(const char *word, slap_verbmasks *v) {
864         struct berval   bword;
865         ber_str2bv( word, 0, 0, &bword );
866         return bverb_to_mask( &bword, v );
867 }
868
869 int
870 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
871         int i, j;
872         for(i = 1; i < argc; i++) {
873                 j = verb_to_mask(argv[i], v);
874                 if(BER_BVISNULL(&v[j].word)) return i;
875                 while (!v[j].mask) j--;
876                 *m |= v[j].mask;
877         }
878         return(0);
879 }
880
881 /* Mask keywords that represent multiple bits should occur before single
882  * bit keywords in the verbmasks array.
883  */
884 int
885 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
886         int i, rc = 1;
887
888         if (m) {
889                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
890                         if (!v[i].mask) continue;
891                         if (( m & v[i].mask ) == v[i].mask ) {
892                                 value_add_one( bva, &v[i].word );
893                                 rc = 0;
894                                 m ^= v[i].mask;
895                                 if ( !m ) break;
896                         }
897                 }
898         }
899         return rc;
900 }
901
902 int
903 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
904 {
905         int             i;
906
907         assert( *vp == NULL );
908
909         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
910
911         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
912
913         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
914                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
915                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
916         }
917
918         BER_BVZERO( &(*vp)[ i ].word );
919
920         return 0;               
921 }
922
923 int
924 slap_verbmasks_destroy( slap_verbmasks *v )
925 {
926         int             i;
927
928         assert( v != NULL );
929
930         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
931                 ch_free( v[ i ].word.bv_val );
932         }
933
934         ch_free( v );
935
936         return 0;
937 }
938
939 int
940 slap_verbmasks_append(
941         slap_verbmasks  **vp,
942         slap_mask_t     m,
943         struct berval   *v,
944         slap_mask_t     *ignore )
945 {
946         int     i;
947
948         if ( !m ) {
949                 return LDAP_OPERATIONS_ERROR;
950         }
951
952         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
953                 if ( !(*vp)[ i ].mask ) continue;
954
955                 if ( ignore != NULL ) {
956                         int     j;
957
958                         for ( j = 0; ignore[ j ] != 0; j++ ) {
959                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
960                                         goto check_next;
961                                 }
962                         }
963                 }
964
965                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
966                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
967                                 /* already set; ignore */
968                                 return LDAP_SUCCESS;
969                         }
970                         /* conflicts */
971                         return LDAP_TYPE_OR_VALUE_EXISTS;
972                 }
973
974                 if ( m & (*vp)[ i ].mask ) {
975                         /* conflicts */
976                         return LDAP_CONSTRAINT_VIOLATION;
977                 }
978 check_next:;
979         }
980
981         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
982         ber_dupbv( &(*vp)[ i ].word, v );
983         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
984         BER_BVZERO( &(*vp)[ i + 1 ].word );
985
986         return LDAP_SUCCESS;
987 }
988
989 int
990 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
991         int i;
992
993         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
994                 if ( m == v[i].mask ) {
995                         if ( bv != NULL ) {
996                                 *bv = v[i].word;
997                         }
998                         return i;
999                 }
1000         }
1001         return -1;
1002 }
1003
1004 #ifdef HAVE_TLS
1005 static slap_verbmasks tlskey[] = {
1006         { BER_BVC("no"),        SB_TLS_OFF },
1007         { BER_BVC("yes"),       SB_TLS_ON },
1008         { BER_BVC("critical"),  SB_TLS_CRITICAL },
1009         { BER_BVNULL, 0 }
1010 };
1011
1012 static slap_verbmasks crlkeys[] = {
1013                 { BER_BVC("none"),      LDAP_OPT_X_TLS_CRL_NONE },
1014                 { BER_BVC("peer"),      LDAP_OPT_X_TLS_CRL_PEER },
1015                 { BER_BVC("all"),       LDAP_OPT_X_TLS_CRL_ALL },
1016                 { BER_BVNULL, 0 }
1017         };
1018
1019 static slap_verbmasks vfykeys[] = {
1020                 { BER_BVC("never"),     LDAP_OPT_X_TLS_NEVER },
1021                 { BER_BVC("demand"),    LDAP_OPT_X_TLS_DEMAND },
1022                 { BER_BVC("try"),       LDAP_OPT_X_TLS_TRY },
1023                 { BER_BVC("hard"),      LDAP_OPT_X_TLS_HARD },
1024                 { BER_BVNULL, 0 }
1025         };
1026 #endif
1027
1028 static slap_verbmasks methkey[] = {
1029         { BER_BVC("none"),      LDAP_AUTH_NONE },
1030         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
1031 #ifdef HAVE_CYRUS_SASL
1032         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
1033 #endif
1034         { BER_BVNULL, 0 }
1035 };
1036
1037 static slap_verbmasks versionkey[] = {
1038         { BER_BVC("2"),         LDAP_VERSION2 },
1039         { BER_BVC("3"),         LDAP_VERSION3 },
1040         { BER_BVNULL, 0 }
1041 };
1042
1043 static slap_cf_aux_table bindkey[] = {
1044         { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1045         { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'i', 0, versionkey },
1046         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1047         { BER_BVC("timeout="), offsetof(slap_bindconf, sb_timeout_api), 'i', 0, NULL },
1048         { BER_BVC("network-timeout="), offsetof(slap_bindconf, sb_timeout_net), 'i', 0, NULL },
1049         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, (slap_verbmasks *)dnNormalize },
1050         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1051         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1052         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1053         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1054         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 1, NULL },
1055         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, (slap_verbmasks *)authzNormalize },
1056 #ifdef HAVE_TLS
1057         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1058
1059         /* NOTE: replace "13" with the actual index
1060          * of the first TLS-related line */
1061 #define aux_TLS (bindkey+13)    /* beginning of TLS keywords */
1062
1063         { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1064         { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1065         { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1066         { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1067         { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1068         { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1069 #ifdef HAVE_OPENSSL_CRL
1070         { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1071 #endif
1072 #endif
1073         { BER_BVNULL, 0, 0, 0, NULL }
1074 };
1075
1076 /*
1077  * 's': char *
1078  * 'b': struct berval; if !NULL, normalize using ((slap_mr_normalize_func *)aux)
1079  * 'i': int; if !NULL, compute using ((slap_verbmasks *)aux)
1080  * 'u': unsigned
1081  * 'I': long
1082  * 'U': unsigned long
1083  */
1084
1085 int
1086 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1087 {
1088         int rc = SLAP_CONF_UNKNOWN;
1089         slap_cf_aux_table *tab;
1090
1091         for ( tab = tab0; !BER_BVISNULL( &tab->key ); tab++ ) {
1092                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len ) ) {
1093                         char **cptr;
1094                         int *iptr, j;
1095                         unsigned *uptr;
1096                         long *lptr;
1097                         unsigned long *ulptr;
1098                         struct berval *bptr;
1099                         const char *val = word + tab->key.bv_len;
1100
1101                         switch ( tab->type ) {
1102                         case 's':
1103                                 cptr = (char **)((char *)dst + tab->off);
1104                                 *cptr = ch_strdup( val );
1105                                 rc = 0;
1106                                 break;
1107
1108                         case 'b':
1109                                 bptr = (struct berval *)((char *)dst + tab->off);
1110                                 if ( tab->aux != NULL ) {
1111                                         struct berval   dn;
1112                                         slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1113
1114                                         ber_str2bv( val, 0, 0, &dn );
1115                                         rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1116
1117                                 } else {
1118                                         ber_str2bv( val, 0, 1, bptr );
1119                                         rc = 0;
1120                                 }
1121                                 break;
1122
1123                         case 'i':
1124                                 iptr = (int *)((char *)dst + tab->off);
1125
1126                                 if ( tab->aux != NULL ) {
1127                                         slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1128
1129                                         assert( aux != NULL );
1130
1131                                         rc = 1;
1132                                         for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1133                                                 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1134                                                         *iptr = aux[j].mask;
1135                                                         rc = 0;
1136                                                         break;
1137                                                 }
1138                                         }
1139
1140                                 } else {
1141                                         rc = lutil_atoix( iptr, val, 0 );
1142                                 }
1143                                 break;
1144
1145                         case 'u':
1146                                 uptr = (unsigned *)((char *)dst + tab->off);
1147
1148                                 rc = lutil_atoux( uptr, val, 0 );
1149                                 break;
1150
1151                         case 'I':
1152                                 lptr = (long *)((char *)dst + tab->off);
1153
1154                                 rc = lutil_atolx( lptr, val, 0 );
1155                                 break;
1156
1157                         case 'U':
1158                                 ulptr = (unsigned long *)((char *)dst + tab->off);
1159
1160                                 rc = lutil_atoulx( ulptr, val, 0 );
1161                                 break;
1162                         }
1163
1164                         if ( rc ) {
1165                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1166                                         tabmsg, word, 0 );
1167                         }
1168                         
1169                         return rc;
1170                 }
1171         }
1172
1173         return rc;
1174 }
1175
1176 int
1177 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1178 {
1179         char buf[AC_LINE_MAX], *ptr;
1180         slap_cf_aux_table *tab;
1181         struct berval tmp;
1182
1183         ptr = buf;
1184         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1185                 char **cptr;
1186                 int *iptr, i;
1187                 unsigned *uptr;
1188                 long *lptr;
1189                 unsigned long *ulptr;
1190                 struct berval *bptr;
1191
1192                 cptr = (char **)((char *)src + tab->off);
1193
1194                 switch ( tab->type ) {
1195                 case 'b':
1196                         bptr = (struct berval *)((char *)src + tab->off);
1197                         cptr = &bptr->bv_val;
1198
1199                 case 's':
1200                         if ( *cptr ) {
1201                                 *ptr++ = ' ';
1202                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1203                                 if ( tab->quote ) *ptr++ = '"';
1204                                 ptr = lutil_strcopy( ptr, *cptr );
1205                                 if ( tab->quote ) *ptr++ = '"';
1206                         }
1207                         break;
1208
1209                 case 'i':
1210                         iptr = (int *)((char *)src + tab->off);
1211
1212                         if ( tab->aux != NULL ) {
1213                                 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1214
1215                                 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1216                                         if ( *iptr == aux[i].mask ) {
1217                                                 *ptr++ = ' ';
1218                                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1219                                                 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1220                                                 break;
1221                                         }
1222                                 }
1223
1224                         } else {
1225                                 *ptr++ = ' ';
1226                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1227                                 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1228                         }
1229                         break;
1230
1231                 case 'u':
1232                         uptr = (unsigned *)((char *)src + tab->off);
1233                         *ptr++ = ' ';
1234                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1235                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1236                         break;
1237
1238                 case 'I':
1239                         lptr = (long *)((char *)src + tab->off);
1240                         *ptr++ = ' ';
1241                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1242                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1243                         break;
1244
1245                 case 'U':
1246                         ulptr = (unsigned long *)((char *)src + tab->off);
1247                         *ptr++ = ' ';
1248                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1249                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1250                         break;
1251
1252                 default:
1253                         assert( 0 );
1254                 }
1255         }
1256         tmp.bv_val = buf;
1257         tmp.bv_len = ptr - buf;
1258         ber_dupbv( bv, &tmp );
1259         return 0;
1260 }
1261
1262 int
1263 slap_tls_get_config( LDAP *ld, int opt, char **val )
1264 {
1265         slap_verbmasks *keys;
1266         int i, ival;
1267
1268         *val = NULL;
1269         switch( opt ) {
1270 #ifdef HAVE_TLS
1271         case LDAP_OPT_X_TLS_CRLCHECK:
1272                 keys = crlkeys;
1273                 break;
1274         case LDAP_OPT_X_TLS_REQUIRE_CERT:
1275                 keys = vfykeys;
1276                 break;
1277 #endif
1278         default:
1279                 return -1;
1280         }
1281         ldap_pvt_tls_get_option( ld, opt, &ival );
1282         for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
1283                 if (keys[i].mask == ival) {
1284                         *val = ch_strdup( keys[i].word.bv_val );
1285                         return 0;
1286                 }
1287         }
1288         return -1;
1289 }
1290
1291 int
1292 bindconf_tls_parse( const char *word, slap_bindconf *bc )
1293 {
1294 #ifdef HAVE_TLS
1295         if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1296                 bc->sb_tls_do_init = 1;
1297                 return 0;
1298         }
1299 #endif
1300         return -1;
1301 }
1302
1303 int
1304 bindconf_tls_unparse( slap_bindconf *bc, struct berval *bv )
1305 {
1306 #ifdef HAVE_TLS
1307         return slap_cf_aux_table_unparse( bc, bv, aux_TLS );
1308 #endif
1309         return -1;
1310 }
1311
1312 int
1313 bindconf_parse( const char *word, slap_bindconf *bc )
1314 {
1315 #ifdef HAVE_TLS
1316         /* Detect TLS config changes explicitly */
1317         if ( bindconf_tls_parse( word, bc ) == 0 ) {
1318                 return 0;
1319         }
1320 #endif
1321         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1322 }
1323
1324 int
1325 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1326 {
1327         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1328 }
1329
1330 void bindconf_free( slap_bindconf *bc ) {
1331         if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1332                 ch_free( bc->sb_uri.bv_val );
1333                 BER_BVZERO( &bc->sb_uri );
1334         }
1335         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1336                 ch_free( bc->sb_binddn.bv_val );
1337                 BER_BVZERO( &bc->sb_binddn );
1338         }
1339         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1340                 ch_free( bc->sb_cred.bv_val );
1341                 BER_BVZERO( &bc->sb_cred );
1342         }
1343         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1344                 ch_free( bc->sb_saslmech.bv_val );
1345                 BER_BVZERO( &bc->sb_saslmech );
1346         }
1347         if ( bc->sb_secprops ) {
1348                 ch_free( bc->sb_secprops );
1349                 bc->sb_secprops = NULL;
1350         }
1351         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1352                 ch_free( bc->sb_realm.bv_val );
1353                 BER_BVZERO( &bc->sb_realm );
1354         }
1355         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1356                 ch_free( bc->sb_authcId.bv_val );
1357                 BER_BVZERO( &bc->sb_authcId );
1358         }
1359         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1360                 ch_free( bc->sb_authzId.bv_val );
1361                 BER_BVZERO( &bc->sb_authzId );
1362         }
1363 #ifdef HAVE_TLS
1364         if ( bc->sb_tls_cert ) {
1365                 ch_free( bc->sb_tls_cert );
1366                 bc->sb_tls_cert = NULL;
1367         }
1368         if ( bc->sb_tls_key ) {
1369                 ch_free( bc->sb_tls_key );
1370                 bc->sb_tls_key = NULL;
1371         }
1372         if ( bc->sb_tls_cacert ) {
1373                 ch_free( bc->sb_tls_cacert );
1374                 bc->sb_tls_cacert = NULL;
1375         }
1376         if ( bc->sb_tls_cacertdir ) {
1377                 ch_free( bc->sb_tls_cacertdir );
1378                 bc->sb_tls_cacertdir = NULL;
1379         }
1380         if ( bc->sb_tls_reqcert ) {
1381                 ch_free( bc->sb_tls_reqcert );
1382                 bc->sb_tls_reqcert = NULL;
1383         }
1384         if ( bc->sb_tls_cipher_suite ) {
1385                 ch_free( bc->sb_tls_cipher_suite );
1386                 bc->sb_tls_cipher_suite = NULL;
1387         }
1388 #ifdef HAVE_OPENSSL_CRL
1389         if ( bc->sb_tls_crlcheck ) {
1390                 ch_free( bc->sb_tls_crlcheck );
1391                 bc->sb_tls_crlcheck = NULL;
1392         }
1393 #endif
1394 #endif
1395 }
1396
1397 void
1398 bindconf_tls_defaults( slap_bindconf *bc )
1399 {
1400 #ifdef HAVE_TLS
1401         if ( bc->sb_tls_do_init ) {
1402                 if ( !bc->sb_tls_cacert )
1403                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE,
1404                                 &bc->sb_tls_cacert );
1405                 if ( !bc->sb_tls_cacertdir )
1406                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR,
1407                                 &bc->sb_tls_cacertdir );
1408                 if ( !bc->sb_tls_cert )
1409                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE,
1410                                 &bc->sb_tls_cert );
1411                 if ( !bc->sb_tls_key )
1412                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE,
1413                                 &bc->sb_tls_key );
1414                 if ( !bc->sb_tls_cipher_suite )
1415                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE,
1416                                 &bc->sb_tls_cipher_suite );
1417                 if ( !bc->sb_tls_reqcert )
1418                         bc->sb_tls_reqcert = ch_strdup("demand");
1419 #ifdef HAVE_OPENSSL_CRL
1420                 if ( !bc->sb_tls_crlcheck )
1421                         slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK,
1422                                 &bc->sb_tls_crlcheck );
1423 #endif
1424         }
1425 #endif
1426 }
1427
1428 #ifdef HAVE_TLS
1429 static struct {
1430         const char *key;
1431         size_t offset;
1432         int opt;
1433 } bindtlsopts[] = {
1434         { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1435         { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1436         { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1437         { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1438         { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1439         {0, 0}
1440 };
1441
1442 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1443 {
1444         int i, rc, newctx = 0, res = 0;
1445         char *ptr = (char *)bc, **word;
1446
1447         bc->sb_tls_do_init = 0;
1448
1449         for (i=0; bindtlsopts[i].opt; i++) {
1450                 word = (char **)(ptr + bindtlsopts[i].offset);
1451                 if ( *word ) {
1452                         rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1453                         if ( rc ) {
1454                                 Debug( LDAP_DEBUG_ANY,
1455                                         "bindconf_tls_set: failed to set %s to %s\n",
1456                                                 bindtlsopts[i].key, *word, 0 );
1457                                 res = -1;
1458                         } else
1459                                 newctx = 1;
1460                 }
1461         }
1462         if ( bc->sb_tls_reqcert ) {
1463                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1464                         bc->sb_tls_reqcert );
1465                 if ( rc ) {
1466                         Debug( LDAP_DEBUG_ANY,
1467                                 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1468                                         bc->sb_tls_reqcert, 0, 0 );
1469                         res = -1;
1470                 } else
1471                         newctx = 1;
1472         }
1473 #ifdef HAVE_OPENSSL_CRL
1474         if ( bc->sb_tls_crlcheck ) {
1475                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1476                         bc->sb_tls_crlcheck );
1477                 if ( rc ) {
1478                         Debug( LDAP_DEBUG_ANY,
1479                                 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1480                                         bc->sb_tls_crlcheck, 0, 0 );
1481                         res = -1;
1482                 } else
1483                         newctx = 1;
1484         }
1485 #endif
1486         if ( newctx ) {
1487                 int opt = 0;
1488
1489                 if ( bc->sb_tls_ctx ) {
1490                         ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1491                         bc->sb_tls_ctx = NULL;
1492                 }
1493                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1494                 if ( rc )
1495                         res = rc;
1496                 else
1497                         ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1498         }
1499         
1500         return res;
1501 }
1502 #endif
1503
1504 /*
1505  * connect to a client using the bindconf data
1506  * note: should move "version" into bindconf...
1507  */
1508 int
1509 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
1510 {
1511         LDAP            *ld = NULL;
1512         int             rc;
1513         struct timeval tv;
1514
1515         /* Init connection to master */
1516         rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1517         if ( rc != LDAP_SUCCESS ) {
1518                 Debug( LDAP_DEBUG_ANY,
1519                         "slap_client_connect: "
1520                         "ldap_initialize(%s) failed (%d)\n",
1521                         sb->sb_uri.bv_val, rc, 0 );
1522                 return rc;
1523         }
1524
1525         if ( sb->sb_version != 0 ) {
1526                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1527                         (const void *)&sb->sb_version );
1528         }
1529
1530         if ( sb->sb_timeout_api ) {
1531                 tv.tv_sec = sb->sb_timeout_api;
1532                 tv.tv_usec = 0;
1533                 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv );
1534         }
1535
1536         if ( sb->sb_timeout_net ) {
1537                 tv.tv_sec = sb->sb_timeout_net;
1538                 tv.tv_usec = 0;
1539                 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv );
1540         }
1541
1542 #ifdef HAVE_TLS
1543         if ( sb->sb_tls_do_init ) {
1544                 rc = bindconf_tls_set( sb, ld );
1545
1546         } else if ( sb->sb_tls_ctx ) {
1547                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1548                         sb->sb_tls_ctx );
1549         }
1550
1551         if ( rc ) {
1552                 Debug( LDAP_DEBUG_ANY,
1553                         "slap_client_connect: "
1554                         "URI=%s TLS context initialization failed (%d)\n",
1555                         sb->sb_uri.bv_val, rc, 0 );
1556                 return rc;
1557         }
1558 #endif
1559
1560         /* Bind */
1561         if ( sb->sb_tls ) {
1562                 rc = ldap_start_tls_s( ld, NULL, NULL );
1563                 if ( rc != LDAP_SUCCESS ) {
1564                         Debug( LDAP_DEBUG_ANY,
1565                                 "slap_client_connect: URI=%s "
1566                                 "%s, ldap_start_tls failed (%d)\n",
1567                                 sb->sb_uri.bv_val,
1568                                 sb->sb_tls == SB_TLS_CRITICAL ?
1569                                         "Error" : "Warning",
1570                                 rc );
1571                         if ( sb->sb_tls == SB_TLS_CRITICAL ) {
1572                                 goto done;
1573                         }
1574                 }
1575         }
1576
1577         if ( sb->sb_method == LDAP_AUTH_SASL ) {
1578 #ifdef HAVE_CYRUS_SASL
1579                 void *defaults;
1580
1581                 if ( sb->sb_secprops != NULL ) {
1582                         rc = ldap_set_option( ld,
1583                                 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
1584
1585                         if( rc != LDAP_OPT_SUCCESS ) {
1586                                 Debug( LDAP_DEBUG_ANY,
1587                                         "slap_client_connect: "
1588                                         "error, ldap_set_option "
1589                                         "(%s,SECPROPS,\"%s\") failed!\n",
1590                                         sb->sb_uri.bv_val, sb->sb_secprops, 0 );
1591                                 goto done;
1592                         }
1593                 }
1594
1595                 defaults = lutil_sasl_defaults( ld,
1596                         sb->sb_saslmech.bv_val,
1597                         sb->sb_realm.bv_val,
1598                         sb->sb_authcId.bv_val,
1599                         sb->sb_cred.bv_val,
1600                         sb->sb_authzId.bv_val );
1601                 if ( defaults == NULL ) {
1602                         rc = LDAP_OTHER;
1603                         goto done;
1604                 }
1605
1606                 rc = ldap_sasl_interactive_bind_s( ld,
1607                                 sb->sb_binddn.bv_val,
1608                                 sb->sb_saslmech.bv_val,
1609                                 NULL, NULL,
1610                                 LDAP_SASL_QUIET,
1611                                 lutil_sasl_interact,
1612                                 defaults );
1613
1614                 lutil_sasl_freedefs( defaults );
1615
1616                 /* FIXME: different error behaviors according to
1617                  *      1) return code
1618                  *      2) on err policy : exit, retry, backoff ...
1619                  */
1620                 if ( rc != LDAP_SUCCESS ) {
1621                         static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
1622
1623                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
1624                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
1625                                 sb->sb_uri.bv_val, rc, 0 );
1626
1627                         /* FIXME (see above comment) */
1628                         /* if Kerberos credentials cache is not active, retry */
1629                         if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
1630                                 rc == LDAP_LOCAL_ERROR )
1631                         {
1632                                 rc = LDAP_SERVER_DOWN;
1633                         }
1634
1635                         goto done;
1636                 }
1637 #else /* HAVE_CYRUS_SASL */
1638                 /* Should never get here, we trapped this at config time */
1639                 assert(0);
1640                 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
1641                 rc = LDAP_OTHER;
1642                 goto done;
1643 #endif
1644
1645         } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
1646                 rc = ldap_sasl_bind_s( ld,
1647                         sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
1648                         &sb->sb_cred, NULL, NULL, NULL );
1649                 if ( rc != LDAP_SUCCESS ) {
1650                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
1651                                 "URI=%s DN=\"%s\" "
1652                                 "ldap_sasl_bind_s failed (%d)\n",
1653                                 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
1654                         goto done;
1655                 }
1656         }
1657
1658 done:;
1659         if ( rc ) {
1660                 if ( ld ) {
1661                         ldap_unbind_ext( ld, NULL, NULL );
1662                         *ldp = NULL;
1663                 }
1664
1665         } else {
1666                 *ldp = ld;
1667         }
1668
1669         return rc;
1670 }
1671
1672 /* -------------------------------------- */
1673
1674
1675 static char *
1676 strtok_quote( char *line, char *sep, char **quote_ptr )
1677 {
1678         int             inquote;
1679         char            *tmp;
1680         static char     *next;
1681
1682         *quote_ptr = NULL;
1683         if ( line != NULL ) {
1684                 next = line;
1685         }
1686         while ( *next && strchr( sep, *next ) ) {
1687                 next++;
1688         }
1689
1690         if ( *next == '\0' ) {
1691                 next = NULL;
1692                 return( NULL );
1693         }
1694         tmp = next;
1695
1696         for ( inquote = 0; *next; ) {
1697                 switch ( *next ) {
1698                 case '"':
1699                         if ( inquote ) {
1700                                 inquote = 0;
1701                         } else {
1702                                 inquote = 1;
1703                         }
1704                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1705                         break;
1706
1707                 case '\\':
1708                         if ( next[1] )
1709                                 AC_MEMCPY( next,
1710                                             next + 1, strlen( next + 1 ) + 1 );
1711                         next++;         /* dont parse the escaped character */
1712                         break;
1713
1714                 default:
1715                         if ( ! inquote ) {
1716                                 if ( strchr( sep, *next ) != NULL ) {
1717                                         *quote_ptr = next;
1718                                         *next++ = '\0';
1719                                         return( tmp );
1720                                 }
1721                         }
1722                         next++;
1723                         break;
1724                 }
1725         }
1726
1727         return( tmp );
1728 }
1729
1730 static char     buf[AC_LINE_MAX];
1731 static char     *line;
1732 static size_t lmax, lcur;
1733
1734 #define CATLINE( buf ) \
1735         do { \
1736                 size_t len = strlen( buf ); \
1737                 while ( lcur + len + 1 > lmax ) { \
1738                         lmax += AC_LINE_MAX; \
1739                         line = (char *) ch_realloc( line, lmax ); \
1740                 } \
1741                 strcpy( line + lcur, buf ); \
1742                 lcur += len; \
1743         } while( 0 )
1744
1745 static void
1746 fp_getline_init(ConfigArgs *c) {
1747         c->lineno = -1;
1748         buf[0] = '\0';
1749 }
1750
1751 static int
1752 fp_getline( FILE *fp, ConfigArgs *c )
1753 {
1754         char    *p;
1755
1756         lcur = 0;
1757         CATLINE(buf);
1758         c->lineno++;
1759
1760         /* avoid stack of bufs */
1761         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1762                 buf[0] = '\0';
1763                 c->line = line;
1764                 return(1);
1765         }
1766
1767         while ( fgets( buf, sizeof( buf ), fp ) ) {
1768                 p = strchr( buf, '\n' );
1769                 if ( p ) {
1770                         if ( p > buf && p[-1] == '\r' ) {
1771                                 --p;
1772                         }
1773                         *p = '\0';
1774                 }
1775                 /* XXX ugly */
1776                 c->line = line;
1777                 if ( line[0]
1778                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1779                                 && p[-1] != '\\' )
1780                 {
1781                         p[0] = '\0';
1782                         lcur--;
1783                         
1784                 } else {
1785                         if ( !isspace( (unsigned char)buf[0] ) ) {
1786                                 return(1);
1787                         }
1788                         buf[0] = ' ';
1789                 }
1790                 CATLINE(buf);
1791                 c->lineno++;
1792         }
1793
1794         buf[0] = '\0';
1795         c->line = line;
1796         return(line[0] ? 1 : 0);
1797 }
1798
1799 static int
1800 fp_parse_line(ConfigArgs *c)
1801 {
1802         char *token;
1803         static char *const hide[] = {
1804                 "rootpw", "replica", "syncrepl",  /* in slapd */
1805                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1806                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1807                 "pseudorootpw",  /* in back-meta */
1808                 "dbpasswd",  /* in back-sql */
1809                 NULL
1810         };
1811         char *quote_ptr;
1812         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1813
1814         c->tline = ch_strdup(c->line);
1815         token = strtok_quote(c->tline, " \t", &quote_ptr);
1816
1817         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1818         if(quote_ptr) *quote_ptr = ' ';
1819         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1820                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1821         if(quote_ptr) *quote_ptr = '\0';
1822
1823         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1824                 if(c->argc >= c->argv_size) {
1825                         char **tmp;
1826                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1827                         if(!tmp) {
1828                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1829                                 return -1;
1830                         }
1831                         c->argv = tmp;
1832                         c->argv_size += ARGS_STEP;
1833                 }
1834                 if(token == NULL)
1835                         break;
1836                 c->argv[c->argc++] = token;
1837         }
1838         c->argv[c->argc] = NULL;
1839         return(0);
1840 }
1841
1842 void
1843 config_destroy( )
1844 {
1845         ucdata_unload( UCDATA_ALL );
1846         if ( frontendDB ) {
1847                 /* NOTE: in case of early exit, frontendDB can be NULL */
1848                 if ( frontendDB->be_schemandn.bv_val )
1849                         free( frontendDB->be_schemandn.bv_val );
1850                 if ( frontendDB->be_schemadn.bv_val )
1851                         free( frontendDB->be_schemadn.bv_val );
1852                 if ( frontendDB->be_acl )
1853                         acl_destroy( frontendDB->be_acl, NULL );
1854         }
1855         free( line );
1856         if ( slapd_args_file )
1857                 free ( slapd_args_file );
1858         if ( slapd_pid_file )
1859                 free ( slapd_pid_file );
1860         if ( default_passwd_hash )
1861                 ldap_charray_free( default_passwd_hash );
1862 }
1863
1864 char **
1865 slap_str2clist( char ***out, char *in, const char *brkstr )
1866 {
1867         char    *str;
1868         char    *s;
1869         char    *lasts;
1870         int     i, j;
1871         char    **new;
1872
1873         /* find last element in list */
1874         for (i = 0; *out && (*out)[i]; i++);
1875
1876         /* protect the input string from strtok */
1877         str = ch_strdup( in );
1878
1879         if ( *str == '\0' ) {
1880                 free( str );
1881                 return( *out );
1882         }
1883
1884         /* Count words in string */
1885         j=1;
1886         for ( s = str; *s; s++ ) {
1887                 if ( strchr( brkstr, *s ) != NULL ) {
1888                         j++;
1889                 }
1890         }
1891
1892         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1893         new = *out + i;
1894         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1895                 s != NULL;
1896                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1897         {
1898                 *new = ch_strdup( s );
1899                 new++;
1900         }
1901
1902         *new = NULL;
1903         free( str );
1904         return( *out );
1905 }
1906
1907 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1908         int argc, char **argv )
1909 {
1910         ConfigArgs c = { 0 };
1911         ConfigTable *ct;
1912         int rc;
1913
1914         c.be = be;
1915         c.fname = fname;
1916         c.lineno = lineno;
1917         c.argc = argc;
1918         c.argv = argv;
1919         c.valx = -1;
1920         c.line = line;
1921         c.op = SLAP_CONFIG_ADD;
1922         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1923
1924         rc = SLAP_CONF_UNKNOWN;
1925         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1926         if ( ct ) {
1927                 c.table = be->be_cf_ocs->co_type;
1928                 rc = config_add_vals( ct, &c );
1929         }
1930         return rc;
1931 }