Back to index

openldap  2.4.31
delete.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
00016  * All rights reserved.
00017  */
00018 
00019 #include "portable.h"
00020 
00021 #include <stdio.h>
00022 
00023 #include <ac/socket.h>
00024 #include <ac/string.h>
00025 #include <ac/time.h>
00026 
00027 #include "ldap-int.h"
00028 
00029 /*
00030  * A delete request looks like this:
00031  *     DelRequet ::= DistinguishedName,
00032  */
00033 
00034 
00035 /*
00036  * ldap_delete_ext - initiate an ldap extended delete operation. Parameters:
00037  *
00038  *     ld            LDAP descriptor
00039  *     dn            DN of the object to delete
00040  *     sctrls Server Controls
00041  *     cctrls Client Controls
00042  *     msgidp Message Id Pointer
00043  *
00044  * Example:
00045  *     rc = ldap_delete( ld, dn, sctrls, cctrls, msgidp );
00046  */
00047 int
00048 ldap_delete_ext(
00049        LDAP *ld,
00050        LDAP_CONST char* dn,
00051        LDAPControl **sctrls,
00052        LDAPControl **cctrls,
00053        int *msgidp )
00054 {
00055        int rc;
00056        BerElement    *ber;
00057        ber_int_t     id;
00058 
00059        Debug( LDAP_DEBUG_TRACE, "ldap_delete_ext\n", 0, 0, 0 );
00060 
00061        assert( ld != NULL );
00062        assert( LDAP_VALID( ld ) );
00063        assert( dn != NULL );
00064        assert( msgidp != NULL );
00065 
00066        /* check client controls */
00067        rc = ldap_int_client_controls( ld, cctrls );
00068        if( rc != LDAP_SUCCESS ) return rc;
00069 
00070        /* create a message to send */
00071        if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
00072               ld->ld_errno = LDAP_NO_MEMORY;
00073               return( ld->ld_errno );
00074        }
00075 
00076        LDAP_NEXT_MSGID( ld, id );
00077        rc = ber_printf( ber, "{its", /* '}' */
00078               id, LDAP_REQ_DELETE, dn );
00079        if ( rc == -1 )
00080        {
00081               ld->ld_errno = LDAP_ENCODING_ERROR;
00082               ber_free( ber, 1 );
00083               return( ld->ld_errno );
00084        }
00085 
00086        /* Put Server Controls */
00087        if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
00088               ber_free( ber, 1 );
00089               return ld->ld_errno;
00090        }
00091 
00092        if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
00093               ld->ld_errno = LDAP_ENCODING_ERROR;
00094               ber_free( ber, 1 );
00095               return( ld->ld_errno );
00096        }
00097 
00098        /* send the message */
00099        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_DELETE, dn, ber, id );
00100 
00101        if(*msgidp < 0)
00102               return ld->ld_errno;
00103 
00104        return LDAP_SUCCESS;
00105 }
00106 
00107 int
00108 ldap_delete_ext_s(
00109        LDAP *ld,
00110        LDAP_CONST char *dn,
00111        LDAPControl **sctrls,
00112        LDAPControl **cctrls )
00113 {
00114        int    msgid;
00115        int rc;
00116        LDAPMessage   *res;
00117 
00118        rc = ldap_delete_ext( ld, dn, sctrls, cctrls, &msgid );
00119        
00120        if( rc != LDAP_SUCCESS )
00121               return( ld->ld_errno );
00122 
00123        if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
00124               return( ld->ld_errno );
00125 
00126        return( ldap_result2error( ld, res, 1 ) );
00127 }
00128 /*
00129  * ldap_delete - initiate an ldap (and X.500) delete operation. Parameters:
00130  *
00131  *     ld            LDAP descriptor
00132  *     dn            DN of the object to delete
00133  *
00134  * Example:
00135  *     msgid = ldap_delete( ld, dn );
00136  */
00137 int
00138 ldap_delete( LDAP *ld, LDAP_CONST char *dn )
00139 {
00140        int msgid;
00141 
00142        /*
00143         * A delete request looks like this:
00144         *     DelRequet ::= DistinguishedName,
00145         */
00146 
00147        Debug( LDAP_DEBUG_TRACE, "ldap_delete\n", 0, 0, 0 );
00148 
00149        return ldap_delete_ext( ld, dn, NULL, NULL, &msgid ) == LDAP_SUCCESS
00150               ? msgid : -1 ;
00151 }
00152 
00153 
00154 int
00155 ldap_delete_s( LDAP *ld, LDAP_CONST char *dn )
00156 {
00157        return ldap_delete_ext_s( ld, dn, NULL, NULL );
00158 }