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