xref: /onnv-gate/usr/src/lib/libldap4/common/delete.c (revision 3857:21b9b714e4ab)
10Sstevel@tonic-gate /*
2*3857Sstevel  * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
3*3857Sstevel  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
90Sstevel@tonic-gate  *  All rights reserved.
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  *  delete.c
120Sstevel@tonic-gate  */
130Sstevel@tonic-gate 
140Sstevel@tonic-gate #ifndef lint
150Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
160Sstevel@tonic-gate #endif
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #include <stdio.h>
190Sstevel@tonic-gate #include <string.h>
200Sstevel@tonic-gate 
210Sstevel@tonic-gate #ifdef MACOS
220Sstevel@tonic-gate #include "macos.h"
230Sstevel@tonic-gate #endif /* MACOS */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
260Sstevel@tonic-gate #include "msdos.h"
270Sstevel@tonic-gate #endif /* DOS */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #endif
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "lber.h"
350Sstevel@tonic-gate #include "ldap.h"
360Sstevel@tonic-gate #include "ldap-private.h"
370Sstevel@tonic-gate #include "ldap-int.h"
380Sstevel@tonic-gate 
ldap_build_delete_req(LDAP * ld,char * dn,LDAPControl ** serverctrls)390Sstevel@tonic-gate BerElement * ldap_build_delete_req(LDAP *ld, char *dn, LDAPControl **serverctrls)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate 	BerElement	*ber;
420Sstevel@tonic-gate 	int rv;
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 	/* create a message to send */
450Sstevel@tonic-gate 	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
460Sstevel@tonic-gate 		ld->ld_errno = LDAP_NO_MEMORY;
470Sstevel@tonic-gate 		return(NULLBER );
480Sstevel@tonic-gate 	}
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	if ( ber_printf( ber, "{its", ++ld->ld_msgid, LDAP_REQ_DELETE, dn ) == -1 ) {
510Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
520Sstevel@tonic-gate 		ber_free( ber, 1 );
530Sstevel@tonic-gate 		return(NULLBER );
540Sstevel@tonic-gate 	}
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	/* LDAPv3 */
570Sstevel@tonic-gate 	/* Code controls if any */
580Sstevel@tonic-gate 	if (serverctrls && serverctrls[0]) {
590Sstevel@tonic-gate 		if (ldap_controls_code(ber, serverctrls) != LDAP_SUCCESS){
600Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
610Sstevel@tonic-gate 			ber_free( ber, 1 );
620Sstevel@tonic-gate 			return( NULLBER );
630Sstevel@tonic-gate 		}
640Sstevel@tonic-gate 	} else if (ld->ld_srvctrls && ld->ld_srvctrls[0]) {
650Sstevel@tonic-gate 		/* Otherwise, is there any global server ctrls ? */
660Sstevel@tonic-gate 		if (ldap_controls_code(ber, ld->ld_srvctrls) != LDAP_SUCCESS){
670Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
680Sstevel@tonic-gate 			ber_free( ber, 1 );
690Sstevel@tonic-gate 			return( NULLBER );
700Sstevel@tonic-gate 		}
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if ( ber_printf( ber, "}" ) == -1 ) {
740Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
750Sstevel@tonic-gate 		ber_free( ber, 1 );
760Sstevel@tonic-gate 		return( NULLBER );
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	return (ber);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * ldap_delete - initiate an ldap (and X.500) delete operation. Parameters:
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  *	ld		LDAP descriptor
860Sstevel@tonic-gate  *	dn		DN of the object to delete
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * Example:
890Sstevel@tonic-gate  *	msgid = ldap_delete( ld, dn );
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate int
ldap_delete(LDAP * ld,char * dn)920Sstevel@tonic-gate ldap_delete( LDAP *ld, char *dn )
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	BerElement	*ber;
950Sstevel@tonic-gate 	int rv;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	/*
980Sstevel@tonic-gate 	 * A delete request looks like this:
990Sstevel@tonic-gate 	 *	DelRequet ::= DistinguishedName,
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 129, "ldap_delete\n"), 0, 0, 0 );
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #ifdef _REENTRANT
1050Sstevel@tonic-gate 	LOCK_LDAP(ld);
1060Sstevel@tonic-gate #endif
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (( ber = ldap_build_delete_req(ld, dn, NULL)) == NULLBER) {
1090Sstevel@tonic-gate #ifdef _REENTRANT
1100Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate 		return (-1);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/* send the message */
1160Sstevel@tonic-gate 	rv = send_initial_request( ld, LDAP_REQ_DELETE, dn, ber );
1170Sstevel@tonic-gate #ifdef _REENTRANT
1180Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
1190Sstevel@tonic-gate #endif
1200Sstevel@tonic-gate 	return ( rv );
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
ldap_delete_s(LDAP * ld,char * dn)1250Sstevel@tonic-gate ldap_delete_s( LDAP *ld, char *dn )
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	int		msgid;
1280Sstevel@tonic-gate 	LDAPMessage	*res;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if ( (msgid = ldap_delete( ld, dn )) == -1 )
1310Sstevel@tonic-gate 		return( ld->ld_errno );
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
1340Sstevel@tonic-gate 		return( ld->ld_errno );
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	return( ldap_result2error( ld, res, 1 ) );
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /* ldapv3 API extensions */
1400Sstevel@tonic-gate 
ldap_delete_ext(LDAP * ld,char * dn,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int * msgidp)1410Sstevel@tonic-gate int ldap_delete_ext(LDAP *ld, char *dn, LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	BerElement	*ber;
1440Sstevel@tonic-gate 	int rv;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate #ifdef _REENTRANT
1470Sstevel@tonic-gate 	LOCK_LDAP(ld);
1480Sstevel@tonic-gate #endif
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 192, "ldap_modify\n"), 0, 0, 0 );
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if ((ber = ldap_build_delete_req(ld, dn, serverctrls)) == NULLBER){
1530Sstevel@tonic-gate 		rv = ld->ld_errno;
1540Sstevel@tonic-gate 		if (rv == LDAP_SUCCESS)
1550Sstevel@tonic-gate 			rv = LDAP_OTHER;
1560Sstevel@tonic-gate #ifdef _REENTRANT
1570Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1580Sstevel@tonic-gate #endif
1590Sstevel@tonic-gate 		return (rv);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/* send the message */
1630Sstevel@tonic-gate 	rv =  send_initial_request( ld, LDAP_REQ_DELETE, dn, ber );
1640Sstevel@tonic-gate 	if (rv == -1){
1650Sstevel@tonic-gate 		rv = ld->ld_errno;
1660Sstevel@tonic-gate 		if (rv == LDAP_SUCCESS){
1670Sstevel@tonic-gate 			rv = LDAP_OTHER;
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate #ifdef _REENTRANT
1700Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate 		return (rv);
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 	*msgidp = rv;
1750Sstevel@tonic-gate #ifdef _REENTRANT
1760Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
1770Sstevel@tonic-gate #endif
1780Sstevel@tonic-gate 	return ( LDAP_SUCCESS );
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
ldap_delete_ext_s(LDAP * ld,char * dn,LDAPControl ** serverctrls,LDAPControl ** clientctrls)1810Sstevel@tonic-gate int ldap_delete_ext_s(LDAP *ld, char *dn, LDAPControl **serverctrls, LDAPControl **clientctrls)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	int msgid;
1840Sstevel@tonic-gate 	int retcode = LDAP_SUCCESS;
1850Sstevel@tonic-gate 	LDAPMessage *res;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if ((retcode = ldap_delete_ext(ld, dn, serverctrls, clientctrls, &msgid)) != LDAP_SUCCESS)
1880Sstevel@tonic-gate 		return (retcode);
1890Sstevel@tonic-gate 	if (ldap_result(ld, msgid, 1, (struct timeval *)NULL, &res ) == -1)
1900Sstevel@tonic-gate 		return (ld->ld_errno );
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate #ifdef  _REENTRANT
1930Sstevel@tonic-gate 	LOCK_LDAP(ld);
1940Sstevel@tonic-gate #endif
1950Sstevel@tonic-gate 	retcode = ldap_parse_result( ld, res,  &ld->ld_errno, &ld->ld_matched, &ld->ld_error,
1960Sstevel@tonic-gate 								 &ld->ld_referrals, &ld->ld_ret_ctrls, 1);
1970Sstevel@tonic-gate 	if (retcode == LDAP_SUCCESS)
1980Sstevel@tonic-gate 		retcode = ld->ld_errno;
1990Sstevel@tonic-gate #ifdef  _REENTRANT
2000Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
2010Sstevel@tonic-gate #endif
2020Sstevel@tonic-gate 	return (retcode);
2030Sstevel@tonic-gate }
204