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