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