]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/compare.c
Fix ITS#3424
[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-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
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 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <sys/types.h>
25
26 #include "slap.h"
27 #include "proto-sql.h"
28
29 int
30 backsql_compare( Operation *op, SlapReply *rs )
31 {
32         SQLHDBC                 dbh;
33         Entry                   *e = NULL, user_entry;
34         Attribute               *a = NULL;
35         backsql_srch_info       bsi;
36         int                     rc;
37         AttributeName           anlist[2];
38
39         user_entry.e_name.bv_val = NULL;
40         user_entry.e_name.bv_len = 0;
41         user_entry.e_nname.bv_val = NULL;
42         user_entry.e_nname.bv_len = 0;
43         user_entry.e_attrs = NULL;
44  
45         Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
46
47         rs->sr_err = backsql_get_db_conn( op, &dbh );
48         if (!dbh) {
49                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
50                         "could not get connection handle - exiting\n",
51                         0, 0, 0 );
52
53                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
54                         ? "SQL-backend error" : NULL;
55                 goto return_results;
56         }
57
58         memset( &anlist[0], 0, 2 * sizeof( AttributeName ) );
59         anlist[0].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname;
60         anlist[0].an_desc = op->oq_compare.rs_ava->aa_desc;
61
62         /*
63          * Try to get attr as dynamic operational
64          */
65         if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
66                 SlapReply       nrs = { 0 };
67
68                 user_entry.e_attrs = NULL;
69                 user_entry.e_name = op->o_req_dn;
70                 user_entry.e_nname = op->o_req_ndn;
71
72                 nrs.sr_attrs = anlist;
73                 nrs.sr_entry = &user_entry;
74                 nrs.sr_attr_flags = SLAP_OPATTRS_NO;
75                 nrs.sr_operational_attrs = NULL;
76
77                 rs->sr_err = backsql_operational( op, &nrs );
78                 if ( rs->sr_err != LDAP_SUCCESS ) {
79                         goto return_results;
80                 }
81                 
82                 user_entry.e_attrs = nrs.sr_operational_attrs;
83
84         } else {
85                 rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE, 
86                                 -1, -1, -1, NULL, dbh, op, rs, anlist,
87                                 ( BACKSQL_ISF_GET_ID | BACKSQL_ISF_MUCK ) );
88                 if ( rc != LDAP_SUCCESS ) {
89                         Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
90                                 "could not retrieve compareDN ID - no such entry\n", 
91                                 0, 0, 0 );
92                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
93                         goto return_results;
94                 }
95
96                 bsi.bsi_e = &user_entry;
97                 rc = backsql_id2entry( &bsi, &bsi.bsi_base_id );
98                 if ( rc != LDAP_SUCCESS ) {
99                         Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
100                                 "error %d in backsql_id2entry() "
101                                 "- compare failed\n", rc, 0, 0 );
102                         rs->sr_err = rc;
103                         goto return_results;
104                 }
105         }
106         e = &user_entry;
107
108         if ( ! access_allowed( op, e, op->oq_compare.rs_ava->aa_desc, 
109                                 &op->oq_compare.rs_ava->aa_value,
110                                 ACL_COMPARE, NULL ) ) {
111                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
112                 goto return_results;
113         }
114
115         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
116         for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
117                         a != NULL;
118                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
119         {
120                 rs->sr_err = LDAP_COMPARE_FALSE;
121                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
122                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
123                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
124                                         a->a_nvals,
125                                         &op->oq_compare.rs_ava->aa_value,
126                                         op->o_tmpmemctx ) == 0 )
127                 {
128                         rs->sr_err = LDAP_COMPARE_TRUE;
129                         break;
130                 }
131         }
132
133 return_results:;
134         send_ldap_result( op, rs );
135
136         if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
137                 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
138         }
139
140         if ( e != NULL ) {
141                 entry_clean( e );
142         }
143
144         Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
145         switch ( rs->sr_err ) {
146         case LDAP_COMPARE_TRUE:
147         case LDAP_COMPARE_FALSE:
148                 return 0;
149
150         default:
151                 return 1;
152         }
153 }
154