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