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