]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/compare.c
minor naming cleanup; improvements to DN mapping layer; major docs update
[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, op->oq_compare.rs_ava->aa_desc, 
113                                 &op->oq_compare.rs_ava->aa_value,
114                                 ACL_COMPARE, NULL ) ) {
115                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
116                 goto return_results;
117         }
118
119         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
120         for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
121                         a != NULL;
122                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
123         {
124                 rs->sr_err = LDAP_COMPARE_FALSE;
125                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
126                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
127                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
128                                         a->a_nvals,
129                                         &op->oq_compare.rs_ava->aa_value,
130                                         op->o_tmpmemctx ) == 0 )
131                 {
132                         rs->sr_err = LDAP_COMPARE_TRUE;
133                         break;
134                 }
135         }
136
137 return_results:;
138         send_ldap_result( op, rs );
139
140         if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
141                 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
142         }
143
144         if ( e != NULL ) {
145                 entry_clean( e );
146         }
147
148         Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
149         switch ( rs->sr_err ) {
150         case LDAP_COMPARE_TRUE:
151         case LDAP_COMPARE_FALSE:
152                 return 0;
153
154         default:
155                 return 1;
156         }
157 }
158