Back to index

lightning-sunbird  0.9+nobinonly
delete.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  delete.c
00039  */
00040 
00041 #if 0
00042 #ifndef lint 
00043 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
00044 #endif
00045 #endif
00046 
00047 #include "ldap-int.h"
00048 
00049 /*
00050  * ldap_delete - initiate an ldap delete operation. Parameters:
00051  *
00052  *     ld            LDAP descriptor
00053  *     dn            DN of the object to delete
00054  *
00055  * Example:
00056  *     msgid = ldap_delete( ld, dn );
00057  */
00058 int
00059 LDAP_CALL
00060 ldap_delete( LDAP *ld, const char *dn )
00061 {
00062        int    msgid;
00063 
00064        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_delete\n", 0, 0, 0 );
00065 
00066        if ( ldap_delete_ext( ld, dn, NULL, NULL, &msgid ) == LDAP_SUCCESS ) {
00067               return( msgid );
00068        } else {
00069               return( -1 ); /* error is in ld handle */
00070        }
00071 }
00072 
00073 int
00074 LDAP_CALL
00075 ldap_delete_ext( LDAP *ld, const char *dn, LDAPControl **serverctrls,
00076     LDAPControl **clientctrls, int *msgidp )
00077 {
00078        BerElement    *ber;
00079        int           rc, lderr;
00080 
00081        /*
00082         * A delete request looks like this:
00083         *     DelRequet ::= DistinguishedName,
00084         */
00085 
00086        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_delete_ext\n", 0, 0, 0 );
00087 
00088        if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00089               return( LDAP_PARAM_ERROR );
00090        }
00091 
00092        if ( !NSLDAPI_VALID_LDAPMESSAGE_POINTER( msgidp )) 
00093         {
00094               LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
00095               return( LDAP_PARAM_ERROR );
00096        }
00097        if ( dn == NULL ) {
00098               dn = "";
00099        }
00100 
00101        LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
00102        *msgidp = ++ld->ld_msgid;
00103        LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
00104 
00105        /* see if we should add to the cache */
00106        if ( ld->ld_cache_on && ld->ld_cache_delete != NULL ) {
00107               LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
00108               if ( (rc = (ld->ld_cache_delete)( ld, *msgidp, LDAP_REQ_DELETE,
00109                   dn )) != 0 ) {
00110                      *msgidp = rc;
00111                      LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00112                      return( LDAP_SUCCESS );
00113               }
00114               LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00115        }
00116 
00117        /* create a message to send */
00118        if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber ))
00119            != LDAP_SUCCESS ) {
00120               return( lderr );
00121        }
00122 
00123        if ( ber_printf( ber, "{its", *msgidp, LDAP_REQ_DELETE, dn )
00124            == -1 ) {
00125               lderr = LDAP_ENCODING_ERROR;
00126               LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
00127               ber_free( ber, 1 );
00128               return( lderr );
00129        }
00130 
00131        if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber ))
00132            != LDAP_SUCCESS ) {
00133               ber_free( ber, 1 );
00134               return( lderr );
00135        }
00136 
00137        /* send the message */
00138        rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_DELETE,
00139               (char *)dn, ber );
00140        *msgidp = rc;
00141        return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
00142 }
00143 
00144 int
00145 LDAP_CALL
00146 ldap_delete_s( LDAP *ld, const char *dn )
00147 {
00148        return( ldap_delete_ext_s( ld, dn, NULL, NULL ));
00149 }
00150 
00151 int
00152 LDAP_CALL
00153 ldap_delete_ext_s( LDAP *ld, const char *dn, LDAPControl **serverctrls,
00154     LDAPControl **clientctrls )
00155 {
00156        int           err, msgid;
00157        LDAPMessage   *res;
00158 
00159        if (( err = ldap_delete_ext( ld, dn, serverctrls, clientctrls,
00160            &msgid )) != LDAP_SUCCESS ) {
00161               return( err );
00162        }
00163 
00164        if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res ) == -1 ) {
00165               return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00166        }
00167 
00168        return( ldap_result2error( ld, res, 1 ) );
00169 }