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