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