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