Debian Codegraph @

servers/slapd/back-meta/modrdn.c

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