1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
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/
8  *
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.
13  *
14  * The Original Code is Mozilla Communicator client code, released
15  * March 31, 1998.
16  *
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.
21  *
22  * Contributor(s):
23  */
24 /*
25  *  Copyright (c) 1990 Regents of the University of Michigan.
26  *  All rights reserved.
27  */
28 /*
29  *  rename.c
30  */
31 
32 #if 0
33 #ifndef lint
34 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
35 #endif
36 #endif
37 
38 #include "ldap-int.h"
39 
40 /*
41  * ldap_rename - initiate an ldap modifyDN operation. Parameters:
42  *
43  *	ld		LDAP descriptor
44  *	dn		DN of the object to modify
45  *	newrdn		RDN that will form leftmost component of entry's new name
46  *      newparent       if present, this is the Distinguished Name of the entry
47  *                      which becomes the immediate parent of the existing entry
48  *	deleteoldrdn	nonzero means to delete old rdn values from the entry
49  *                      while zero means to retain them as attributes of the entry
50  *      serverctrls     list of LDAP server controls
51  *      clientctrls     list of client controls
52  *      msgidp          this result parameter will be set to the message id of the
53  *                      request if the ldap_rename() call succeeds
54  *
55  * Example:
56  *      int rc;
57  *	rc = ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid );
58  */
59 int
60 LDAP_CALL
61 ldap_rename(
62 	   LDAP *ld,
63 	   const char *dn,
64 	   const char *newrdn,
65 	   const char *newparent,
66 	   int deleteoldrdn,
67 	   LDAPControl	**serverctrls,
68 	   LDAPControl	**clientctrls,  /* not used for anything yet */
69 	   int *msgidp
70 )
71 {
72 	BerElement	*ber;
73 	int		rc, err;
74 
75 	/*
76 	 * A modify dn request looks like this:
77 	 *	ModifyDNRequest ::= SEQUENCE {
78 	 *		entry		LDAPDN,
79 	 *		newrdn		RelativeLDAPDN,
80 	 *              newparent       [0] LDAPDN OPTIONAL,
81 	 *		deleteoldrdn	BOOLEAN
82 	 *	}
83 	 */
84 
85 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_rename\n", 0, 0, 0 );
86 
87 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
88 		return( LDAP_PARAM_ERROR );
89 	}
90 	if ( NULL == newrdn) {
91 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
92 		return( LDAP_PARAM_ERROR );
93 	}
94 
95 	/* only ldapv3 or higher can do a proper rename
96 	 * (i.e. with non-NULL newparent and/or controls)
97 	 */
98 
99 	if (( NSLDAPI_LDAP_VERSION( ld ) < LDAP_VERSION3 )
100 	    && ((newparent != NULL) || (serverctrls != NULL)
101 	    || (clientctrls != NULL))) {
102 		LDAP_SET_LDERRNO( ld, LDAP_NOT_SUPPORTED, NULL, NULL );
103 		return( LDAP_NOT_SUPPORTED );
104 	}
105 
106 	if ( msgidp == NULL ) {
107 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
108                 return( LDAP_PARAM_ERROR );
109         }
110 
111 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
112 	*msgidp = ++ld->ld_msgid;
113         LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
114 
115 	/* see if modRDN or modDN is handled by the cache */
116  	if ( ld->ld_cache_on ) {
117 		if ( newparent == NULL && ld->ld_cache_modrdn != NULL ) {
118 			LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
119 			if ( (rc = (ld->ld_cache_modrdn)( ld, *msgidp,
120 			    LDAP_REQ_MODRDN, dn, newrdn, deleteoldrdn ))
121 			    != 0 ) {
122 				*msgidp = rc;
123 				LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
124 				return( LDAP_SUCCESS );
125 			}
126 			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
127 #if 0
128 		} else if ( ld->ld_cache_rename != NULL ) {
129 			LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
130 			if ( (rc = (ld->ld_cache_rename)( ld, *msgidp,
131 			    LDAP_REQ_MODDN, dn, newrdn, newparent,
132 			    deleteoldrdn )) != 0 ) {
133 				*msgidp = rc;
134 				return( LDAP_SUCCESS );
135 			}
136 			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
137 #endif
138 		}
139 	}
140 
141 	/* create a message to send */
142 	if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
143 	    != LDAP_SUCCESS ) {
144 		return( err );
145 	}
146 
147 	/* fill it in */
148 	if ( ber_printf( ber, "{it{ssb", *msgidp, LDAP_REQ_MODDN, dn,
149 	    newrdn, deleteoldrdn ) == -1 ) {
150 		LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
151 		ber_free( ber, 1 );
152 		return( LDAP_ENCODING_ERROR );
153 	}
154 
155 	if ( newparent == NULL ) {
156 		if ( ber_printf( ber, "}" ) == -1 ) {
157 			LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
158 			ber_free( ber, 1 );
159 			return( LDAP_ENCODING_ERROR );
160 		}
161 	} else {
162 		if ( ber_printf( ber, "ts}", LDAP_TAG_NEWSUPERIOR, newparent )
163 		    == -1 ) {
164 			LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
165 			ber_free( ber, 1 );
166 			return( LDAP_ENCODING_ERROR );
167 		}
168 	}
169 
170 	if (( rc = nsldapi_put_controls( ld, serverctrls, 1, ber ))
171 	    != LDAP_SUCCESS ) {
172 		ber_free( ber, 1 );
173 		return( rc );
174 	}
175 
176 	/* send the message */
177 	rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_MODDN,
178 		(char *) dn, ber );
179 	*msgidp = rc;
180 	return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
181 }
182 
183 int
184 LDAP_CALL
185 ldap_modrdn2( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
186 {
187 	int             msgid;
188 
189 	if ( ldap_rename( ld, dn, newrdn, NULL, deleteoldrdn, NULL, NULL, &msgid ) == LDAP_SUCCESS ) {
190 		return( msgid );
191 	} else {
192 		return( -1 );	/* error is in ld handle */
193 	}
194 }
195 
196 int
197 LDAP_CALL
198 ldap_modrdn( LDAP *ld, const char *dn, const char *newrdn )
199 {
200 	return( ldap_modrdn2( ld, dn, newrdn, 1 ) );
201 }
202 
203 int
204 LDAP_CALL
205 ldap_rename_s(
206 	   LDAP *ld,
207 	   const char *dn,
208 	   const char *newrdn,
209 	   const char *newparent,
210 	   int deleteoldrdn,
211 	   LDAPControl	**serverctrls,
212 	   LDAPControl	**clientctrls  /* not used for anything yet */
213 )
214 {
215 	int		msgid;
216 	LDAPMessage	*res;
217 
218 	if ( ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid ) != LDAP_SUCCESS ) {
219 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
220 	}
221 
222  	if ( msgid == -1 )
223 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
224 
225 	if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
226 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
227 
228 	return( ldap_result2error( ld, res, 1 ) );
229 }
230 
231 int
232 LDAP_CALL
233 ldap_modrdn2_s( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
234 {
235         int             msgid;
236         LDAPMessage     *res;
237 
238         if ( (msgid = ldap_modrdn2( ld, dn, newrdn, deleteoldrdn )) == -1 )
239                 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
240 
241         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
242                 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
243 
244         return( ldap_result2error( ld, res, 1 ) );
245 }
246 
247 int
248 LDAP_CALL
249 ldap_modrdn_s( LDAP *ld, const char *dn, const char *newrdn )
250 {
251 	return( ldap_modrdn2_s( ld, dn, newrdn, 1 ) );
252 }
253