]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/compare.c
ITS#2449, broken NOT filters
[openldap] / servers / slapd / back-bdb / compare.c
1 /* compare.c - bdb backend compare routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_compare( Operation *op, SlapReply *rs )
18 {
19         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
20         Entry           *e;
21         EntryInfo       *ei;
22         Attribute       *a;
23         int             manageDSAit = get_manageDSAit( op );
24
25         u_int32_t       locker;
26         DB_LOCK         lock;
27
28         rs->sr_err = LOCK_ID(bdb->bi_dbenv, &locker);
29         switch(rs->sr_err) {
30         case 0:
31                 break;
32         default:
33                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
34                 return rs->sr_err;
35         }
36
37 dn2entry_retry:
38         /* get entry */
39         rs->sr_err = bdb_dn2entry( op->o_bd, NULL, &op->o_req_ndn, &ei, 1, locker, &lock, op->o_tmpmemctx );
40
41         switch( rs->sr_err ) {
42         case DB_NOTFOUND:
43         case 0:
44                 break;
45         case LDAP_BUSY:
46                 rs->sr_text = "ldap server busy";
47                 goto return_results;
48         case DB_LOCK_DEADLOCK:
49         case DB_LOCK_NOTGRANTED:
50                 goto dn2entry_retry;
51         default:
52                 rs->sr_err = LDAP_OTHER;
53                 rs->sr_text = "internal error";
54                 goto return_results;
55         }
56
57         e = ei->bei_e;
58         if ( rs->sr_err == DB_NOTFOUND ) {
59                 if ( e != NULL ) {
60                         rs->sr_matched = ch_strdup( e->e_dn );
61                         rs->sr_ref = is_entry_referral( e )
62                                 ? get_entry_referrals( op, e )
63                                 : NULL;
64                         bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
65                         e = NULL;
66
67                 } else {
68                         rs->sr_ref = referral_rewrite( default_referral,
69                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
70                 }
71
72                 rs->sr_err = LDAP_REFERRAL;
73                 send_ldap_result( op, rs );
74
75                 ber_bvarray_free( rs->sr_ref );
76                 free( (char *)rs->sr_matched );
77                 rs->sr_ref = NULL;
78                 rs->sr_matched = NULL;
79
80                 goto done;
81         }
82
83         if (!manageDSAit && is_entry_referral( e ) ) {
84                 /* entry is a referral, don't allow add */
85                 rs->sr_ref = get_entry_referrals( op, e );
86
87 #ifdef NEW_LOGGING
88                 LDAP_LOG ( OPERATION, DETAIL1, 
89                         "bdb_compare: entry is referral\n", 0, 0, 0 );
90 #else
91                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
92                         0, 0 );
93 #endif
94
95                 rs->sr_err = LDAP_REFERRAL;
96                 rs->sr_matched = e->e_name.bv_val;
97                 send_ldap_result( op, rs );
98
99                 ber_bvarray_free( rs->sr_ref );
100                 rs->sr_ref = NULL;
101                 rs->sr_matched = NULL;
102                 goto done;
103         }
104
105         rs->sr_err = access_allowed( op, e,
106                 op->oq_compare.rs_ava->aa_desc, &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
107         if ( ! rs->sr_err ) {
108                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
109                 goto return_results;
110         }
111
112         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
113
114         for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
115                 a != NULL;
116                 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
117         {
118                 rs->sr_err = LDAP_COMPARE_FALSE;
119
120                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
121                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
122                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
123                         a->a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
124                 {
125                         rs->sr_err = LDAP_COMPARE_TRUE;
126                         break;
127                 }
128         }
129
130 return_results:
131         send_ldap_result( op, rs );
132
133         if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
134                 rs->sr_err = LDAP_SUCCESS;
135         }
136
137 done:
138         /* free entry */
139         if( e != NULL ) {
140                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
141         }
142
143         LOCK_ID_FREE ( bdb->bi_dbenv, locker );
144
145         return rs->sr_err;
146 }