]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/compare.c
Happy new year
[openldap] / servers / slapd / back-ldbm / compare.c
1 /* compare.c - ldbm backend compare routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/socket.h>
22 #include <ac/string.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
27
28 int
29 ldbm_back_compare(
30         Operation       *op,
31         SlapReply       *rs )
32 {
33         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
34         Entry           *matched;
35         Entry           *e;
36         Attribute       *a;
37         int             manageDSAit = get_manageDSAit( op );
38
39         /* grab giant lock for reading */
40         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
41
42         /* get entry with reader lock */
43         if ( (e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched )) == NULL ) {
44                 if ( matched != NULL ) {
45                         rs->sr_matched = ch_strdup( matched->e_dn );
46                         rs->sr_ref = is_entry_referral( matched )
47                                 ? get_entry_referrals( op, matched )
48                                 : NULL;
49                         cache_return_entry_r( &li->li_cache, matched );
50                 } else {
51                         rs->sr_ref = referral_rewrite( default_referral,
52                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
53                 }
54
55                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
56
57                 rs->sr_err = LDAP_REFERRAL;
58                 send_ldap_result( op, rs );
59
60                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
61                 free( (char *)rs->sr_matched );
62                 rs->sr_ref = NULL;
63                 rs->sr_matched = NULL;
64                 return( 1 );
65         }
66
67         if (!manageDSAit && is_entry_referral( e ) ) {
68                 /* entry is a referral, don't allow add */
69                 rs->sr_ref = get_entry_referrals( op, e );
70
71 #ifdef NEW_LOGGING
72                 LDAP_LOG( BACK_LDBM, INFO, 
73                         "ldbm_back_compare: entry (%s) is a referral.\n", e->e_dn, 0, 0 );
74 #else
75                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
76                     0, 0 );
77 #endif
78
79
80                 rs->sr_err = LDAP_REFERRAL;
81                 rs->sr_matched = e->e_name.bv_val;
82                 send_ldap_result( op, rs );
83
84                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
85                 rs->sr_ref = NULL;
86                 rs->sr_matched = NULL;
87                 rs->sr_err = 1;
88                 goto return_results;
89         }
90
91         if ( ! access_allowed( op, e,
92                 op->oq_compare.rs_ava->aa_desc, &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL ) )
93         {
94                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
95                         NULL );
96                 rs->sr_err = 1;
97                 goto return_results;
98         }
99
100         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
101
102         for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
103                 a != NULL;
104                 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
105         {
106                 rs->sr_err = LDAP_COMPARE_FALSE;
107
108                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
109                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
110                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
111                         a->a_nvals, &op->oq_compare.rs_ava->aa_value,
112                         op->o_tmpmemctx ) == 0 )
113                 {
114                         rs->sr_err = LDAP_COMPARE_TRUE;
115                         break;
116                 }
117         }
118
119         send_ldap_result( op, rs );
120
121         if( rs->sr_err != LDAP_NO_SUCH_ATTRIBUTE ) {
122                 rs->sr_err = 0;
123         }
124
125
126 return_results:;
127         cache_return_entry_r( &li->li_cache, e );
128         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
129         return( rs->sr_err );
130 }