]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
b462046dd73086fa2366323905237f34e75170f7
[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-2005 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 #include <unistd.h>
40
41 #include "slap.h"
42 #ifdef LDAP_SLAPI
43 #include "slapi/slapi.h"
44 #endif
45 #include "lutil.h"
46 #include "config.h"
47
48 #define ARGS_STEP       512
49
50 /*
51  * defaults for various global variables
52  */
53 slap_mask_t             global_allows = 0;
54 slap_mask_t             global_disallows = 0;
55 int             global_gentlehup = 0;
56 int             global_idletimeout = 0;
57 char    *global_host = NULL;
58 char    *global_realm = NULL;
59 char            *ldap_srvtab = "";
60 char            **default_passwd_hash = NULL;
61 struct berval default_search_base = BER_BVNULL;
62 struct berval default_search_nbase = BER_BVNULL;
63
64 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
65 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
66
67 int     slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
68 int     slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
69
70 char   *slapd_pid_file  = NULL;
71 char   *slapd_args_file = NULL;
72
73 int use_reverse_lookup = 0;
74
75 #ifdef LDAP_SLAPI
76 int slapi_plugins_used = 0;
77 #endif
78
79 static int fp_getline(FILE *fp, ConfigArgs *c);
80 static void fp_getline_init(ConfigArgs *c);
81 static int fp_parse_line(ConfigArgs *c);
82
83 static char     *strtok_quote(char *line, char *sep, char **quote_ptr);
84 static char *strtok_quote_ldif(char **line);
85
86 ConfigArgs *
87 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
88 {
89         ConfigArgs *c;
90         c = ch_calloc( 1, sizeof( ConfigArgs ) );
91         if ( c == NULL ) return(NULL);
92         c->be     = be; 
93         c->fname  = fname;
94         c->argc   = argc;
95         c->argv   = argv; 
96         c->lineno = lineno;
97         snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
98         return(c);
99 }
100
101 void
102 init_config_argv( ConfigArgs *c )
103 {
104         c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
105         c->argv_size = ARGS_STEP + 1;
106 }
107
108 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
109         int i;
110
111         for(i = 0; Conf[i].name; i++)
112                 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
113                         (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
114         if ( !Conf[i].name ) return NULL;
115         return Conf+i;
116 }
117
118 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
119         int rc, arg_user, arg_type, iarg;
120         long larg;
121         ber_len_t barg;
122         
123         arg_type = Conf->arg_type;
124         if(arg_type == ARG_IGNORED) {
125                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
126                         c->log, Conf->name, 0);
127                 return(0);
128         }
129         if((arg_type & ARG_DN) && c->argc == 1) {
130                 c->argc = 2;
131                 c->argv[1] = "";
132         }
133         if(Conf->min_args && (c->argc < Conf->min_args)) {
134                 sprintf( c->msg, "<%s> missing <%s> argument",
135                         c->argv[0], Conf->what );
136                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n", c->log, c->msg, 0 );
137                 return(ARG_BAD_CONF);
138         }
139         if(Conf->max_args && (c->argc > Conf->max_args)) {
140                 char    *ignored = " ignored";
141
142                 sprintf( c->msg, "<%s> extra cruft after <%s>",
143                         c->argv[0], Conf->what );
144
145 #ifdef LDAP_DEVEL
146                 ignored = "";
147 #endif /* LDAP_DEVEL */
148                 Debug(LDAP_DEBUG_CONFIG, "%s: %s%s.\n",
149                                 c->log, c->msg, ignored );
150 #ifdef LDAP_DEVEL
151                 return(ARG_BAD_CONF);
152 #endif /* LDAP_DEVEL */
153         }
154         if((arg_type & ARG_DB) && !c->be) {
155                 sprintf( c->msg, "<%s> only allowed within database declaration",
156                         c->argv[0] );
157                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
158                         c->log, c->msg, 0);
159                 return(ARG_BAD_CONF);
160         }
161         if((arg_type & ARG_PRE_BI) && c->bi) {
162                 sprintf( c->msg, "<%s> must occur before any backend %sdeclaration",
163                         c->argv[0], (arg_type & ARG_PRE_DB) ? "or database " : "" );
164                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
165                         c->log, c->msg, 0 );
166                 return(ARG_BAD_CONF);
167         }
168         if((arg_type & ARG_PRE_DB) && c->be && c->be != frontendDB) {
169                 sprintf( c->msg, "<%s> must occur before any database declaration",
170                         c->argv[0] );
171                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
172                         c->log, c->msg, 0);
173                 return(ARG_BAD_CONF);
174         }
175         if((arg_type & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
176                 sprintf( c->msg, "<%s> old format not supported", c->argv[0] );
177                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
178                         c->log, c->msg, 0);
179                 return(ARG_BAD_CONF);
180         }
181         if((arg_type & ARGS_POINTER) && !Conf->arg_item && !(arg_type & ARG_OFFSET)) {
182                 sprintf( c->msg, "<%s> invalid config_table, arg_item is NULL",
183                         c->argv[0] );
184                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
185                         c->log, c->msg, 0);
186                 return(ARG_BAD_CONF);
187         }
188         c->type = arg_user = (arg_type & ARGS_USERLAND);
189         memset(&c->values, 0, sizeof(c->values));
190         if(arg_type & ARGS_NUMERIC) {
191                 int j;
192                 iarg = 0; larg = 0; barg = 0;
193                 switch(arg_type & ARGS_NUMERIC) {
194                         case ARG_INT:           iarg = strtol(c->argv[1], NULL, 0); break;
195                         case ARG_LONG:          larg = strtol(c->argv[1], NULL, 0);     break;
196                         case ARG_BER_LEN_T:     barg = (ber_len_t)atol(c->argv[1]);     break;
197                         case ARG_ON_OFF:
198                                 if(c->argc == 1) {
199                                         iarg = 1;
200                                 } else if(!strcasecmp(c->argv[1], "on") ||
201                                         !strcasecmp(c->argv[1], "true")) {
202                                         iarg = 1;
203                                 } else if(!strcasecmp(c->argv[1], "off") ||
204                                         !strcasecmp(c->argv[1], "false")) {
205                                         iarg = 0;
206                                 } else {
207                                         sprintf( c->msg, "<%s> invalid value, ignored",
208                                                 c->argv[0] );
209                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
210                                                 c->log, c->msg, 0 );
211                                         return(0);
212                                 }
213                                 break;
214                 }
215                 j = (arg_type & ARG_NONZERO) ? 1 : 0;
216                 if(iarg < j && larg < j && barg < j ) {
217                         larg = larg ? larg : (barg ? barg : iarg);
218                         sprintf( c->msg, "<%s> invalid value, ignored",
219                                 c->argv[0] );
220                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
221                                 c->log, c->msg, 0 );
222                         return(ARG_BAD_CONF);
223                 }
224                 switch(arg_type & ARGS_NUMERIC) {
225                         case ARG_ON_OFF:
226                         case ARG_INT:           c->value_int = iarg;            break;
227                         case ARG_LONG:          c->value_long = larg;           break;
228                         case ARG_BER_LEN_T:     c->value_ber_t = barg;          break;
229                 }
230         } else if(arg_type & ARG_STRING) {
231                 if ( !check_only )
232                         c->value_string = ch_strdup(c->argv[1]);
233         } else if(arg_type & ARG_BERVAL) {
234                 if ( !check_only )
235                         ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
236         } else if(arg_type & ARG_DN) {
237                 struct berval bv;
238                 ber_str2bv( c->argv[1], 0, 0, &bv );
239                 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
240                 if ( rc != LDAP_SUCCESS ) {
241                         sprintf( c->msg, "<%s> invalid DN %d (%s)",
242                                 c->argv[0], rc, ldap_err2string( rc ));
243                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
244                         return(ARG_BAD_CONF);
245                 }
246                 if ( check_only ) {
247                         ch_free( c->value_ndn.bv_val );
248                         ch_free( c->value_dn.bv_val );
249                 }
250         }
251         return 0;
252 }
253
254 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
255         int rc, arg_type;
256         void *ptr = NULL;
257
258         arg_type = Conf->arg_type;
259         if(arg_type & ARG_MAGIC) {
260                 if(!c->be) c->be = frontendDB;
261                 c->msg[0] = '\0';
262                 rc = (*((ConfigDriver*)Conf->arg_item))(c);
263 #if 0
264                 if(c->be == frontendDB) c->be = NULL;
265 #endif
266                 if(rc) {
267                         if ( !c->msg[0] ) {
268                                 sprintf( c->msg, "<%s> handler exited with %d",
269                                         c->argv[0], rc );
270                                 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
271                                         c->log, c->msg, 0 );
272                         }
273                         return(ARG_BAD_CONF);
274                 }
275                 return(0);
276         }
277         if(arg_type & ARG_OFFSET) {
278                 if (c->be)
279                         ptr = c->be->be_private;
280                 else if (c->bi)
281                         ptr = c->bi->bi_private;
282                 else {
283                         sprintf( c->msg, "<%s> offset is missing base pointer",
284                                 c->argv[0] );
285                         Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
286                                 c->log, c->msg, 0);
287                         return(ARG_BAD_CONF);
288                 }
289                 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
290         } else if (arg_type & ARGS_POINTER) {
291                 ptr = Conf->arg_item;
292         }
293         if(arg_type & ARGS_POINTER)
294                 switch(arg_type & ARGS_POINTER) {
295                         case ARG_ON_OFF:
296                         case ARG_INT:           *(int*)ptr = c->value_int;                      break;
297                         case ARG_LONG:          *(long*)ptr = c->value_long;                    break;
298                         case ARG_BER_LEN_T:     *(ber_len_t*)ptr = c->value_ber_t;                      break;
299                         case ARG_STRING: {
300                                 char *cc = *(char**)ptr;
301                                 if(cc) {
302                                         if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
303                                                 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
304                                                         c->log, Conf->name, 0 );
305                                                 return(ARG_BAD_CONF);
306                                         }
307                                         ch_free(cc);
308                                 }
309                                 *(char **)ptr = c->value_string;
310                                 break;
311                                 }
312                         case ARG_BERVAL:
313                                 *(struct berval *)ptr = c->value_bv;
314                                 break;
315                 }
316         return(0);
317 }
318
319 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
320         int rc, arg_type;
321
322         arg_type = Conf->arg_type;
323         if(arg_type == ARG_IGNORED) {
324                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
325                         c->log, Conf->name, 0);
326                 return(0);
327         }
328         rc = config_check_vals( Conf, c, 0 );
329         if ( rc ) return rc;
330         return config_set_vals( Conf, c );
331 }
332
333 int
334 config_del_vals(ConfigTable *cf, ConfigArgs *c)
335 {
336         int rc = 0;
337
338         /* If there is no handler, just ignore it */
339         if ( cf->arg_type & ARG_MAGIC ) {
340                 c->op = LDAP_MOD_DELETE;
341                 c->type = cf->arg_type & ARGS_USERLAND;
342                 rc = (*((ConfigDriver*)cf->arg_item))(c);
343         }
344         return rc;
345 }
346
347 int
348 config_get_vals(ConfigTable *cf, ConfigArgs *c)
349 {
350         int rc = 0;
351         struct berval bv;
352         void *ptr;
353
354         if ( cf->arg_type & ARG_IGNORED ) {
355                 return 1;
356         }
357
358         memset(&c->values, 0, sizeof(c->values));
359         c->rvalue_vals = NULL;
360         c->rvalue_nvals = NULL;
361         c->op = SLAP_CONFIG_EMIT;
362         c->type = cf->arg_type & ARGS_USERLAND;
363
364         if ( cf->arg_type & ARG_MAGIC ) {
365                 rc = (*((ConfigDriver*)cf->arg_item))(c);
366                 if ( rc ) return rc;
367         } else {
368                 if ( cf->arg_type & ARG_OFFSET ) {
369                         if ( c->be )
370                                 ptr = c->be->be_private;
371                         else if ( c->bi )
372                                 ptr = c->bi->bi_private;
373                         else
374                                 return 1;
375                         ptr = (void *)((char *)ptr + (long)cf->arg_item);
376                 } else {
377                         ptr = cf->arg_item;
378                 }
379                 
380                 switch(cf->arg_type & ARGS_POINTER) {
381                 case ARG_ON_OFF:
382                 case ARG_INT:   c->value_int = *(int *)ptr; break;
383                 case ARG_LONG:  c->value_long = *(long *)ptr; break;
384                 case ARG_BER_LEN_T:     c->value_ber_t = *(ber_len_t *)ptr; break;
385                 case ARG_STRING:
386                         if ( *(char **)ptr )
387                                 c->value_string = ch_strdup(*(char **)ptr);
388                         break;
389                 case ARG_BERVAL:
390                         ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
391                 }
392         }
393         if ( cf->arg_type & ARGS_POINTER) {
394                 bv.bv_val = c->log;
395                 switch(cf->arg_type & ARGS_POINTER) {
396                 case ARG_INT: bv.bv_len = sprintf(bv.bv_val, "%d", c->value_int); break;
397                 case ARG_LONG: bv.bv_len = sprintf(bv.bv_val, "%ld", c->value_long); break;
398                 case ARG_BER_LEN_T: bv.bv_len = sprintf(bv.bv_val, "%ld", c->value_ber_t); break;
399                 case ARG_ON_OFF: bv.bv_len = sprintf(bv.bv_val, "%s",
400                         c->value_int ? "TRUE" : "FALSE"); break;
401                 case ARG_STRING:
402                         if ( c->value_string && c->value_string[0]) {
403                                 ber_str2bv( c->value_string, 0, 0, &bv);
404                         } else {
405                                 return 1;
406                         }
407                         break;
408                 case ARG_BERVAL:
409                         if ( !BER_BVISEMPTY( &c->value_bv )) {
410                                 bv = c->value_bv;
411                         } else {
412                                 return 1;
413                         }
414                         break;
415                 }
416                 if (( cf->arg_type & ARGS_POINTER ) == ARG_STRING )
417                         ber_bvarray_add(&c->rvalue_vals, &bv);
418                 else
419                         value_add_one(&c->rvalue_vals, &bv);
420         }
421         return rc;
422 }
423
424 int
425 init_config_attrs(ConfigTable *ct) {
426         LDAPAttributeType *at;
427         int i, code;
428         const char *err;
429
430         for (i=0; ct[i].name; i++ ) {
431                 int             freeit = 0;
432
433                 if ( !ct[i].attribute ) continue;
434                 at = ldap_str2attributetype( ct[i].attribute,
435                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
436                 if ( !at ) {
437                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
438                                 ct[i].attribute, ldap_scherr2str(code), err );
439                         return code;
440                 }
441
442                 code = at_add( at, 0, NULL, &err );
443                 if ( code ) {
444                         if ( code == SLAP_SCHERR_ATTR_DUP ) {
445                                 freeit = 1;
446
447                         } else {
448                                 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
449                                         ct[i].attribute, scherr2str(code), err );
450                                 return code;
451                         }
452                 }
453                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
454                 if ( freeit ) {
455                         ldap_attributetype_free( at );
456                 } else {
457                         ldap_memfree( at );
458                 }
459                 if ( code ) {
460                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
461                                 ct[i].attribute, err );
462                         return code;
463                 }
464         }
465
466         return 0;
467 }
468
469 int
470 init_config_ocs( ConfigOCs *ocs ) {
471         int i;
472
473         for (i=0;ocs[i].co_def;i++) {
474                 LDAPObjectClass *oc;
475                 int code;
476                 const char *err;
477
478                 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
479                         LDAP_SCHEMA_ALLOW_ALL );
480                 if ( !oc ) {
481                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
482                                 ocs[i].co_def, ldap_scherr2str(code), err );
483                         return code;
484                 }
485                 code = oc_add(oc,0,NULL,&err);
486                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
487                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
488                                 ocs[i].co_def, scherr2str(code), err );
489                         return code;
490                 }
491                 ocs[i].co_oc = oc_find(oc->oc_names[0]);
492                 ldap_memfree(oc);
493         }
494         return 0;
495 }
496
497 /* Split an LDIF line into space-separated tokens. Words may be grouped
498  * by quotes. A quoted string may begin in the middle of a word, but must
499  * end at the end of the word (be followed by whitespace or EOS). Any other
500  * quotes are passed through unchanged. All other characters are passed
501  * through unchanged.
502  */
503 static char *
504 strtok_quote_ldif( char **line )
505 {
506         char *beg, *ptr, *quote=NULL;
507         int inquote=0;
508
509         ptr = *line;
510
511         if ( !ptr || !*ptr )
512                 return NULL;
513
514         while( isspace( *ptr )) ptr++;
515
516         if ( *ptr == '"' ) {
517                 inquote = 1;
518                 ptr++;
519         }
520
521         beg = ptr;
522
523         for (;*ptr;ptr++) {
524                 if ( *ptr == '"' ) {
525                         if ( inquote && ( !ptr[1] || isspace(ptr[1]))) {
526                                 *ptr++ = '\0';
527                                 break;
528                         }
529                         inquote = 1;
530                         quote = ptr;
531                         continue;
532                 }
533                 if ( inquote )
534                         continue;
535                 if ( isspace( *ptr )) {
536                         *ptr++ = '\0';
537                         break;
538                 }
539         }
540         if ( quote ) {
541                 while ( quote < ptr ) {
542                         *quote = quote[1];
543                         quote++;
544                 }
545         }
546         if ( !*ptr ) {
547                 *line = NULL;
548         } else {
549                 while ( isspace( *ptr )) ptr++;
550                 *line = ptr;
551         }
552         return beg;
553 }
554
555 static void
556 config_parse_ldif( ConfigArgs *c )
557 {
558         char *next;
559         c->tline = ch_strdup(c->line);
560         next = c->tline;
561
562         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
563                 c->argc++;
564                 if ( c->argc >= c->argv_size ) {
565                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
566                                 sizeof( *c->argv ));
567                         c->argv = tmp;
568                         c->argv_size += ARGS_STEP;
569                 }
570         }
571         c->argv[c->argc] = NULL;
572 }
573
574 int
575 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
576 {
577         int     rc = 0;
578
579         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
580                 ct->ad->ad_cname.bv_val, valx );
581         c->argc = 1;
582         c->argv[0] = ct->ad->ad_cname.bv_val;
583
584         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
585                 c->argv[c->argc] = c->line;
586                 c->argc++;
587                 c->argv[c->argc] = NULL;
588                 c->tline = NULL;
589         } else {
590                 config_parse_ldif( c );
591         }
592         rc = config_check_vals( ct, c, 1 );
593         ch_free( c->tline );
594
595         if ( rc )
596                 rc = LDAP_CONSTRAINT_VIOLATION;
597
598         return rc;
599 }
600
601 int
602 config_parse_add(ConfigTable *ct, ConfigArgs *c)
603 {
604         int     rc = 0;
605
606         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
607                 ct->ad->ad_cname.bv_val, c->valx );
608         c->argc = 1;
609         c->argv[0] = ct->ad->ad_cname.bv_val;
610
611         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
612                 c->argv[c->argc] = c->line;
613                 c->argc++;
614                 c->argv[c->argc] = NULL;
615                 c->tline = NULL;
616         } else {
617                 config_parse_ldif( c );
618         }
619         c->op = LDAP_MOD_ADD;
620         rc = config_add_vals( ct, c );
621         ch_free( c->tline );
622
623         return rc;
624 }
625
626 int
627 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
628 {
629         FILE *fp;
630         ConfigTable *ct;
631         ConfigArgs *c;
632         int rc;
633         struct stat s;
634
635         c = ch_calloc( 1, sizeof( ConfigArgs ) );
636         if ( c == NULL ) {
637                 return 1;
638         }
639
640         if ( depth ) {
641                 memcpy( c, cf, sizeof( ConfigArgs ) );
642         } else {
643                 c->depth = depth; /* XXX */
644                 c->bi = NULL;
645                 c->be = NULL;
646         }
647
648         c->valx = -1;
649         c->fname = fname;
650         init_config_argv( c );
651
652         if ( stat( fname, &s ) != 0 ) {
653                 ldap_syslog = 1;
654                 Debug(LDAP_DEBUG_ANY,
655                     "could not stat config file \"%s\": %s (%d)\n",
656                     fname, strerror(errno), errno);
657                 return(1);
658         }
659
660         if ( !S_ISREG( s.st_mode ) ) {
661                 ldap_syslog = 1;
662                 Debug(LDAP_DEBUG_ANY,
663                     "regular file expected, got \"%s\"\n",
664                     fname, 0, 0 );
665                 return(1);
666         }
667
668         fp = fopen( fname, "r" );
669         if ( fp == NULL ) {
670                 ldap_syslog = 1;
671                 Debug(LDAP_DEBUG_ANY,
672                     "could not open config file \"%s\": %s (%d)\n",
673                     fname, strerror(errno), errno);
674                 return(1);
675         }
676
677         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
678
679         fp_getline_init(c);
680
681         c->tline = NULL;
682
683         while ( fp_getline( fp, c ) ) {
684                 /* skip comments and blank lines */
685                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
686                         continue;
687                 }
688
689                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
690                                 c->fname, c->lineno );
691
692                 c->argc = 0;
693                 ch_free( c->tline );
694                 if ( fp_parse_line( c ) ) {
695                         rc = 1;
696                         goto done;
697                 }
698
699                 if ( c->argc < 1 ) {
700                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: bad config line" 
701                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
702                                 c->log, 0, 0);
703 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
704                         rc = 1;
705                         goto done;
706 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
707                         continue;
708 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
709                 }
710
711                 c->op = SLAP_CONFIG_ADD;
712
713                 ct = config_find_keyword( cft, c );
714                 if ( ct ) {
715                         rc = config_add_vals( ct, c );
716                         if ( !rc ) continue;
717
718                         if ( rc & ARGS_USERLAND ) {
719                                 /* XXX a usertype would be opaque here */
720                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
721                                         c->log, c->argv[0], 0);
722                                 rc = 1;
723                                 goto done;
724
725                         } else if ( rc == ARG_BAD_CONF ) {
726                                 rc = 1;
727                                 goto done;
728                         }
729                         
730                 } else if ( c->bi && !c->be ) {
731                         rc = SLAP_CONF_UNKNOWN;
732                         if ( c->bi->bi_cf_ocs ) {
733                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
734                                 if ( ct ) {
735                                         rc = config_add_vals( ct, c );
736                                 }
737                         }
738                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
739                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
740                                         c->argc, c->argv);
741                         }
742                         if ( rc ) {
743                                 switch(rc) {
744                                 case SLAP_CONF_UNKNOWN:
745                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
746                                                 "unknown directive <%s> inside backend info definition"
747                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
748                                                 c->log, *c->argv, 0);
749 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
750                                         continue;
751 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
752                                 default:
753                                         rc = 1;
754                                         goto done;
755                                 }
756                         }
757
758                 } else if ( c->be ) {
759                         rc = SLAP_CONF_UNKNOWN;
760                         if ( c->be->be_cf_ocs ) {
761                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
762                                 if ( ct ) {
763                                         rc = config_add_vals( ct, c );
764                                 }
765                         }
766                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
767                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
768                                         c->argc, c->argv);
769                         }
770                         if ( rc ) {
771                                 switch(rc) {
772                                 case SLAP_CONF_UNKNOWN:
773                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
774                                                 "unknown directive <%s> inside backend database "
775                                                 "definition" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
776                                                 c->log, *c->argv, 0);
777 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
778                                         continue;
779 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
780                                 default:
781                                         rc = 1;
782                                         goto done;
783                                 }
784                         }
785
786                 } else if ( frontendDB->be_config ) {
787                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
788                         if ( rc ) {
789                                 switch(rc) {
790                                 case SLAP_CONF_UNKNOWN:
791                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
792                                                 "unknown directive <%s> inside global database definition"
793                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
794                                                 c->log, *c->argv, 0);
795 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
796                                         continue;
797 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
798                                 default:
799                                         rc = 1;
800                                         goto done;
801                                 }
802                         }
803                         
804                 } else {
805                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
806                                 "unknown directive <%s> outside backend info and database definitions"
807                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
808                                 c->log, *c->argv, 0);
809 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
810                         rc = 1;
811                         goto done;
812 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
813                         continue;
814 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
815                 }
816         }
817
818         rc = 0;
819
820 done:
821         ch_free(c->tline);
822         fclose(fp);
823         ch_free(c->argv);
824         ch_free(c);
825         return(rc);
826 }
827
828 /* restrictops, allows, disallows, requires, loglevel */
829
830 int
831 verb_to_mask(const char *word, slap_verbmasks *v) {
832         int i;
833         for(i = 0; !BER_BVISNULL(&v[i].word); i++)
834                 if(!strcasecmp(word, v[i].word.bv_val))
835                         break;
836         return(i);
837 }
838
839 int
840 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
841         int i, j;
842         for(i = 1; i < argc; i++) {
843                 j = verb_to_mask(argv[i], v);
844                 if(BER_BVISNULL(&v[j].word)) return(1);
845                 while (!v[j].mask) j--;
846                 *m |= v[j].mask;
847         }
848         return(0);
849 }
850
851 /* Mask keywords that represent multiple bits should occur before single
852  * bit keywords in the verbmasks array.
853  */
854 int
855 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
856         int i, rc = 1;
857
858         if (m) {
859                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
860                         if (!v[i].mask) continue;
861                         if (( m & v[i].mask ) == v[i].mask ) {
862                                 value_add_one( bva, &v[i].word );
863                                 rc = 0;
864                                 m ^= v[i].mask;
865                                 if ( !m ) break;
866                         }
867                 }
868         }
869         return rc;
870 }
871
872 int
873 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
874 {
875         int             i;
876
877         assert( *vp == NULL );
878
879         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ )
880                 ;
881
882         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
883
884         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
885                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
886                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
887         }
888
889         BER_BVZERO( &(*vp)[ i ].word );
890
891         return 0;               
892 }
893
894 int
895 slap_verbmasks_destroy( slap_verbmasks *v )
896 {
897         int             i;
898
899         assert( v != NULL );
900
901         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
902                 ch_free( v[ i ].word.bv_val );
903         }
904
905         ch_free( v );
906
907         return 0;
908 }
909
910 int
911 slap_verbmasks_append(
912         slap_verbmasks  **vp,
913         slap_mask_t     m,
914         struct berval   *v,
915         slap_mask_t     *ignore )
916 {
917         int     i;
918
919         if ( !m ) {
920                 return LDAP_OPERATIONS_ERROR;
921         }
922
923         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
924                 if ( !(*vp)[ i ].mask ) continue;
925
926                 if ( ignore != NULL ) {
927                         int     j;
928
929                         for ( j = 0; ignore[ j ] != 0; j++ ) {
930                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
931                                         goto check_next;
932                                 }
933                         }
934                 }
935
936                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
937                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
938                                 /* already set; ignore */
939                                 return LDAP_SUCCESS;
940                         }
941                         /* conflicts */
942                         return LDAP_TYPE_OR_VALUE_EXISTS;
943                 }
944
945                 if ( m & (*vp)[ i ].mask ) {
946                         /* conflicts */
947                         return LDAP_CONSTRAINT_VIOLATION;
948                 }
949 check_next:;
950         }
951
952         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
953         ber_dupbv( &(*vp)[ i ].word, v );
954         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
955         BER_BVZERO( &(*vp)[ i + 1 ].word );
956
957         return LDAP_SUCCESS;
958 }
959
960 int
961 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
962         int i;
963
964         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
965                 if ( m == v[i].mask ) {
966                         if ( bv != NULL ) {
967                                 *bv = v[i].word;
968                         }
969                         return i;
970                 }
971         }
972         return -1;
973 }
974
975 static slap_verbmasks tlskey[] = {
976         { BER_BVC("no"),        SB_TLS_OFF },
977         { BER_BVC("yes"),       SB_TLS_ON },
978         { BER_BVC("critical"),  SB_TLS_CRITICAL },
979         { BER_BVNULL, 0 }
980 };
981
982 static slap_verbmasks methkey[] = {
983         { BER_BVC("none"),      LDAP_AUTH_NONE },
984         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
985 #ifdef HAVE_CYRUS_SASL
986         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
987 #endif
988         { BER_BVNULL, 0 }
989 };
990
991 typedef struct cf_aux_table {
992         struct berval key;
993         int off;
994         char type;
995         char quote;
996         slap_verbmasks *aux;
997 } cf_aux_table;
998
999 static cf_aux_table bindkey[] = {
1000         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
1001         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
1002         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
1003         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1004         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1005         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1006         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1007         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1008         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1009         { BER_BVNULL, 0, 0, 0, NULL }
1010 };
1011
1012 int bindconf_parse( const char *word, slap_bindconf *bc ) {
1013         int rc = 0;
1014         cf_aux_table *tab;
1015
1016         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
1017                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1018                         char **cptr;
1019                         int *iptr, j;
1020                         struct berval *bptr;
1021                         const char *val = word + tab->key.bv_len;
1022
1023                         switch ( tab->type ) {
1024                         case 's':
1025                                 cptr = (char **)((char *)bc + tab->off);
1026                                 *cptr = ch_strdup( val );
1027                                 break;
1028
1029                         case 'b':
1030                                 bptr = (struct berval *)((char *)bc + tab->off);
1031                                 ber_str2bv( val, 0, 1, bptr );
1032                                 break;
1033
1034                         case 'd':
1035                                 assert( tab->aux != NULL );
1036                                 iptr = (int *)((char *)bc + tab->off);
1037
1038                                 rc = 1;
1039                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1040                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1041                                                 *iptr = tab->aux[j].mask;
1042                                                 rc = 0;
1043                                         }
1044                                 }
1045                                 break;
1046                         }
1047
1048                         if ( rc ) {
1049                                 Debug( LDAP_DEBUG_ANY, "invalid bind config value %s\n",
1050                                         word, 0, 0 );
1051                         }
1052                         
1053                         return rc;
1054                 }
1055         }
1056
1057         return rc;
1058 }
1059
1060 int bindconf_unparse( slap_bindconf *bc, struct berval *bv ) {
1061         char buf[BUFSIZ], *ptr;
1062         cf_aux_table *tab;
1063         struct berval tmp;
1064
1065         ptr = buf;
1066         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
1067                 char **cptr;
1068                 int *iptr, i;
1069                 struct berval *bptr;
1070
1071                 cptr = (char **)((char *)bc + tab->off);
1072
1073                 switch ( tab->type ) {
1074                 case 'b':
1075                         bptr = (struct berval *)((char *)bc + tab->off);
1076                         cptr = &bptr->bv_val;
1077                 case 's':
1078                         if ( *cptr ) {
1079                                 *ptr++ = ' ';
1080                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1081                                 if ( tab->quote ) *ptr++ = '"';
1082                                 ptr = lutil_strcopy( ptr, *cptr );
1083                                 if ( tab->quote ) *ptr++ = '"';
1084                         }
1085                         break;
1086
1087                 case 'd':
1088                         assert( tab->aux != NULL );
1089                         iptr = (int *)((char *)bc + tab->off);
1090                 
1091                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1092                                 if ( *iptr == tab->aux[i].mask ) {
1093                                         *ptr++ = ' ';
1094                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1095                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1096                                         break;
1097                                 }
1098                         }
1099                         break;
1100                 }
1101         }
1102         tmp.bv_val = buf;
1103         tmp.bv_len = ptr - buf;
1104         ber_dupbv( bv, &tmp );
1105         return 0;
1106 }
1107
1108 void bindconf_free( slap_bindconf *bc ) {
1109         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1110                 ch_free( bc->sb_binddn.bv_val );
1111                 BER_BVZERO( &bc->sb_binddn );
1112         }
1113         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1114                 ch_free( bc->sb_cred.bv_val );
1115                 BER_BVZERO( &bc->sb_cred );
1116         }
1117         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1118                 ch_free( bc->sb_saslmech.bv_val );
1119                 BER_BVZERO( &bc->sb_saslmech );
1120         }
1121         if ( bc->sb_secprops ) {
1122                 ch_free( bc->sb_secprops );
1123                 bc->sb_secprops = NULL;
1124         }
1125         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1126                 ch_free( bc->sb_realm.bv_val );
1127                 BER_BVZERO( &bc->sb_realm );
1128         }
1129         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1130                 ch_free( bc->sb_authcId.bv_val );
1131                 BER_BVZERO( &bc->sb_authcId );
1132         }
1133         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1134                 ch_free( bc->sb_authzId.bv_val );
1135                 BER_BVZERO( &bc->sb_authzId );
1136         }
1137 }
1138
1139
1140 /* -------------------------------------- */
1141
1142
1143 static char *
1144 strtok_quote( char *line, char *sep, char **quote_ptr )
1145 {
1146         int             inquote;
1147         char            *tmp;
1148         static char     *next;
1149
1150         *quote_ptr = NULL;
1151         if ( line != NULL ) {
1152                 next = line;
1153         }
1154         while ( *next && strchr( sep, *next ) ) {
1155                 next++;
1156         }
1157
1158         if ( *next == '\0' ) {
1159                 next = NULL;
1160                 return( NULL );
1161         }
1162         tmp = next;
1163
1164         for ( inquote = 0; *next; ) {
1165                 switch ( *next ) {
1166                 case '"':
1167                         if ( inquote ) {
1168                                 inquote = 0;
1169                         } else {
1170                                 inquote = 1;
1171                         }
1172                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1173                         break;
1174
1175                 case '\\':
1176                         if ( next[1] )
1177                                 AC_MEMCPY( next,
1178                                             next + 1, strlen( next + 1 ) + 1 );
1179                         next++;         /* dont parse the escaped character */
1180                         break;
1181
1182                 default:
1183                         if ( ! inquote ) {
1184                                 if ( strchr( sep, *next ) != NULL ) {
1185                                         *quote_ptr = next;
1186                                         *next++ = '\0';
1187                                         return( tmp );
1188                                 }
1189                         }
1190                         next++;
1191                         break;
1192                 }
1193         }
1194
1195         return( tmp );
1196 }
1197
1198 static char     buf[BUFSIZ];
1199 static char     *line;
1200 static size_t lmax, lcur;
1201
1202 #define CATLINE( buf ) \
1203         do { \
1204                 size_t len = strlen( buf ); \
1205                 while ( lcur + len + 1 > lmax ) { \
1206                         lmax += BUFSIZ; \
1207                         line = (char *) ch_realloc( line, lmax ); \
1208                 } \
1209                 strcpy( line + lcur, buf ); \
1210                 lcur += len; \
1211         } while( 0 )
1212
1213 static void
1214 fp_getline_init(ConfigArgs *c) {
1215         c->lineno = -1;
1216         buf[0] = '\0';
1217 }
1218
1219 static int
1220 fp_getline( FILE *fp, ConfigArgs *c )
1221 {
1222         char    *p;
1223
1224         lcur = 0;
1225         CATLINE(buf);
1226         c->lineno++;
1227
1228         /* avoid stack of bufs */
1229         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1230                 buf[0] = '\0';
1231                 c->line = line;
1232                 return(1);
1233         }
1234
1235         while ( fgets( buf, sizeof( buf ), fp ) ) {
1236                 p = strchr( buf, '\n' );
1237                 if ( p ) {
1238                         if ( p > buf && p[-1] == '\r' ) {
1239                                 --p;
1240                         }
1241                         *p = '\0';
1242                 }
1243                 /* XXX ugly */
1244                 c->line = line;
1245                 if ( line[0]
1246                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1247                                 && p[-1] != '\\' )
1248                 {
1249                         p[0] = '\0';
1250                         lcur--;
1251                         
1252                 } else {
1253                         if ( !isspace( (unsigned char)buf[0] ) ) {
1254                                 return(1);
1255                         }
1256                         buf[0] = ' ';
1257                 }
1258                 CATLINE(buf);
1259                 c->lineno++;
1260         }
1261
1262         buf[0] = '\0';
1263         c->line = line;
1264         return(line[0] ? 1 : 0);
1265 }
1266
1267 static int
1268 fp_parse_line(ConfigArgs *c)
1269 {
1270         char *token;
1271         static char *const hide[] = {
1272                 "rootpw", "replica", "syncrepl",  /* in slapd */
1273                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1274                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1275                 "pseudorootpw",  /* in back-meta */
1276                 "dbpasswd",  /* in back-sql */
1277                 NULL
1278         };
1279         char *quote_ptr;
1280         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1281
1282         c->tline = ch_strdup(c->line);
1283         token = strtok_quote(c->tline, " \t", &quote_ptr);
1284
1285         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1286         if(quote_ptr) *quote_ptr = ' ';
1287         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1288                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1289         if(quote_ptr) *quote_ptr = '\0';
1290
1291         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1292                 if(c->argc >= c->argv_size) {
1293                         char **tmp;
1294                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1295                         if(!tmp) {
1296                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1297                                 return -1;
1298                         }
1299                         c->argv = tmp;
1300                         c->argv_size += ARGS_STEP;
1301                 }
1302                 if(token == NULL)
1303                         break;
1304                 c->argv[c->argc++] = token;
1305         }
1306         c->argv[c->argc] = NULL;
1307         return(0);
1308 }
1309
1310 void
1311 config_destroy( )
1312 {
1313         ucdata_unload( UCDATA_ALL );
1314         if ( frontendDB ) {
1315                 /* NOTE: in case of early exit, frontendDB can be NULL */
1316                 if ( frontendDB->be_schemandn.bv_val )
1317                         free( frontendDB->be_schemandn.bv_val );
1318                 if ( frontendDB->be_schemadn.bv_val )
1319                         free( frontendDB->be_schemadn.bv_val );
1320                 if ( frontendDB->be_acl )
1321                         acl_destroy( frontendDB->be_acl, NULL );
1322         }
1323         free( line );
1324         if ( slapd_args_file )
1325                 free ( slapd_args_file );
1326         if ( slapd_pid_file )
1327                 free ( slapd_pid_file );
1328         if ( default_passwd_hash )
1329                 ldap_charray_free( default_passwd_hash );
1330 }
1331
1332 char **
1333 slap_str2clist( char ***out, char *in, const char *brkstr )
1334 {
1335         char    *str;
1336         char    *s;
1337         char    *lasts;
1338         int     i, j;
1339         char    **new;
1340
1341         /* find last element in list */
1342         for (i = 0; *out && (*out)[i]; i++);
1343
1344         /* protect the input string from strtok */
1345         str = ch_strdup( in );
1346
1347         if ( *str == '\0' ) {
1348                 free( str );
1349                 return( *out );
1350         }
1351
1352         /* Count words in string */
1353         j=1;
1354         for ( s = str; *s; s++ ) {
1355                 if ( strchr( brkstr, *s ) != NULL ) {
1356                         j++;
1357                 }
1358         }
1359
1360         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1361         new = *out + i;
1362         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1363                 s != NULL;
1364                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1365         {
1366                 *new = ch_strdup( s );
1367                 new++;
1368         }
1369
1370         *new = NULL;
1371         free( str );
1372         return( *out );
1373 }
1374
1375 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1376         int argc, char **argv )
1377 {
1378         ConfigArgs c = { 0 };
1379         ConfigTable *ct;
1380         int rc;
1381
1382         c.be = be;
1383         c.fname = fname;
1384         c.lineno = lineno;
1385         c.argc = argc;
1386         c.argv = argv;
1387         c.valx = -1;
1388         sprintf( c.log, "%s: line %d", fname, lineno );
1389
1390         rc = SLAP_CONF_UNKNOWN;
1391         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1392         if ( ct )
1393                 rc = config_add_vals( ct, &c );
1394         return rc;
1395 }