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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
24 #include <sys/types.h>
27 #include "proto-sql.h"
30 backsql_compare( Operation *op, SlapReply *rs )
32 SQLHDBC dbh = SQL_NULL_HDBC;
33 Entry *e = NULL, user_entry;
35 backsql_srch_info bsi;
37 AttributeName anlist[2];
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;
45 Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
47 rs->sr_err = backsql_get_db_conn( op, &dbh );
49 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
50 "could not get connection handle - exiting\n",
53 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
54 ? "SQL-backend error" : NULL;
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;
63 * Try to get attr as dynamic operational
65 if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
66 SlapReply nrs = { 0 };
68 user_entry.e_attrs = NULL;
69 user_entry.e_name = op->o_req_dn;
70 user_entry.e_nname = op->o_req_ndn;
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;
77 rs->sr_err = backsql_operational( op, &nrs );
78 if ( rs->sr_err != LDAP_SUCCESS ) {
82 user_entry.e_attrs = nrs.sr_operational_attrs;
85 rc = backsql_init_search( &bsi, &op->o_req_ndn,
87 SLAP_NO_LIMIT, SLAP_NO_LIMIT,
88 (time_t)(-1), NULL, dbh, op, rs, anlist,
90 if ( rc != LDAP_SUCCESS ) {
91 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
92 "could not retrieve compareDN ID - no such entry\n",
94 rs->sr_err = LDAP_NO_SUCH_OBJECT;
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 );
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;
117 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
118 for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
120 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
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,
127 &op->oq_compare.rs_ava->aa_value,
128 op->o_tmpmemctx ) == 0 )
130 rs->sr_err = LDAP_COMPARE_TRUE;
136 send_ldap_result( op, rs );
138 if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
139 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
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: