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