]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
2aaa4d5128ba63cd2492e08148460c230cb49060
[openldap] / servers / slapd / config.c
1 /* config.c - configuration file handling routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/ctype.h>
33 #include <ac/signal.h>
34 #include <ac/socket.h>
35 #include <ac/errno.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40 #ifndef S_ISREG
41 #define S_ISREG(m)      (((m) & _S_IFMT) == _S_IFREG)
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 #include "slap.h"
49 #ifdef LDAP_SLAPI
50 #include "slapi/slapi.h"
51 #endif
52 #include "lutil.h"
53 #include "lutil_ldap.h"
54 #include "config.h"
55
56 #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 ( bc->sb_tls_cert ) {
1355                 ch_free( bc->sb_tls_cert );
1356                 bc->sb_tls_cert = NULL;
1357         }
1358         if ( bc->sb_tls_key ) {
1359                 ch_free( bc->sb_tls_key );
1360                 bc->sb_tls_key = NULL;
1361         }
1362         if ( bc->sb_tls_cacert ) {
1363                 ch_free( bc->sb_tls_cacert );
1364                 bc->sb_tls_cacert = NULL;
1365         }
1366         if ( bc->sb_tls_cacertdir ) {
1367                 ch_free( bc->sb_tls_cacertdir );
1368                 bc->sb_tls_cacertdir = NULL;
1369         }
1370         if ( bc->sb_tls_reqcert ) {
1371                 ch_free( bc->sb_tls_reqcert );
1372                 bc->sb_tls_reqcert = NULL;
1373         }
1374         if ( bc->sb_tls_cipher_suite ) {
1375                 ch_free( bc->sb_tls_cipher_suite );
1376                 bc->sb_tls_cipher_suite = NULL;
1377         }
1378 #ifdef HAVE_OPENSSL_CRL
1379         if ( bc->sb_tls_crlcheck ) {
1380                 ch_free( bc->sb_tls_crlcheck );
1381                 bc->sb_tls_crlcheck = NULL;
1382         }
1383 #endif
1384 #endif
1385 }
1386
1387 void
1388 bindconf_tls_defaults( slap_bindconf *bc )
1389 {
1390 #ifdef HAVE_TLS
1391         if ( bc->sb_tls_do_init ) {
1392                 if ( !bc->sb_tls_cacert )
1393                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE,
1394                                 &bc->sb_tls_cacert );
1395                 if ( !bc->sb_tls_cacertdir )
1396                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR,
1397                                 &bc->sb_tls_cacertdir );
1398                 if ( !bc->sb_tls_cert )
1399                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE,
1400                                 &bc->sb_tls_cert );
1401                 if ( !bc->sb_tls_key )
1402                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE,
1403                                 &bc->sb_tls_key );
1404                 if ( !bc->sb_tls_cipher_suite )
1405                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE,
1406                                 &bc->sb_tls_cipher_suite );
1407                 if ( !bc->sb_tls_reqcert )
1408                         bc->sb_tls_reqcert = ch_strdup("demand");
1409 #ifdef HAVE_OPENSSL_CRL
1410                 if ( !bc->sb_tls_crlcheck )
1411                         slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK,
1412                                 &bc->sb_tls_crlcheck );
1413 #endif
1414         }
1415 #endif
1416 }
1417
1418 #ifdef HAVE_TLS
1419 static struct {
1420         const char *key;
1421         size_t offset;
1422         int opt;
1423 } bindtlsopts[] = {
1424         { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1425         { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1426         { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1427         { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1428         { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1429         {0, 0}
1430 };
1431
1432 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1433 {
1434         int i, rc, newctx = 0, res = 0;
1435         char *ptr = (char *)bc, **word;
1436
1437         bc->sb_tls_do_init = 0;
1438
1439         for (i=0; bindtlsopts[i].opt; i++) {
1440                 word = (char **)(ptr + bindtlsopts[i].offset);
1441                 if ( *word ) {
1442                         rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1443                         if ( rc ) {
1444                                 Debug( LDAP_DEBUG_ANY,
1445                                         "bindconf_tls_set: failed to set %s to %s\n",
1446                                                 bindtlsopts[i].key, *word, 0 );
1447                                 res = -1;
1448                         } else
1449                                 newctx = 1;
1450                 }
1451         }
1452         if ( bc->sb_tls_reqcert ) {
1453                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1454                         bc->sb_tls_reqcert );
1455                 if ( rc ) {
1456                         Debug( LDAP_DEBUG_ANY,
1457                                 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1458                                         bc->sb_tls_reqcert, 0, 0 );
1459                         res = -1;
1460                 } else
1461                         newctx = 1;
1462         }
1463 #ifdef HAVE_OPENSSL_CRL
1464         if ( bc->sb_tls_crlcheck ) {
1465                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1466                         bc->sb_tls_crlcheck );
1467                 if ( rc ) {
1468                         Debug( LDAP_DEBUG_ANY,
1469                                 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1470                                         bc->sb_tls_crlcheck, 0, 0 );
1471                         res = -1;
1472                 } else
1473                         newctx = 1;
1474         }
1475 #endif
1476         if ( newctx ) {
1477                 int opt = 0;
1478
1479                 if ( bc->sb_tls_ctx ) {
1480                         ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1481                         bc->sb_tls_ctx = NULL;
1482                 }
1483                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1484                 if ( rc )
1485                         res = rc;
1486                 else
1487                         ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1488         }
1489         
1490         return res;
1491 }
1492 #endif
1493
1494 /*
1495  * connect to a client using the bindconf data
1496  * note: should move "version" into bindconf...
1497  */
1498 int
1499 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
1500 {
1501         LDAP            *ld = NULL;
1502         int             rc;
1503         struct timeval tv;
1504
1505         /* Init connection to master */
1506         rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1507         if ( rc != LDAP_SUCCESS ) {
1508                 Debug( LDAP_DEBUG_ANY,
1509                         "slap_client_connect: "
1510                         "ldap_initialize(%s) failed (%d)\n",
1511                         sb->sb_uri.bv_val, rc, 0 );
1512                 return rc;
1513         }
1514
1515         if ( sb->sb_version != 0 ) {
1516                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1517                         (const void *)&sb->sb_version );
1518         }
1519
1520         if ( sb->sb_timeout_api ) {
1521                 tv.tv_sec = sb->sb_timeout_api;
1522                 tv.tv_usec = 0;
1523                 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv );
1524         }
1525
1526         if ( sb->sb_timeout_net ) {
1527                 tv.tv_sec = sb->sb_timeout_net;
1528                 tv.tv_usec = 0;
1529                 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv );
1530         }
1531
1532 #ifdef HAVE_TLS
1533         if ( sb->sb_tls_do_init ) {
1534                 rc = bindconf_tls_set( sb, ld );
1535
1536         } else if ( sb->sb_tls_ctx ) {
1537                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1538                         sb->sb_tls_ctx );
1539         }
1540
1541         if ( rc ) {
1542                 Debug( LDAP_DEBUG_ANY,
1543                         "slap_client_connect: "
1544                         "URI=%s TLS context initialization failed (%d)\n",
1545                         sb->sb_uri.bv_val, rc, 0 );
1546                 return rc;
1547         }
1548 #endif
1549
1550         /* Bind */
1551         if ( sb->sb_tls ) {
1552                 rc = ldap_start_tls_s( ld, NULL, NULL );
1553                 if ( rc != LDAP_SUCCESS ) {
1554                         Debug( LDAP_DEBUG_ANY,
1555                                 "slap_client_connect: URI=%s "
1556                                 "%s, ldap_start_tls failed (%d)\n",
1557                                 sb->sb_uri.bv_val,
1558                                 sb->sb_tls == SB_TLS_CRITICAL ?
1559                                         "Error" : "Warning",
1560                                 rc );
1561                         if ( sb->sb_tls == SB_TLS_CRITICAL ) {
1562                                 goto done;
1563                         }
1564                 }
1565         }
1566
1567         if ( sb->sb_method == LDAP_AUTH_SASL ) {
1568 #ifdef HAVE_CYRUS_SASL
1569                 void *defaults;
1570
1571                 if ( sb->sb_secprops != NULL ) {
1572                         rc = ldap_set_option( ld,
1573                                 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
1574
1575                         if( rc != LDAP_OPT_SUCCESS ) {
1576                                 Debug( LDAP_DEBUG_ANY,
1577                                         "slap_client_connect: "
1578                                         "error, ldap_set_option "
1579                                         "(%s,SECPROPS,\"%s\") failed!\n",
1580                                         sb->sb_uri.bv_val, sb->sb_secprops, 0 );
1581                                 goto done;
1582                         }
1583                 }
1584
1585                 defaults = lutil_sasl_defaults( ld,
1586                         sb->sb_saslmech.bv_val,
1587                         sb->sb_realm.bv_val,
1588                         sb->sb_authcId.bv_val,
1589                         sb->sb_cred.bv_val,
1590                         sb->sb_authzId.bv_val );
1591
1592                 rc = ldap_sasl_interactive_bind_s( ld,
1593                                 sb->sb_binddn.bv_val,
1594                                 sb->sb_saslmech.bv_val,
1595                                 NULL, NULL,
1596                                 LDAP_SASL_QUIET,
1597                                 lutil_sasl_interact,
1598                                 defaults );
1599
1600                 lutil_sasl_freedefs( defaults );
1601
1602                 /* FIXME: different error behaviors according to
1603                  *      1) return code
1604                  *      2) on err policy : exit, retry, backoff ...
1605                  */
1606                 if ( rc != LDAP_SUCCESS ) {
1607                         static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
1608
1609                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
1610                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
1611                                 sb->sb_uri.bv_val, rc, 0 );
1612
1613                         /* FIXME (see above comment) */
1614                         /* if Kerberos credentials cache is not active, retry */
1615                         if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
1616                                 rc == LDAP_LOCAL_ERROR )
1617                         {
1618                                 rc = LDAP_SERVER_DOWN;
1619                         }
1620
1621                         goto done;
1622                 }
1623 #else /* HAVE_CYRUS_SASL */
1624                 /* Should never get here, we trapped this at config time */
1625                 assert(0);
1626                 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
1627                 rc = LDAP_OTHER;
1628                 goto done;
1629 #endif
1630
1631         } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
1632                 rc = ldap_sasl_bind_s( ld,
1633                         sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
1634                         &sb->sb_cred, NULL, NULL, NULL );
1635                 if ( rc != LDAP_SUCCESS ) {
1636                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
1637                                 "URI=%s DN=\"%s\" "
1638                                 "ldap_sasl_bind_s failed (%d)\n",
1639                                 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
1640                         goto done;
1641                 }
1642         }
1643
1644 done:;
1645         if ( rc ) {
1646                 if ( ld ) {
1647                         ldap_unbind_ext( ld, NULL, NULL );
1648                         *ldp = NULL;
1649                 }
1650
1651         } else {
1652                 *ldp = ld;
1653         }
1654
1655         return rc;
1656 }
1657
1658 /* -------------------------------------- */
1659
1660
1661 static char *
1662 strtok_quote( char *line, char *sep, char **quote_ptr )
1663 {
1664         int             inquote;
1665         char            *tmp;
1666         static char     *next;
1667
1668         *quote_ptr = NULL;
1669         if ( line != NULL ) {
1670                 next = line;
1671         }
1672         while ( *next && strchr( sep, *next ) ) {
1673                 next++;
1674         }
1675
1676         if ( *next == '\0' ) {
1677                 next = NULL;
1678                 return( NULL );
1679         }
1680         tmp = next;
1681
1682         for ( inquote = 0; *next; ) {
1683                 switch ( *next ) {
1684                 case '"':
1685                         if ( inquote ) {
1686                                 inquote = 0;
1687                         } else {
1688                                 inquote = 1;
1689                         }
1690                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1691                         break;
1692
1693                 case '\\':
1694                         if ( next[1] )
1695                                 AC_MEMCPY( next,
1696                                             next + 1, strlen( next + 1 ) + 1 );
1697                         next++;         /* dont parse the escaped character */
1698                         break;
1699
1700                 default:
1701                         if ( ! inquote ) {
1702                                 if ( strchr( sep, *next ) != NULL ) {
1703                                         *quote_ptr = next;
1704                                         *next++ = '\0';
1705                                         return( tmp );
1706                                 }
1707                         }
1708                         next++;
1709                         break;
1710                 }
1711         }
1712
1713         return( tmp );
1714 }
1715
1716 static char     buf[AC_LINE_MAX];
1717 static char     *line;
1718 static size_t lmax, lcur;
1719
1720 #define CATLINE( buf ) \
1721         do { \
1722                 size_t len = strlen( buf ); \
1723                 while ( lcur + len + 1 > lmax ) { \
1724                         lmax += AC_LINE_MAX; \
1725                         line = (char *) ch_realloc( line, lmax ); \
1726                 } \
1727                 strcpy( line + lcur, buf ); \
1728                 lcur += len; \
1729         } while( 0 )
1730
1731 static void
1732 fp_getline_init(ConfigArgs *c) {
1733         c->lineno = -1;
1734         buf[0] = '\0';
1735 }
1736
1737 static int
1738 fp_getline( FILE *fp, ConfigArgs *c )
1739 {
1740         char    *p;
1741
1742         lcur = 0;
1743         CATLINE(buf);
1744         c->lineno++;
1745
1746         /* avoid stack of bufs */
1747         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1748                 buf[0] = '\0';
1749                 c->line = line;
1750                 return(1);
1751         }
1752
1753         while ( fgets( buf, sizeof( buf ), fp ) ) {
1754                 p = strchr( buf, '\n' );
1755                 if ( p ) {
1756                         if ( p > buf && p[-1] == '\r' ) {
1757                                 --p;
1758                         }
1759                         *p = '\0';
1760                 }
1761                 /* XXX ugly */
1762                 c->line = line;
1763                 if ( line[0]
1764                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1765                                 && p[-1] != '\\' )
1766                 {
1767                         p[0] = '\0';
1768                         lcur--;
1769                         
1770                 } else {
1771                         if ( !isspace( (unsigned char)buf[0] ) ) {
1772                                 return(1);
1773                         }
1774                         buf[0] = ' ';
1775                 }
1776                 CATLINE(buf);
1777                 c->lineno++;
1778         }
1779
1780         buf[0] = '\0';
1781         c->line = line;
1782         return(line[0] ? 1 : 0);
1783 }
1784
1785 static int
1786 fp_parse_line(ConfigArgs *c)
1787 {
1788         char *token;
1789         static char *const hide[] = {
1790                 "rootpw", "replica", "syncrepl",  /* in slapd */
1791                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1792                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1793                 "pseudorootpw",  /* in back-meta */
1794                 "dbpasswd",  /* in back-sql */
1795                 NULL
1796         };
1797         char *quote_ptr;
1798         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1799
1800         c->tline = ch_strdup(c->line);
1801         token = strtok_quote(c->tline, " \t", &quote_ptr);
1802
1803         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1804         if(quote_ptr) *quote_ptr = ' ';
1805         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1806                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1807         if(quote_ptr) *quote_ptr = '\0';
1808
1809         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1810                 if(c->argc >= c->argv_size) {
1811                         char **tmp;
1812                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1813                         if(!tmp) {
1814                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1815                                 return -1;
1816                         }
1817                         c->argv = tmp;
1818                         c->argv_size += ARGS_STEP;
1819                 }
1820                 if(token == NULL)
1821                         break;
1822                 c->argv[c->argc++] = token;
1823         }
1824         c->argv[c->argc] = NULL;
1825         return(0);
1826 }
1827
1828 void
1829 config_destroy( )
1830 {
1831         ucdata_unload( UCDATA_ALL );
1832         if ( frontendDB ) {
1833                 /* NOTE: in case of early exit, frontendDB can be NULL */
1834                 if ( frontendDB->be_schemandn.bv_val )
1835                         free( frontendDB->be_schemandn.bv_val );
1836                 if ( frontendDB->be_schemadn.bv_val )
1837                         free( frontendDB->be_schemadn.bv_val );
1838                 if ( frontendDB->be_acl )
1839                         acl_destroy( frontendDB->be_acl, NULL );
1840         }
1841         free( line );
1842         if ( slapd_args_file )
1843                 free ( slapd_args_file );
1844         if ( slapd_pid_file )
1845                 free ( slapd_pid_file );
1846         if ( default_passwd_hash )
1847                 ldap_charray_free( default_passwd_hash );
1848 }
1849
1850 char **
1851 slap_str2clist( char ***out, char *in, const char *brkstr )
1852 {
1853         char    *str;
1854         char    *s;
1855         char    *lasts;
1856         int     i, j;
1857         char    **new;
1858
1859         /* find last element in list */
1860         for (i = 0; *out && (*out)[i]; i++);
1861
1862         /* protect the input string from strtok */
1863         str = ch_strdup( in );
1864
1865         if ( *str == '\0' ) {
1866                 free( str );
1867                 return( *out );
1868         }
1869
1870         /* Count words in string */
1871         j=1;
1872         for ( s = str; *s; s++ ) {
1873                 if ( strchr( brkstr, *s ) != NULL ) {
1874                         j++;
1875                 }
1876         }
1877
1878         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1879         new = *out + i;
1880         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1881                 s != NULL;
1882                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1883         {
1884                 *new = ch_strdup( s );
1885                 new++;
1886         }
1887
1888         *new = NULL;
1889         free( str );
1890         return( *out );
1891 }
1892
1893 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1894         int argc, char **argv )
1895 {
1896         ConfigArgs c = { 0 };
1897         ConfigTable *ct;
1898         int rc;
1899
1900         c.be = be;
1901         c.fname = fname;
1902         c.lineno = lineno;
1903         c.argc = argc;
1904         c.argv = argv;
1905         c.valx = -1;
1906         c.line = line;
1907         c.op = SLAP_CONFIG_ADD;
1908         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1909
1910         rc = SLAP_CONF_UNKNOWN;
1911         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1912         if ( ct ) {
1913                 c.table = be->be_cf_ocs->co_type;
1914                 rc = config_add_vals( ct, &c );
1915         }
1916         return rc;
1917 }