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  *  Copyright (c) 1990 Regents of the University of Michigan.
11*0Sstevel@tonic-gate  *  All rights reserved.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  *  delete.c
14*0Sstevel@tonic-gate  */
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate #ifndef lint
17*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
18*0Sstevel@tonic-gate #endif
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate #include <stdio.h>
21*0Sstevel@tonic-gate #include <string.h>
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #ifdef MACOS
24*0Sstevel@tonic-gate #include "macos.h"
25*0Sstevel@tonic-gate #endif /* MACOS */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
28*0Sstevel@tonic-gate #include "msdos.h"
29*0Sstevel@tonic-gate #endif /* DOS */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/socket.h>
34*0Sstevel@tonic-gate #endif
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include "lber.h"
37*0Sstevel@tonic-gate #include "ldap.h"
38*0Sstevel@tonic-gate #include "ldap-private.h"
39*0Sstevel@tonic-gate #include "ldap-int.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate BerElement * ldap_build_delete_req(LDAP *ld, char *dn, LDAPControl **serverctrls)
42*0Sstevel@tonic-gate {
43*0Sstevel@tonic-gate 	BerElement	*ber;
44*0Sstevel@tonic-gate 	int rv;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	/* create a message to send */
47*0Sstevel@tonic-gate 	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
48*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_NO_MEMORY;
49*0Sstevel@tonic-gate 		return(NULLBER );
50*0Sstevel@tonic-gate 	}
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	if ( ber_printf( ber, "{its", ++ld->ld_msgid, LDAP_REQ_DELETE, dn ) == -1 ) {
53*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
54*0Sstevel@tonic-gate 		ber_free( ber, 1 );
55*0Sstevel@tonic-gate 		return(NULLBER );
56*0Sstevel@tonic-gate 	}
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	/* LDAPv3 */
59*0Sstevel@tonic-gate 	/* Code controls if any */
60*0Sstevel@tonic-gate 	if (serverctrls && serverctrls[0]) {
61*0Sstevel@tonic-gate 		if (ldap_controls_code(ber, serverctrls) != LDAP_SUCCESS){
62*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
63*0Sstevel@tonic-gate 			ber_free( ber, 1 );
64*0Sstevel@tonic-gate 			return( NULLBER );
65*0Sstevel@tonic-gate 		}
66*0Sstevel@tonic-gate 	} else if (ld->ld_srvctrls && ld->ld_srvctrls[0]) {
67*0Sstevel@tonic-gate 		/* Otherwise, is there any global server ctrls ? */
68*0Sstevel@tonic-gate 		if (ldap_controls_code(ber, ld->ld_srvctrls) != LDAP_SUCCESS){
69*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
70*0Sstevel@tonic-gate 			ber_free( ber, 1 );
71*0Sstevel@tonic-gate 			return( NULLBER );
72*0Sstevel@tonic-gate 		}
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if ( ber_printf( ber, "}" ) == -1 ) {
76*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
77*0Sstevel@tonic-gate 		ber_free( ber, 1 );
78*0Sstevel@tonic-gate 		return( NULLBER );
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	return (ber);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate /*
85*0Sstevel@tonic-gate  * ldap_delete - initiate an ldap (and X.500) delete operation. Parameters:
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  *	ld		LDAP descriptor
88*0Sstevel@tonic-gate  *	dn		DN of the object to delete
89*0Sstevel@tonic-gate  *
90*0Sstevel@tonic-gate  * Example:
91*0Sstevel@tonic-gate  *	msgid = ldap_delete( ld, dn );
92*0Sstevel@tonic-gate  */
93*0Sstevel@tonic-gate int
94*0Sstevel@tonic-gate ldap_delete( LDAP *ld, char *dn )
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	BerElement	*ber;
97*0Sstevel@tonic-gate 	int rv;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/*
100*0Sstevel@tonic-gate 	 * A delete request looks like this:
101*0Sstevel@tonic-gate 	 *	DelRequet ::= DistinguishedName,
102*0Sstevel@tonic-gate 	 */
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 129, "ldap_delete\n"), 0, 0, 0 );
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate #ifdef _REENTRANT
107*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
108*0Sstevel@tonic-gate #endif
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	if (( ber = ldap_build_delete_req(ld, dn, NULL)) == NULLBER) {
111*0Sstevel@tonic-gate #ifdef _REENTRANT
112*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
113*0Sstevel@tonic-gate #endif
114*0Sstevel@tonic-gate 		return (-1);
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	/* send the message */
118*0Sstevel@tonic-gate 	rv = send_initial_request( ld, LDAP_REQ_DELETE, dn, ber );
119*0Sstevel@tonic-gate #ifdef _REENTRANT
120*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
121*0Sstevel@tonic-gate #endif
122*0Sstevel@tonic-gate 	return ( rv );
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate int
127*0Sstevel@tonic-gate ldap_delete_s( LDAP *ld, char *dn )
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate 	int		msgid;
130*0Sstevel@tonic-gate 	LDAPMessage	*res;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if ( (msgid = ldap_delete( ld, dn )) == -1 )
133*0Sstevel@tonic-gate 		return( ld->ld_errno );
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
136*0Sstevel@tonic-gate 		return( ld->ld_errno );
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	return( ldap_result2error( ld, res, 1 ) );
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /* ldapv3 API extensions */
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate int ldap_delete_ext(LDAP *ld, char *dn, LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	BerElement	*ber;
146*0Sstevel@tonic-gate 	int rv;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate #ifdef _REENTRANT
149*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
150*0Sstevel@tonic-gate #endif
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 192, "ldap_modify\n"), 0, 0, 0 );
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	if ((ber = ldap_build_delete_req(ld, dn, serverctrls)) == NULLBER){
155*0Sstevel@tonic-gate 		rv = ld->ld_errno;
156*0Sstevel@tonic-gate 		if (rv == LDAP_SUCCESS)
157*0Sstevel@tonic-gate 			rv = LDAP_OTHER;
158*0Sstevel@tonic-gate #ifdef _REENTRANT
159*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
160*0Sstevel@tonic-gate #endif
161*0Sstevel@tonic-gate 		return (rv);
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	/* send the message */
165*0Sstevel@tonic-gate 	rv =  send_initial_request( ld, LDAP_REQ_DELETE, dn, ber );
166*0Sstevel@tonic-gate 	if (rv == -1){
167*0Sstevel@tonic-gate 		rv = ld->ld_errno;
168*0Sstevel@tonic-gate 		if (rv == LDAP_SUCCESS){
169*0Sstevel@tonic-gate 			rv = LDAP_OTHER;
170*0Sstevel@tonic-gate 		}
171*0Sstevel@tonic-gate #ifdef _REENTRANT
172*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
173*0Sstevel@tonic-gate #endif
174*0Sstevel@tonic-gate 		return (rv);
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 	*msgidp = rv;
177*0Sstevel@tonic-gate #ifdef _REENTRANT
178*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
179*0Sstevel@tonic-gate #endif
180*0Sstevel@tonic-gate 	return ( LDAP_SUCCESS );
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate int ldap_delete_ext_s(LDAP *ld, char *dn, LDAPControl **serverctrls, LDAPControl **clientctrls)
184*0Sstevel@tonic-gate {
185*0Sstevel@tonic-gate 	int msgid;
186*0Sstevel@tonic-gate 	int retcode = LDAP_SUCCESS;
187*0Sstevel@tonic-gate 	LDAPMessage *res;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if ((retcode = ldap_delete_ext(ld, dn, serverctrls, clientctrls, &msgid)) != LDAP_SUCCESS)
190*0Sstevel@tonic-gate 		return (retcode);
191*0Sstevel@tonic-gate 	if (ldap_result(ld, msgid, 1, (struct timeval *)NULL, &res ) == -1)
192*0Sstevel@tonic-gate 		return (ld->ld_errno );
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate #ifdef  _REENTRANT
195*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
196*0Sstevel@tonic-gate #endif
197*0Sstevel@tonic-gate 	retcode = ldap_parse_result( ld, res,  &ld->ld_errno, &ld->ld_matched, &ld->ld_error,
198*0Sstevel@tonic-gate 								 &ld->ld_referrals, &ld->ld_ret_ctrls, 1);
199*0Sstevel@tonic-gate 	if (retcode == LDAP_SUCCESS)
200*0Sstevel@tonic-gate 		retcode = ld->ld_errno;
201*0Sstevel@tonic-gate #ifdef  _REENTRANT
202*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
203*0Sstevel@tonic-gate #endif
204*0Sstevel@tonic-gate 	return (retcode);
205*0Sstevel@tonic-gate }
206