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