1 #pragma ident "%Z%%M% %I% %E% SMI"
4 * The contents of this file are subject to the Netscape Public
5 * License Version 1.1 (the "License"); you may not use this file
6 * except in compliance with the License. You may obtain a copy of
7 * the License at http://www.mozilla.org/NPL/
9 * Software distributed under the License is distributed on an "AS
10 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * rights and limitations under the License.
14 * The Original Code is Mozilla Communicator client code, released
17 * The Initial Developer of the Original Code is Netscape
18 * Communications Corporation. Portions created by Netscape are
19 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25 * Copyright (c) 1990 Regents of the University of Michigan.
26 * All rights reserved.
33 static char copyright
[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
39 * ldap_rename - initiate an ldap modifyDN operation. Parameters:
42 * dn DN of the object to modify
43 * newrdn RDN that will form leftmost component of entry's new name
44 * newparent if present, this is the Distinguished Name of the entry
45 * which becomes the immediate parent of the existing entry
46 * deleteoldrdn nonzero means to delete old rdn values from the entry
47 * while zero means to retain them as attributes of the entry
48 * serverctrls list of LDAP server controls
49 * clientctrls list of client controls
50 * msgidp this result parameter will be set to the message id of the
51 * request if the ldap_rename() call succeeds
55 * rc = ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid );
63 const char *newparent
,
65 LDAPControl
**serverctrls
,
66 LDAPControl
**clientctrls
, /* not used for anything yet */
74 * A modify dn request looks like this:
75 * ModifyDNRequest ::= SEQUENCE {
77 * newrdn RelativeLDAPDN,
78 * newparent [0] LDAPDN OPTIONAL,
79 * deleteoldrdn BOOLEAN
83 LDAPDebug( LDAP_DEBUG_TRACE
, "ldap_rename\n", 0, 0, 0 );
85 if ( !NSLDAPI_VALID_LDAP_POINTER( ld
)) {
86 return( LDAP_PARAM_ERROR
);
88 if ( NULL
== newrdn
) {
89 LDAP_SET_LDERRNO( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
90 return( LDAP_PARAM_ERROR
);
93 /* only ldapv3 or higher can do a proper rename
94 * (i.e. with non-NULL newparent and/or controls)
97 if (( NSLDAPI_LDAP_VERSION( ld
) < LDAP_VERSION3
)
98 && ((newparent
!= NULL
) || (serverctrls
!= NULL
)
99 || (clientctrls
!= NULL
))) {
100 LDAP_SET_LDERRNO( ld
, LDAP_NOT_SUPPORTED
, NULL
, NULL
);
101 return( LDAP_NOT_SUPPORTED
);
104 if ( msgidp
== NULL
) {
105 LDAP_SET_LDERRNO( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
106 return( LDAP_PARAM_ERROR
);
109 LDAP_MUTEX_LOCK( ld
, LDAP_MSGID_LOCK
);
110 *msgidp
= ++ld
->ld_msgid
;
111 LDAP_MUTEX_UNLOCK( ld
, LDAP_MSGID_LOCK
);
113 /* see if modRDN or modDN is handled by the cache */
114 if ( ld
->ld_cache_on
) {
115 if ( newparent
== NULL
&& ld
->ld_cache_modrdn
!= NULL
) {
116 LDAP_MUTEX_LOCK( ld
, LDAP_CACHE_LOCK
);
117 if ( (rc
= (ld
->ld_cache_modrdn
)( ld
, *msgidp
,
118 LDAP_REQ_MODRDN
, dn
, newrdn
, deleteoldrdn
))
121 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
122 return( LDAP_SUCCESS
);
124 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
126 } else if ( ld
->ld_cache_rename
!= NULL
) {
127 LDAP_MUTEX_LOCK( ld
, LDAP_CACHE_LOCK
);
128 if ( (rc
= (ld
->ld_cache_rename
)( ld
, *msgidp
,
129 LDAP_REQ_MODDN
, dn
, newrdn
, newparent
,
130 deleteoldrdn
)) != 0 ) {
132 return( LDAP_SUCCESS
);
134 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
139 /* create a message to send */
140 if (( err
= nsldapi_alloc_ber_with_options( ld
, &ber
))
146 if ( ber_printf( ber
, "{it{ssb", *msgidp
, LDAP_REQ_MODDN
, dn
,
147 newrdn
, deleteoldrdn
) == -1 ) {
148 LDAP_SET_LDERRNO( ld
, LDAP_ENCODING_ERROR
, NULL
, NULL
);
150 return( LDAP_ENCODING_ERROR
);
153 if ( newparent
== NULL
) {
154 if ( ber_printf( ber
, "}" ) == -1 ) {
155 LDAP_SET_LDERRNO( ld
, LDAP_ENCODING_ERROR
, NULL
, NULL
);
157 return( LDAP_ENCODING_ERROR
);
160 if ( ber_printf( ber
, "ts}", LDAP_TAG_NEWSUPERIOR
, newparent
)
162 LDAP_SET_LDERRNO( ld
, LDAP_ENCODING_ERROR
, NULL
, NULL
);
164 return( LDAP_ENCODING_ERROR
);
168 if (( rc
= nsldapi_put_controls( ld
, serverctrls
, 1, ber
))
174 /* send the message */
175 rc
= nsldapi_send_initial_request( ld
, *msgidp
, LDAP_REQ_MODDN
,
178 return( rc
< 0 ? LDAP_GET_LDERRNO( ld
, NULL
, NULL
) : LDAP_SUCCESS
);
183 ldap_modrdn2( LDAP
*ld
, const char *dn
, const char *newrdn
, int deleteoldrdn
)
187 if ( ldap_rename( ld
, dn
, newrdn
, NULL
, deleteoldrdn
, NULL
, NULL
, &msgid
) == LDAP_SUCCESS
) {
190 return( -1 ); /* error is in ld handle */
196 ldap_modrdn( LDAP
*ld
, const char *dn
, const char *newrdn
)
198 return( ldap_modrdn2( ld
, dn
, newrdn
, 1 ) );
207 const char *newparent
,
209 LDAPControl
**serverctrls
,
210 LDAPControl
**clientctrls
/* not used for anything yet */
216 if ( ldap_rename( ld
, dn
, newrdn
, newparent
, deleteoldrdn
, serverctrls
, clientctrls
, &msgid
) != LDAP_SUCCESS
) {
217 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
221 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
223 if ( ldap_result( ld
, msgid
, 1, NULL
, &res
) == -1 )
224 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
226 return( ldap_result2error( ld
, res
, 1 ) );
231 ldap_modrdn2_s( LDAP
*ld
, const char *dn
, const char *newrdn
, int deleteoldrdn
)
236 if ( (msgid
= ldap_modrdn2( ld
, dn
, newrdn
, deleteoldrdn
)) == -1 )
237 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
239 if ( ldap_result( ld
, msgid
, 1, NULL
, &res
) == -1 )
240 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
242 return( ldap_result2error( ld
, res
, 1 ) );
247 ldap_modrdn_s( LDAP
*ld
, const char *dn
, const char *newrdn
)
249 return( ldap_modrdn2_s( ld
, dn
, newrdn
, 1 ) );