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