1 /*
2  * The contents of this file are subject to the Netscape Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/NPL/
6  *
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  *
12  * The Original Code is Mozilla Communicator client code, released
13  * March 31, 1998.
14  *
15  * The Initial Developer of the Original Code is Netscape
16  * Communications Corporation. Portions created by Netscape are
17  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
18  * Rights Reserved.
19  *
20  * Contributor(s):
21  */
22 /*
23  *  delete.c
24  */
25 
26 #if 0
27 #ifndef lint
28 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
29 #endif
30 #endif
31 
32 #include "ldap-int.h"
33 
34 /*
35  * ldap_delete - initiate an ldap delete operation. Parameters:
36  *
37  *	ld		LDAP descriptor
38  *	dn		DN of the object to delete
39  *
40  * Example:
41  *	msgid = ldap_delete( ld, dn );
42  */
43 int
44 LDAP_CALL
ldap_delete(LDAP * ld,const char * dn)45 ldap_delete( LDAP *ld, const char *dn )
46 {
47 	int	msgid;
48 
49 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_delete\n", 0, 0, 0 );
50 
51 	if ( ldap_delete_ext( ld, dn, NULL, NULL, &msgid ) == LDAP_SUCCESS ) {
52 		return( msgid );
53 	} else {
54 		return( -1 );	/* error is in ld handle */
55 	}
56 }
57 
58 int
59 LDAP_CALL
ldap_delete_ext(LDAP * ld,const char * dn,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int * msgidp)60 ldap_delete_ext( LDAP *ld, const char *dn, LDAPControl **serverctrls,
61     LDAPControl **clientctrls, int *msgidp )
62 {
63 	BerElement	*ber;
64 	int		rc, lderr;
65 
66 	/*
67 	 * A delete request looks like this:
68 	 *	DelRequet ::= DistinguishedName,
69 	 */
70 
71 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_delete_ext\n", 0, 0, 0 );
72 
73 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
74 		return( LDAP_PARAM_ERROR );
75 	}
76 
77 	if ( !NSLDAPI_VALID_LDAPMESSAGE_POINTER( msgidp ))
78         {
79 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
80 		return( LDAP_PARAM_ERROR );
81 	}
82 	if ( dn == NULL ) {
83 		dn = "";
84 	}
85 
86 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
87 	*msgidp = ++ld->ld_msgid;
88 	LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
89 
90 	/* see if we should add to the cache */
91 	if ( ld->ld_cache_on && ld->ld_cache_delete != NULL ) {
92 		LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
93 		if ( (rc = (ld->ld_cache_delete)( ld, *msgidp, LDAP_REQ_DELETE,
94 		    dn )) != 0 ) {
95 			*msgidp = rc;
96 			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
97 			return( LDAP_SUCCESS );
98 		}
99 		LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
100 	}
101 
102 	/* create a message to send */
103 	if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber ))
104 	    != LDAP_SUCCESS ) {
105 		return( lderr );
106 	}
107 
108 	if ( ber_printf( ber, "{its", *msgidp, LDAP_REQ_DELETE, dn )
109 	    == -1 ) {
110 		lderr = LDAP_ENCODING_ERROR;
111 		LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
112 		ber_free( ber, 1 );
113 		return( lderr );
114 	}
115 
116 	if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber ))
117 	    != LDAP_SUCCESS ) {
118 		ber_free( ber, 1 );
119 		return( lderr );
120 	}
121 
122 	/* send the message */
123 	rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_DELETE,
124 		(char *)dn, ber );
125 	*msgidp = rc;
126 	return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
127 }
128 
129 int
130 LDAP_CALL
ldap_delete_s(LDAP * ld,const char * dn)131 ldap_delete_s( LDAP *ld, const char *dn )
132 {
133 	return( ldap_delete_ext_s( ld, dn, NULL, NULL ));
134 }
135 
136 int
137 LDAP_CALL
ldap_delete_ext_s(LDAP * ld,const char * dn,LDAPControl ** serverctrls,LDAPControl ** clientctrls)138 ldap_delete_ext_s( LDAP *ld, const char *dn, LDAPControl **serverctrls,
139     LDAPControl **clientctrls )
140 {
141 	int		err, msgid;
142 	LDAPMessage	*res;
143 
144 	if (( err = ldap_delete_ext( ld, dn, serverctrls, clientctrls,
145 	    &msgid )) != LDAP_SUCCESS ) {
146 		return( err );
147 	}
148 
149 	if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res ) == -1 ) {
150 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
151 	}
152 
153 	return( ldap_result2error( ld, res, 1 ) );
154 }
155