dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libldap5 / sources / ldap / common / rename.c
blob0ee1b95701b2e6b2cc34e2def99384ccf707c3ea
1 #pragma ident "%Z%%M% %I% %E% SMI"
3 /*
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
15 * March 31, 1998.
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
20 * Rights Reserved.
22 * Contributor(s):
25 * Copyright (c) 1990 Regents of the University of Michigan.
26 * All rights reserved.
29 * rename.c
32 #if 0
33 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
34 #endif
36 #include "ldap-int.h"
39 * ldap_rename - initiate an ldap modifyDN operation. Parameters:
41 * ld LDAP descriptor
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
53 * Example:
54 * int rc;
55 * rc = ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid );
57 int
58 LDAP_CALL
59 ldap_rename(
60 LDAP *ld,
61 const char *dn,
62 const char *newrdn,
63 const char *newparent,
64 int deleteoldrdn,
65 LDAPControl **serverctrls,
66 LDAPControl **clientctrls, /* not used for anything yet */
67 int *msgidp
70 BerElement *ber;
71 int rc, err;
74 * A modify dn request looks like this:
75 * ModifyDNRequest ::= SEQUENCE {
76 * entry LDAPDN,
77 * newrdn RelativeLDAPDN,
78 * newparent [0] LDAPDN OPTIONAL,
79 * deleteoldrdn BOOLEAN
80 * }
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 ))
119 != 0 ) {
120 *msgidp = rc;
121 LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
122 return( LDAP_SUCCESS );
124 LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
125 #if 0
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 ) {
131 *msgidp = rc;
132 return( LDAP_SUCCESS );
134 LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
135 #endif
139 /* create a message to send */
140 if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
141 != LDAP_SUCCESS ) {
142 return( err );
145 /* fill it in */
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 );
149 ber_free( ber, 1 );
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 );
156 ber_free( ber, 1 );
157 return( LDAP_ENCODING_ERROR );
159 } else {
160 if ( ber_printf( ber, "ts}", LDAP_TAG_NEWSUPERIOR, newparent )
161 == -1 ) {
162 LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
163 ber_free( ber, 1 );
164 return( LDAP_ENCODING_ERROR );
168 if (( rc = nsldapi_put_controls( ld, serverctrls, 1, ber ))
169 != LDAP_SUCCESS ) {
170 ber_free( ber, 1 );
171 return( rc );
174 /* send the message */
175 rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_MODDN,
176 (char *) dn, ber );
177 *msgidp = rc;
178 return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
182 LDAP_CALL
183 ldap_modrdn2( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
185 int msgid;
187 if ( ldap_rename( ld, dn, newrdn, NULL, deleteoldrdn, NULL, NULL, &msgid ) == LDAP_SUCCESS ) {
188 return( msgid );
189 } else {
190 return( -1 ); /* error is in ld handle */
195 LDAP_CALL
196 ldap_modrdn( LDAP *ld, const char *dn, const char *newrdn )
198 return( ldap_modrdn2( ld, dn, newrdn, 1 ) );
202 LDAP_CALL
203 ldap_rename_s(
204 LDAP *ld,
205 const char *dn,
206 const char *newrdn,
207 const char *newparent,
208 int deleteoldrdn,
209 LDAPControl **serverctrls,
210 LDAPControl **clientctrls /* not used for anything yet */
213 int msgid;
214 LDAPMessage *res;
216 if ( ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid ) != LDAP_SUCCESS ) {
217 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
220 if ( msgid == -1 )
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 ) );
230 LDAP_CALL
231 ldap_modrdn2_s( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
233 int msgid;
234 LDAPMessage *res;
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 ) );
246 LDAP_CALL
247 ldap_modrdn_s( LDAP *ld, const char *dn, const char *newrdn )
249 return( ldap_modrdn2_s( ld, dn, newrdn, 1 ) );