2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Dmitry Kovalev for inclusion
19 * by OpenLDAP Software. Additional significant contributors include
20 * Pierangelo Masarati.
26 #include <sys/types.h>
29 #include "proto-sql.h"
32 backsql_compare( Operation *op, SlapReply *rs )
34 SQLHDBC dbh = SQL_NULL_HDBC;
35 Entry *e = NULL, user_entry;
37 backsql_srch_info bsi;
39 AttributeName anlist[2],
42 BER_BVZERO( &user_entry.e_name );
43 BER_BVZERO( &user_entry.e_nname );
44 user_entry.e_attrs = NULL;
46 Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
48 rs->sr_err = backsql_get_db_conn( op, &dbh );
50 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
51 "could not get connection handle - exiting\n",
54 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
55 ? "SQL-backend error" : NULL;
59 memset( &anlist[0], 0, 2 * sizeof( AttributeName ) );
60 anlist[0].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname;
61 anlist[0].an_desc = op->oq_compare.rs_ava->aa_desc;
64 * Try to get attr as dynamic operational
66 if ( !is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
71 * FIXME: deal with matchedDN/referral?
73 rc = backsql_init_search( &bsi, &op->o_req_ndn,
75 SLAP_NO_LIMIT, SLAP_NO_LIMIT,
76 (time_t)(-1), NULL, dbh, op, rs, anlistp,
78 if ( rc != LDAP_SUCCESS ) {
79 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
80 "could not retrieve compareDN ID - no such entry\n",
82 rs->sr_err = LDAP_NO_SUCH_OBJECT;
86 if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
87 SlapReply nrs = { 0 };
89 user_entry.e_attrs = NULL;
90 user_entry.e_name = bsi.bsi_base_id.eid_dn;
91 user_entry.e_nname = bsi.bsi_base_id.eid_ndn;
93 nrs.sr_attrs = anlist;
94 nrs.sr_entry = &user_entry;
95 nrs.sr_attr_flags = SLAP_OPATTRS_NO;
96 nrs.sr_operational_attrs = NULL;
98 rs->sr_err = backsql_operational( op, &nrs );
99 if ( rs->sr_err != LDAP_SUCCESS ) {
103 user_entry.e_attrs = nrs.sr_operational_attrs;
106 bsi.bsi_e = &user_entry;
107 rc = backsql_id2entry( &bsi, &bsi.bsi_base_id );
108 if ( rc != LDAP_SUCCESS ) {
109 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
110 "error %d in backsql_id2entry() "
111 "- compare failed\n", rc, 0, 0 );
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 #ifdef SLAP_ACL_HONOR_DISCLOSE
122 if ( ! access_allowed( op, &e, slap_schema.si_ad_entry, NULL,
123 ACL_DISCLOSE, NULL ) ) {
124 rs->sr_err = LDAP_NO_SUCH_OBJECT;
126 #endif /* SLAP_ACL_HONOR_DISCLOSE */
128 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
133 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
134 for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
136 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
138 rs->sr_err = LDAP_COMPARE_FALSE;
139 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
140 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
141 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
143 &op->oq_compare.rs_ava->aa_value,
144 op->o_tmpmemctx ) == 0 )
146 rs->sr_err = LDAP_COMPARE_TRUE;
152 send_ldap_result( op, rs );
154 if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
155 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
162 if ( bsi.bsi_attrs != NULL ) {
163 op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
166 Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
167 switch ( rs->sr_err ) {
168 case LDAP_COMPARE_TRUE:
169 case LDAP_COMPARE_FALSE: