1/* $OpenLDAP$ */
2/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1999-2011 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
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 the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
20 * Masarati.
21 */
22
23#include "portable.h"
24
25#include <stdio.h>
26
27#include <ac/string.h>
28#include <ac/socket.h>
29
30#include "slap.h"
31#include "../back-ldap/back-ldap.h"
32#include "back-meta.h"
33
34int
35meta_back_compare( Operation *op, SlapReply *rs )
36{
37	metainfo_t	*mi = ( metainfo_t * )op->o_bd->be_private;
38	metatarget_t	*mt;
39	metaconn_t	*mc;
40	int		rc = 0;
41	int		candidate = -1;
42	struct berval	mdn = BER_BVNULL;
43	dncookie	dc;
44	struct berval	mapped_attr = op->orc_ava->aa_desc->ad_cname;
45	struct berval	mapped_value = op->orc_ava->aa_value;
46	int		msgid;
47	ldap_back_send_t	retrying = LDAP_BACK_RETRYING;
48	LDAPControl	**ctrls = NULL;
49
50	mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
51	if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
52		return rs->sr_err;
53	}
54
55	assert( mc->mc_conns[ candidate ].msc_ld != NULL );
56
57	/*
58	 * Rewrite the modify dn, if needed
59	 */
60	mt = mi->mi_targets[ candidate ];
61	dc.target = mt;
62	dc.conn = op->o_conn;
63	dc.rs = rs;
64	dc.ctx = "compareDN";
65
66	switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
67	case LDAP_UNWILLING_TO_PERFORM:
68		rc = 1;
69		goto cleanup;
70
71	default:
72		break;
73	}
74
75	/*
76	 * if attr is objectClass, try to remap the value
77	 */
78	if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) {
79		ldap_back_map( &mt->mt_rwmap.rwm_oc,
80				&op->orc_ava->aa_value,
81				&mapped_value, BACKLDAP_MAP );
82
83		if ( BER_BVISNULL( &mapped_value ) || BER_BVISEMPTY( &mapped_value ) ) {
84			goto cleanup;
85		}
86
87	/*
88	 * else try to remap the attribute
89	 */
90	} else {
91		ldap_back_map( &mt->mt_rwmap.rwm_at,
92			&op->orc_ava->aa_desc->ad_cname,
93			&mapped_attr, BACKLDAP_MAP );
94		if ( BER_BVISNULL( &mapped_attr ) || BER_BVISEMPTY( &mapped_attr ) ) {
95			goto cleanup;
96		}
97
98		if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
99		{
100			dc.ctx = "compareAttrDN";
101
102			switch ( ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) )
103			{
104			case LDAP_UNWILLING_TO_PERFORM:
105				rc = 1;
106				goto cleanup;
107
108			default:
109				break;
110			}
111		}
112	}
113
114retry:;
115	ctrls = op->o_ctrls;
116	rc = meta_back_controls_add( op, rs, mc, candidate, &ctrls );
117	if ( rc != LDAP_SUCCESS ) {
118		send_ldap_result( op, rs );
119		goto cleanup;
120	}
121
122	rs->sr_err = ldap_compare_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
123			mapped_attr.bv_val, &mapped_value,
124			ctrls, NULL, &msgid );
125
126	rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
127		mt->mt_timeout[ SLAP_OP_COMPARE ], ( LDAP_BACK_SENDRESULT | retrying ) );
128	if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
129		retrying &= ~LDAP_BACK_RETRYING;
130		if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
131			/* if the identity changed, there might be need to re-authz */
132			(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
133			goto retry;
134		}
135	}
136
137cleanup:;
138	(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
139
140	if ( mdn.bv_val != op->o_req_dn.bv_val ) {
141		free( mdn.bv_val );
142	}
143
144	if ( op->orc_ava->aa_value.bv_val != mapped_value.bv_val ) {
145		free( mapped_value.bv_val );
146	}
147
148	if ( mc ) {
149		meta_back_release_conn( mi, mc );
150	}
151
152	return rs->sr_err;
153}
154
155