]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/compare.c
ITS#1716 is_entry_subentr/ies/y/
[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 #ifdef NEW_LOGGING
79                 LDAP_LOG (( "compare", LDAP_LEVEL_DETAIL1,"bdb_compare: entry is referral\n" ));
80 #else
81                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
82                         0, 0 );
83 #endif
84
85                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
86                         e->e_dn, NULL, refs, NULL );
87
88                 ber_bvarray_free( refs );
89                 goto done;
90         }
91
92         if ( ! access_allowed( be, conn, op, e,
93                 ava->aa_desc, &ava->aa_value, ACL_COMPARE, NULL ) )
94         {
95                 rc = LDAP_INSUFFICIENT_ACCESS;
96                 goto return_results;
97         }
98
99         rc = LDAP_NO_SUCH_ATTRIBUTE;
100
101         for(a = attrs_find( e->e_attrs, ava->aa_desc );
102                 a != NULL;
103                 a = attrs_find( a->a_next, ava->aa_desc ))
104         {
105                 rc = LDAP_COMPARE_FALSE;
106
107                 if ( value_find( ava->aa_desc, a->a_vals, &ava->aa_value ) == 0 ) {
108                         rc = LDAP_COMPARE_TRUE;
109                         break;
110                 }
111
112         }
113
114 return_results:
115         send_ldap_result( conn, op, rc,
116                 NULL, text, NULL, NULL );
117
118         if( rc == LDAP_COMPARE_FALSE || rc == LDAP_COMPARE_TRUE ) {
119                 rc = LDAP_SUCCESS;
120         }
121
122 done:
123         /* free entry */
124         if( e != NULL ) {
125                 bdb_cache_return_entry_r( &bdb->bi_cache, e );
126         }
127
128         return rc;
129 }