]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
Fix void* pointer arithmetic from ber_bvchr()
[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                 snprintf( c->msg, sizeof( 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                 snprintf( c->msg, sizeof( 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                 snprintf( c->msg, sizeof( 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                 snprintf( c->msg, sizeof( 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                 snprintf( c->msg, sizeof( 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                 snprintf( c->msg, sizeof( 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                 snprintf( c->msg, sizeof( 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                                         snprintf( c->msg, sizeof( 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                         snprintf( c->msg, sizeof( 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                         snprintf( c->msg, sizeof( 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                                 snprintf( c->msg, sizeof( 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                         snprintf( c->msg, sizeof( 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 = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
397                 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
398                 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
399                 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%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 (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
417                         return 1;
418                 }
419                 if (( cf->arg_type & ARGS_POINTER ) == ARG_STRING )
420                         ber_bvarray_add(&c->rvalue_vals, &bv);
421                 else
422                         value_add_one(&c->rvalue_vals, &bv);
423         }
424         return rc;
425 }
426
427 int
428 init_config_attrs(ConfigTable *ct) {
429         LDAPAttributeType *at;
430         int i, code;
431         const char *err;
432
433         for (i=0; ct[i].name; i++ ) {
434                 int             freeit = 0;
435
436                 if ( !ct[i].attribute ) continue;
437                 at = ldap_str2attributetype( ct[i].attribute,
438                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
439                 if ( !at ) {
440                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
441                                 ct[i].attribute, ldap_scherr2str(code), err );
442                         return code;
443                 }
444
445                 code = at_add( at, 0, NULL, &err );
446                 if ( code ) {
447                         if ( code == SLAP_SCHERR_ATTR_DUP ) {
448                                 freeit = 1;
449
450                         } else {
451                                 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
452                                         ct[i].attribute, scherr2str(code), err );
453                                 return code;
454                         }
455                 }
456                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
457                 if ( freeit ) {
458                         ldap_attributetype_free( at );
459                 } else {
460                         ldap_memfree( at );
461                 }
462                 if ( code ) {
463                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
464                                 ct[i].attribute, err );
465                         return code;
466                 }
467         }
468
469         return 0;
470 }
471
472 int
473 init_config_ocs( ConfigOCs *ocs ) {
474         int i;
475
476         for (i=0;ocs[i].co_def;i++) {
477                 LDAPObjectClass *oc;
478                 int code;
479                 const char *err;
480
481                 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
482                         LDAP_SCHEMA_ALLOW_ALL );
483                 if ( !oc ) {
484                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
485                                 ocs[i].co_def, ldap_scherr2str(code), err );
486                         return code;
487                 }
488                 code = oc_add(oc,0,NULL,&err);
489                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
490                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
491                                 ocs[i].co_def, scherr2str(code), err );
492                         return code;
493                 }
494                 ocs[i].co_oc = oc_find(oc->oc_names[0]);
495                 ldap_memfree(oc);
496         }
497         return 0;
498 }
499
500 /* Split an LDIF line into space-separated tokens. Words may be grouped
501  * by quotes. A quoted string may begin in the middle of a word, but must
502  * end at the end of the word (be followed by whitespace or EOS). Any other
503  * quotes are passed through unchanged. All other characters are passed
504  * through unchanged.
505  */
506 static char *
507 strtok_quote_ldif( char **line )
508 {
509         char *beg, *ptr, *quote=NULL;
510         int inquote=0;
511
512         ptr = *line;
513
514         if ( !ptr || !*ptr )
515                 return NULL;
516
517         while( isspace( *ptr )) ptr++;
518
519         if ( *ptr == '"' ) {
520                 inquote = 1;
521                 ptr++;
522         }
523
524         beg = ptr;
525
526         for (;*ptr;ptr++) {
527                 if ( *ptr == '"' ) {
528                         if ( inquote && ( !ptr[1] || isspace(ptr[1]))) {
529                                 *ptr++ = '\0';
530                                 break;
531                         }
532                         inquote = 1;
533                         quote = ptr;
534                         continue;
535                 }
536                 if ( inquote )
537                         continue;
538                 if ( isspace( *ptr )) {
539                         *ptr++ = '\0';
540                         break;
541                 }
542         }
543         if ( quote ) {
544                 while ( quote < ptr ) {
545                         *quote = quote[1];
546                         quote++;
547                 }
548         }
549         if ( !*ptr ) {
550                 *line = NULL;
551         } else {
552                 while ( isspace( *ptr )) ptr++;
553                 *line = ptr;
554         }
555         return beg;
556 }
557
558 static void
559 config_parse_ldif( ConfigArgs *c )
560 {
561         char *next;
562         c->tline = ch_strdup(c->line);
563         next = c->tline;
564
565         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
566                 c->argc++;
567                 if ( c->argc >= c->argv_size ) {
568                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
569                                 sizeof( *c->argv ));
570                         c->argv = tmp;
571                         c->argv_size += ARGS_STEP;
572                 }
573         }
574         c->argv[c->argc] = NULL;
575 }
576
577 int
578 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
579 {
580         int     rc = 0;
581
582         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
583                 ct->ad->ad_cname.bv_val, valx );
584         c->argc = 1;
585         c->argv[0] = ct->ad->ad_cname.bv_val;
586
587         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
588                 c->argv[c->argc] = c->line;
589                 c->argc++;
590                 c->argv[c->argc] = NULL;
591                 c->tline = NULL;
592         } else {
593                 config_parse_ldif( c );
594         }
595         rc = config_check_vals( ct, c, 1 );
596         ch_free( c->tline );
597
598         if ( rc )
599                 rc = LDAP_CONSTRAINT_VIOLATION;
600
601         return rc;
602 }
603
604 int
605 config_parse_add(ConfigTable *ct, ConfigArgs *c)
606 {
607         int     rc = 0;
608
609         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
610                 ct->ad->ad_cname.bv_val, c->valx );
611         c->argc = 1;
612         c->argv[0] = ct->ad->ad_cname.bv_val;
613
614         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
615                 c->argv[c->argc] = c->line;
616                 c->argc++;
617                 c->argv[c->argc] = NULL;
618                 c->tline = NULL;
619         } else {
620                 config_parse_ldif( c );
621         }
622         c->op = LDAP_MOD_ADD;
623         rc = config_add_vals( ct, c );
624         ch_free( c->tline );
625
626         return rc;
627 }
628
629 int
630 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
631 {
632         FILE *fp;
633         ConfigTable *ct;
634         ConfigArgs *c;
635         int rc;
636         struct stat s;
637
638         c = ch_calloc( 1, sizeof( ConfigArgs ) );
639         if ( c == NULL ) {
640                 return 1;
641         }
642
643         if ( depth ) {
644                 memcpy( c, cf, sizeof( ConfigArgs ) );
645         } else {
646                 c->depth = depth; /* XXX */
647                 c->bi = NULL;
648                 c->be = NULL;
649         }
650
651         c->valx = -1;
652         c->fname = fname;
653         init_config_argv( c );
654
655         if ( stat( fname, &s ) != 0 ) {
656                 ldap_syslog = 1;
657                 Debug(LDAP_DEBUG_ANY,
658                     "could not stat config file \"%s\": %s (%d)\n",
659                     fname, strerror(errno), errno);
660                 return(1);
661         }
662
663         if ( !S_ISREG( s.st_mode ) ) {
664                 ldap_syslog = 1;
665                 Debug(LDAP_DEBUG_ANY,
666                     "regular file expected, got \"%s\"\n",
667                     fname, 0, 0 );
668                 return(1);
669         }
670
671         fp = fopen( fname, "r" );
672         if ( fp == NULL ) {
673                 ldap_syslog = 1;
674                 Debug(LDAP_DEBUG_ANY,
675                     "could not open config file \"%s\": %s (%d)\n",
676                     fname, strerror(errno), errno);
677                 return(1);
678         }
679
680         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
681
682         fp_getline_init(c);
683
684         c->tline = NULL;
685
686         while ( fp_getline( fp, c ) ) {
687                 /* skip comments and blank lines */
688                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
689                         continue;
690                 }
691
692                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
693                                 c->fname, c->lineno );
694
695                 c->argc = 0;
696                 ch_free( c->tline );
697                 if ( fp_parse_line( c ) ) {
698                         rc = 1;
699                         goto done;
700                 }
701
702                 if ( c->argc < 1 ) {
703                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: bad config line" 
704                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
705                                 c->log, 0, 0);
706 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
707                         rc = 1;
708                         goto done;
709 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
710                         continue;
711 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
712                 }
713
714                 c->op = SLAP_CONFIG_ADD;
715
716                 ct = config_find_keyword( cft, c );
717                 if ( ct ) {
718                         rc = config_add_vals( ct, c );
719                         if ( !rc ) continue;
720
721                         if ( rc & ARGS_USERLAND ) {
722                                 /* XXX a usertype would be opaque here */
723                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
724                                         c->log, c->argv[0], 0);
725                                 rc = 1;
726                                 goto done;
727
728                         } else if ( rc == ARG_BAD_CONF ) {
729                                 rc = 1;
730                                 goto done;
731                         }
732                         
733                 } else if ( c->bi && !c->be ) {
734                         rc = SLAP_CONF_UNKNOWN;
735                         if ( c->bi->bi_cf_ocs ) {
736                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
737                                 if ( ct ) {
738                                         rc = config_add_vals( ct, c );
739                                 }
740                         }
741                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
742                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
743                                         c->argc, c->argv);
744                         }
745                         if ( rc ) {
746                                 switch(rc) {
747                                 case SLAP_CONF_UNKNOWN:
748                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
749                                                 "unknown directive <%s> inside backend info definition"
750                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
751                                                 c->log, *c->argv, 0);
752 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
753                                         continue;
754 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
755                                 default:
756                                         rc = 1;
757                                         goto done;
758                                 }
759                         }
760
761                 } else if ( c->be ) {
762                         rc = SLAP_CONF_UNKNOWN;
763                         if ( c->be->be_cf_ocs ) {
764                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
765                                 if ( ct ) {
766                                         rc = config_add_vals( ct, c );
767                                 }
768                         }
769                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
770                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
771                                         c->argc, c->argv);
772                         }
773                         if ( rc ) {
774                                 switch(rc) {
775                                 case SLAP_CONF_UNKNOWN:
776                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
777                                                 "unknown directive <%s> inside backend database "
778                                                 "definition" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
779                                                 c->log, *c->argv, 0);
780 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
781                                         continue;
782 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
783                                 default:
784                                         rc = 1;
785                                         goto done;
786                                 }
787                         }
788
789                 } else if ( frontendDB->be_config ) {
790                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
791                         if ( rc ) {
792                                 switch(rc) {
793                                 case SLAP_CONF_UNKNOWN:
794                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
795                                                 "unknown directive <%s> inside global database definition"
796                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
797                                                 c->log, *c->argv, 0);
798 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
799                                         continue;
800 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
801                                 default:
802                                         rc = 1;
803                                         goto done;
804                                 }
805                         }
806                         
807                 } else {
808                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
809                                 "unknown directive <%s> outside backend info and database definitions"
810                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
811                                 c->log, *c->argv, 0);
812 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
813                         rc = 1;
814                         goto done;
815 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
816                         continue;
817 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
818                 }
819         }
820
821         rc = 0;
822
823 done:
824         ch_free(c->tline);
825         fclose(fp);
826         ch_free(c->argv);
827         ch_free(c);
828         return(rc);
829 }
830
831 /* restrictops, allows, disallows, requires, loglevel */
832
833 int
834 verb_to_mask(const char *word, slap_verbmasks *v) {
835         int i;
836         for(i = 0; !BER_BVISNULL(&v[i].word); i++)
837                 if(!strcasecmp(word, v[i].word.bv_val))
838                         break;
839         return(i);
840 }
841
842 int
843 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
844         int i, j;
845         for(i = 1; i < argc; i++) {
846                 j = verb_to_mask(argv[i], v);
847                 if(BER_BVISNULL(&v[j].word)) return i;
848                 while (!v[j].mask) j--;
849                 *m |= v[j].mask;
850         }
851         return(0);
852 }
853
854 /* Mask keywords that represent multiple bits should occur before single
855  * bit keywords in the verbmasks array.
856  */
857 int
858 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
859         int i, rc = 1;
860
861         if (m) {
862                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
863                         if (!v[i].mask) continue;
864                         if (( m & v[i].mask ) == v[i].mask ) {
865                                 value_add_one( bva, &v[i].word );
866                                 rc = 0;
867                                 m ^= v[i].mask;
868                                 if ( !m ) break;
869                         }
870                 }
871         }
872         return rc;
873 }
874
875 int
876 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
877 {
878         int             i;
879
880         assert( *vp == NULL );
881
882         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ )
883                 ;
884
885         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
886
887         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
888                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
889                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
890         }
891
892         BER_BVZERO( &(*vp)[ i ].word );
893
894         return 0;               
895 }
896
897 int
898 slap_verbmasks_destroy( slap_verbmasks *v )
899 {
900         int             i;
901
902         assert( v != NULL );
903
904         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
905                 ch_free( v[ i ].word.bv_val );
906         }
907
908         ch_free( v );
909
910         return 0;
911 }
912
913 int
914 slap_verbmasks_append(
915         slap_verbmasks  **vp,
916         slap_mask_t     m,
917         struct berval   *v,
918         slap_mask_t     *ignore )
919 {
920         int     i;
921
922         if ( !m ) {
923                 return LDAP_OPERATIONS_ERROR;
924         }
925
926         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
927                 if ( !(*vp)[ i ].mask ) continue;
928
929                 if ( ignore != NULL ) {
930                         int     j;
931
932                         for ( j = 0; ignore[ j ] != 0; j++ ) {
933                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
934                                         goto check_next;
935                                 }
936                         }
937                 }
938
939                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
940                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
941                                 /* already set; ignore */
942                                 return LDAP_SUCCESS;
943                         }
944                         /* conflicts */
945                         return LDAP_TYPE_OR_VALUE_EXISTS;
946                 }
947
948                 if ( m & (*vp)[ i ].mask ) {
949                         /* conflicts */
950                         return LDAP_CONSTRAINT_VIOLATION;
951                 }
952 check_next:;
953         }
954
955         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
956         ber_dupbv( &(*vp)[ i ].word, v );
957         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
958         BER_BVZERO( &(*vp)[ i + 1 ].word );
959
960         return LDAP_SUCCESS;
961 }
962
963 int
964 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
965         int i;
966
967         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
968                 if ( m == v[i].mask ) {
969                         if ( bv != NULL ) {
970                                 *bv = v[i].word;
971                         }
972                         return i;
973                 }
974         }
975         return -1;
976 }
977
978 static slap_verbmasks tlskey[] = {
979         { BER_BVC("no"),        SB_TLS_OFF },
980         { BER_BVC("yes"),       SB_TLS_ON },
981         { BER_BVC("critical"),  SB_TLS_CRITICAL },
982         { BER_BVNULL, 0 }
983 };
984
985 static slap_verbmasks methkey[] = {
986         { BER_BVC("none"),      LDAP_AUTH_NONE },
987         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
988 #ifdef HAVE_CYRUS_SASL
989         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
990 #endif
991         { BER_BVNULL, 0 }
992 };
993
994 static slap_cf_aux_table bindkey[] = {
995         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
996         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
997         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
998         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
999         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1000         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1001         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1002         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1003         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1004         { BER_BVNULL, 0, 0, 0, NULL }
1005 };
1006
1007 int
1008 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1009 {
1010         int rc = 0;
1011         slap_cf_aux_table *tab;
1012
1013         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1014                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1015                         char **cptr, *next;
1016                         int *iptr, j;
1017                         unsigned *uptr;
1018                         struct berval *bptr;
1019                         const char *val = word + tab->key.bv_len;
1020
1021                         switch ( tab->type ) {
1022                         case 's':
1023                                 cptr = (char **)((char *)dst + tab->off);
1024                                 *cptr = ch_strdup( val );
1025                                 break;
1026
1027                         case 'b':
1028                                 bptr = (struct berval *)((char *)dst + tab->off);
1029                                 ber_str2bv( val, 0, 1, bptr );
1030                                 break;
1031
1032                         case 'd':
1033                                 assert( tab->aux != NULL );
1034                                 iptr = (int *)((char *)dst + tab->off);
1035
1036                                 rc = 1;
1037                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1038                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1039                                                 *iptr = tab->aux[j].mask;
1040                                                 rc = 0;
1041                                         }
1042                                 }
1043                                 break;
1044
1045                         case 'i':
1046                                 iptr = (int *)((char *)dst + tab->off);
1047
1048                                 *iptr = strtol( val, &next, 0 );
1049                                 if ( next == val || next[ 0 ] != '\0' ) {
1050                                         rc = 1;
1051                                 }
1052                                 break;
1053
1054                         case 'u':
1055                                 uptr = (unsigned *)((char *)dst + tab->off);
1056
1057                                 *uptr = strtoul( val, &next, 0 );
1058                                 if ( next == val || next[ 0 ] != '\0' ) {
1059                                         rc = 1;
1060                                 }
1061                                 break;
1062                         }
1063
1064                         if ( rc ) {
1065                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1066                                         tabmsg, word, 0 );
1067                         }
1068                         
1069                         return rc;
1070                 }
1071         }
1072
1073         return rc;
1074 }
1075
1076 int
1077 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1078 {
1079         char buf[BUFSIZ], *ptr;
1080         slap_cf_aux_table *tab;
1081         struct berval tmp;
1082
1083         ptr = buf;
1084         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1085                 char **cptr;
1086                 int *iptr, i;
1087                 unsigned *uptr;
1088                 struct berval *bptr;
1089
1090                 cptr = (char **)((char *)src + tab->off);
1091
1092                 switch ( tab->type ) {
1093                 case 'b':
1094                         bptr = (struct berval *)((char *)src + tab->off);
1095                         cptr = &bptr->bv_val;
1096                 case 's':
1097                         if ( *cptr ) {
1098                                 *ptr++ = ' ';
1099                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1100                                 if ( tab->quote ) *ptr++ = '"';
1101                                 ptr = lutil_strcopy( ptr, *cptr );
1102                                 if ( tab->quote ) *ptr++ = '"';
1103                         }
1104                         break;
1105
1106                 case 'd':
1107                         assert( tab->aux != NULL );
1108                         iptr = (int *)((char *)src + tab->off);
1109                 
1110                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1111                                 if ( *iptr == tab->aux[i].mask ) {
1112                                         *ptr++ = ' ';
1113                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1114                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1115                                         break;
1116                                 }
1117                         }
1118                         break;
1119
1120                 case 'i':
1121                         iptr = (int *)((char *)src + tab->off);
1122                         *ptr++ = ' ';
1123                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1124                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1125                         break;
1126
1127                 case 'u':
1128                         uptr = (unsigned *)((char *)src + tab->off);
1129                         *ptr++ = ' ';
1130                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1131                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1132                         break;
1133                 }
1134         }
1135         tmp.bv_val = buf;
1136         tmp.bv_len = ptr - buf;
1137         ber_dupbv( bv, &tmp );
1138         return 0;
1139 }
1140
1141 int
1142 bindconf_parse( const char *word, slap_bindconf *bc )
1143 {
1144         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1145 }
1146
1147 int
1148 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1149 {
1150         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1151 }
1152
1153 void bindconf_free( slap_bindconf *bc ) {
1154         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1155                 ch_free( bc->sb_binddn.bv_val );
1156                 BER_BVZERO( &bc->sb_binddn );
1157         }
1158         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1159                 ch_free( bc->sb_cred.bv_val );
1160                 BER_BVZERO( &bc->sb_cred );
1161         }
1162         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1163                 ch_free( bc->sb_saslmech.bv_val );
1164                 BER_BVZERO( &bc->sb_saslmech );
1165         }
1166         if ( bc->sb_secprops ) {
1167                 ch_free( bc->sb_secprops );
1168                 bc->sb_secprops = NULL;
1169         }
1170         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1171                 ch_free( bc->sb_realm.bv_val );
1172                 BER_BVZERO( &bc->sb_realm );
1173         }
1174         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1175                 ch_free( bc->sb_authcId.bv_val );
1176                 BER_BVZERO( &bc->sb_authcId );
1177         }
1178         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1179                 ch_free( bc->sb_authzId.bv_val );
1180                 BER_BVZERO( &bc->sb_authzId );
1181         }
1182 }
1183
1184
1185 /* -------------------------------------- */
1186
1187
1188 static char *
1189 strtok_quote( char *line, char *sep, char **quote_ptr )
1190 {
1191         int             inquote;
1192         char            *tmp;
1193         static char     *next;
1194
1195         *quote_ptr = NULL;
1196         if ( line != NULL ) {
1197                 next = line;
1198         }
1199         while ( *next && strchr( sep, *next ) ) {
1200                 next++;
1201         }
1202
1203         if ( *next == '\0' ) {
1204                 next = NULL;
1205                 return( NULL );
1206         }
1207         tmp = next;
1208
1209         for ( inquote = 0; *next; ) {
1210                 switch ( *next ) {
1211                 case '"':
1212                         if ( inquote ) {
1213                                 inquote = 0;
1214                         } else {
1215                                 inquote = 1;
1216                         }
1217                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1218                         break;
1219
1220                 case '\\':
1221                         if ( next[1] )
1222                                 AC_MEMCPY( next,
1223                                             next + 1, strlen( next + 1 ) + 1 );
1224                         next++;         /* dont parse the escaped character */
1225                         break;
1226
1227                 default:
1228                         if ( ! inquote ) {
1229                                 if ( strchr( sep, *next ) != NULL ) {
1230                                         *quote_ptr = next;
1231                                         *next++ = '\0';
1232                                         return( tmp );
1233                                 }
1234                         }
1235                         next++;
1236                         break;
1237                 }
1238         }
1239
1240         return( tmp );
1241 }
1242
1243 static char     buf[BUFSIZ];
1244 static char     *line;
1245 static size_t lmax, lcur;
1246
1247 #define CATLINE( buf ) \
1248         do { \
1249                 size_t len = strlen( buf ); \
1250                 while ( lcur + len + 1 > lmax ) { \
1251                         lmax += BUFSIZ; \
1252                         line = (char *) ch_realloc( line, lmax ); \
1253                 } \
1254                 strcpy( line + lcur, buf ); \
1255                 lcur += len; \
1256         } while( 0 )
1257
1258 static void
1259 fp_getline_init(ConfigArgs *c) {
1260         c->lineno = -1;
1261         buf[0] = '\0';
1262 }
1263
1264 static int
1265 fp_getline( FILE *fp, ConfigArgs *c )
1266 {
1267         char    *p;
1268
1269         lcur = 0;
1270         CATLINE(buf);
1271         c->lineno++;
1272
1273         /* avoid stack of bufs */
1274         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1275                 buf[0] = '\0';
1276                 c->line = line;
1277                 return(1);
1278         }
1279
1280         while ( fgets( buf, sizeof( buf ), fp ) ) {
1281                 p = strchr( buf, '\n' );
1282                 if ( p ) {
1283                         if ( p > buf && p[-1] == '\r' ) {
1284                                 --p;
1285                         }
1286                         *p = '\0';
1287                 }
1288                 /* XXX ugly */
1289                 c->line = line;
1290                 if ( line[0]
1291                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1292                                 && p[-1] != '\\' )
1293                 {
1294                         p[0] = '\0';
1295                         lcur--;
1296                         
1297                 } else {
1298                         if ( !isspace( (unsigned char)buf[0] ) ) {
1299                                 return(1);
1300                         }
1301                         buf[0] = ' ';
1302                 }
1303                 CATLINE(buf);
1304                 c->lineno++;
1305         }
1306
1307         buf[0] = '\0';
1308         c->line = line;
1309         return(line[0] ? 1 : 0);
1310 }
1311
1312 static int
1313 fp_parse_line(ConfigArgs *c)
1314 {
1315         char *token;
1316         static char *const hide[] = {
1317                 "rootpw", "replica", "syncrepl",  /* in slapd */
1318                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1319                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1320                 "pseudorootpw",  /* in back-meta */
1321                 "dbpasswd",  /* in back-sql */
1322                 NULL
1323         };
1324         char *quote_ptr;
1325         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1326
1327         c->tline = ch_strdup(c->line);
1328         token = strtok_quote(c->tline, " \t", &quote_ptr);
1329
1330         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1331         if(quote_ptr) *quote_ptr = ' ';
1332         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1333                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1334         if(quote_ptr) *quote_ptr = '\0';
1335
1336         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1337                 if(c->argc >= c->argv_size) {
1338                         char **tmp;
1339                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1340                         if(!tmp) {
1341                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1342                                 return -1;
1343                         }
1344                         c->argv = tmp;
1345                         c->argv_size += ARGS_STEP;
1346                 }
1347                 if(token == NULL)
1348                         break;
1349                 c->argv[c->argc++] = token;
1350         }
1351         c->argv[c->argc] = NULL;
1352         return(0);
1353 }
1354
1355 void
1356 config_destroy( )
1357 {
1358         ucdata_unload( UCDATA_ALL );
1359         if ( frontendDB ) {
1360                 /* NOTE: in case of early exit, frontendDB can be NULL */
1361                 if ( frontendDB->be_schemandn.bv_val )
1362                         free( frontendDB->be_schemandn.bv_val );
1363                 if ( frontendDB->be_schemadn.bv_val )
1364                         free( frontendDB->be_schemadn.bv_val );
1365                 if ( frontendDB->be_acl )
1366                         acl_destroy( frontendDB->be_acl, NULL );
1367         }
1368         free( line );
1369         if ( slapd_args_file )
1370                 free ( slapd_args_file );
1371         if ( slapd_pid_file )
1372                 free ( slapd_pid_file );
1373         if ( default_passwd_hash )
1374                 ldap_charray_free( default_passwd_hash );
1375 }
1376
1377 char **
1378 slap_str2clist( char ***out, char *in, const char *brkstr )
1379 {
1380         char    *str;
1381         char    *s;
1382         char    *lasts;
1383         int     i, j;
1384         char    **new;
1385
1386         /* find last element in list */
1387         for (i = 0; *out && (*out)[i]; i++);
1388
1389         /* protect the input string from strtok */
1390         str = ch_strdup( in );
1391
1392         if ( *str == '\0' ) {
1393                 free( str );
1394                 return( *out );
1395         }
1396
1397         /* Count words in string */
1398         j=1;
1399         for ( s = str; *s; s++ ) {
1400                 if ( strchr( brkstr, *s ) != NULL ) {
1401                         j++;
1402                 }
1403         }
1404
1405         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1406         new = *out + i;
1407         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1408                 s != NULL;
1409                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1410         {
1411                 *new = ch_strdup( s );
1412                 new++;
1413         }
1414
1415         *new = NULL;
1416         free( str );
1417         return( *out );
1418 }
1419
1420 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1421         int argc, char **argv )
1422 {
1423         ConfigArgs c = { 0 };
1424         ConfigTable *ct;
1425         int rc;
1426
1427         c.be = be;
1428         c.fname = fname;
1429         c.lineno = lineno;
1430         c.argc = argc;
1431         c.argv = argv;
1432         c.valx = -1;
1433         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1434
1435         rc = SLAP_CONF_UNKNOWN;
1436         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1437         if ( ct )
1438                 rc = config_add_vals( ct, &c );
1439         return rc;
1440 }