1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  * Portions Copyright %G% Sun Microsystems, Inc.
4*0Sstevel@tonic-gate  * All Rights Reserved
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate /*
11*0Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
12*0Sstevel@tonic-gate  *  All rights reserved.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  *  modrdn.c
15*0Sstevel@tonic-gate  */
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #ifndef lint
18*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
19*0Sstevel@tonic-gate #endif
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #include <stdio.h>
22*0Sstevel@tonic-gate #include <string.h>
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate #ifdef MACOS
25*0Sstevel@tonic-gate #include "macos.h"
26*0Sstevel@tonic-gate #endif /* MACOS */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/socket.h>
31*0Sstevel@tonic-gate #endif
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "lber.h"
34*0Sstevel@tonic-gate #include "ldap.h"
35*0Sstevel@tonic-gate #include "ldap-private.h"
36*0Sstevel@tonic-gate #include "ldap-int.h"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * ldap_modrdn - initiate an ldap (and X.500) modifyRDN operation. Parameters:
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  *	ld		LDAP descriptor
42*0Sstevel@tonic-gate  *	dn		DN of the object to modify
43*0Sstevel@tonic-gate  *	newrdn		RDN to give the object
44*0Sstevel@tonic-gate  *	deleteoldrdn	nonzero means to delete old rdn values from the entry
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  * Example:
47*0Sstevel@tonic-gate  *	msgid = ldap_modrdn( ld, dn, newrdn );
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate int
50*0Sstevel@tonic-gate ldap_modrdn( LDAP *ld, char *dn, char *newrdn, int deleteoldrdn )
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate 	BerElement	*ber;
53*0Sstevel@tonic-gate 	int rv;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	/*
56*0Sstevel@tonic-gate 	 * A modify rdn request looks like this:
57*0Sstevel@tonic-gate 	 *	ModifyRDNRequest ::= SEQUENCE {
58*0Sstevel@tonic-gate 	 *		entry		DistinguishedName,
59*0Sstevel@tonic-gate 	 *		newrdn		RelativeDistinguishedName,
60*0Sstevel@tonic-gate 	 *		deleteoldrdn	BOOLEAN
61*0Sstevel@tonic-gate 	 *	}
62*0Sstevel@tonic-gate 	 */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate #ifdef _REENTRANT
65*0Sstevel@tonic-gate         LOCK_LDAP(ld);
66*0Sstevel@tonic-gate #endif
67*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 193, "ldap_modrdn\n"), 0, 0, 0 );
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	/* create a message to send */
70*0Sstevel@tonic-gate 	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
71*0Sstevel@tonic-gate #ifdef _REENTRANT
72*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
73*0Sstevel@tonic-gate #endif
74*0Sstevel@tonic-gate 		return( -1 );
75*0Sstevel@tonic-gate 	}
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	if ( ber_printf( ber, "{it{ssb}}", ++ld->ld_msgid, LDAP_REQ_MODRDN, dn,
78*0Sstevel@tonic-gate 	    newrdn, deleteoldrdn ) == -1 ) {
79*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
80*0Sstevel@tonic-gate 		ber_free( ber, 1 );
81*0Sstevel@tonic-gate #ifdef _REENTRANT
82*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
83*0Sstevel@tonic-gate #endif
84*0Sstevel@tonic-gate 		return( -1 );
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	/* send the message */
88*0Sstevel@tonic-gate 	rv = send_initial_request( ld, LDAP_REQ_MODRDN, dn, ber );
89*0Sstevel@tonic-gate #ifdef _REENTRANT
90*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
91*0Sstevel@tonic-gate #endif
92*0Sstevel@tonic-gate 	return ( rv );
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate int
96*0Sstevel@tonic-gate ldap_modrdn0( LDAP *ld, char *dn, char *newrdn )
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	return( ldap_modrdn( ld, dn, newrdn, 1 ) );
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate int
102*0Sstevel@tonic-gate ldap_modrdn_s( LDAP *ld, char *dn, char *newrdn, int deleteoldrdn )
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	int		msgid;
105*0Sstevel@tonic-gate 	LDAPMessage	*res;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	if ( (msgid = ldap_modrdn( ld, dn, newrdn, deleteoldrdn )) == -1 )
108*0Sstevel@tonic-gate 		return( ld->ld_errno );
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
111*0Sstevel@tonic-gate 		return( ld->ld_errno );
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	return( ldap_result2error( ld, res, 1 ) );
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate int
117*0Sstevel@tonic-gate ldap_modrdn0_s( LDAP *ld, char *dn, char *newrdn )
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	return( ldap_modrdn_s( ld, dn, newrdn, 1 ) );
120*0Sstevel@tonic-gate }
121