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