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