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