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