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