]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/compare.c
cleanup & more
[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           *matched;
21         Entry           *e;
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_r( op->o_bd, NULL, &op->o_req_ndn, &e, &matched, 0, locker, &lock );
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         if ( e == NULL ) {
58                 if ( matched != NULL ) {
59                         rs->sr_matched = ch_strdup( matched->e_dn );
60                         rs->sr_ref = is_entry_referral( matched )
61                                 ? get_entry_referrals( op, matched )
62                                 : NULL;
63                         bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, matched, &lock );
64                         matched = NULL;
65
66                 } else {
67                         rs->sr_ref = referral_rewrite( default_referral,
68                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
69                 }
70
71                 rs->sr_err = LDAP_REFERRAL;
72                 send_ldap_result( op, rs );
73
74                 ber_bvarray_free( rs->sr_ref );
75                 free( (char *)rs->sr_matched );
76                 rs->sr_ref = NULL;
77                 rs->sr_matched = NULL;
78
79                 goto done;
80         }
81
82         if (!manageDSAit && is_entry_referral( e ) ) {
83                 /* entry is a referral, don't allow add */
84                 rs->sr_ref = get_entry_referrals( op, e );
85
86 #ifdef NEW_LOGGING
87                 LDAP_LOG ( OPERATION, DETAIL1, 
88                         "bdb_compare: entry is referral\n", 0, 0, 0 );
89 #else
90                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
91                         0, 0 );
92 #endif
93
94                 rs->sr_err = LDAP_REFERRAL;
95                 rs->sr_matched = e->e_name.bv_val;
96                 send_ldap_result( op, rs );
97
98                 ber_bvarray_free( rs->sr_ref );
99                 rs->sr_ref = NULL;
100                 rs->sr_matched = NULL;
101                 goto done;
102         }
103
104         rs->sr_err = access_allowed( op, e,
105                 op->oq_compare.rs_ava->aa_desc, &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
106         if ( ! rs->sr_err ) {
107                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
108                 goto return_results;
109         }
110
111         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
112
113         for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
114                 a != NULL;
115                 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
116         {
117                 rs->sr_err = LDAP_COMPARE_FALSE;
118
119                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
120                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
121                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
122                         a->a_nvals, &op->oq_compare.rs_ava->aa_value ) == 0 )
123                 {
124                         rs->sr_err = LDAP_COMPARE_TRUE;
125                         break;
126                 }
127         }
128
129 return_results:
130         send_ldap_result( op, rs );
131
132         if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
133                 rs->sr_err = LDAP_SUCCESS;
134         }
135
136 done:
137         /* free entry */
138         if( e != NULL ) {
139                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
140         }
141
142         LOCK_ID_FREE ( bdb->bi_dbenv, locker );
143
144         return rs->sr_err;
145 }