]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/compare.c
Happy New Year!
[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 #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 = SQL_NULL_HDBC;
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,
86                                 LDAP_SCOPE_BASE, 
87                                 SLAP_NO_LIMIT, SLAP_NO_LIMIT,
88                                 (time_t)(-1), NULL, dbh, op, rs, anlist,
89                                 BACKSQL_ISF_GET_ID );
90                 if ( rc != LDAP_SUCCESS ) {
91                         Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
92                                 "could not retrieve compareDN ID - no such entry\n", 
93                                 0, 0, 0 );
94                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
95                         goto return_results;
96                 }
97
98                 bsi.bsi_e = &user_entry;
99                 rc = backsql_id2entry( &bsi, &bsi.bsi_base_id );
100                 if ( rc != LDAP_SUCCESS ) {
101                         Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
102                                 "error %d in backsql_id2entry() "
103                                 "- compare failed\n", rc, 0, 0 );
104                         rs->sr_err = rc;
105                         goto return_results;
106                 }
107         }
108         e = &user_entry;
109
110         if ( ! access_allowed( op, e, op->oq_compare.rs_ava->aa_desc, 
111                                 &op->oq_compare.rs_ava->aa_value,
112                                 ACL_COMPARE, NULL ) ) {
113                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
114                 goto return_results;
115         }
116
117         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
118         for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
119                         a != NULL;
120                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
121         {
122                 rs->sr_err = LDAP_COMPARE_FALSE;
123                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
124                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
125                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
126                                         a->a_nvals,
127                                         &op->oq_compare.rs_ava->aa_value,
128                                         op->o_tmpmemctx ) == 0 )
129                 {
130                         rs->sr_err = LDAP_COMPARE_TRUE;
131                         break;
132                 }
133         }
134
135 return_results:;
136         send_ldap_result( op, rs );
137
138         if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
139                 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
140         }
141
142         if ( e != NULL ) {
143                 entry_clean( e );
144         }
145
146         Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
147         switch ( rs->sr_err ) {
148         case LDAP_COMPARE_TRUE:
149         case LDAP_COMPARE_FALSE:
150                 return 0;
151
152         default:
153                 return 1;
154         }
155 }
156