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