]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
integer indexing must be unsigned
[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         c = ch_calloc( 1, sizeof( ConfigArgs ) );
664         if ( c == NULL ) {
665                 return 1;
666         }
667
668         if ( depth ) {
669                 memcpy( c, cf, sizeof( ConfigArgs ) );
670         } else {
671                 c->depth = depth; /* XXX */
672                 c->bi = NULL;
673                 c->be = NULL;
674         }
675
676         c->valx = -1;
677         c->fname = fname;
678         init_config_argv( c );
679
680         if ( stat( fname, &s ) != 0 ) {
681                 ldap_syslog = 1;
682                 Debug(LDAP_DEBUG_ANY,
683                     "could not stat config file \"%s\": %s (%d)\n",
684                     fname, strerror(errno), errno);
685                 ch_free( c );
686                 return(1);
687         }
688
689         if ( !S_ISREG( s.st_mode ) ) {
690                 ldap_syslog = 1;
691                 Debug(LDAP_DEBUG_ANY,
692                     "regular file expected, got \"%s\"\n",
693                     fname, 0, 0 );
694                 ch_free( c );
695                 return(1);
696         }
697
698         fp = fopen( fname, "r" );
699         if ( fp == NULL ) {
700                 ldap_syslog = 1;
701                 Debug(LDAP_DEBUG_ANY,
702                     "could not open config file \"%s\": %s (%d)\n",
703                     fname, strerror(errno), errno);
704                 ch_free( c );
705                 return(1);
706         }
707
708         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
709
710         fp_getline_init(c);
711
712         c->tline = NULL;
713
714         while ( fp_getline( fp, c ) ) {
715                 /* skip comments and blank lines */
716                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
717                         continue;
718                 }
719
720                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
721                                 c->fname, c->lineno );
722
723                 c->argc = 0;
724                 ch_free( c->tline );
725                 if ( fp_parse_line( c ) ) {
726                         rc = 1;
727                         goto done;
728                 }
729
730                 if ( c->argc < 1 ) {
731                         Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
732                                 c->log, 0, 0);
733                         rc = 1;
734                         goto done;
735                 }
736
737                 c->op = SLAP_CONFIG_ADD;
738
739                 ct = config_find_keyword( cft, c );
740                 if ( ct ) {
741                         c->table = Cft_Global;
742                         rc = config_add_vals( ct, c );
743                         if ( !rc ) continue;
744
745                         if ( rc & ARGS_USERLAND ) {
746                                 /* XXX a usertype would be opaque here */
747                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
748                                         c->log, c->argv[0], 0);
749                                 rc = 1;
750                                 goto done;
751
752                         } else if ( rc == ARG_BAD_CONF ) {
753                                 rc = 1;
754                                 goto done;
755                         }
756                         
757                 } else if ( c->bi && !c->be ) {
758                         rc = SLAP_CONF_UNKNOWN;
759                         if ( c->bi->bi_cf_ocs ) {
760                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
761                                 if ( ct ) {
762                                         c->table = c->bi->bi_cf_ocs->co_type;
763                                         rc = config_add_vals( ct, c );
764                                 }
765                         }
766                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
767                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
768                                         c->argc, c->argv);
769                         }
770                         if ( rc ) {
771                                 switch(rc) {
772                                 case SLAP_CONF_UNKNOWN:
773                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
774                                                 "<%s> inside backend info definition.\n",
775                                                 c->log, *c->argv, 0);
776                                 default:
777                                         rc = 1;
778                                         goto done;
779                                 }
780                         }
781
782                 } else if ( c->be && c->be != frontendDB ) {
783                         rc = SLAP_CONF_UNKNOWN;
784                         if ( c->be->be_cf_ocs ) {
785                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
786                                 if ( ct ) {
787                                         c->table = c->be->be_cf_ocs->co_type;
788                                         rc = config_add_vals( ct, c );
789                                 }
790                         }
791                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
792                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
793                                         c->argc, c->argv);
794                         }
795                         if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
796                         {
797                                 /* global overlays may need 
798                                  * definitions inside other databases...
799                                  */
800                                 rc = (*frontendDB->be_config)( frontendDB,
801                                         c->fname, (int)c->lineno, c->argc, c->argv );
802                         }
803
804                         switch ( rc ) {
805                         case 0:
806                                 break;
807
808                         case SLAP_CONF_UNKNOWN:
809                                 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
810                                         "<%s> inside backend database definition.\n",
811                                         c->log, *c->argv, 0);
812                                 
813                         default:
814                                 rc = 1;
815                                 goto done;
816                         }
817
818                 } else if ( frontendDB->be_config ) {
819                         rc = (*frontendDB->be_config)( frontendDB,
820                                 c->fname, (int)c->lineno, c->argc, c->argv);
821                         if ( rc ) {
822                                 switch(rc) {
823                                 case SLAP_CONF_UNKNOWN:
824                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
825                                                 "<%s> inside global database definition.\n",
826                                                 c->log, *c->argv, 0);
827
828                                 default:
829                                         rc = 1;
830                                         goto done;
831                                 }
832                         }
833                         
834                 } else {
835                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
836                                 "<%s> outside backend info and database definitions.\n",
837                                 c->log, *c->argv, 0);
838                         rc = 1;
839                         goto done;
840                 }
841         }
842
843         rc = 0;
844
845 done:
846         ch_free(c->tline);
847         fclose(fp);
848         ch_free(c->argv);
849         ch_free(c);
850         return(rc);
851 }
852
853 /* restrictops, allows, disallows, requires, loglevel */
854
855 int
856 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
857         int i;
858         for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
859                 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
860         }
861         return(i);
862 }
863
864 int
865 verb_to_mask(const char *word, slap_verbmasks *v) {
866         struct berval   bword;
867         ber_str2bv( word, 0, 0, &bword );
868         return bverb_to_mask( &bword, v );
869 }
870
871 int
872 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
873         int i, j;
874         for(i = 1; i < argc; i++) {
875                 j = verb_to_mask(argv[i], v);
876                 if(BER_BVISNULL(&v[j].word)) return i;
877                 while (!v[j].mask) j--;
878                 *m |= v[j].mask;
879         }
880         return(0);
881 }
882
883 /* Mask keywords that represent multiple bits should occur before single
884  * bit keywords in the verbmasks array.
885  */
886 int
887 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
888         int i, rc = 1;
889
890         if (m) {
891                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
892                         if (!v[i].mask) continue;
893                         if (( m & v[i].mask ) == v[i].mask ) {
894                                 value_add_one( bva, &v[i].word );
895                                 rc = 0;
896                                 m ^= v[i].mask;
897                                 if ( !m ) break;
898                         }
899                 }
900         }
901         return rc;
902 }
903
904 int
905 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
906 {
907         int             i;
908
909         assert( *vp == NULL );
910
911         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
912
913         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
914
915         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
916                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
917                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
918         }
919
920         BER_BVZERO( &(*vp)[ i ].word );
921
922         return 0;               
923 }
924
925 int
926 slap_verbmasks_destroy( slap_verbmasks *v )
927 {
928         int             i;
929
930         assert( v != NULL );
931
932         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
933                 ch_free( v[ i ].word.bv_val );
934         }
935
936         ch_free( v );
937
938         return 0;
939 }
940
941 int
942 slap_verbmasks_append(
943         slap_verbmasks  **vp,
944         slap_mask_t     m,
945         struct berval   *v,
946         slap_mask_t     *ignore )
947 {
948         int     i;
949
950         if ( !m ) {
951                 return LDAP_OPERATIONS_ERROR;
952         }
953
954         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
955                 if ( !(*vp)[ i ].mask ) continue;
956
957                 if ( ignore != NULL ) {
958                         int     j;
959
960                         for ( j = 0; ignore[ j ] != 0; j++ ) {
961                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
962                                         goto check_next;
963                                 }
964                         }
965                 }
966
967                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
968                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
969                                 /* already set; ignore */
970                                 return LDAP_SUCCESS;
971                         }
972                         /* conflicts */
973                         return LDAP_TYPE_OR_VALUE_EXISTS;
974                 }
975
976                 if ( m & (*vp)[ i ].mask ) {
977                         /* conflicts */
978                         return LDAP_CONSTRAINT_VIOLATION;
979                 }
980 check_next:;
981         }
982
983         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
984         ber_dupbv( &(*vp)[ i ].word, v );
985         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
986         BER_BVZERO( &(*vp)[ i + 1 ].word );
987
988         return LDAP_SUCCESS;
989 }
990
991 int
992 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
993         int i;
994
995         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
996                 if ( m == v[i].mask ) {
997                         if ( bv != NULL ) {
998                                 *bv = v[i].word;
999                         }
1000                         return i;
1001                 }
1002         }
1003         return -1;
1004 }
1005
1006 #ifdef HAVE_TLS
1007 static slap_verbmasks tlskey[] = {
1008         { BER_BVC("no"),        SB_TLS_OFF },
1009         { BER_BVC("yes"),       SB_TLS_ON },
1010         { BER_BVC("critical"),  SB_TLS_CRITICAL },
1011         { BER_BVNULL, 0 }
1012 };
1013
1014 static slap_verbmasks crlkeys[] = {
1015                 { BER_BVC("none"),      LDAP_OPT_X_TLS_CRL_NONE },
1016                 { BER_BVC("peer"),      LDAP_OPT_X_TLS_CRL_PEER },
1017                 { BER_BVC("all"),       LDAP_OPT_X_TLS_CRL_ALL },
1018                 { BER_BVNULL, 0 }
1019         };
1020
1021 static slap_verbmasks vfykeys[] = {
1022                 { BER_BVC("never"),     LDAP_OPT_X_TLS_NEVER },
1023                 { BER_BVC("demand"),    LDAP_OPT_X_TLS_DEMAND },
1024                 { BER_BVC("try"),       LDAP_OPT_X_TLS_TRY },
1025                 { BER_BVC("hard"),      LDAP_OPT_X_TLS_HARD },
1026                 { BER_BVNULL, 0 }
1027         };
1028 #endif
1029
1030 static slap_verbmasks methkey[] = {
1031         { BER_BVC("none"),      LDAP_AUTH_NONE },
1032         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
1033 #ifdef HAVE_CYRUS_SASL
1034         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
1035 #endif
1036         { BER_BVNULL, 0 }
1037 };
1038
1039 static slap_verbmasks versionkey[] = {
1040         { BER_BVC("2"),         LDAP_VERSION2 },
1041         { BER_BVC("3"),         LDAP_VERSION3 },
1042         { BER_BVNULL, 0 }
1043 };
1044
1045 static slap_cf_aux_table bindkey[] = {
1046         { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1047         { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'i', 0, versionkey },
1048         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1049         { BER_BVC("timeout="), offsetof(slap_bindconf, sb_timeout_api), 'i', 0, NULL },
1050         { BER_BVC("network-timeout="), offsetof(slap_bindconf, sb_timeout_net), 'i', 0, NULL },
1051         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, (slap_verbmasks *)dnNormalize },
1052         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1053         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1054         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1055         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1056         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 1, NULL },
1057         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, (slap_verbmasks *)authzNormalize },
1058 #ifdef HAVE_TLS
1059         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1060
1061         /* NOTE: replace "13" with the actual index
1062          * of the first TLS-related line */
1063 #define aux_TLS (bindkey+13)    /* beginning of TLS keywords */
1064
1065         { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1066         { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1067         { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1068         { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1069         { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1070         { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1071 #ifdef HAVE_OPENSSL_CRL
1072         { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1073 #endif
1074 #endif
1075         { BER_BVNULL, 0, 0, 0, NULL }
1076 };
1077
1078 /*
1079  * 's': char *
1080  * 'b': struct berval; if !NULL, normalize using ((slap_mr_normalize_func *)aux)
1081  * 'i': int; if !NULL, compute using ((slap_verbmasks *)aux)
1082  * 'u': unsigned
1083  * 'I': long
1084  * 'U': unsigned long
1085  */
1086
1087 int
1088 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1089 {
1090         int rc = SLAP_CONF_UNKNOWN;
1091         slap_cf_aux_table *tab;
1092
1093         for ( tab = tab0; !BER_BVISNULL( &tab->key ); tab++ ) {
1094                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len ) ) {
1095                         char **cptr;
1096                         int *iptr, j;
1097                         unsigned *uptr;
1098                         long *lptr;
1099                         unsigned long *ulptr;
1100                         struct berval *bptr;
1101                         const char *val = word + tab->key.bv_len;
1102
1103                         switch ( tab->type ) {
1104                         case 's':
1105                                 cptr = (char **)((char *)dst + tab->off);
1106                                 *cptr = ch_strdup( val );
1107                                 rc = 0;
1108                                 break;
1109
1110                         case 'b':
1111                                 bptr = (struct berval *)((char *)dst + tab->off);
1112                                 if ( tab->aux != NULL ) {
1113                                         struct berval   dn;
1114                                         slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1115
1116                                         ber_str2bv( val, 0, 0, &dn );
1117                                         rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1118
1119                                 } else {
1120                                         ber_str2bv( val, 0, 1, bptr );
1121                                         rc = 0;
1122                                 }
1123                                 break;
1124
1125                         case 'i':
1126                                 iptr = (int *)((char *)dst + tab->off);
1127
1128                                 if ( tab->aux != NULL ) {
1129                                         slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1130
1131                                         assert( aux != NULL );
1132
1133                                         rc = 1;
1134                                         for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1135                                                 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1136                                                         *iptr = aux[j].mask;
1137                                                         rc = 0;
1138                                                         break;
1139                                                 }
1140                                         }
1141
1142                                 } else {
1143                                         rc = lutil_atoix( iptr, val, 0 );
1144                                 }
1145                                 break;
1146
1147                         case 'u':
1148                                 uptr = (unsigned *)((char *)dst + tab->off);
1149
1150                                 rc = lutil_atoux( uptr, val, 0 );
1151                                 break;
1152
1153                         case 'I':
1154                                 lptr = (long *)((char *)dst + tab->off);
1155
1156                                 rc = lutil_atolx( lptr, val, 0 );
1157                                 break;
1158
1159                         case 'U':
1160                                 ulptr = (unsigned long *)((char *)dst + tab->off);
1161
1162                                 rc = lutil_atoulx( ulptr, val, 0 );
1163                                 break;
1164                         }
1165
1166                         if ( rc ) {
1167                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1168                                         tabmsg, word, 0 );
1169                         }
1170                         
1171                         return rc;
1172                 }
1173         }
1174
1175         return rc;
1176 }
1177
1178 int
1179 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1180 {
1181         char buf[AC_LINE_MAX], *ptr;
1182         slap_cf_aux_table *tab;
1183         struct berval tmp;
1184
1185         ptr = buf;
1186         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1187                 char **cptr;
1188                 int *iptr, i;
1189                 unsigned *uptr;
1190                 long *lptr;
1191                 unsigned long *ulptr;
1192                 struct berval *bptr;
1193
1194                 cptr = (char **)((char *)src + tab->off);
1195
1196                 switch ( tab->type ) {
1197                 case 'b':
1198                         bptr = (struct berval *)((char *)src + tab->off);
1199                         cptr = &bptr->bv_val;
1200
1201                 case 's':
1202                         if ( *cptr ) {
1203                                 *ptr++ = ' ';
1204                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1205                                 if ( tab->quote ) *ptr++ = '"';
1206                                 ptr = lutil_strcopy( ptr, *cptr );
1207                                 if ( tab->quote ) *ptr++ = '"';
1208                         }
1209                         break;
1210
1211                 case 'i':
1212                         iptr = (int *)((char *)src + tab->off);
1213
1214                         if ( tab->aux != NULL ) {
1215                                 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1216
1217                                 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1218                                         if ( *iptr == aux[i].mask ) {
1219                                                 *ptr++ = ' ';
1220                                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1221                                                 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1222                                                 break;
1223                                         }
1224                                 }
1225
1226                         } else {
1227                                 *ptr++ = ' ';
1228                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1229                                 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1230                         }
1231                         break;
1232
1233                 case 'u':
1234                         uptr = (unsigned *)((char *)src + tab->off);
1235                         *ptr++ = ' ';
1236                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1237                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1238                         break;
1239
1240                 case 'I':
1241                         lptr = (long *)((char *)src + tab->off);
1242                         *ptr++ = ' ';
1243                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1244                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1245                         break;
1246
1247                 case 'U':
1248                         ulptr = (unsigned long *)((char *)src + tab->off);
1249                         *ptr++ = ' ';
1250                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1251                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1252                         break;
1253
1254                 default:
1255                         assert( 0 );
1256                 }
1257         }
1258         tmp.bv_val = buf;
1259         tmp.bv_len = ptr - buf;
1260         ber_dupbv( bv, &tmp );
1261         return 0;
1262 }
1263
1264 int
1265 slap_tls_get_config( LDAP *ld, int opt, char **val )
1266 {
1267         slap_verbmasks *keys;
1268         int i, ival;
1269
1270         *val = NULL;
1271         switch( opt ) {
1272 #ifdef HAVE_TLS
1273         case LDAP_OPT_X_TLS_CRLCHECK:
1274                 keys = crlkeys;
1275                 break;
1276         case LDAP_OPT_X_TLS_REQUIRE_CERT:
1277                 keys = vfykeys;
1278                 break;
1279 #endif
1280         default:
1281                 return -1;
1282         }
1283         ldap_pvt_tls_get_option( ld, opt, &ival );
1284         for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
1285                 if (keys[i].mask == ival) {
1286                         *val = ch_strdup( keys[i].word.bv_val );
1287                         return 0;
1288                 }
1289         }
1290         return -1;
1291 }
1292
1293 int
1294 bindconf_tls_parse( const char *word, slap_bindconf *bc )
1295 {
1296 #ifdef HAVE_TLS
1297         if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1298                 bc->sb_tls_do_init = 1;
1299                 return 0;
1300         }
1301 #endif
1302         return -1;
1303 }
1304
1305 int
1306 bindconf_tls_unparse( slap_bindconf *bc, struct berval *bv )
1307 {
1308 #ifdef HAVE_TLS
1309         return slap_cf_aux_table_unparse( bc, bv, aux_TLS );
1310 #endif
1311         return -1;
1312 }
1313
1314 int
1315 bindconf_parse( const char *word, slap_bindconf *bc )
1316 {
1317 #ifdef HAVE_TLS
1318         /* Detect TLS config changes explicitly */
1319         if ( bindconf_tls_parse( word, bc ) == 0 ) {
1320                 return 0;
1321         }
1322 #endif
1323         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1324 }
1325
1326 int
1327 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1328 {
1329         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1330 }
1331
1332 void bindconf_free( slap_bindconf *bc ) {
1333         if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1334                 ch_free( bc->sb_uri.bv_val );
1335                 BER_BVZERO( &bc->sb_uri );
1336         }
1337         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1338                 ch_free( bc->sb_binddn.bv_val );
1339                 BER_BVZERO( &bc->sb_binddn );
1340         }
1341         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1342                 ch_free( bc->sb_cred.bv_val );
1343                 BER_BVZERO( &bc->sb_cred );
1344         }
1345         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1346                 ch_free( bc->sb_saslmech.bv_val );
1347                 BER_BVZERO( &bc->sb_saslmech );
1348         }
1349         if ( bc->sb_secprops ) {
1350                 ch_free( bc->sb_secprops );
1351                 bc->sb_secprops = NULL;
1352         }
1353         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1354                 ch_free( bc->sb_realm.bv_val );
1355                 BER_BVZERO( &bc->sb_realm );
1356         }
1357         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1358                 ch_free( bc->sb_authcId.bv_val );
1359                 BER_BVZERO( &bc->sb_authcId );
1360         }
1361         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1362                 ch_free( bc->sb_authzId.bv_val );
1363                 BER_BVZERO( &bc->sb_authzId );
1364         }
1365 #ifdef HAVE_TLS
1366         if ( bc->sb_tls_cert ) {
1367                 ch_free( bc->sb_tls_cert );
1368                 bc->sb_tls_cert = NULL;
1369         }
1370         if ( bc->sb_tls_key ) {
1371                 ch_free( bc->sb_tls_key );
1372                 bc->sb_tls_key = NULL;
1373         }
1374         if ( bc->sb_tls_cacert ) {
1375                 ch_free( bc->sb_tls_cacert );
1376                 bc->sb_tls_cacert = NULL;
1377         }
1378         if ( bc->sb_tls_cacertdir ) {
1379                 ch_free( bc->sb_tls_cacertdir );
1380                 bc->sb_tls_cacertdir = NULL;
1381         }
1382         if ( bc->sb_tls_reqcert ) {
1383                 ch_free( bc->sb_tls_reqcert );
1384                 bc->sb_tls_reqcert = NULL;
1385         }
1386         if ( bc->sb_tls_cipher_suite ) {
1387                 ch_free( bc->sb_tls_cipher_suite );
1388                 bc->sb_tls_cipher_suite = NULL;
1389         }
1390 #ifdef HAVE_OPENSSL_CRL
1391         if ( bc->sb_tls_crlcheck ) {
1392                 ch_free( bc->sb_tls_crlcheck );
1393                 bc->sb_tls_crlcheck = NULL;
1394         }
1395 #endif
1396 #endif
1397 }
1398
1399 void
1400 bindconf_tls_defaults( slap_bindconf *bc )
1401 {
1402 #ifdef HAVE_TLS
1403         if ( bc->sb_tls_do_init ) {
1404                 if ( !bc->sb_tls_cacert )
1405                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE,
1406                                 &bc->sb_tls_cacert );
1407                 if ( !bc->sb_tls_cacertdir )
1408                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR,
1409                                 &bc->sb_tls_cacertdir );
1410                 if ( !bc->sb_tls_cert )
1411                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE,
1412                                 &bc->sb_tls_cert );
1413                 if ( !bc->sb_tls_key )
1414                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE,
1415                                 &bc->sb_tls_key );
1416                 if ( !bc->sb_tls_cipher_suite )
1417                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE,
1418                                 &bc->sb_tls_cipher_suite );
1419                 if ( !bc->sb_tls_reqcert )
1420                         bc->sb_tls_reqcert = ch_strdup("demand");
1421 #ifdef HAVE_OPENSSL_CRL
1422                 if ( !bc->sb_tls_crlcheck )
1423                         slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK,
1424                                 &bc->sb_tls_crlcheck );
1425 #endif
1426         }
1427 #endif
1428 }
1429
1430 #ifdef HAVE_TLS
1431 static struct {
1432         const char *key;
1433         size_t offset;
1434         int opt;
1435 } bindtlsopts[] = {
1436         { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1437         { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1438         { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1439         { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1440         { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1441         {0, 0}
1442 };
1443
1444 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1445 {
1446         int i, rc, newctx = 0, res = 0;
1447         char *ptr = (char *)bc, **word;
1448
1449         bc->sb_tls_do_init = 0;
1450
1451         for (i=0; bindtlsopts[i].opt; i++) {
1452                 word = (char **)(ptr + bindtlsopts[i].offset);
1453                 if ( *word ) {
1454                         rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1455                         if ( rc ) {
1456                                 Debug( LDAP_DEBUG_ANY,
1457                                         "bindconf_tls_set: failed to set %s to %s\n",
1458                                                 bindtlsopts[i].key, *word, 0 );
1459                                 res = -1;
1460                         } else
1461                                 newctx = 1;
1462                 }
1463         }
1464         if ( bc->sb_tls_reqcert ) {
1465                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1466                         bc->sb_tls_reqcert );
1467                 if ( rc ) {
1468                         Debug( LDAP_DEBUG_ANY,
1469                                 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1470                                         bc->sb_tls_reqcert, 0, 0 );
1471                         res = -1;
1472                 } else
1473                         newctx = 1;
1474         }
1475 #ifdef HAVE_OPENSSL_CRL
1476         if ( bc->sb_tls_crlcheck ) {
1477                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1478                         bc->sb_tls_crlcheck );
1479                 if ( rc ) {
1480                         Debug( LDAP_DEBUG_ANY,
1481                                 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1482                                         bc->sb_tls_crlcheck, 0, 0 );
1483                         res = -1;
1484                 } else
1485                         newctx = 1;
1486         }
1487 #endif
1488         if ( newctx ) {
1489                 int opt = 0;
1490
1491                 if ( bc->sb_tls_ctx ) {
1492                         ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1493                         bc->sb_tls_ctx = NULL;
1494                 }
1495                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1496                 if ( rc )
1497                         res = rc;
1498                 else
1499                         ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1500         }
1501         
1502         return res;
1503 }
1504 #endif
1505
1506 /*
1507  * connect to a client using the bindconf data
1508  * note: should move "version" into bindconf...
1509  */
1510 int
1511 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
1512 {
1513         LDAP            *ld = NULL;
1514         int             rc;
1515         struct timeval tv;
1516
1517         /* Init connection to master */
1518         rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1519         if ( rc != LDAP_SUCCESS ) {
1520                 Debug( LDAP_DEBUG_ANY,
1521                         "slap_client_connect: "
1522                         "ldap_initialize(%s) failed (%d)\n",
1523                         sb->sb_uri.bv_val, rc, 0 );
1524                 return rc;
1525         }
1526
1527         if ( sb->sb_version != 0 ) {
1528                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1529                         (const void *)&sb->sb_version );
1530         }
1531
1532         if ( sb->sb_timeout_api ) {
1533                 tv.tv_sec = sb->sb_timeout_api;
1534                 tv.tv_usec = 0;
1535                 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv );
1536         }
1537
1538         if ( sb->sb_timeout_net ) {
1539                 tv.tv_sec = sb->sb_timeout_net;
1540                 tv.tv_usec = 0;
1541                 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv );
1542         }
1543
1544 #ifdef HAVE_TLS
1545         if ( sb->sb_tls_do_init ) {
1546                 rc = bindconf_tls_set( sb, ld );
1547
1548         } else if ( sb->sb_tls_ctx ) {
1549                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1550                         sb->sb_tls_ctx );
1551         }
1552
1553         if ( rc ) {
1554                 Debug( LDAP_DEBUG_ANY,
1555                         "slap_client_connect: "
1556                         "URI=%s TLS context initialization failed (%d)\n",
1557                         sb->sb_uri.bv_val, rc, 0 );
1558                 return rc;
1559         }
1560 #endif
1561
1562         /* Bind */
1563         if ( sb->sb_tls ) {
1564                 rc = ldap_start_tls_s( ld, NULL, NULL );
1565                 if ( rc != LDAP_SUCCESS ) {
1566                         Debug( LDAP_DEBUG_ANY,
1567                                 "slap_client_connect: URI=%s "
1568                                 "%s, ldap_start_tls failed (%d)\n",
1569                                 sb->sb_uri.bv_val,
1570                                 sb->sb_tls == SB_TLS_CRITICAL ?
1571                                         "Error" : "Warning",
1572                                 rc );
1573                         if ( sb->sb_tls == SB_TLS_CRITICAL ) {
1574                                 goto done;
1575                         }
1576                 }
1577         }
1578
1579         if ( sb->sb_method == LDAP_AUTH_SASL ) {
1580 #ifdef HAVE_CYRUS_SASL
1581                 void *defaults;
1582
1583                 if ( sb->sb_secprops != NULL ) {
1584                         rc = ldap_set_option( ld,
1585                                 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
1586
1587                         if( rc != LDAP_OPT_SUCCESS ) {
1588                                 Debug( LDAP_DEBUG_ANY,
1589                                         "slap_client_connect: "
1590                                         "error, ldap_set_option "
1591                                         "(%s,SECPROPS,\"%s\") failed!\n",
1592                                         sb->sb_uri.bv_val, sb->sb_secprops, 0 );
1593                                 goto done;
1594                         }
1595                 }
1596
1597                 defaults = lutil_sasl_defaults( ld,
1598                         sb->sb_saslmech.bv_val,
1599                         sb->sb_realm.bv_val,
1600                         sb->sb_authcId.bv_val,
1601                         sb->sb_cred.bv_val,
1602                         sb->sb_authzId.bv_val );
1603                 if ( defaults == NULL ) {
1604                         rc = LDAP_OTHER;
1605                         goto done;
1606                 }
1607
1608                 rc = ldap_sasl_interactive_bind_s( ld,
1609                                 sb->sb_binddn.bv_val,
1610                                 sb->sb_saslmech.bv_val,
1611                                 NULL, NULL,
1612                                 LDAP_SASL_QUIET,
1613                                 lutil_sasl_interact,
1614                                 defaults );
1615
1616                 lutil_sasl_freedefs( defaults );
1617
1618                 /* FIXME: different error behaviors according to
1619                  *      1) return code
1620                  *      2) on err policy : exit, retry, backoff ...
1621                  */
1622                 if ( rc != LDAP_SUCCESS ) {
1623                         static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
1624
1625                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
1626                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
1627                                 sb->sb_uri.bv_val, rc, 0 );
1628
1629                         /* FIXME (see above comment) */
1630                         /* if Kerberos credentials cache is not active, retry */
1631                         if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
1632                                 rc == LDAP_LOCAL_ERROR )
1633                         {
1634                                 rc = LDAP_SERVER_DOWN;
1635                         }
1636
1637                         goto done;
1638                 }
1639 #else /* HAVE_CYRUS_SASL */
1640                 /* Should never get here, we trapped this at config time */
1641                 assert(0);
1642                 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
1643                 rc = LDAP_OTHER;
1644                 goto done;
1645 #endif
1646
1647         } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
1648                 rc = ldap_sasl_bind_s( ld,
1649                         sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
1650                         &sb->sb_cred, NULL, NULL, NULL );
1651                 if ( rc != LDAP_SUCCESS ) {
1652                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
1653                                 "URI=%s DN=\"%s\" "
1654                                 "ldap_sasl_bind_s failed (%d)\n",
1655                                 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
1656                         goto done;
1657                 }
1658         }
1659
1660 done:;
1661         if ( rc ) {
1662                 if ( ld ) {
1663                         ldap_unbind_ext( ld, NULL, NULL );
1664                         *ldp = NULL;
1665                 }
1666
1667         } else {
1668                 *ldp = ld;
1669         }
1670
1671         return rc;
1672 }
1673
1674 /* -------------------------------------- */
1675
1676
1677 static char *
1678 strtok_quote( char *line, char *sep, char **quote_ptr )
1679 {
1680         int             inquote;
1681         char            *tmp;
1682         static char     *next;
1683
1684         *quote_ptr = NULL;
1685         if ( line != NULL ) {
1686                 next = line;
1687         }
1688         while ( *next && strchr( sep, *next ) ) {
1689                 next++;
1690         }
1691
1692         if ( *next == '\0' ) {
1693                 next = NULL;
1694                 return( NULL );
1695         }
1696         tmp = next;
1697
1698         for ( inquote = 0; *next; ) {
1699                 switch ( *next ) {
1700                 case '"':
1701                         if ( inquote ) {
1702                                 inquote = 0;
1703                         } else {
1704                                 inquote = 1;
1705                         }
1706                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1707                         break;
1708
1709                 case '\\':
1710                         if ( next[1] )
1711                                 AC_MEMCPY( next,
1712                                             next + 1, strlen( next + 1 ) + 1 );
1713                         next++;         /* dont parse the escaped character */
1714                         break;
1715
1716                 default:
1717                         if ( ! inquote ) {
1718                                 if ( strchr( sep, *next ) != NULL ) {
1719                                         *quote_ptr = next;
1720                                         *next++ = '\0';
1721                                         return( tmp );
1722                                 }
1723                         }
1724                         next++;
1725                         break;
1726                 }
1727         }
1728
1729         return( tmp );
1730 }
1731
1732 static char     buf[AC_LINE_MAX];
1733 static char     *line;
1734 static size_t lmax, lcur;
1735
1736 #define CATLINE( buf ) \
1737         do { \
1738                 size_t len = strlen( buf ); \
1739                 while ( lcur + len + 1 > lmax ) { \
1740                         lmax += AC_LINE_MAX; \
1741                         line = (char *) ch_realloc( line, lmax ); \
1742                 } \
1743                 strcpy( line + lcur, buf ); \
1744                 lcur += len; \
1745         } while( 0 )
1746
1747 static void
1748 fp_getline_init(ConfigArgs *c) {
1749         c->lineno = -1;
1750         buf[0] = '\0';
1751 }
1752
1753 static int
1754 fp_getline( FILE *fp, ConfigArgs *c )
1755 {
1756         char    *p;
1757
1758         lcur = 0;
1759         CATLINE(buf);
1760         c->lineno++;
1761
1762         /* avoid stack of bufs */
1763         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1764                 buf[0] = '\0';
1765                 c->line = line;
1766                 return(1);
1767         }
1768
1769         while ( fgets( buf, sizeof( buf ), fp ) ) {
1770                 p = strchr( buf, '\n' );
1771                 if ( p ) {
1772                         if ( p > buf && p[-1] == '\r' ) {
1773                                 --p;
1774                         }
1775                         *p = '\0';
1776                 }
1777                 /* XXX ugly */
1778                 c->line = line;
1779                 if ( line[0]
1780                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1781                                 && p[-1] != '\\' )
1782                 {
1783                         p[0] = '\0';
1784                         lcur--;
1785                         
1786                 } else {
1787                         if ( !isspace( (unsigned char)buf[0] ) ) {
1788                                 return(1);
1789                         }
1790                         buf[0] = ' ';
1791                 }
1792                 CATLINE(buf);
1793                 c->lineno++;
1794         }
1795
1796         buf[0] = '\0';
1797         c->line = line;
1798         return(line[0] ? 1 : 0);
1799 }
1800
1801 static int
1802 fp_parse_line(ConfigArgs *c)
1803 {
1804         char *token;
1805         static char *const hide[] = {
1806                 "rootpw", "replica", "syncrepl",  /* in slapd */
1807                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1808                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1809                 "pseudorootpw",  /* in back-meta */
1810                 "dbpasswd",  /* in back-sql */
1811                 NULL
1812         };
1813         char *quote_ptr;
1814         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1815
1816         c->tline = ch_strdup(c->line);
1817         token = strtok_quote(c->tline, " \t", &quote_ptr);
1818
1819         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1820         if(quote_ptr) *quote_ptr = ' ';
1821         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1822                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1823         if(quote_ptr) *quote_ptr = '\0';
1824
1825         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1826                 if(c->argc >= c->argv_size) {
1827                         char **tmp;
1828                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1829                         if(!tmp) {
1830                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1831                                 return -1;
1832                         }
1833                         c->argv = tmp;
1834                         c->argv_size += ARGS_STEP;
1835                 }
1836                 if(token == NULL)
1837                         break;
1838                 c->argv[c->argc++] = token;
1839         }
1840         c->argv[c->argc] = NULL;
1841         return(0);
1842 }
1843
1844 void
1845 config_destroy( )
1846 {
1847         ucdata_unload( UCDATA_ALL );
1848         if ( frontendDB ) {
1849                 /* NOTE: in case of early exit, frontendDB can be NULL */
1850                 if ( frontendDB->be_schemandn.bv_val )
1851                         free( frontendDB->be_schemandn.bv_val );
1852                 if ( frontendDB->be_schemadn.bv_val )
1853                         free( frontendDB->be_schemadn.bv_val );
1854                 if ( frontendDB->be_acl )
1855                         acl_destroy( frontendDB->be_acl, NULL );
1856         }
1857         free( line );
1858         if ( slapd_args_file )
1859                 free ( slapd_args_file );
1860         if ( slapd_pid_file )
1861                 free ( slapd_pid_file );
1862         if ( default_passwd_hash )
1863                 ldap_charray_free( default_passwd_hash );
1864 }
1865
1866 char **
1867 slap_str2clist( char ***out, char *in, const char *brkstr )
1868 {
1869         char    *str;
1870         char    *s;
1871         char    *lasts;
1872         int     i, j;
1873         char    **new;
1874
1875         /* find last element in list */
1876         for (i = 0; *out && (*out)[i]; i++);
1877
1878         /* protect the input string from strtok */
1879         str = ch_strdup( in );
1880
1881         if ( *str == '\0' ) {
1882                 free( str );
1883                 return( *out );
1884         }
1885
1886         /* Count words in string */
1887         j=1;
1888         for ( s = str; *s; s++ ) {
1889                 if ( strchr( brkstr, *s ) != NULL ) {
1890                         j++;
1891                 }
1892         }
1893
1894         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1895         new = *out + i;
1896         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1897                 s != NULL;
1898                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1899         {
1900                 *new = ch_strdup( s );
1901                 new++;
1902         }
1903
1904         *new = NULL;
1905         free( str );
1906         return( *out );
1907 }
1908
1909 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1910         int argc, char **argv )
1911 {
1912         ConfigArgs c = { 0 };
1913         ConfigTable *ct;
1914         int rc;
1915
1916         c.be = be;
1917         c.fname = fname;
1918         c.lineno = lineno;
1919         c.argc = argc;
1920         c.argv = argv;
1921         c.valx = -1;
1922         c.line = line;
1923         c.op = SLAP_CONFIG_ADD;
1924         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1925
1926         rc = SLAP_CONF_UNKNOWN;
1927         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1928         if ( ct ) {
1929                 c.table = be->be_cf_ocs->co_type;
1930                 rc = config_add_vals( ct, &c );
1931         }
1932         return rc;
1933 }