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