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