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