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];
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;
47 Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
49 rs->sr_err = backsql_get_db_conn( op, &dbh );
51 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
52 "could not get connection handle - exiting\n",
55 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
56 ? "SQL-backend error" : NULL;
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;
65 * Try to get attr as dynamic operational
67 if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
68 SlapReply nrs = { 0 };
70 user_entry.e_attrs = NULL;
71 user_entry.e_name = op->o_req_dn;
72 user_entry.e_nname = op->o_req_ndn;
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;
79 rs->sr_err = backsql_operational( op, &nrs );
80 if ( rs->sr_err != LDAP_SUCCESS ) {
84 user_entry.e_attrs = nrs.sr_operational_attrs;
87 rc = backsql_init_search( &bsi, &op->o_req_ndn,
89 SLAP_NO_LIMIT, SLAP_NO_LIMIT,
90 (time_t)(-1), NULL, dbh, op, rs, anlist,
92 if ( rc != LDAP_SUCCESS ) {
93 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
94 "could not retrieve compareDN ID - no such entry\n",
96 rs->sr_err = LDAP_NO_SUCH_OBJECT;
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 );
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;
119 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
120 for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
122 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
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,
129 &op->oq_compare.rs_ava->aa_value,
130 op->o_tmpmemctx ) == 0 )
132 rs->sr_err = LDAP_COMPARE_TRUE;
138 send_ldap_result( op, rs );
140 if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
141 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
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: