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