]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
import fix for ITS#5091
[openldap] / servers / slapd / config.c
1 /* config.c - configuration file handling routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/ctype.h>
33 #include <ac/signal.h>
34 #include <ac/socket.h>
35 #include <ac/errno.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #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:
195                                 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
196                                         snprintf( c->msg, sizeof( c->msg ),
197                                                 "<%s> unable to parse \"%s\" as int",
198                                                 c->argv[0], c->argv[1] );
199                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
200                                                 c->log, c->msg, 0);
201                                         return(ARG_BAD_CONF);
202                                 }
203                                 break;
204                         case ARG_LONG:
205                                 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
206                                         snprintf( c->msg, sizeof( c->msg ),
207                                                 "<%s> unable to parse \"%s\" as long",
208                                                 c->argv[0], c->argv[1] );
209                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
210                                                 c->log, c->msg, 0);
211                                         return(ARG_BAD_CONF);
212                                 }
213                                 break;
214                         case ARG_BER_LEN_T: {
215                                 unsigned long   l;
216                                 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
217                                         snprintf( c->msg, sizeof( c->msg ),
218                                                 "<%s> unable to parse \"%s\" as ber_len_t",
219                                                 c->argv[0], c->argv[1] );
220                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
221                                                 c->log, c->msg, 0);
222                                         return(ARG_BAD_CONF);
223                                 }
224                                 barg = (ber_len_t)l;
225                                 } break;
226                         case ARG_ON_OFF:
227                                 if (c->argc == 1) {
228                                         iarg = 1;
229                                 } else if ( !strcasecmp(c->argv[1], "on") ||
230                                         !strcasecmp(c->argv[1], "true") ||
231                                         !strcasecmp(c->argv[1], "yes") )
232                                 {
233                                         iarg = 1;
234                                 } else if ( !strcasecmp(c->argv[1], "off") ||
235                                         !strcasecmp(c->argv[1], "false") ||
236                                         !strcasecmp(c->argv[1], "no") )
237                                 {
238                                         iarg = 0;
239                                 } else {
240                                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value, ignored",
241                                                 c->argv[0] );
242                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
243                                                 c->log, c->msg, 0 );
244                                         return(0);
245                                 }
246                                 break;
247                 }
248                 j = (arg_type & ARG_NONZERO) ? 1 : 0;
249                 if(iarg < j && larg < j && barg < j ) {
250                         larg = larg ? larg : (barg ? barg : iarg);
251                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value, ignored",
252                                 c->argv[0] );
253                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
254                                 c->log, c->msg, 0 );
255                         return(ARG_BAD_CONF);
256                 }
257                 switch(arg_type & ARGS_NUMERIC) {
258                         case ARG_ON_OFF:
259                         case ARG_INT:           c->value_int = iarg;            break;
260                         case ARG_LONG:          c->value_long = larg;           break;
261                         case ARG_BER_LEN_T:     c->value_ber_t = barg;          break;
262                 }
263         } else if(arg_type & ARG_STRING) {
264                 if ( !check_only )
265                         c->value_string = ch_strdup(c->argv[1]);
266         } else if(arg_type & ARG_BERVAL) {
267                 if ( !check_only )
268                         ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
269         } else if(arg_type & ARG_DN) {
270                 struct berval bv;
271                 ber_str2bv( c->argv[1], 0, 0, &bv );
272                 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
273                 if ( rc != LDAP_SUCCESS ) {
274                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid DN %d (%s)",
275                                 c->argv[0], rc, ldap_err2string( rc ));
276                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
277                         return(ARG_BAD_CONF);
278                 }
279                 if ( check_only ) {
280                         ch_free( c->value_ndn.bv_val );
281                         ch_free( c->value_dn.bv_val );
282                 }
283         }
284         return 0;
285 }
286
287 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
288         int rc, arg_type;
289         void *ptr = NULL;
290
291         arg_type = Conf->arg_type;
292         if(arg_type & ARG_MAGIC) {
293                 if(!c->be) c->be = frontendDB;
294                 c->msg[0] = '\0';
295                 rc = (*((ConfigDriver*)Conf->arg_item))(c);
296 #if 0
297                 if(c->be == frontendDB) c->be = NULL;
298 #endif
299                 if(rc) {
300                         if ( !c->msg[0] ) {
301                                 snprintf( c->msg, sizeof( c->msg ), "<%s> handler exited with %d",
302                                         c->argv[0], rc );
303                                 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
304                                         c->log, c->msg, 0 );
305                         }
306                         return(ARG_BAD_CONF);
307                 }
308                 return(0);
309         }
310         if(arg_type & ARG_OFFSET) {
311                 if (c->be && (!overlay_is_over(c->be) || 
312                         ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
313                         ptr = c->be->be_private;
314                 else if (c->bi)
315                         ptr = c->bi->bi_private;
316                 else {
317                         snprintf( c->msg, sizeof( c->msg ), "<%s> offset is missing base pointer",
318                                 c->argv[0] );
319                         Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
320                                 c->log, c->msg, 0);
321                         return(ARG_BAD_CONF);
322                 }
323                 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
324         } else if (arg_type & ARGS_POINTER) {
325                 ptr = Conf->arg_item;
326         }
327         if(arg_type & ARGS_POINTER)
328                 switch(arg_type & ARGS_POINTER) {
329                         case ARG_ON_OFF:
330                         case ARG_INT:           *(int*)ptr = c->value_int;                      break;
331                         case ARG_LONG:          *(long*)ptr = c->value_long;                    break;
332                         case ARG_BER_LEN_T:     *(ber_len_t*)ptr = c->value_ber_t;                      break;
333                         case ARG_STRING: {
334                                 char *cc = *(char**)ptr;
335                                 if(cc) {
336                                         if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
337                                                 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
338                                                         c->log, Conf->name, 0 );
339                                                 return(ARG_BAD_CONF);
340                                         }
341                                         ch_free(cc);
342                                 }
343                                 *(char **)ptr = c->value_string;
344                                 break;
345                                 }
346                         case ARG_BERVAL:
347                                 *(struct berval *)ptr = c->value_bv;
348                                 break;
349                 }
350         return(0);
351 }
352
353 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
354         int rc, arg_type;
355
356         arg_type = Conf->arg_type;
357         if(arg_type == ARG_IGNORED) {
358                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
359                         c->log, Conf->name, 0);
360                 return(0);
361         }
362         rc = config_check_vals( Conf, c, 0 );
363         if ( rc ) return rc;
364         return config_set_vals( Conf, c );
365 }
366
367 int
368 config_del_vals(ConfigTable *cf, ConfigArgs *c)
369 {
370         int rc = 0;
371
372         /* If there is no handler, just ignore it */
373         if ( cf->arg_type & ARG_MAGIC ) {
374                 c->op = LDAP_MOD_DELETE;
375                 c->type = cf->arg_type & ARGS_USERLAND;
376                 rc = (*((ConfigDriver*)cf->arg_item))(c);
377         }
378         return rc;
379 }
380
381 int
382 config_get_vals(ConfigTable *cf, ConfigArgs *c)
383 {
384         int rc = 0;
385         struct berval bv;
386         void *ptr;
387
388         if ( cf->arg_type & ARG_IGNORED ) {
389                 return 1;
390         }
391
392         memset(&c->values, 0, sizeof(c->values));
393         c->rvalue_vals = NULL;
394         c->rvalue_nvals = NULL;
395         c->op = SLAP_CONFIG_EMIT;
396         c->type = cf->arg_type & ARGS_USERLAND;
397
398         if ( cf->arg_type & ARG_MAGIC ) {
399                 rc = (*((ConfigDriver*)cf->arg_item))(c);
400                 if ( rc ) return rc;
401         } else {
402                 if ( cf->arg_type & ARG_OFFSET ) {
403                         if (c->be && (!overlay_is_over(c->be) || 
404                                 ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
405                                 ptr = c->be->be_private;
406                         else if ( c->bi )
407                                 ptr = c->bi->bi_private;
408                         else
409                                 return 1;
410                         ptr = (void *)((char *)ptr + (long)cf->arg_item);
411                 } else {
412                         ptr = cf->arg_item;
413                 }
414                 
415                 switch(cf->arg_type & ARGS_POINTER) {
416                 case ARG_ON_OFF:
417                 case ARG_INT:   c->value_int = *(int *)ptr; break;
418                 case ARG_LONG:  c->value_long = *(long *)ptr; break;
419                 case ARG_BER_LEN_T:     c->value_ber_t = *(ber_len_t *)ptr; break;
420                 case ARG_STRING:
421                         if ( *(char **)ptr )
422                                 c->value_string = ch_strdup(*(char **)ptr);
423                         break;
424                 case ARG_BERVAL:
425                         ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
426                 }
427         }
428         if ( cf->arg_type & ARGS_POINTER) {
429                 bv.bv_val = c->log;
430                 switch(cf->arg_type & ARGS_POINTER) {
431                 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
432                 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
433                 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
434                 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
435                         c->value_int ? "TRUE" : "FALSE"); break;
436                 case ARG_STRING:
437                         if ( c->value_string && c->value_string[0]) {
438                                 ber_str2bv( c->value_string, 0, 0, &bv);
439                         } else {
440                                 return 1;
441                         }
442                         break;
443                 case ARG_BERVAL:
444                         if ( !BER_BVISEMPTY( &c->value_bv )) {
445                                 bv = c->value_bv;
446                         } else {
447                                 return 1;
448                         }
449                         break;
450                 }
451                 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
452                         return 1;
453                 }
454                 if (( cf->arg_type & ARGS_POINTER ) == ARG_STRING )
455                         ber_bvarray_add(&c->rvalue_vals, &bv);
456                 else
457                         value_add_one(&c->rvalue_vals, &bv);
458         }
459         return rc;
460 }
461
462 int
463 init_config_attrs(ConfigTable *ct) {
464         LDAPAttributeType *at;
465         int i, code;
466         const char *err;
467
468         for (i=0; ct[i].name; i++ ) {
469                 int             freeit = 0;
470
471                 if ( !ct[i].attribute ) continue;
472                 at = ldap_str2attributetype( ct[i].attribute,
473                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
474                 if ( !at ) {
475                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
476                                 ct[i].attribute, ldap_scherr2str(code), err );
477                         return code;
478                 }
479
480                 code = at_add( at, 0, NULL, &err );
481                 if ( code ) {
482                         if ( code == SLAP_SCHERR_ATTR_DUP ) {
483                                 freeit = 1;
484
485                         } else {
486                                 ldap_attributetype_free( at );
487                                 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
488                                         ct[i].attribute, scherr2str(code), err );
489                                 return code;
490                         }
491                 }
492                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
493                 if ( freeit ) {
494                         ldap_attributetype_free( at );
495                 } else {
496                         ldap_memfree( at );
497                 }
498                 if ( code ) {
499                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
500                                 ct[i].attribute, err );
501                         return code;
502                 }
503         }
504
505         return 0;
506 }
507
508 int
509 init_config_ocs( ConfigOCs *ocs ) {
510         int i;
511
512         for (i=0;ocs[i].co_def;i++) {
513                 LDAPObjectClass *oc;
514                 int code;
515                 const char *err;
516
517                 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
518                         LDAP_SCHEMA_ALLOW_ALL );
519                 if ( !oc ) {
520                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
521                                 ocs[i].co_def, ldap_scherr2str(code), err );
522                         return code;
523                 }
524                 code = oc_add(oc,0,NULL,&err);
525                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
526                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
527                                 ocs[i].co_def, scherr2str(code), err );
528                         return code;
529                 }
530                 ocs[i].co_oc = oc_find(oc->oc_names[0]);
531                 ldap_memfree(oc);
532         }
533         return 0;
534 }
535
536 /* Split an LDIF line into space-separated tokens. Words may be grouped
537  * by quotes. A quoted string may begin in the middle of a word, but must
538  * end at the end of the word (be followed by whitespace or EOS). Any other
539  * quotes are passed through unchanged. All other characters are passed
540  * through unchanged.
541  */
542 static char *
543 strtok_quote_ldif( char **line )
544 {
545         char *beg, *ptr, *quote=NULL;
546         int inquote=0;
547
548         ptr = *line;
549
550         if ( !ptr || !*ptr )
551                 return NULL;
552
553         while( isspace( *ptr )) ptr++;
554
555         if ( *ptr == '"' ) {
556                 inquote = 1;
557                 ptr++;
558         }
559
560         beg = ptr;
561
562         for (;*ptr;ptr++) {
563                 if ( *ptr == '"' ) {
564                         if ( inquote && ( !ptr[1] || isspace(ptr[1]))) {
565                                 *ptr++ = '\0';
566                                 break;
567                         }
568                         inquote = 1;
569                         quote = ptr;
570                         continue;
571                 }
572                 if ( inquote )
573                         continue;
574                 if ( isspace( *ptr )) {
575                         *ptr++ = '\0';
576                         break;
577                 }
578         }
579         if ( quote ) {
580                 while ( quote < ptr ) {
581                         *quote = quote[1];
582                         quote++;
583                 }
584         }
585         if ( !*ptr ) {
586                 *line = NULL;
587         } else {
588                 while ( isspace( *ptr )) ptr++;
589                 *line = ptr;
590         }
591         return beg;
592 }
593
594 static void
595 config_parse_ldif( ConfigArgs *c )
596 {
597         char *next;
598         c->tline = ch_strdup(c->line);
599         next = c->tline;
600
601         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
602                 c->argc++;
603                 if ( c->argc >= c->argv_size ) {
604                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
605                                 sizeof( *c->argv ));
606                         c->argv = tmp;
607                         c->argv_size += ARGS_STEP;
608                 }
609         }
610         c->argv[c->argc] = NULL;
611 }
612
613 int
614 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
615 {
616         int     rc = 0;
617
618         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
619                 ct->ad->ad_cname.bv_val, valx );
620         c->argc = 1;
621         c->argv[0] = ct->ad->ad_cname.bv_val;
622
623         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
624                 c->argv[c->argc] = c->line;
625                 c->argc++;
626                 c->argv[c->argc] = NULL;
627                 c->tline = NULL;
628         } else {
629                 config_parse_ldif( c );
630         }
631         rc = config_check_vals( ct, c, 1 );
632         ch_free( c->tline );
633         c->tline = NULL;
634
635         if ( rc )
636                 rc = LDAP_CONSTRAINT_VIOLATION;
637
638         return rc;
639 }
640
641 int
642 config_parse_add(ConfigTable *ct, ConfigArgs *c)
643 {
644         int     rc = 0;
645
646         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
647                 ct->ad->ad_cname.bv_val, c->valx );
648         c->argc = 1;
649         c->argv[0] = ct->ad->ad_cname.bv_val;
650
651         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
652                 c->argv[c->argc] = c->line;
653                 c->argc++;
654                 c->argv[c->argc] = NULL;
655                 c->tline = NULL;
656         } else {
657                 config_parse_ldif( c );
658         }
659         c->op = LDAP_MOD_ADD;
660         rc = config_add_vals( ct, c );
661         ch_free( c->tline );
662
663         return rc;
664 }
665
666 int
667 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
668 {
669         FILE *fp;
670         ConfigTable *ct;
671         ConfigArgs *c;
672         int rc;
673         struct stat s;
674
675         c = ch_calloc( 1, sizeof( ConfigArgs ) );
676         if ( c == NULL ) {
677                 return 1;
678         }
679
680         if ( depth ) {
681                 memcpy( c, cf, sizeof( ConfigArgs ) );
682         } else {
683                 c->depth = depth; /* XXX */
684                 c->bi = NULL;
685                 c->be = NULL;
686         }
687
688         c->valx = -1;
689         c->fname = fname;
690         init_config_argv( c );
691
692         if ( stat( fname, &s ) != 0 ) {
693                 ldap_syslog = 1;
694                 Debug(LDAP_DEBUG_ANY,
695                     "could not stat config file \"%s\": %s (%d)\n",
696                     fname, strerror(errno), errno);
697                 return(1);
698         }
699
700         if ( !S_ISREG( s.st_mode ) ) {
701                 ldap_syslog = 1;
702                 Debug(LDAP_DEBUG_ANY,
703                     "regular file expected, got \"%s\"\n",
704                     fname, 0, 0 );
705                 return(1);
706         }
707
708         fp = fopen( fname, "r" );
709         if ( fp == NULL ) {
710                 ldap_syslog = 1;
711                 Debug(LDAP_DEBUG_ANY,
712                     "could not open config file \"%s\": %s (%d)\n",
713                     fname, strerror(errno), errno);
714                 return(1);
715         }
716
717         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
718
719         fp_getline_init(c);
720
721         c->tline = NULL;
722
723         while ( fp_getline( fp, c ) ) {
724                 /* skip comments and blank lines */
725                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
726                         continue;
727                 }
728
729                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
730                                 c->fname, c->lineno );
731
732                 c->argc = 0;
733                 ch_free( c->tline );
734                 if ( fp_parse_line( c ) ) {
735                         rc = 1;
736                         goto done;
737                 }
738
739                 if ( c->argc < 1 ) {
740                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: bad config line" 
741                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
742                                 c->log, 0, 0);
743 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
744                         rc = 1;
745                         goto done;
746 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
747                         continue;
748 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
749                 }
750
751                 c->op = SLAP_CONFIG_ADD;
752
753                 ct = config_find_keyword( cft, c );
754                 if ( ct ) {
755                         rc = config_add_vals( ct, c );
756                         if ( !rc ) continue;
757
758                         if ( rc & ARGS_USERLAND ) {
759                                 /* XXX a usertype would be opaque here */
760                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
761                                         c->log, c->argv[0], 0);
762                                 rc = 1;
763                                 goto done;
764
765                         } else if ( rc == ARG_BAD_CONF ) {
766                                 rc = 1;
767                                 goto done;
768                         }
769                         
770                 } else if ( c->bi && !c->be ) {
771                         rc = SLAP_CONF_UNKNOWN;
772                         if ( c->bi->bi_cf_ocs ) {
773                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
774                                 if ( ct ) {
775                                         rc = config_add_vals( ct, c );
776                                 }
777                         }
778                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
779                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
780                                         c->argc, c->argv);
781                         }
782                         if ( rc ) {
783                                 switch(rc) {
784                                 case SLAP_CONF_UNKNOWN:
785                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
786                                                 "unknown directive <%s> inside backend info definition"
787                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
788                                                 c->log, *c->argv, 0);
789 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
790                                         continue;
791 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
792                                 default:
793                                         rc = 1;
794                                         goto done;
795                                 }
796                         }
797
798                 } else if ( c->be ) {
799                         rc = SLAP_CONF_UNKNOWN;
800                         if ( c->be->be_cf_ocs ) {
801                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
802                                 if ( ct ) {
803                                         rc = config_add_vals( ct, c );
804                                 }
805                         }
806                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
807                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
808                                         c->argc, c->argv);
809                         }
810                         if ( rc ) {
811                                 switch(rc) {
812                                 case SLAP_CONF_UNKNOWN:
813                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
814                                                 "unknown directive <%s> inside backend database "
815                                                 "definition" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
816                                                 c->log, *c->argv, 0);
817 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
818                                         continue;
819 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
820                                 default:
821                                         rc = 1;
822                                         goto done;
823                                 }
824                         }
825
826                 } else if ( frontendDB->be_config ) {
827                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
828                         if ( rc ) {
829                                 switch(rc) {
830                                 case SLAP_CONF_UNKNOWN:
831                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
832                                                 "unknown directive <%s> inside global database definition"
833                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
834                                                 c->log, *c->argv, 0);
835 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
836                                         continue;
837 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
838                                 default:
839                                         rc = 1;
840                                         goto done;
841                                 }
842                         }
843                         
844                 } else {
845                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
846                                 "unknown directive <%s> outside backend info and database definitions"
847                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
848                                 c->log, *c->argv, 0);
849 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
850                         rc = 1;
851                         goto done;
852 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
853                         continue;
854 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
855                 }
856         }
857
858         rc = 0;
859
860 done:
861         ch_free(c->tline);
862         fclose(fp);
863         ch_free(c->argv);
864         ch_free(c);
865         return(rc);
866 }
867
868 /* restrictops, allows, disallows, requires, loglevel */
869
870 int
871 verb_to_mask(const char *word, slap_verbmasks *v) {
872         int i;
873         for(i = 0; !BER_BVISNULL(&v[i].word); i++)
874                 if(!strcasecmp(word, v[i].word.bv_val))
875                         break;
876         return(i);
877 }
878
879 int
880 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
881         int i, j;
882         for(i = 1; i < argc; i++) {
883                 j = verb_to_mask(argv[i], v);
884                 if(BER_BVISNULL(&v[j].word)) return i;
885                 while (!v[j].mask) j--;
886                 *m |= v[j].mask;
887         }
888         return(0);
889 }
890
891 /* Mask keywords that represent multiple bits should occur before single
892  * bit keywords in the verbmasks array.
893  */
894 int
895 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
896         int i, rc = 1;
897
898         if (m) {
899                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
900                         if (!v[i].mask) continue;
901                         if (( m & v[i].mask ) == v[i].mask ) {
902                                 value_add_one( bva, &v[i].word );
903                                 rc = 0;
904                                 m ^= v[i].mask;
905                                 if ( !m ) break;
906                         }
907                 }
908         }
909         return rc;
910 }
911
912 int
913 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
914 {
915         int             i;
916
917         assert( *vp == NULL );
918
919         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ )
920                 ;
921
922         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
923
924         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
925                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
926                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
927         }
928
929         BER_BVZERO( &(*vp)[ i ].word );
930
931         return 0;               
932 }
933
934 int
935 slap_verbmasks_destroy( slap_verbmasks *v )
936 {
937         int             i;
938
939         assert( v != NULL );
940
941         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
942                 ch_free( v[ i ].word.bv_val );
943         }
944
945         ch_free( v );
946
947         return 0;
948 }
949
950 int
951 slap_verbmasks_append(
952         slap_verbmasks  **vp,
953         slap_mask_t     m,
954         struct berval   *v,
955         slap_mask_t     *ignore )
956 {
957         int     i;
958
959         if ( !m ) {
960                 return LDAP_OPERATIONS_ERROR;
961         }
962
963         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
964                 if ( !(*vp)[ i ].mask ) continue;
965
966                 if ( ignore != NULL ) {
967                         int     j;
968
969                         for ( j = 0; ignore[ j ] != 0; j++ ) {
970                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
971                                         goto check_next;
972                                 }
973                         }
974                 }
975
976                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
977                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
978                                 /* already set; ignore */
979                                 return LDAP_SUCCESS;
980                         }
981                         /* conflicts */
982                         return LDAP_TYPE_OR_VALUE_EXISTS;
983                 }
984
985                 if ( m & (*vp)[ i ].mask ) {
986                         /* conflicts */
987                         return LDAP_CONSTRAINT_VIOLATION;
988                 }
989 check_next:;
990         }
991
992         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
993         ber_dupbv( &(*vp)[ i ].word, v );
994         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
995         BER_BVZERO( &(*vp)[ i + 1 ].word );
996
997         return LDAP_SUCCESS;
998 }
999
1000 int
1001 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1002         int i;
1003
1004         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1005                 if ( m == v[i].mask ) {
1006                         if ( bv != NULL ) {
1007                                 *bv = v[i].word;
1008                         }
1009                         return i;
1010                 }
1011         }
1012         return -1;
1013 }
1014
1015 static slap_verbmasks tlskey[] = {
1016         { BER_BVC("no"),        SB_TLS_OFF },
1017         { BER_BVC("yes"),       SB_TLS_ON },
1018         { BER_BVC("critical"),  SB_TLS_CRITICAL },
1019         { BER_BVNULL, 0 }
1020 };
1021
1022 static slap_verbmasks methkey[] = {
1023         { BER_BVC("none"),      LDAP_AUTH_NONE },
1024         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
1025 #ifdef HAVE_CYRUS_SASL
1026         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
1027 #endif
1028         { BER_BVNULL, 0 }
1029 };
1030
1031 static slap_cf_aux_table bindkey[] = {
1032         { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1033         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1034         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1035         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, dnNormalize },
1036         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1037         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1038         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1039         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1040 #ifndef SLAP_AUTHZ_SYNTAX
1041         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1042         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1043 #else /* SLAP_AUTHZ_SYNTAX */
1044         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, authzNormalize },
1045         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, authzNormalize },
1046 #endif /* SLAP_AUTHZ_SYNTAX */
1047
1048         { BER_BVNULL, 0, 0, 0, NULL }
1049 };
1050
1051 int
1052 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1053 {
1054         int rc = 0;
1055         slap_cf_aux_table *tab;
1056
1057         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1058                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1059                         char **cptr;
1060                         int *iptr, j;
1061                         unsigned *uptr;
1062                         long *lptr;
1063                         unsigned long *ulptr;
1064                         struct berval *bptr;
1065                         const char *val = word + tab->key.bv_len;
1066
1067                         switch ( tab->type ) {
1068                         case 's':
1069                                 cptr = (char **)((char *)dst + tab->off);
1070                                 *cptr = ch_strdup( val );
1071                                 break;
1072
1073                         case 'b':
1074                                 bptr = (struct berval *)((char *)dst + tab->off);
1075                                 if ( tab->aux != NULL ) {
1076                                         struct berval   dn;
1077                                         slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1078
1079                                         ber_str2bv( val, 0, 0, &dn );
1080                                         rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1081
1082                                 } else {
1083                                         ber_str2bv( val, 0, 1, bptr );
1084                                         rc = 0;
1085                                 }
1086                                 break;
1087
1088                         case 'i':
1089                                 iptr = (int *)((char *)dst + tab->off);
1090
1091                                 if ( tab->aux != NULL ) {
1092                                         slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1093
1094                                         assert( aux != NULL );
1095
1096                                         rc = 1;
1097                                         for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1098                                                 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1099                                                         *iptr = aux[j].mask;
1100                                                         rc = 0;
1101                                                         break;
1102                                                 }
1103                                         }
1104
1105                                 } else {
1106                                         rc = lutil_atoix( iptr, val, 0 );
1107                                 }
1108                                 break;
1109
1110                         case 'u':
1111                                 uptr = (unsigned *)((char *)dst + tab->off);
1112
1113                                 rc = lutil_atoux( uptr, val, 0 );
1114                                 break;
1115
1116                         case 'I':
1117                                 lptr = (long *)((char *)dst + tab->off);
1118
1119                                 rc = lutil_atolx( lptr, val, 0 );
1120                                 break;
1121
1122                         case 'U':
1123                                 ulptr = (unsigned long *)((char *)dst + tab->off);
1124
1125                                 rc = lutil_atoulx( ulptr, val, 0 );
1126                                 break;
1127                         }
1128
1129                         if ( rc ) {
1130                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1131                                         tabmsg, word, 0 );
1132                         }
1133                         
1134                         return rc;
1135                 }
1136         }
1137
1138         return rc;
1139 }
1140
1141 int
1142 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1143 {
1144         char buf[AC_LINE_MAX], *ptr;
1145         slap_cf_aux_table *tab;
1146         struct berval tmp;
1147
1148         ptr = buf;
1149         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1150                 char **cptr;
1151                 int *iptr, i;
1152                 unsigned *uptr;
1153                 long *lptr;
1154                 unsigned long *ulptr;
1155                 struct berval *bptr;
1156
1157                 cptr = (char **)((char *)src + tab->off);
1158
1159                 switch ( tab->type ) {
1160                 case 'b':
1161                         bptr = (struct berval *)((char *)src + tab->off);
1162                         cptr = &bptr->bv_val;
1163
1164                 case 's':
1165                         if ( *cptr ) {
1166                                 *ptr++ = ' ';
1167                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1168                                 if ( tab->quote ) *ptr++ = '"';
1169                                 ptr = lutil_strcopy( ptr, *cptr );
1170                                 if ( tab->quote ) *ptr++ = '"';
1171                         }
1172                         break;
1173
1174                 case 'i':
1175                         iptr = (int *)((char *)src + tab->off);
1176
1177                         if ( tab->aux != NULL ) {
1178                                 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1179
1180                                 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1181                                         if ( *iptr == aux[i].mask ) {
1182                                                 *ptr++ = ' ';
1183                                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1184                                                 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1185                                                 break;
1186                                         }
1187                                 }
1188
1189                         } else {
1190                                 *ptr++ = ' ';
1191                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1192                                 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1193                         }
1194                         break;
1195
1196                 case 'u':
1197                         uptr = (unsigned *)((char *)src + tab->off);
1198                         *ptr++ = ' ';
1199                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1200                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1201                         break;
1202
1203                 case 'I':
1204                         lptr = (long *)((char *)src + tab->off);
1205                         *ptr++ = ' ';
1206                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1207                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1208                         break;
1209
1210                 case 'U':
1211                         ulptr = (unsigned long *)((char *)src + tab->off);
1212                         *ptr++ = ' ';
1213                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1214                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1215                         break;
1216
1217                 default:
1218                         assert( 0 );
1219                 }
1220         }
1221         tmp.bv_val = buf;
1222         tmp.bv_len = ptr - buf;
1223         ber_dupbv( bv, &tmp );
1224         return 0;
1225 }
1226
1227 int
1228 bindconf_parse( const char *word, slap_bindconf *bc )
1229 {
1230         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1231 }
1232
1233 int
1234 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1235 {
1236         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1237 }
1238
1239 void bindconf_free( slap_bindconf *bc ) {
1240         if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1241                 ch_free( bc->sb_uri.bv_val );
1242                 BER_BVZERO( &bc->sb_uri );
1243         }
1244         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1245                 ch_free( bc->sb_binddn.bv_val );
1246                 BER_BVZERO( &bc->sb_binddn );
1247         }
1248         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1249                 ch_free( bc->sb_cred.bv_val );
1250                 BER_BVZERO( &bc->sb_cred );
1251         }
1252         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1253                 ch_free( bc->sb_saslmech.bv_val );
1254                 BER_BVZERO( &bc->sb_saslmech );
1255         }
1256         if ( bc->sb_secprops ) {
1257                 ch_free( bc->sb_secprops );
1258                 bc->sb_secprops = NULL;
1259         }
1260         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1261                 ch_free( bc->sb_realm.bv_val );
1262                 BER_BVZERO( &bc->sb_realm );
1263         }
1264         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1265                 ch_free( bc->sb_authcId.bv_val );
1266                 BER_BVZERO( &bc->sb_authcId );
1267         }
1268         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1269                 ch_free( bc->sb_authzId.bv_val );
1270                 BER_BVZERO( &bc->sb_authzId );
1271         }
1272 }
1273
1274
1275 /* -------------------------------------- */
1276
1277
1278 static char *
1279 strtok_quote( char *line, char *sep, char **quote_ptr )
1280 {
1281         int             inquote;
1282         char            *tmp;
1283         static char     *next;
1284
1285         *quote_ptr = NULL;
1286         if ( line != NULL ) {
1287                 next = line;
1288         }
1289         while ( *next && strchr( sep, *next ) ) {
1290                 next++;
1291         }
1292
1293         if ( *next == '\0' ) {
1294                 next = NULL;
1295                 return( NULL );
1296         }
1297         tmp = next;
1298
1299         for ( inquote = 0; *next; ) {
1300                 switch ( *next ) {
1301                 case '"':
1302                         if ( inquote ) {
1303                                 inquote = 0;
1304                         } else {
1305                                 inquote = 1;
1306                         }
1307                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1308                         break;
1309
1310                 case '\\':
1311                         if ( next[1] )
1312                                 AC_MEMCPY( next,
1313                                             next + 1, strlen( next + 1 ) + 1 );
1314                         next++;         /* dont parse the escaped character */
1315                         break;
1316
1317                 default:
1318                         if ( ! inquote ) {
1319                                 if ( strchr( sep, *next ) != NULL ) {
1320                                         *quote_ptr = next;
1321                                         *next++ = '\0';
1322                                         return( tmp );
1323                                 }
1324                         }
1325                         next++;
1326                         break;
1327                 }
1328         }
1329
1330         return( tmp );
1331 }
1332
1333 static char     buf[AC_LINE_MAX];
1334 static char     *line;
1335 static size_t lmax, lcur;
1336
1337 #define CATLINE( buf ) \
1338         do { \
1339                 size_t len = strlen( buf ); \
1340                 while ( lcur + len + 1 > lmax ) { \
1341                         lmax += AC_LINE_MAX; \
1342                         line = (char *) ch_realloc( line, lmax ); \
1343                 } \
1344                 strcpy( line + lcur, buf ); \
1345                 lcur += len; \
1346         } while( 0 )
1347
1348 static void
1349 fp_getline_init(ConfigArgs *c) {
1350         c->lineno = -1;
1351         buf[0] = '\0';
1352 }
1353
1354 static int
1355 fp_getline( FILE *fp, ConfigArgs *c )
1356 {
1357         char    *p;
1358
1359         lcur = 0;
1360         CATLINE(buf);
1361         c->lineno++;
1362
1363         /* avoid stack of bufs */
1364         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1365                 buf[0] = '\0';
1366                 c->line = line;
1367                 return(1);
1368         }
1369
1370         while ( fgets( buf, sizeof( buf ), fp ) ) {
1371                 p = strchr( buf, '\n' );
1372                 if ( p ) {
1373                         if ( p > buf && p[-1] == '\r' ) {
1374                                 --p;
1375                         }
1376                         *p = '\0';
1377                 }
1378                 /* XXX ugly */
1379                 c->line = line;
1380                 if ( line[0]
1381                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1382                                 && p[-1] != '\\' )
1383                 {
1384                         p[0] = '\0';
1385                         lcur--;
1386                         
1387                 } else {
1388                         if ( !isspace( (unsigned char)buf[0] ) ) {
1389                                 return(1);
1390                         }
1391                         buf[0] = ' ';
1392                 }
1393                 CATLINE(buf);
1394                 c->lineno++;
1395         }
1396
1397         buf[0] = '\0';
1398         c->line = line;
1399         return(line[0] ? 1 : 0);
1400 }
1401
1402 static int
1403 fp_parse_line(ConfigArgs *c)
1404 {
1405         char *token;
1406         static char *const hide[] = {
1407                 "rootpw", "replica", "syncrepl",  /* in slapd */
1408                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1409                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1410                 "pseudorootpw",  /* in back-meta */
1411                 "dbpasswd",  /* in back-sql */
1412                 NULL
1413         };
1414         char *quote_ptr;
1415         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1416
1417         c->tline = ch_strdup(c->line);
1418         token = strtok_quote(c->tline, " \t", &quote_ptr);
1419
1420         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1421         if(quote_ptr) *quote_ptr = ' ';
1422         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1423                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1424         if(quote_ptr) *quote_ptr = '\0';
1425
1426         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1427                 if(c->argc >= c->argv_size) {
1428                         char **tmp;
1429                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1430                         if(!tmp) {
1431                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1432                                 return -1;
1433                         }
1434                         c->argv = tmp;
1435                         c->argv_size += ARGS_STEP;
1436                 }
1437                 if(token == NULL)
1438                         break;
1439                 c->argv[c->argc++] = token;
1440         }
1441         c->argv[c->argc] = NULL;
1442         return(0);
1443 }
1444
1445 void
1446 config_destroy( )
1447 {
1448         ucdata_unload( UCDATA_ALL );
1449         if ( frontendDB ) {
1450                 /* NOTE: in case of early exit, frontendDB can be NULL */
1451                 if ( frontendDB->be_schemandn.bv_val )
1452                         free( frontendDB->be_schemandn.bv_val );
1453                 if ( frontendDB->be_schemadn.bv_val )
1454                         free( frontendDB->be_schemadn.bv_val );
1455                 if ( frontendDB->be_acl )
1456                         acl_destroy( frontendDB->be_acl, NULL );
1457         }
1458         free( line );
1459         if ( slapd_args_file )
1460                 free ( slapd_args_file );
1461         if ( slapd_pid_file )
1462                 free ( slapd_pid_file );
1463         if ( default_passwd_hash )
1464                 ldap_charray_free( default_passwd_hash );
1465 }
1466
1467 char **
1468 slap_str2clist( char ***out, char *in, const char *brkstr )
1469 {
1470         char    *str;
1471         char    *s;
1472         char    *lasts;
1473         int     i, j;
1474         char    **new;
1475
1476         /* find last element in list */
1477         for (i = 0; *out && (*out)[i]; i++);
1478
1479         /* protect the input string from strtok */
1480         str = ch_strdup( in );
1481
1482         if ( *str == '\0' ) {
1483                 free( str );
1484                 return( *out );
1485         }
1486
1487         /* Count words in string */
1488         j=1;
1489         for ( s = str; *s; s++ ) {
1490                 if ( strchr( brkstr, *s ) != NULL ) {
1491                         j++;
1492                 }
1493         }
1494
1495         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1496         new = *out + i;
1497         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1498                 s != NULL;
1499                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1500         {
1501                 *new = ch_strdup( s );
1502                 new++;
1503         }
1504
1505         *new = NULL;
1506         free( str );
1507         return( *out );
1508 }
1509
1510 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1511         int argc, char **argv )
1512 {
1513         ConfigArgs c = { 0 };
1514         ConfigTable *ct;
1515         int rc;
1516
1517         c.be = be;
1518         c.fname = fname;
1519         c.lineno = lineno;
1520         c.argc = argc;
1521         c.argv = argv;
1522         c.valx = -1;
1523         c.line = line;
1524         c.op = SLAP_CONFIG_ADD;
1525         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1526
1527         rc = SLAP_CONF_UNKNOWN;
1528         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1529         if ( ct )
1530                 rc = config_add_vals( ct, &c );
1531         return rc;
1532 }