]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/compare.c
2764cdf9e8d957b2dffb479b49f87172aec0cbaa
[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                 break;
40         default:
41                 rc = LDAP_OTHER;
42                 text = "internal error";
43                 goto return_results;
44         }
45
46         if ( e == NULL ) {
47                 char *matched_dn = NULL;
48                 struct berval **refs = NULL;
49
50                 if ( matched != NULL ) {
51                         matched_dn = ch_strdup( matched->e_dn );
52                         refs = is_entry_referral( matched )
53                                 ? get_entry_referrals( be, conn, op, matched )
54                                 : NULL;
55                         bdb_entry_return( be, matched );
56                 } else {
57                         refs = default_referral;
58                 }
59
60                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
61                         matched_dn, NULL, refs, NULL );
62
63                 if( matched != NULL ) {
64                         ber_bvecfree( refs );
65                         free( matched_dn );
66                 }
67
68                 goto done;
69         }
70
71         if (!manageDSAit && is_entry_referral( e ) ) {
72                 /* entry is a referral, don't allow add */
73                 struct berval **refs = get_entry_referrals( be,
74                         conn, op, e );
75
76                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
77                         0, 0 );
78
79                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
80                         e->e_dn, NULL, refs, NULL );
81
82                 ber_bvecfree( refs );
83                 goto done;
84         }
85
86         if ( ! access_allowed( be, conn, op, e,
87                 ava->aa_desc, ava->aa_value, ACL_COMPARE ) )
88         {
89                 rc = LDAP_INSUFFICIENT_ACCESS;
90                 goto return_results;
91         }
92
93         rc = LDAP_NO_SUCH_ATTRIBUTE;
94
95         for(a = attrs_find( e->e_attrs, ava->aa_desc );
96                 a != NULL;
97                 a = attrs_find( a->a_next, ava->aa_desc ))
98         {
99                 rc = LDAP_COMPARE_FALSE;
100
101                 if ( value_find( ava->aa_desc, a->a_vals, ava->aa_value ) == 0 )
102                 {
103                         rc = LDAP_COMPARE_TRUE;
104                         break;
105                 }
106
107         }
108
109         if( rc != LDAP_NO_SUCH_ATTRIBUTE ) {
110                 rc = LDAP_SUCCESS;
111         }
112
113
114 return_results:
115         send_ldap_result( conn, op, LDAP_SUCCESS,
116                 NULL, text, NULL, NULL );
117
118 done:
119         /* free entry */
120         if( e != NULL ) bdb_entry_return( be, e );
121
122         return rc;
123 }