Home | History | Annotate | Line # | Download | only in libldap
      1 /*	$NetBSD: delete.c,v 1.4 2025/09/05 21:16:21 christos Exp $	*/
      2 
      3 /* $OpenLDAP$ */
      4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5  *
      6  * Copyright 1998-2024 The OpenLDAP Foundation.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted only as authorized by the OpenLDAP
     11  * Public License.
     12  *
     13  * A copy of this license is available in the file LICENSE in the
     14  * top-level directory of the distribution or, alternatively, at
     15  * <http://www.OpenLDAP.org/license.html>.
     16  */
     17 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
     18  * All rights reserved.
     19  */
     20 
     21 #include <sys/cdefs.h>
     22 __RCSID("$NetBSD: delete.c,v 1.4 2025/09/05 21:16:21 christos Exp $");
     23 
     24 #include "portable.h"
     25 
     26 #include <stdio.h>
     27 
     28 #include <ac/socket.h>
     29 #include <ac/string.h>
     30 #include <ac/time.h>
     31 
     32 #include "ldap-int.h"
     33 
     34 /*
     35  * A delete request looks like this:
     36  *	DelRequest ::= DistinguishedName,
     37  */
     38 
     39 BerElement *
     40 ldap_build_delete_req(
     41 	LDAP *ld,
     42 	LDAP_CONST char *dn,
     43 	LDAPControl **sctrls,
     44 	LDAPControl **cctrls,
     45 	int	*msgidp )
     46 {
     47 	BerElement	*ber;
     48 	int rc;
     49 
     50 	/* create a message to send */
     51 	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
     52 		return( NULL );
     53 	}
     54 
     55 	LDAP_NEXT_MSGID( ld, *msgidp );
     56 	rc = ber_printf( ber, "{its", /* '}' */
     57 		*msgidp, LDAP_REQ_DELETE, dn );
     58 	if ( rc == -1 )
     59 	{
     60 		ld->ld_errno = LDAP_ENCODING_ERROR;
     61 		ber_free( ber, 1 );
     62 		return( NULL );
     63 	}
     64 
     65 	/* Put Server Controls */
     66 	if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
     67 		ber_free( ber, 1 );
     68 		return( NULL );
     69 	}
     70 
     71 	if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
     72 		ld->ld_errno = LDAP_ENCODING_ERROR;
     73 		ber_free( ber, 1 );
     74 		return( NULL );
     75 	}
     76 
     77 	return( ber );
     78 }
     79 
     80 /*
     81  * ldap_delete_ext - initiate an ldap extended delete operation. Parameters:
     82  *
     83  *	ld		LDAP descriptor
     84  *	dn		DN of the object to delete
     85  *	sctrls	Server Controls
     86  *	cctrls	Client Controls
     87  *	msgidp	Message Id Pointer
     88  *
     89  * Example:
     90  *	rc = ldap_delete( ld, dn, sctrls, cctrls, msgidp );
     91  */
     92 int
     93 ldap_delete_ext(
     94 	LDAP *ld,
     95 	LDAP_CONST char* dn,
     96 	LDAPControl **sctrls,
     97 	LDAPControl **cctrls,
     98 	int *msgidp )
     99 {
    100 	int rc;
    101 	BerElement	*ber;
    102 	ber_int_t	id;
    103 
    104 	Debug0( LDAP_DEBUG_TRACE, "ldap_delete_ext\n" );
    105 
    106 	assert( ld != NULL );
    107 	assert( LDAP_VALID( ld ) );
    108 	assert( dn != NULL );
    109 	assert( msgidp != NULL );
    110 
    111 	/* check client controls */
    112 	rc = ldap_int_client_controls( ld, cctrls );
    113 	if( rc != LDAP_SUCCESS ) return rc;
    114 
    115 	ber = ldap_build_delete_req( ld, dn, sctrls, cctrls, &id );
    116 	if( !ber )
    117 		return ld->ld_errno;
    118 
    119 	/* send the message */
    120 	*msgidp = ldap_send_initial_request( ld, LDAP_REQ_DELETE, dn, ber, id );
    121 
    122 	if(*msgidp < 0)
    123 		return ld->ld_errno;
    124 
    125 	return LDAP_SUCCESS;
    126 }
    127 
    128 int
    129 ldap_delete_ext_s(
    130 	LDAP *ld,
    131 	LDAP_CONST char *dn,
    132 	LDAPControl **sctrls,
    133 	LDAPControl **cctrls )
    134 {
    135 	int	msgid;
    136 	int rc;
    137 	LDAPMessage	*res;
    138 
    139 	rc = ldap_delete_ext( ld, dn, sctrls, cctrls, &msgid );
    140 
    141 	if( rc != LDAP_SUCCESS )
    142 		return( ld->ld_errno );
    143 
    144 	if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
    145 		return( ld->ld_errno );
    146 
    147 	return( ldap_result2error( ld, res, 1 ) );
    148 }
    149 /*
    150  * ldap_delete - initiate an ldap (and X.500) delete operation. Parameters:
    151  *
    152  *	ld		LDAP descriptor
    153  *	dn		DN of the object to delete
    154  *
    155  * Example:
    156  *	msgid = ldap_delete( ld, dn );
    157  */
    158 int
    159 ldap_delete( LDAP *ld, LDAP_CONST char *dn )
    160 {
    161 	int msgid;
    162 
    163 	/*
    164 	 * A delete request looks like this:
    165 	 *	DelRequest ::= DistinguishedName,
    166 	 */
    167 
    168 	Debug0( LDAP_DEBUG_TRACE, "ldap_delete\n" );
    169 
    170 	return ldap_delete_ext( ld, dn, NULL, NULL, &msgid ) == LDAP_SUCCESS
    171 		? msgid : -1 ;
    172 }
    173 
    174 
    175 int
    176 ldap_delete_s( LDAP *ld, LDAP_CONST char *dn )
    177 {
    178 	return ldap_delete_ext_s( ld, dn, NULL, NULL );
    179 }
    180