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