]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/compare.c
42164ac577c8525334fc7e044280e9b2043715e7
[openldap] / servers / slapd / back-bdb / compare.c
1 /* compare.c - bdb backend compare routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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         const char      *dn,
22         const char      *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( 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                 struct berval **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                                         dn, LDAP_SCOPE_DEFAULT )
56                                 : NULL;
57                         bdb_entry_return( be, matched );
58                         matched = NULL;
59
60                 } else {
61                         refs = referral_rewrite( default_referral,
62                                 NULL, dn, LDAP_SCOPE_DEFAULT );
63                 }
64
65                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
66                         matched_dn, NULL, refs, NULL );
67
68                 ber_bvecfree( refs );
69                 free( matched_dn );
70
71                 goto done;
72         }
73
74         if (!manageDSAit && is_entry_referral( e ) ) {
75                 /* entry is a referral, don't allow add */
76                 struct berval **refs = get_entry_referrals( be,
77                         conn, op, e, dn, LDAP_SCOPE_DEFAULT );
78
79                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
80                         0, 0 );
81
82                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
83                         e->e_dn, NULL, refs, NULL );
84
85                 ber_bvecfree( refs );
86                 goto done;
87         }
88
89         if ( ! access_allowed( be, conn, op, e,
90                 ava->aa_desc, ava->aa_value, ACL_COMPARE ) )
91         {
92                 rc = LDAP_INSUFFICIENT_ACCESS;
93                 goto return_results;
94         }
95
96         rc = LDAP_NO_SUCH_ATTRIBUTE;
97
98         for(a = attrs_find( e->e_attrs, ava->aa_desc );
99                 a != NULL;
100                 a = attrs_find( a->a_next, ava->aa_desc ))
101         {
102                 rc = LDAP_COMPARE_FALSE;
103
104                 if ( value_find_ex( ava->aa_desc,
105                                     SLAP_MR_VALUE_IS_IN_MR_SYNTAX,
106                                     a->a_vals, ava->aa_value ) == 0 )
107                 {
108                         rc = LDAP_COMPARE_TRUE;
109                         break;
110                 }
111
112         }
113
114         if( rc != LDAP_NO_SUCH_ATTRIBUTE ) {
115                 rc = LDAP_SUCCESS;
116         }
117
118
119 return_results:
120         send_ldap_result( conn, op, LDAP_SUCCESS,
121                 NULL, text, NULL, NULL );
122
123 done:
124         /* free entry */
125         if( e != NULL ) {
126                 bdb_entry_return( be, e );
127         }
128
129         return rc;
130 }