delete.c 3.5 KB

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