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