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