compare.c revision 1.1 1 1.1 lukem /* $OpenLDAP: pkg/ldap/servers/slapd/back-sql/compare.c,v 1.24.2.5 2008/02/11 23:26:48 kurt Exp $ */
2 1.1 lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 1.1 lukem *
4 1.1 lukem * Copyright 1999-2008 The OpenLDAP Foundation.
5 1.1 lukem * Portions Copyright 1999 Dmitry Kovalev.
6 1.1 lukem * Portions Copyright 2002 Pierangelo Masarati.
7 1.1 lukem * All rights reserved.
8 1.1 lukem *
9 1.1 lukem * Redistribution and use in source and binary forms, with or without
10 1.1 lukem * modification, are permitted only as authorized by the OpenLDAP
11 1.1 lukem * Public License.
12 1.1 lukem *
13 1.1 lukem * A copy of this license is available in the file LICENSE in the
14 1.1 lukem * top-level directory of the distribution or, alternatively, at
15 1.1 lukem * <http://www.OpenLDAP.org/license.html>.
16 1.1 lukem */
17 1.1 lukem /* ACKNOWLEDGEMENTS:
18 1.1 lukem * This work was initially developed by Dmitry Kovalev for inclusion
19 1.1 lukem * by OpenLDAP Software. Additional significant contributors include
20 1.1 lukem * Pierangelo Masarati.
21 1.1 lukem */
22 1.1 lukem
23 1.1 lukem #include "portable.h"
24 1.1 lukem
25 1.1 lukem #include <stdio.h>
26 1.1 lukem #include <sys/types.h>
27 1.1 lukem
28 1.1 lukem #include "slap.h"
29 1.1 lukem #include "proto-sql.h"
30 1.1 lukem
31 1.1 lukem int
32 1.1 lukem backsql_compare( Operation *op, SlapReply *rs )
33 1.1 lukem {
34 1.1 lukem SQLHDBC dbh = SQL_NULL_HDBC;
35 1.1 lukem Entry e = { 0 };
36 1.1 lukem Attribute *a = NULL;
37 1.1 lukem backsql_srch_info bsi = { 0 };
38 1.1 lukem int rc;
39 1.1 lukem int manageDSAit = get_manageDSAit( op );
40 1.1 lukem AttributeName anlist[2];
41 1.1 lukem
42 1.1 lukem Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
43 1.1 lukem
44 1.1 lukem rs->sr_err = backsql_get_db_conn( op, &dbh );
45 1.1 lukem if ( rs->sr_err != LDAP_SUCCESS ) {
46 1.1 lukem Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
47 1.1 lukem "could not get connection handle - exiting\n",
48 1.1 lukem 0, 0, 0 );
49 1.1 lukem
50 1.1 lukem rs->sr_text = ( rs->sr_err == LDAP_OTHER )
51 1.1 lukem ? "SQL-backend error" : NULL;
52 1.1 lukem goto return_results;
53 1.1 lukem }
54 1.1 lukem
55 1.1 lukem anlist[ 0 ].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname;
56 1.1 lukem anlist[ 0 ].an_desc = op->oq_compare.rs_ava->aa_desc;
57 1.1 lukem BER_BVZERO( &anlist[ 1 ].an_name );
58 1.1 lukem
59 1.1 lukem /*
60 1.1 lukem * Get the entry
61 1.1 lukem */
62 1.1 lukem bsi.bsi_e = &e;
63 1.1 lukem rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE,
64 1.1 lukem (time_t)(-1), NULL, dbh, op, rs, anlist,
65 1.1 lukem ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
66 1.1 lukem switch ( rc ) {
67 1.1 lukem case LDAP_SUCCESS:
68 1.1 lukem break;
69 1.1 lukem
70 1.1 lukem case LDAP_REFERRAL:
71 1.1 lukem if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
72 1.1 lukem dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) )
73 1.1 lukem {
74 1.1 lukem rs->sr_err = LDAP_SUCCESS;
75 1.1 lukem rs->sr_text = NULL;
76 1.1 lukem rs->sr_matched = NULL;
77 1.1 lukem if ( rs->sr_ref ) {
78 1.1 lukem ber_bvarray_free( rs->sr_ref );
79 1.1 lukem rs->sr_ref = NULL;
80 1.1 lukem }
81 1.1 lukem break;
82 1.1 lukem }
83 1.1 lukem /* fallthru */
84 1.1 lukem
85 1.1 lukem default:
86 1.1 lukem Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
87 1.1 lukem "could not retrieve compareDN ID - no such entry\n",
88 1.1 lukem 0, 0, 0 );
89 1.1 lukem goto return_results;
90 1.1 lukem }
91 1.1 lukem
92 1.1 lukem if ( get_assert( op ) &&
93 1.1 lukem ( test_filter( op, &e, get_assertion( op ) )
94 1.1 lukem != LDAP_COMPARE_TRUE ) )
95 1.1 lukem {
96 1.1 lukem rs->sr_err = LDAP_ASSERTION_FAILED;
97 1.1 lukem goto return_results;
98 1.1 lukem }
99 1.1 lukem
100 1.1 lukem if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
101 1.1 lukem SlapReply nrs = { 0 };
102 1.1 lukem Attribute **ap;
103 1.1 lukem
104 1.1 lukem for ( ap = &e.e_attrs; *ap; ap = &(*ap)->a_next )
105 1.1 lukem ;
106 1.1 lukem
107 1.1 lukem nrs.sr_attrs = anlist;
108 1.1 lukem nrs.sr_entry = &e;
109 1.1 lukem nrs.sr_attr_flags = SLAP_OPATTRS_NO;
110 1.1 lukem nrs.sr_operational_attrs = NULL;
111 1.1 lukem
112 1.1 lukem rs->sr_err = backsql_operational( op, &nrs );
113 1.1 lukem if ( rs->sr_err != LDAP_SUCCESS ) {
114 1.1 lukem goto return_results;
115 1.1 lukem }
116 1.1 lukem
117 1.1 lukem *ap = nrs.sr_operational_attrs;
118 1.1 lukem }
119 1.1 lukem
120 1.1 lukem if ( ! access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc,
121 1.1 lukem &op->oq_compare.rs_ava->aa_value,
122 1.1 lukem ACL_COMPARE, NULL ) )
123 1.1 lukem {
124 1.1 lukem rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
125 1.1 lukem goto return_results;
126 1.1 lukem }
127 1.1 lukem
128 1.1 lukem rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
129 1.1 lukem for ( a = attrs_find( e.e_attrs, op->oq_compare.rs_ava->aa_desc );
130 1.1 lukem a != NULL;
131 1.1 lukem a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
132 1.1 lukem {
133 1.1 lukem rs->sr_err = LDAP_COMPARE_FALSE;
134 1.1 lukem if ( attr_valfind( a,
135 1.1 lukem SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
136 1.1 lukem SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
137 1.1 lukem &op->oq_compare.rs_ava->aa_value, NULL,
138 1.1 lukem op->o_tmpmemctx ) == 0 )
139 1.1 lukem {
140 1.1 lukem rs->sr_err = LDAP_COMPARE_TRUE;
141 1.1 lukem break;
142 1.1 lukem }
143 1.1 lukem }
144 1.1 lukem
145 1.1 lukem return_results:;
146 1.1 lukem switch ( rs->sr_err ) {
147 1.1 lukem case LDAP_COMPARE_TRUE:
148 1.1 lukem case LDAP_COMPARE_FALSE:
149 1.1 lukem break;
150 1.1 lukem
151 1.1 lukem default:
152 1.1 lukem if ( !BER_BVISNULL( &e.e_nname ) &&
153 1.1 lukem ! access_allowed( op, &e,
154 1.1 lukem slap_schema.si_ad_entry, NULL,
155 1.1 lukem ACL_DISCLOSE, NULL ) )
156 1.1 lukem {
157 1.1 lukem rs->sr_err = LDAP_NO_SUCH_OBJECT;
158 1.1 lukem rs->sr_text = NULL;
159 1.1 lukem }
160 1.1 lukem break;
161 1.1 lukem }
162 1.1 lukem
163 1.1 lukem send_ldap_result( op, rs );
164 1.1 lukem
165 1.1 lukem if ( rs->sr_matched ) {
166 1.1 lukem rs->sr_matched = NULL;
167 1.1 lukem }
168 1.1 lukem
169 1.1 lukem if ( rs->sr_ref ) {
170 1.1 lukem ber_bvarray_free( rs->sr_ref );
171 1.1 lukem rs->sr_ref = NULL;
172 1.1 lukem }
173 1.1 lukem
174 1.1 lukem if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
175 1.1 lukem (void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
176 1.1 lukem }
177 1.1 lukem
178 1.1 lukem if ( !BER_BVISNULL( &e.e_nname ) ) {
179 1.1 lukem backsql_entry_clean( op, &e );
180 1.1 lukem }
181 1.1 lukem
182 1.1 lukem if ( bsi.bsi_attrs != NULL ) {
183 1.1 lukem op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
184 1.1 lukem }
185 1.1 lukem
186 1.1 lukem Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
187 1.1 lukem switch ( rs->sr_err ) {
188 1.1 lukem case LDAP_COMPARE_TRUE:
189 1.1 lukem case LDAP_COMPARE_FALSE:
190 1.1 lukem return LDAP_SUCCESS;
191 1.1 lukem
192 1.1 lukem default:
193 1.1 lukem return rs->sr_err;
194 1.1 lukem }
195 1.1 lukem }
196 1.1 lukem
197