]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/compare.c
Import ITS#4158 fixes from HEAD
[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  * 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 #ifdef SLAPD_SQL
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         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
35         backsql_entryID         user_id = BACKSQL_ENTRYID_INIT;
36         SQLHDBC                 dbh;
37         Entry                   *e = NULL, user_entry;
38         Attribute               *a = NULL;
39         backsql_srch_info       bsi;
40         int                     rc;
41         AttributeName           anlist[2];
42         struct berval           dn;
43
44         user_entry.e_name.bv_val = NULL;
45         user_entry.e_name.bv_len = 0;
46         user_entry.e_nname.bv_val = NULL;
47         user_entry.e_nname.bv_len = 0;
48         user_entry.e_attrs = NULL;
49  
50         Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
51
52         rs->sr_err = backsql_get_db_conn( op, &dbh );
53         if (!dbh) {
54                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
55                         "could not get connection handle - exiting\n",
56                         0, 0, 0 );
57
58                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
59                         ? "SQL-backend error" : NULL;
60                 goto return_results;
61         }
62
63         dn = op->o_req_dn;
64         if ( backsql_api_dn2odbc( op, rs, &dn ) ) {
65                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
66                         "backsql_api_dn2odbc failed\n", 
67                         0, 0, 0 );
68                 rs->sr_err = LDAP_OTHER;
69                 rs->sr_text = "SQL-backend error";
70                 goto return_results;
71         }
72
73         rc = backsql_dn2id( bi, &user_id, dbh, &dn );
74         if ( rc != LDAP_SUCCESS ) {
75                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
76                         "could not retrieve compare dn id - no such entry\n", 
77                         0, 0, 0 );
78                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
79                 goto return_results;
80         }
81
82         memset( &anlist[0], 0, 2 * sizeof( AttributeName ) );
83         anlist[0].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname;
84         anlist[0].an_desc = op->oq_compare.rs_ava->aa_desc;
85
86         /*
87          * Try to get attr as dynamic operational
88          */
89         if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
90                 SlapReply       nrs = { 0 };
91  
92                 user_entry.e_attrs = NULL;
93                 user_entry.e_name = op->o_req_dn;
94                 user_entry.e_nname = op->o_req_ndn;
95  
96                 nrs.sr_attrs = anlist;
97                 nrs.sr_entry = &user_entry;
98                 rs->sr_err = backsql_operational( op, &nrs, 0, &user_entry.e_attrs );
99  
100                 if ( rs->sr_err != LDAP_SUCCESS ) {
101                         goto return_results;
102                 }
103                 
104         } else {
105                 backsql_init_search( &bsi, &dn, LDAP_SCOPE_BASE, 
106                                         -1, -1, -1, NULL, dbh, op, rs, anlist );
107                 bsi.bsi_e = &user_entry;
108                 rc = backsql_id2entry( &bsi, &user_id );
109                 if ( rc != LDAP_SUCCESS ) {
110                         Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
111                                 "error %d in backsql_id2entry() "
112                                 "- compare failed\n", rc, 0, 0 );
113                         rs->sr_err = rc;
114                         goto return_results;
115                 }
116         }
117         e = &user_entry;
118
119         if ( ! access_allowed( op, e, op->oq_compare.rs_ava->aa_desc, 
120                                 &op->oq_compare.rs_ava->aa_value,
121                                 ACL_COMPARE, NULL ) ) {
122                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
123                 goto return_results;
124         }
125
126         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
127         for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
128                         a != NULL;
129                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
130         {
131                 rs->sr_err = LDAP_COMPARE_FALSE;
132                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
133                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
134                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
135                                         a->a_nvals,
136                                         &op->oq_compare.rs_ava->aa_value,
137                                         op->o_tmpmemctx ) == 0 )
138                 {
139                         rs->sr_err = LDAP_COMPARE_TRUE;
140                         break;
141                 }
142         }
143
144 return_results:;
145         send_ldap_result( op, rs );
146
147         if ( dn.bv_val != op->o_req_dn.bv_val ) {
148                 ch_free( dn.bv_val );
149         }
150
151         if ( e != NULL ) {
152                 entry_clean( e );
153         }
154
155         Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
156         switch ( rs->sr_err ) {
157         case LDAP_COMPARE_TRUE:
158         case LDAP_COMPARE_FALSE:
159                 return 0;
160
161         default:
162                 return 1;
163         }
164 }
165  
166 #endif /* SLAPD_SQL */
167