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