modrdn.c revision 1.1.1.10 1 1.1.1.9 christos /* $NetBSD: modrdn.c,v 1.1.1.10 2025/09/05 21:09:32 christos Exp $ */
2 1.1.1.2 lukem
3 1.1.1.4 tron /* $OpenLDAP$ */
4 1.1 lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 1.1 lukem *
6 1.1.1.10 christos * Copyright 1998-2024 The OpenLDAP Foundation.
7 1.1 lukem * All rights reserved.
8 1.1 lukem *
9 1.1 lukem * Redistribution and use in source and binary forms, with or without
10 1.1 lukem * modification, are permitted only as authorized by the OpenLDAP
11 1.1 lukem * Public License.
12 1.1 lukem *
13 1.1 lukem * A copy of this license is available in the file LICENSE in the
14 1.1 lukem * top-level directory of the distribution or, alternatively, at
15 1.1 lukem * <http://www.OpenLDAP.org/license.html>.
16 1.1 lukem */
17 1.1 lukem /* Portions Copyright (c) 1990 Regents of the University of Michigan.
18 1.1 lukem * All rights reserved.
19 1.1 lukem */
20 1.1 lukem /* Copyright 1999, Juan C. Gomez, All rights reserved.
21 1.1 lukem * This software is not subject to any license of Silicon Graphics
22 1.1 lukem * Inc. or Purdue University.
23 1.1 lukem *
24 1.1 lukem * Redistribution and use in source and binary forms are permitted
25 1.1 lukem * without restriction or fee of any kind as long as this notice
26 1.1 lukem * is preserved.
27 1.1 lukem */
28 1.1 lukem
29 1.1 lukem /* ACKNOWLEDGEMENTS:
30 1.1 lukem * Juan C. Gomez
31 1.1 lukem */
32 1.1 lukem
33 1.1.1.5 christos #include <sys/cdefs.h>
34 1.1.1.9 christos __RCSID("$NetBSD: modrdn.c,v 1.1.1.10 2025/09/05 21:09:32 christos Exp $");
35 1.1.1.5 christos
36 1.1 lukem #include "portable.h"
37 1.1 lukem
38 1.1 lukem #include <stdio.h>
39 1.1 lukem
40 1.1 lukem #include <ac/socket.h>
41 1.1 lukem #include <ac/string.h>
42 1.1 lukem #include <ac/time.h>
43 1.1 lukem
44 1.1 lukem #include "ldap-int.h"
45 1.1 lukem
46 1.1 lukem /*
47 1.1 lukem * A modify rdn request looks like this:
48 1.1 lukem * ModifyRDNRequest ::= SEQUENCE {
49 1.1 lukem * entry DistinguishedName,
50 1.1 lukem * newrdn RelativeDistinguishedName,
51 1.1 lukem * deleteoldrdn BOOLEAN
52 1.1 lukem * newSuperior [0] DistinguishedName [v3 only]
53 1.1 lukem * }
54 1.1 lukem */
55 1.1 lukem
56 1.1.1.5 christos BerElement *
57 1.1.1.5 christos ldap_build_moddn_req(
58 1.1 lukem LDAP *ld,
59 1.1 lukem LDAP_CONST char *dn,
60 1.1 lukem LDAP_CONST char *newrdn,
61 1.1 lukem LDAP_CONST char *newSuperior,
62 1.1 lukem int deleteoldrdn,
63 1.1 lukem LDAPControl **sctrls,
64 1.1 lukem LDAPControl **cctrls,
65 1.1.1.5 christos ber_int_t *msgidp )
66 1.1 lukem {
67 1.1 lukem BerElement *ber;
68 1.1 lukem int rc;
69 1.1 lukem
70 1.1 lukem /* create a message to send */
71 1.1 lukem if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
72 1.1.1.5 christos return( NULL );
73 1.1 lukem }
74 1.1 lukem
75 1.1.1.5 christos LDAP_NEXT_MSGID( ld, *msgidp );
76 1.1 lukem if( newSuperior != NULL ) {
77 1.1 lukem /* must be version 3 (or greater) */
78 1.1 lukem if ( ld->ld_version < LDAP_VERSION3 ) {
79 1.1 lukem ld->ld_errno = LDAP_NOT_SUPPORTED;
80 1.1 lukem ber_free( ber, 1 );
81 1.1.1.5 christos return( NULL );
82 1.1 lukem }
83 1.1 lukem rc = ber_printf( ber, "{it{ssbtsN}", /* '}' */
84 1.1.1.5 christos *msgidp, LDAP_REQ_MODDN,
85 1.1 lukem dn, newrdn, (ber_int_t) deleteoldrdn,
86 1.1 lukem LDAP_TAG_NEWSUPERIOR, newSuperior );
87 1.1 lukem
88 1.1 lukem } else {
89 1.1 lukem rc = ber_printf( ber, "{it{ssbN}", /* '}' */
90 1.1.1.5 christos *msgidp, LDAP_REQ_MODDN,
91 1.1 lukem dn, newrdn, (ber_int_t) deleteoldrdn );
92 1.1 lukem }
93 1.1 lukem
94 1.1 lukem if ( rc < 0 ) {
95 1.1 lukem ld->ld_errno = LDAP_ENCODING_ERROR;
96 1.1 lukem ber_free( ber, 1 );
97 1.1.1.5 christos return( NULL );
98 1.1 lukem }
99 1.1 lukem
100 1.1 lukem /* Put Server Controls */
101 1.1 lukem if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
102 1.1 lukem ber_free( ber, 1 );
103 1.1.1.5 christos return( NULL );
104 1.1 lukem }
105 1.1 lukem
106 1.1 lukem rc = ber_printf( ber, /*{*/ "N}" );
107 1.1 lukem if ( rc < 0 ) {
108 1.1 lukem ld->ld_errno = LDAP_ENCODING_ERROR;
109 1.1 lukem ber_free( ber, 1 );
110 1.1.1.5 christos return( NULL );
111 1.1 lukem }
112 1.1 lukem
113 1.1.1.5 christos return( ber );
114 1.1.1.5 christos }
115 1.1.1.5 christos
116 1.1.1.5 christos /*
117 1.1.1.5 christos * ldap_rename - initiate an ldap extended modifyDN operation.
118 1.1.1.5 christos *
119 1.1.1.5 christos * Parameters:
120 1.1.1.5 christos * ld LDAP descriptor
121 1.1.1.5 christos * dn DN of the object to modify
122 1.1.1.5 christos * newrdn RDN to give the object
123 1.1.1.5 christos * deleteoldrdn nonzero means to delete old rdn values from the entry
124 1.1.1.5 christos * newSuperior DN of the new parent if applicable
125 1.1.1.5 christos *
126 1.1.1.5 christos * Returns the LDAP error code.
127 1.1.1.5 christos */
128 1.1.1.5 christos
129 1.1.1.5 christos int
130 1.1.1.5 christos ldap_rename(
131 1.1.1.5 christos LDAP *ld,
132 1.1.1.5 christos LDAP_CONST char *dn,
133 1.1.1.5 christos LDAP_CONST char *newrdn,
134 1.1.1.5 christos LDAP_CONST char *newSuperior,
135 1.1.1.5 christos int deleteoldrdn,
136 1.1.1.5 christos LDAPControl **sctrls,
137 1.1.1.5 christos LDAPControl **cctrls,
138 1.1.1.5 christos int *msgidp )
139 1.1.1.5 christos {
140 1.1.1.5 christos BerElement *ber;
141 1.1.1.5 christos int rc;
142 1.1.1.5 christos ber_int_t id;
143 1.1.1.5 christos
144 1.1.1.9 christos Debug0( LDAP_DEBUG_TRACE, "ldap_rename\n" );
145 1.1.1.5 christos
146 1.1.1.5 christos /* check client controls */
147 1.1.1.5 christos rc = ldap_int_client_controls( ld, cctrls );
148 1.1.1.5 christos if( rc != LDAP_SUCCESS ) return rc;
149 1.1.1.5 christos
150 1.1.1.5 christos ber = ldap_build_moddn_req( ld, dn, newrdn, newSuperior,
151 1.1.1.5 christos deleteoldrdn, sctrls, cctrls, &id );
152 1.1.1.5 christos if( !ber )
153 1.1.1.5 christos return ld->ld_errno;
154 1.1.1.5 christos
155 1.1 lukem /* send the message */
156 1.1 lukem *msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODRDN, dn, ber, id );
157 1.1 lukem
158 1.1 lukem if( *msgidp < 0 ) {
159 1.1 lukem return( ld->ld_errno );
160 1.1 lukem }
161 1.1 lukem
162 1.1 lukem return LDAP_SUCCESS;
163 1.1 lukem }
164 1.1 lukem
165 1.1 lukem
166 1.1 lukem /*
167 1.1 lukem * ldap_rename2 - initiate an ldap (and X.500) modifyDN operation. Parameters:
168 1.1 lukem * (LDAP V3 MODIFYDN REQUEST)
169 1.1 lukem * ld LDAP descriptor
170 1.1 lukem * dn DN of the object to modify
171 1.1 lukem * newrdn RDN to give the object
172 1.1 lukem * deleteoldrdn nonzero means to delete old rdn values from the entry
173 1.1 lukem * newSuperior DN of the new parent if applicable
174 1.1 lukem *
175 1.1 lukem * ldap_rename2 uses a U-Mich Style API. It returns the msgid.
176 1.1 lukem */
177 1.1 lukem
178 1.1 lukem int
179 1.1 lukem ldap_rename2(
180 1.1 lukem LDAP *ld,
181 1.1 lukem LDAP_CONST char *dn,
182 1.1 lukem LDAP_CONST char *newrdn,
183 1.1 lukem LDAP_CONST char *newSuperior,
184 1.1 lukem int deleteoldrdn )
185 1.1 lukem {
186 1.1 lukem int msgid;
187 1.1 lukem int rc;
188 1.1 lukem
189 1.1.1.9 christos Debug0( LDAP_DEBUG_TRACE, "ldap_rename2\n" );
190 1.1 lukem
191 1.1 lukem rc = ldap_rename( ld, dn, newrdn, newSuperior,
192 1.1 lukem deleteoldrdn, NULL, NULL, &msgid );
193 1.1 lukem
194 1.1 lukem return rc == LDAP_SUCCESS ? msgid : -1;
195 1.1 lukem }
196 1.1 lukem
197 1.1 lukem
198 1.1 lukem /*
199 1.1 lukem * ldap_modrdn2 - initiate an ldap modifyRDN operation. Parameters:
200 1.1 lukem *
201 1.1 lukem * ld LDAP descriptor
202 1.1 lukem * dn DN of the object to modify
203 1.1 lukem * newrdn RDN to give the object
204 1.1 lukem * deleteoldrdn nonzero means to delete old rdn values from the entry
205 1.1 lukem *
206 1.1 lukem * Example:
207 1.1 lukem * msgid = ldap_modrdn( ld, dn, newrdn );
208 1.1 lukem */
209 1.1 lukem int
210 1.1 lukem ldap_modrdn2( LDAP *ld,
211 1.1 lukem LDAP_CONST char *dn,
212 1.1 lukem LDAP_CONST char *newrdn,
213 1.1 lukem int deleteoldrdn )
214 1.1 lukem {
215 1.1 lukem return ldap_rename2( ld, dn, newrdn, NULL, deleteoldrdn );
216 1.1 lukem }
217 1.1 lukem
218 1.1 lukem int
219 1.1 lukem ldap_modrdn( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn )
220 1.1 lukem {
221 1.1 lukem return( ldap_rename2( ld, dn, newrdn, NULL, 1 ) );
222 1.1 lukem }
223 1.1 lukem
224 1.1 lukem
225 1.1 lukem int
226 1.1 lukem ldap_rename_s(
227 1.1 lukem LDAP *ld,
228 1.1 lukem LDAP_CONST char *dn,
229 1.1 lukem LDAP_CONST char *newrdn,
230 1.1 lukem LDAP_CONST char *newSuperior,
231 1.1 lukem int deleteoldrdn,
232 1.1 lukem LDAPControl **sctrls,
233 1.1 lukem LDAPControl **cctrls )
234 1.1 lukem {
235 1.1 lukem int rc;
236 1.1 lukem int msgid;
237 1.1 lukem LDAPMessage *res;
238 1.1 lukem
239 1.1 lukem rc = ldap_rename( ld, dn, newrdn, newSuperior,
240 1.1 lukem deleteoldrdn, sctrls, cctrls, &msgid );
241 1.1 lukem
242 1.1 lukem if( rc != LDAP_SUCCESS ) {
243 1.1 lukem return rc;
244 1.1 lukem }
245 1.1 lukem
246 1.1 lukem rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &res );
247 1.1 lukem
248 1.1 lukem if( rc == -1 || !res ) {
249 1.1 lukem return ld->ld_errno;
250 1.1 lukem }
251 1.1 lukem
252 1.1 lukem return ldap_result2error( ld, res, 1 );
253 1.1 lukem }
254 1.1 lukem
255 1.1 lukem int
256 1.1 lukem ldap_rename2_s(
257 1.1 lukem LDAP *ld,
258 1.1 lukem LDAP_CONST char *dn,
259 1.1 lukem LDAP_CONST char *newrdn,
260 1.1 lukem LDAP_CONST char *newSuperior,
261 1.1 lukem int deleteoldrdn )
262 1.1 lukem {
263 1.1 lukem return ldap_rename_s( ld, dn, newrdn, newSuperior,
264 1.1 lukem deleteoldrdn, NULL, NULL );
265 1.1 lukem }
266 1.1 lukem
267 1.1 lukem int
268 1.1 lukem ldap_modrdn2_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn, int deleteoldrdn )
269 1.1 lukem {
270 1.1 lukem return ldap_rename_s( ld, dn, newrdn, NULL, deleteoldrdn, NULL, NULL );
271 1.1 lukem }
272 1.1 lukem
273 1.1 lukem int
274 1.1 lukem ldap_modrdn_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn )
275 1.1 lukem {
276 1.1 lukem return ldap_rename_s( ld, dn, newrdn, NULL, 1, NULL, NULL );
277 1.1 lukem }
278 1.1 lukem
279