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