]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
90254a46f38c87fcfb9c165c5230b005c187a71c
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2004 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31
32 #include "slap.h"
33 #include "back-ldap.h"
34 #undef ldap_debug       /* silence a warning in ldap-int.h */
35 #include "../../../libraries/libldap/ldap-int.h"
36
37 #include "lutil.h"
38
39 static int
40 ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
41          struct berval *bdn, int flags );
42 #define LDAP_BUILD_ENTRY_PRIVATE        0x01
43 #define LDAP_BUILD_ENTRY_NORMALIZE      0x02
44
45 static struct berval dummy = { 0, NULL };
46
47 int
48 ldap_back_search(
49     Operation   *op,
50     SlapReply *rs )
51 {
52         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
53         struct ldapconn *lc;
54         struct timeval  tv;
55         LDAPMessage             *res, *e;
56         int     rc = 0, msgid; 
57         struct berval match = { 0, NULL };
58         char **mapped_attrs = NULL;
59         struct berval mbase;
60         struct berval mfilter = { 0, NULL };
61         struct slap_limits_set *limit = NULL;
62         int isroot = 0;
63         int dontfreetext = 0;
64         dncookie dc;
65 #ifdef LDAP_BACK_PROXY_AUTHZ
66         LDAPControl **ctrls = NULL;
67 #endif /* LDAP_BACK_PROXY_AUTHZ */
68
69         lc = ldap_back_getconn(op, rs);
70         if ( !lc ) {
71                 return( -1 );
72         }
73
74         /*
75          * FIXME: in case of values return filter, we might want
76          * to map attrs and maybe rewrite value
77          */
78         if ( !ldap_back_dobind( lc, op, rs ) ) {
79                 return( -1 );
80         }
81
82         /* if not root, get appropriate limits */
83         if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
84                 isroot = 1;
85         } else {
86                 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
87         }
88         
89         /* if no time limit requested, rely on remote server limits */
90         /* if requested limit higher than hard limit, abort */
91         if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
92                 /* no hard limit means use soft instead */
93                 if ( limit->lms_t_hard == 0
94                                 && limit->lms_t_soft > -1
95                                 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
96                         op->oq_search.rs_tlimit = limit->lms_t_soft;
97                         
98                 /* positive hard limit means abort */
99                 } else if ( limit->lms_t_hard > 0 ) {
100                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
101                         rc = 0;
102                         goto finish;
103                 }
104                 
105                 /* negative hard limit means no limit */
106         }
107         
108         /* if no size limit requested, rely on remote server limits */
109         /* if requested limit higher than hard limit, abort */
110         if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
111                 /* no hard limit means use soft instead */
112                 if ( limit->lms_s_hard == 0
113                                 && limit->lms_s_soft > -1
114                                 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
115                         op->oq_search.rs_slimit = limit->lms_s_soft;
116                         
117                 /* positive hard limit means abort */
118                 } else if ( limit->lms_s_hard > 0 ) {
119                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
120                         rc = 0;
121                         goto finish;
122                 }
123                 
124                 /* negative hard limit means no limit */
125         }
126
127         /* should we check return values? */
128         if (op->oq_search.rs_deref != -1)
129                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->oq_search.rs_deref);
130         if (op->oq_search.rs_tlimit != -1) {
131                 tv.tv_sec = op->oq_search.rs_tlimit;
132                 tv.tv_usec = 0;
133         } else {
134                 tv.tv_sec = 0;
135         }
136
137         /*
138          * Rewrite the search base, if required
139          */
140         dc.rwmap = &li->rwmap;
141 #ifdef ENABLE_REWRITE
142         dc.conn = op->o_conn;
143         dc.rs = rs;
144         dc.ctx = "searchBase";
145 #else
146         dc.tofrom = 1;
147         dc.normalized = 0;
148 #endif
149         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mbase ) ) {
150                 send_ldap_result( op, rs );
151                 return -1;
152         }
153
154         rc = ldap_back_filter_map_rewrite( &dc, op->oq_search.rs_filter,
155                         &mfilter, BACKLDAP_MAP );
156
157         if ( rc ) {
158                 rs->sr_err = LDAP_OTHER;
159                 rs->sr_text = "Rewrite error";
160                 dontfreetext = 1;
161                 rc = -1;
162                 goto finish;
163         }
164
165         rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
166                         op->oq_search.rs_attrs,
167                         BACKLDAP_MAP, &mapped_attrs );
168         if ( rs->sr_err ) {
169                 rc = -1;
170                 goto finish;
171         }
172
173 #ifdef LDAP_BACK_PROXY_AUTHZ
174         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
175         if ( rc != LDAP_SUCCESS ) {
176                 dontfreetext = 1;
177                 goto finish;
178         }
179 #endif /* LDAP_BACK_PROXY_AUTHZ */
180         
181         rs->sr_err = ldap_search_ext(lc->ld, mbase.bv_val,
182                         op->oq_search.rs_scope, mfilter.bv_val,
183                         mapped_attrs, op->oq_search.rs_attrsonly,
184 #ifdef LDAP_BACK_PROXY_AUTHZ
185                         ctrls,
186 #else /* ! LDAP_BACK_PROXY_AUTHZ */
187                         op->o_ctrls,
188 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
189                         NULL,
190                         tv.tv_sec ? &tv : NULL, op->oq_search.rs_slimit,
191                         &msgid );
192
193         if ( rs->sr_err != LDAP_SUCCESS ) {
194 fail:;
195                 rc = ldap_back_op_result(lc, op, rs, msgid, 0);
196                 goto finish;
197         }
198
199         /* We pull apart the ber result, stuff it into a slapd entry, and
200          * let send_search_entry stuff it back into ber format. Slow & ugly,
201          * but this is necessary for version matching, and for ACL processing.
202          */
203
204         for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
205         {
206                 /* check for abandon */
207                 if (op->o_abandon) {
208                         ldap_abandon(lc->ld, msgid);
209                         rc = 0;
210                         goto finish;
211                 }
212
213                 if (rc == 0) {
214                         tv.tv_sec = 0;
215                         tv.tv_usec = 100000;
216                         ldap_pvt_thread_yield();
217
218                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
219                         Entry ent = {0};
220                         struct berval bdn;
221                         e = ldap_first_entry(lc->ld,res);
222                         if ( ldap_build_entry(op, e, &ent, &bdn,
223                                                 LDAP_BUILD_ENTRY_PRIVATE) == LDAP_SUCCESS ) {
224                                 rs->sr_entry = &ent;
225                                 rs->sr_attrs = op->oq_search.rs_attrs;
226                                 send_search_entry( op, rs );
227                                 while (ent.e_attrs) {
228                                         Attribute *a;
229                                         BerVarray v;
230
231                                         a = ent.e_attrs;
232                                         ent.e_attrs = a->a_next;
233
234                                         v = a->a_vals;
235                                         if (a->a_vals != &dummy)
236                                                 ber_bvarray_free(a->a_vals);
237                                         if (a->a_nvals != v)
238                                                 ber_bvarray_free(a->a_nvals);
239                                         ch_free(a);
240                                 }
241                                 
242                                 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
243                                         free( ent.e_dn );
244                                 if ( ent.e_ndn )
245                                         free( ent.e_ndn );
246                         }
247                         ldap_msgfree(res);
248
249                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
250                         char            **references = NULL;
251                         int             cnt;
252
253                         rc = ldap_parse_reference( lc->ld, res,
254                                         &references, &rs->sr_ctrls, 1 );
255
256                         if ( rc != LDAP_SUCCESS ) {
257                                 continue;
258                         }
259
260                         if ( references == NULL ) {
261                                 continue;
262                         }
263
264                         for ( cnt = 0; references[ cnt ]; cnt++ )
265                                 /* NO OP */ ;
266                                 
267                         rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
268
269                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
270                                 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
271                                 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
272                         }
273
274                         /* ignore return value by now */
275                         ( void )send_search_reference( op, rs );
276
277                         /* cleanup */
278                         if ( references ) {
279                                 ldap_value_free( references );
280                                 ch_free( rs->sr_ref );
281                                 rs->sr_ref = NULL;
282                         }
283
284                         if ( rs->sr_ctrls ) {
285                                 ldap_controls_free( rs->sr_ctrls );
286                                 rs->sr_ctrls = NULL;
287                         }
288
289                 } else {
290                         rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
291                                         &match.bv_val, (char **)&rs->sr_text,
292                                         NULL, NULL, 1);
293                         if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
294                         rs->sr_err = ldap_back_map_result(rs);
295                         rc = 0;
296                         break;
297                 }
298         }
299
300         if (rc == -1)
301                 goto fail;
302
303         /*
304          * Rewrite the matched portion of the search base, if required
305          */
306         if ( match.bv_val && *match.bv_val ) {
307                 struct berval mdn;
308
309 #ifdef ENABLE_REWRITE
310                 dc.ctx = "matchedDn";
311 #else
312                 dc.tofrom = 0;
313                 dc.normalized = 0;
314 #endif
315                 match.bv_len = strlen( match.bv_val );
316                 ldap_back_dn_massage(&dc, &match, &mdn);
317                 rs->sr_matched = mdn.bv_val;
318         }
319         if ( rs->sr_v2ref ) {
320                 rs->sr_err = LDAP_REFERRAL;
321         }
322
323 finish:;
324         send_ldap_result( op, rs );
325
326 #ifdef LDAP_BACK_PROXY_AUTHZ
327         if ( ctrls && ctrls != op->o_ctrls ) {
328                 free( ctrls[ 0 ] );
329                 free( ctrls );
330         }
331 #endif /* LDAP_BACK_PROXY_AUTHZ */
332
333         if ( match.bv_val ) {
334                 if ( rs->sr_matched != match.bv_val ) {
335                         free( (char *)rs->sr_matched );
336                 }
337                 rs->sr_matched = NULL;
338                 LDAP_FREE( match.bv_val );
339         }
340         if ( rs->sr_text ) {
341                 if ( !dontfreetext ) {
342                         LDAP_FREE( (char *)rs->sr_text );
343                 }
344                 rs->sr_text = NULL;
345         }
346         if ( mapped_attrs ) {
347                 ch_free( mapped_attrs );
348         }
349         if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
350                 ch_free( mfilter.bv_val );
351         }
352         if ( mbase.bv_val != op->o_req_dn.bv_val ) {
353                 free( mbase.bv_val );
354         }
355         
356         return rc;
357 }
358
359 static int
360 ldap_build_entry(
361         Operation *op,
362         LDAPMessage *e,
363         Entry *ent,
364         struct berval *bdn,
365         int flags
366 )
367 {
368         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
369         struct berval a, mapped;
370         BerElement ber = *e->lm_ber;
371         Attribute *attr, **attrp;
372         struct berval *bv;
373         const char *text;
374         int last;
375         int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
376         int normalize = flags & LDAP_BUILD_ENTRY_NORMALIZE;
377         dncookie dc;
378
379         /* safe assumptions ... */
380         assert( ent );
381         ent->e_bv.bv_val = NULL;
382
383         if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
384                 return LDAP_DECODING_ERROR;
385         }
386
387         /*
388          * Rewrite the dn of the result, if needed
389          */
390         dc.rwmap = &li->rwmap;
391 #ifdef ENABLE_REWRITE
392         dc.conn = op->o_conn;
393         dc.rs = NULL;
394         dc.ctx = "searchResult";
395 #else
396         dc.tofrom = 0;
397         dc.normalized = 0;
398 #endif
399         if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
400                 return LDAP_OTHER;
401         }
402
403         /*
404          * Note: this may fail if the target host(s) schema differs
405          * from the one known to the meta, and a DN with unknown
406          * attributes is returned.
407          * 
408          * FIXME: should we log anything, or delegate to dnNormalize?
409          */
410         if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
411                 op->o_tmpmemctx ) != LDAP_SUCCESS )
412         {
413                 return LDAP_INVALID_DN_SYNTAX;
414         }
415         
416         attrp = &ent->e_attrs;
417
418 #ifdef ENABLE_REWRITE
419         dc.ctx = "searchAttrDN";
420 #endif
421         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
422                 ldap_back_map(&li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP);
423                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
424                         continue;
425                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
426                 if (attr == NULL)
427                         continue;
428                 attr->a_flags = 0;
429                 attr->a_next = 0;
430                 attr->a_desc = NULL;
431                 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
432                         if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text) 
433                                         != LDAP_SUCCESS) {
434 #ifdef NEW_LOGGING
435                                 LDAP_LOG( BACK_LDAP, DETAIL1, 
436                                         "slap_bv2undef_ad(%s):  %s\n", mapped.bv_val, text, 0 );
437 #else /* !NEW_LOGGING */
438                                 Debug( LDAP_DEBUG_ANY, 
439                                                 "slap_bv2undef_ad(%s):  "
440                                                 "%s\n%s", mapped.bv_val, text, "" );
441 #endif /* !NEW_LOGGING */
442                                 ch_free(attr);
443                                 continue;
444                         }
445                 }
446
447                 /* no subschemaSubentry */
448                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
449
450                         /* 
451                          * We eat target's subschemaSubentry because
452                          * a search for this value is likely not
453                          * to resolve to the appropriate backend;
454                          * later, the local subschemaSubentry is
455                          * added.
456                          */
457                         ( void )ber_scanf( &ber, "x" /* [W] */ );
458
459                         ch_free(attr);
460                         continue;
461                 }
462                 
463                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
464                                 || attr->a_vals == NULL ) {
465                         /*
466                          * Note: attr->a_vals can be null when using
467                          * values result filter
468                          */
469                         if (private) {
470                                 attr->a_vals = &dummy;
471                         } else {
472                                 attr->a_vals = ch_malloc(sizeof(struct berval));
473                                 attr->a_vals->bv_val = NULL;
474                                 attr->a_vals->bv_len = 0;
475                         }
476                         last = 0;
477                 } else {
478                         for ( last = 0; attr->a_vals[last].bv_val; last++ );
479                 }
480                 if ( last == 0 ) {
481                         /* empty */
482                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
483                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
484                         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
485                                 ldap_back_map(&li->rwmap.rwm_oc, bv, &mapped,
486                                                 BACKLDAP_REMAP);
487                                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
488                                         LBER_FREE(bv->bv_val);
489                                         bv->bv_val = NULL;
490                                         if (--last < 0)
491                                                 break;
492                                         *bv = attr->a_vals[last];
493                                         attr->a_vals[last].bv_val = NULL;
494                                         bv--;
495
496                                 } else if ( mapped.bv_val != bv->bv_val ) {
497                                         /*
498                                          * FIXME: after LBER_FREEing
499                                          * the value is replaced by
500                                          * ch_alloc'ed memory
501                                          */
502                                         LBER_FREE(bv->bv_val);
503                                         ber_dupbv( bv, &mapped );
504                                 }
505                         }
506
507                 /*
508                  * It is necessary to try to rewrite attributes with
509                  * dn syntax because they might be used in ACLs as
510                  * members of groups; since ACLs are applied to the
511                  * rewritten stuff, no dn-based subject clause could
512                  * be used at the ldap backend side (see
513                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
514                  * The problem can be overcome by moving the dn-based
515                  * ACLs to the target directory server, and letting
516                  * everything pass thru the ldap backend.
517                  */
518                 } else if ( attr->a_desc->ad_type->sat_syntax ==
519                                 slap_schema.si_syn_distinguishedName ) {
520                         ldap_dnattr_result_rewrite( &dc, attr->a_vals );
521                 }
522
523                 if ( normalize && last && attr->a_desc->ad_type->sat_equality &&
524                         attr->a_desc->ad_type->sat_equality->smr_normalize ) {
525                         int i;
526
527                         attr->a_nvals = ch_malloc((last+1)*sizeof(struct berval));
528                         for (i=0; i<last; i++) {
529                                 attr->a_desc->ad_type->sat_equality->smr_normalize(
530                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
531                                         attr->a_desc->ad_type->sat_syntax,
532                                         attr->a_desc->ad_type->sat_equality,
533                                         &attr->a_vals[i], &attr->a_nvals[i],
534                                         NULL /* op->o_tmpmemctx */ );
535                         }
536                         attr->a_nvals[i].bv_val = NULL;
537                         attr->a_nvals[i].bv_len = 0;
538                 } else {
539                         attr->a_nvals = attr->a_vals;
540                 }
541                 *attrp = attr;
542                 attrp = &attr->a_next;
543         }
544
545         /* make sure it's free'able */
546         if (!private && ent->e_name.bv_val == bdn->bv_val)
547                 ber_dupbv( &ent->e_name, bdn );
548         return LDAP_SUCCESS;
549 }
550
551 /* return 0 IFF we can retrieve the entry with ndn
552  */
553 int
554 ldap_back_entry_get(
555         Operation *op,
556         struct berval   *ndn,
557         ObjectClass *oc,
558         AttributeDescription *at,
559         int rw,
560         Entry **ent
561 )
562 {
563         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;    
564         struct ldapconn *lc;
565         int rc = 1, is_oc;
566         struct berval mapped = { 0, NULL }, bdn, mdn;
567         LDAPMessage     *result = NULL, *e = NULL;
568         char *gattr[3];
569         char *filter = NULL;
570         Connection *oconn;
571         SlapReply rs;
572         dncookie dc;
573
574         /* Tell getconn this is a privileged op */
575         is_oc = op->o_do_not_cache;
576         op->o_do_not_cache = 1;
577         lc = ldap_back_getconn(op, &rs);
578         oconn = op->o_conn;
579         op->o_conn = NULL;
580         if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
581                 op->o_do_not_cache = is_oc;
582                 op->o_conn = oconn;
583                 return 1;
584         }
585         op->o_do_not_cache = is_oc;
586         op->o_conn = oconn;
587
588         /*
589          * Rewrite the search base, if required
590          */
591         dc.rwmap = &li->rwmap;
592 #ifdef ENABLE_REWRITE
593         dc.conn = op->o_conn;
594         dc.rs = &rs;
595         dc.ctx = "searchBase";
596 #else
597         dc.tofrom = 1;
598         dc.normalized = 1;
599 #endif
600         if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
601                 return 1;
602         }
603
604         if ( at ) {
605                 ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
606                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
607                         rc = 1;
608                         goto cleanup;
609                 }
610         }
611
612         is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
613         if (oc && !is_oc) {
614                 gattr[0] = "objectclass";
615                 gattr[1] = mapped.bv_val;
616                 gattr[2] = NULL;
617         } else {
618                 gattr[0] = mapped.bv_val;
619                 gattr[1] = NULL;
620         }
621         if (oc) {
622                 char *ptr;
623                 ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
624                                                 BACKLDAP_MAP);
625                 filter = ch_malloc(sizeof("(objectclass=)") + mapped.bv_len);
626                 ptr = lutil_strcopy(filter, "(objectclass=");
627                 ptr = lutil_strcopy(ptr, mapped.bv_val);
628                 *ptr++ = ')';
629                 *ptr++ = '\0';
630         }
631
632         if (ldap_search_ext_s(lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
633                                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
634                                 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
635         {
636                 goto cleanup;
637         }
638
639         if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
640                 goto cleanup;
641         }
642
643         *ent = ch_calloc(1,sizeof(Entry));
644
645         rc = ldap_build_entry(op, e, *ent, &bdn, LDAP_BUILD_ENTRY_NORMALIZE);
646
647         if (rc != LDAP_SUCCESS) {
648                 ch_free(*ent);
649                 *ent = NULL;
650         }
651
652 cleanup:
653         if (result) {
654                 ldap_msgfree(result);
655         }
656
657         if ( filter ) {
658                 ch_free( filter );
659         }
660
661         if ( mdn.bv_val != ndn->bv_val ) {
662                 ch_free( mdn.bv_val );
663         }
664
665         return(rc);
666 }
667