17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
37c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
47c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
57c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
87c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
97c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
107c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
137c478bd9Sstevel@tonic-gate  * March 31, 1998.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
167c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
177c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
187c478bd9Sstevel@tonic-gate  * Rights Reserved.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * Contributor(s):
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
247c478bd9Sstevel@tonic-gate  *  All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *  rename.c
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #if 0
31*1da57d55SToomas Soome #ifndef lint
327c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "ldap-int.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * ldap_rename - initiate an ldap modifyDN operation. Parameters:
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *	ld		LDAP descriptor
427c478bd9Sstevel@tonic-gate  *	dn		DN of the object to modify
437c478bd9Sstevel@tonic-gate  *	newrdn		RDN that will form leftmost component of entry's new name
447c478bd9Sstevel@tonic-gate  *      newparent       if present, this is the Distinguished Name of the entry
457c478bd9Sstevel@tonic-gate  *                      which becomes the immediate parent of the existing entry
467c478bd9Sstevel@tonic-gate  *	deleteoldrdn	nonzero means to delete old rdn values from the entry
477c478bd9Sstevel@tonic-gate  *                      while zero means to retain them as attributes of the entry
487c478bd9Sstevel@tonic-gate  *      serverctrls     list of LDAP server controls
497c478bd9Sstevel@tonic-gate  *      clientctrls     list of client controls
507c478bd9Sstevel@tonic-gate  *      msgidp          this result parameter will be set to the message id of the
517c478bd9Sstevel@tonic-gate  *                      request if the ldap_rename() call succeeds
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * Example:
547c478bd9Sstevel@tonic-gate  *      int rc;
557c478bd9Sstevel@tonic-gate  *	rc = ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid );
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate int
587c478bd9Sstevel@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)59*1da57d55SToomas Soome ldap_rename(
60*1da57d55SToomas Soome 	   LDAP *ld,
61*1da57d55SToomas Soome 	   const char *dn,
62*1da57d55SToomas Soome 	   const char *newrdn,
637c478bd9Sstevel@tonic-gate 	   const char *newparent,
64*1da57d55SToomas Soome 	   int deleteoldrdn,
657c478bd9Sstevel@tonic-gate 	   LDAPControl	**serverctrls,
667c478bd9Sstevel@tonic-gate 	   LDAPControl	**clientctrls,  /* not used for anything yet */
677c478bd9Sstevel@tonic-gate 	   int *msgidp
687c478bd9Sstevel@tonic-gate )
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	BerElement	*ber;
717c478bd9Sstevel@tonic-gate 	int		rc, err;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/*
747c478bd9Sstevel@tonic-gate 	 * A modify dn request looks like this:
757c478bd9Sstevel@tonic-gate 	 *	ModifyDNRequest ::= SEQUENCE {
767c478bd9Sstevel@tonic-gate 	 *		entry		LDAPDN,
777c478bd9Sstevel@tonic-gate 	 *		newrdn		RelativeLDAPDN,
787c478bd9Sstevel@tonic-gate 	 *              newparent       [0] LDAPDN OPTIONAL,
797c478bd9Sstevel@tonic-gate 	 *		deleteoldrdn	BOOLEAN
807c478bd9Sstevel@tonic-gate 	 *	}
817c478bd9Sstevel@tonic-gate 	 */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_rename\n", 0, 0, 0 );
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
867c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	if ( NULL == newrdn) {
897c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
907c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/* only ldapv3 or higher can do a proper rename
947c478bd9Sstevel@tonic-gate 	 * (i.e. with non-NULL newparent and/or controls)
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (( NSLDAPI_LDAP_VERSION( ld ) < LDAP_VERSION3 )
987c478bd9Sstevel@tonic-gate 	    && ((newparent != NULL) || (serverctrls != NULL)
997c478bd9Sstevel@tonic-gate 	    || (clientctrls != NULL))) {
1007c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_NOT_SUPPORTED, NULL, NULL );
1017c478bd9Sstevel@tonic-gate 		return( LDAP_NOT_SUPPORTED );
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if ( msgidp == NULL ) {
1057c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
1067c478bd9Sstevel@tonic-gate                 return( LDAP_PARAM_ERROR );
1077c478bd9Sstevel@tonic-gate         }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
1107c478bd9Sstevel@tonic-gate 	*msgidp = ++ld->ld_msgid;
1117c478bd9Sstevel@tonic-gate         LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/* see if modRDN or modDN is handled by the cache */
1147c478bd9Sstevel@tonic-gate  	if ( ld->ld_cache_on ) {
1157c478bd9Sstevel@tonic-gate 		if ( newparent == NULL && ld->ld_cache_modrdn != NULL ) {
1167c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
1177c478bd9Sstevel@tonic-gate 			if ( (rc = (ld->ld_cache_modrdn)( ld, *msgidp,
1187c478bd9Sstevel@tonic-gate 			    LDAP_REQ_MODRDN, dn, newrdn, deleteoldrdn ))
119*1da57d55SToomas Soome 			    != 0 ) {
1207c478bd9Sstevel@tonic-gate 				*msgidp = rc;
1217c478bd9Sstevel@tonic-gate 				LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1227c478bd9Sstevel@tonic-gate 				return( LDAP_SUCCESS );
1237c478bd9Sstevel@tonic-gate 			}
1247c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1257c478bd9Sstevel@tonic-gate #if 0
1267c478bd9Sstevel@tonic-gate 		} else if ( ld->ld_cache_rename != NULL ) {
1277c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
1287c478bd9Sstevel@tonic-gate 			if ( (rc = (ld->ld_cache_rename)( ld, *msgidp,
1297c478bd9Sstevel@tonic-gate 			    LDAP_REQ_MODDN, dn, newrdn, newparent,
130*1da57d55SToomas Soome 			    deleteoldrdn )) != 0 ) {
1317c478bd9Sstevel@tonic-gate 				*msgidp = rc;
1327c478bd9Sstevel@tonic-gate 				return( LDAP_SUCCESS );
1337c478bd9Sstevel@tonic-gate 			}
1347c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1357c478bd9Sstevel@tonic-gate #endif
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* create a message to send */
1407c478bd9Sstevel@tonic-gate 	if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
1417c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
1427c478bd9Sstevel@tonic-gate 		return( err );
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/* fill it in */
1467c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "{it{ssb", *msgidp, LDAP_REQ_MODDN, dn,
1477c478bd9Sstevel@tonic-gate 	    newrdn, deleteoldrdn ) == -1 ) {
1487c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
1497c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
1507c478bd9Sstevel@tonic-gate 		return( LDAP_ENCODING_ERROR );
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if ( newparent == NULL ) {
1547c478bd9Sstevel@tonic-gate 		if ( ber_printf( ber, "}" ) == -1 ) {
1557c478bd9Sstevel@tonic-gate 			LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
1567c478bd9Sstevel@tonic-gate 			ber_free( ber, 1 );
1577c478bd9Sstevel@tonic-gate 			return( LDAP_ENCODING_ERROR );
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	} else {
1607c478bd9Sstevel@tonic-gate 		if ( ber_printf( ber, "ts}", LDAP_TAG_NEWSUPERIOR, newparent )
1617c478bd9Sstevel@tonic-gate 		    == -1 ) {
1627c478bd9Sstevel@tonic-gate 			LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
1637c478bd9Sstevel@tonic-gate 			ber_free( ber, 1 );
1647c478bd9Sstevel@tonic-gate 			return( LDAP_ENCODING_ERROR );
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (( rc = nsldapi_put_controls( ld, serverctrls, 1, ber ))
1697c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
1707c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
1717c478bd9Sstevel@tonic-gate 		return( rc );
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/* send the message */
1757c478bd9Sstevel@tonic-gate 	rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_MODDN,
1767c478bd9Sstevel@tonic-gate 		(char *) dn, ber );
1777c478bd9Sstevel@tonic-gate 	*msgidp = rc;
1787c478bd9Sstevel@tonic-gate 	return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate int
1827c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_modrdn2(LDAP * ld,const char * dn,const char * newrdn,int deleteoldrdn)1837c478bd9Sstevel@tonic-gate ldap_modrdn2( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	int             msgid;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if ( ldap_rename( ld, dn, newrdn, NULL, deleteoldrdn, NULL, NULL, &msgid ) == LDAP_SUCCESS ) {
1887c478bd9Sstevel@tonic-gate 		return( msgid );
1897c478bd9Sstevel@tonic-gate 	} else {
1907c478bd9Sstevel@tonic-gate 		return( -1 );	/* error is in ld handle */
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate int
1957c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_modrdn(LDAP * ld,const char * dn,const char * newrdn)1967c478bd9Sstevel@tonic-gate ldap_modrdn( LDAP *ld, const char *dn, const char *newrdn )
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	return( ldap_modrdn2( ld, dn, newrdn, 1 ) );
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate int
2027c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_rename_s(LDAP * ld,const char * dn,const char * newrdn,const char * newparent,int deleteoldrdn,LDAPControl ** serverctrls,LDAPControl ** clientctrls)203*1da57d55SToomas Soome ldap_rename_s(
204*1da57d55SToomas Soome 	   LDAP *ld,
205*1da57d55SToomas Soome 	   const char *dn,
206*1da57d55SToomas Soome 	   const char *newrdn,
2077c478bd9Sstevel@tonic-gate 	   const char *newparent,
208*1da57d55SToomas Soome 	   int deleteoldrdn,
2097c478bd9Sstevel@tonic-gate 	   LDAPControl	**serverctrls,
2107c478bd9Sstevel@tonic-gate 	   LDAPControl	**clientctrls  /* not used for anything yet */
2117c478bd9Sstevel@tonic-gate )
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	int		msgid;
2147c478bd9Sstevel@tonic-gate 	LDAPMessage	*res;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if ( ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid ) != LDAP_SUCCESS ) {
2177c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
220*1da57d55SToomas Soome  	if ( msgid == -1 )
2217c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
2247c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	return( ldap_result2error( ld, res, 1 ) );
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate int
2307c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_modrdn2_s(LDAP * ld,const char * dn,const char * newrdn,int deleteoldrdn)2317c478bd9Sstevel@tonic-gate ldap_modrdn2_s( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate         int             msgid;
2347c478bd9Sstevel@tonic-gate         LDAPMessage     *res;
235*1da57d55SToomas Soome 
2367c478bd9Sstevel@tonic-gate         if ( (msgid = ldap_modrdn2( ld, dn, newrdn, deleteoldrdn )) == -1 )
2377c478bd9Sstevel@tonic-gate                 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
238*1da57d55SToomas Soome 
2397c478bd9Sstevel@tonic-gate         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
2407c478bd9Sstevel@tonic-gate                 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
241*1da57d55SToomas Soome 
2427c478bd9Sstevel@tonic-gate         return( ldap_result2error( ld, res, 1 ) );
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate int
2467c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_modrdn_s(LDAP * ld,const char * dn,const char * newrdn)2477c478bd9Sstevel@tonic-gate ldap_modrdn_s( LDAP *ld, const char *dn, const char *newrdn )
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	return( ldap_modrdn2_s( ld, dn, newrdn, 1 ) );
2507c478bd9Sstevel@tonic-gate }
251