]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/compare.c
ac23f9e6c17705025dddfe6755699eb23d2fd203
[openldap] / servers / slapd / back-sql / compare.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * Portions Copyright 2002 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Dmitry Kovalev for inclusion
19  * by OpenLDAP Software.  Additional significant contributors include
20  * Pierangelo Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <sys/types.h>
27
28 #include "slap.h"
29 #include "proto-sql.h"
30
31 int
32 backsql_compare( Operation *op, SlapReply *rs )
33 {
34         SQLHDBC                 dbh = SQL_NULL_HDBC;
35         Entry                   *e = NULL, user_entry;
36         Attribute               *a = NULL;
37         backsql_srch_info       bsi;
38         int                     rc;
39         AttributeName           anlist[2],
40                                 *anlistp = NULL;
41
42         BER_BVZERO( &user_entry.e_name );
43         BER_BVZERO( &user_entry.e_nname );
44         user_entry.e_attrs = NULL;
45  
46         Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
47
48         rs->sr_err = backsql_get_db_conn( op, &dbh );
49         if (!dbh) {
50                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
51                         "could not get connection handle - exiting\n",
52                         0, 0, 0 );
53
54                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
55                         ? "SQL-backend error" : NULL;
56                 goto return_results;
57         }
58
59         memset( &anlist[0], 0, 2 * sizeof( AttributeName ) );
60         anlist[0].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname;
61         anlist[0].an_desc = op->oq_compare.rs_ava->aa_desc;
62
63         /*
64          * Try to get attr as dynamic operational
65          */
66         if ( !is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
67                 anlistp = anlist;
68         }
69
70         /*
71          * FIXME: deal with matchedDN/referral?
72          */
73         rc = backsql_init_search( &bsi, &op->o_req_ndn,
74                         LDAP_SCOPE_BASE, 
75                         SLAP_NO_LIMIT, SLAP_NO_LIMIT,
76                         (time_t)(-1), NULL, dbh, op, rs, anlistp,
77                         BACKSQL_ISF_GET_ID );
78         if ( rc != LDAP_SUCCESS ) {
79                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
80                         "could not retrieve compareDN ID - no such entry\n", 
81                         0, 0, 0 );
82                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
83                 goto return_results;
84         }
85
86         if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
87                 SlapReply       nrs = { 0 };
88
89                 user_entry.e_attrs = NULL;
90                 user_entry.e_name = bsi.bsi_base_id.eid_dn;
91                 user_entry.e_nname = bsi.bsi_base_id.eid_ndn;
92
93                 nrs.sr_attrs = anlist;
94                 nrs.sr_entry = &user_entry;
95                 nrs.sr_attr_flags = SLAP_OPATTRS_NO;
96                 nrs.sr_operational_attrs = NULL;
97
98                 rs->sr_err = backsql_operational( op, &nrs );
99                 if ( rs->sr_err != LDAP_SUCCESS ) {
100                         goto return_results;
101                 }
102                 
103                 user_entry.e_attrs = nrs.sr_operational_attrs;
104
105         } else {
106                 bsi.bsi_e = &user_entry;
107                 rc = backsql_id2entry( &bsi, &bsi.bsi_base_id );
108                 if ( rc != LDAP_SUCCESS ) {
109                         Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
110                                 "error %d in backsql_id2entry() "
111                                 "- compare failed\n", rc, 0, 0 );
112                         rs->sr_err = rc;
113                         goto return_results;
114                 }
115         }
116         e = &user_entry;
117
118         if ( ! access_allowed( op, e, op->oq_compare.rs_ava->aa_desc, 
119                                 &op->oq_compare.rs_ava->aa_value,
120                                 ACL_COMPARE, NULL ) ) {
121 #ifdef SLAP_ACL_HONOR_DISCLOSE
122                 if ( ! access_allowed( op, &e, slap_schema.si_ad_entry, NULL,
123                                         ACL_DISCLOSE, NULL ) ) {
124                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
125                 } else
126 #endif /* SLAP_ACL_HONOR_DISCLOSE */
127                 {
128                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
129                 }
130                 goto return_results;
131         }
132
133         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
134         for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
135                         a != NULL;
136                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
137         {
138                 rs->sr_err = LDAP_COMPARE_FALSE;
139                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
140                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
141                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
142                                         a->a_nvals,
143                                         &op->oq_compare.rs_ava->aa_value,
144                                         op->o_tmpmemctx ) == 0 )
145                 {
146                         rs->sr_err = LDAP_COMPARE_TRUE;
147                         break;
148                 }
149         }
150
151 return_results:;
152         send_ldap_result( op, rs );
153
154         if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
155                 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
156         }
157
158         if ( e != NULL ) {
159                 entry_clean( e );
160         }
161
162         if ( bsi.bsi_attrs != NULL ) {
163                 op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
164         }
165
166         Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
167         switch ( rs->sr_err ) {
168         case LDAP_COMPARE_TRUE:
169         case LDAP_COMPARE_FALSE:
170                 return 0;
171
172         default:
173                 return 1;
174         }
175 }
176