]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/compare.c
Fix result
[openldap] / servers / slapd / back-bdb / compare.c
1 /* compare.c - bdb backend compare routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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(
18         BackendDB       *be,
19         Connection      *conn,
20         Operation       *op,
21         struct berval   *dn,
22         struct berval   *ndn,
23         AttributeAssertion *ava
24 )
25 {
26         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
27         Entry           *matched;
28         Entry           *e;
29         Attribute       *a;
30         int                     rc; 
31         const char      *text = NULL;
32         int             manageDSAit = get_manageDSAit( op );
33
34         /* get entry */
35         rc = bdb_dn2entry_r( be, NULL, ndn, &e, &matched, 0 );
36
37         switch( rc ) {
38         case DB_NOTFOUND:
39         case 0:
40                 break;
41         default:
42                 rc = LDAP_OTHER;
43                 text = "internal error";
44                 goto return_results;
45         }
46
47         if ( e == NULL ) {
48                 char *matched_dn = NULL;
49                 BerVarray refs;
50
51                 if ( matched != NULL ) {
52                         matched_dn = ch_strdup( matched->e_dn );
53                         refs = is_entry_referral( matched )
54                                 ? get_entry_referrals( be, conn, op, matched )
55                                 : NULL;
56                         bdb_cache_return_entry_r( &bdb->bi_cache, matched );
57                         matched = NULL;
58
59                 } else {
60                         refs = referral_rewrite( default_referral,
61                                 NULL, dn, LDAP_SCOPE_DEFAULT );
62                 }
63
64                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
65                         matched_dn, NULL, refs, NULL );
66
67                 ber_bvarray_free( refs );
68                 free( matched_dn );
69
70                 goto done;
71         }
72
73         if (!manageDSAit && is_entry_referral( e ) ) {
74                 /* entry is a referral, don't allow add */
75                 BerVarray refs = get_entry_referrals( be,
76                         conn, op, e );
77
78                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
79                         0, 0 );
80
81                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
82                         e->e_dn, NULL, refs, NULL );
83
84                 ber_bvarray_free( refs );
85                 goto done;
86         }
87
88         if ( ! access_allowed( be, conn, op, e,
89                 ava->aa_desc, &ava->aa_value, ACL_COMPARE ) )
90         {
91                 rc = LDAP_INSUFFICIENT_ACCESS;
92                 goto return_results;
93         }
94
95         rc = LDAP_NO_SUCH_ATTRIBUTE;
96
97         for(a = attrs_find( e->e_attrs, ava->aa_desc );
98                 a != NULL;
99                 a = attrs_find( a->a_next, ava->aa_desc ))
100         {
101                 rc = LDAP_COMPARE_FALSE;
102
103                 if ( value_find( ava->aa_desc, a->a_vals, &ava->aa_value ) == 0 ) {
104                         rc = LDAP_COMPARE_TRUE;
105                         break;
106                 }
107
108         }
109
110 return_results:
111         send_ldap_result( conn, op, rc,
112                 NULL, text, NULL, NULL );
113
114         if( rc == LDAP_COMPARE_FALSE || rc == LDAP_COMPARE_TRUE ) {
115                 rc = LDAP_SUCCESS;
116         }
117
118 done:
119         /* free entry */
120         if( e != NULL ) {
121                 bdb_cache_return_entry_r( &bdb->bi_cache, e );
122         }
123
124         return rc;
125 }