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