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