1/*	$NetBSD$	*/
2
3/* OpenLDAP: pkg/ldap/servers/slapd/back-meta/modrdn.c,v 1.39.2.12 2010/04/13 20:23:31 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/socket.h>
30#include <ac/string.h>
31
32#include "slap.h"
33#include "../back-ldap/back-ldap.h"
34#include "back-meta.h"
35
36int
37meta_back_modrdn( 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		candidate = -1;
43	struct berval	mdn = BER_BVNULL,
44			mnewSuperior = BER_BVNULL;
45	dncookie	dc;
46	int		msgid;
47	ldap_back_send_t	retrying = LDAP_BACK_RETRYING;
48	LDAPControl	**ctrls = NULL;
49	struct berval	newrdn = BER_BVNULL;
50
51	mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
52	if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
53		return rs->sr_err;
54	}
55
56	assert( mc->mc_conns[ candidate ].msc_ld != NULL );
57
58	mt = mi->mi_targets[ candidate ];
59	dc.target = mt;
60	dc.conn = op->o_conn;
61	dc.rs = rs;
62
63	if ( op->orr_newSup ) {
64
65		/*
66		 * NOTE: the newParent, if defined, must be on the
67		 * same target as the entry to be renamed.  This check
68		 * has been anticipated in meta_back_getconn()
69		 */
70		/*
71		 * FIXME: one possibility is to delete the entry
72		 * from one target and add it to the other;
73		 * unfortunately we'd need write access to both,
74		 * which is nearly impossible; for administration
75		 * needs, the rootdn of the metadirectory could
76		 * be mapped to an administrative account on each
77		 * target (the binddn?); we'll see.
78		 */
79		/*
80		 * NOTE: we need to port the identity assertion
81		 * feature from back-ldap
82		 */
83
84		/* needs LDAPv3 */
85		switch ( mt->mt_version ) {
86		case LDAP_VERSION3:
87			break;
88
89		case 0:
90			if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
91				break;
92			}
93			/* fall thru */
94
95		default:
96			/* op->o_protocol cannot be anything but LDAPv3,
97			 * otherwise wouldn't be here */
98			rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
99			send_ldap_result( op, rs );
100			goto cleanup;
101		}
102
103		/*
104		 * Rewrite the new superior, if defined and required
105	 	 */
106		dc.ctx = "newSuperiorDN";
107		if ( ldap_back_dn_massage( &dc, op->orr_newSup, &mnewSuperior ) ) {
108			rs->sr_err = LDAP_OTHER;
109			send_ldap_result( op, rs );
110			goto cleanup;
111		}
112	}
113
114	/*
115	 * Rewrite the modrdn dn, if required
116	 */
117	dc.ctx = "modrDN";
118	if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
119		rs->sr_err = LDAP_OTHER;
120		send_ldap_result( op, rs );
121		goto cleanup;
122	}
123
124	/* NOTE: we need to copy the newRDN in case it was formed
125	 * from a DN by simply changing the length (ITS#5397) */
126	newrdn = op->orr_newrdn;
127	if ( newrdn.bv_val[ newrdn.bv_len ] != '\0' ) {
128		ber_dupbv_x( &newrdn, &op->orr_newrdn, op->o_tmpmemctx );
129	}
130
131retry:;
132	ctrls = op->o_ctrls;
133	if ( meta_back_controls_add( op, rs, mc, candidate, &ctrls ) != LDAP_SUCCESS )
134	{
135		send_ldap_result( op, rs );
136		goto cleanup;
137	}
138
139	rs->sr_err = ldap_rename( mc->mc_conns[ candidate ].msc_ld,
140			mdn.bv_val, newrdn.bv_val,
141			mnewSuperior.bv_val, op->orr_deleteoldrdn,
142			ctrls, NULL, &msgid );
143	rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
144		mt->mt_timeout[ SLAP_OP_MODRDN ], ( LDAP_BACK_SENDRESULT | retrying ) );
145	if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
146		retrying &= ~LDAP_BACK_RETRYING;
147		if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
148			/* if the identity changed, there might be need to re-authz */
149			(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
150			goto retry;
151		}
152	}
153
154cleanup:;
155	(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
156
157	if ( mdn.bv_val != op->o_req_dn.bv_val ) {
158		free( mdn.bv_val );
159		BER_BVZERO( &mdn );
160	}
161
162	if ( !BER_BVISNULL( &mnewSuperior )
163			&& mnewSuperior.bv_val != op->orr_newSup->bv_val )
164	{
165		free( mnewSuperior.bv_val );
166		BER_BVZERO( &mnewSuperior );
167	}
168
169	if ( newrdn.bv_val != op->orr_newrdn.bv_val ) {
170		op->o_tmpfree( newrdn.bv_val, op->o_tmpmemctx );
171	}
172
173	if ( mc ) {
174		meta_back_release_conn( mi, mc );
175	}
176
177	return rs->sr_err;
178}
179
180