]> git.sur5r.net Git - openldap/blob - libraries/libldap/delete.c
8d6e482693302f8ca1abc7e65902e78ca7e2503d
[openldap] / libraries / libldap / delete.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  delete.c
11  */
12
13 /*
14  * A delete request looks like this:
15  *      DelRequet ::= DistinguishedName,
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/time.h>
25
26 #include "ldap-int.h"
27
28 /*
29  * ldap_delete_ext - initiate an ldap extended delete operation. Parameters:
30  *
31  *      ld              LDAP descriptor
32  *      dn              DN of the object to delete
33  *      sctrls  Server Controls
34  *      cctrls  Client Controls
35  *      msgidp  Message Id Pointer
36  *
37  * Example:
38  *      rc = ldap_delete( ld, dn, sctrls, cctrls, msgidp );
39  */
40 int
41 ldap_delete_ext(
42         LDAP *ld,
43         LDAP_CONST char* dn,
44         LDAPControl **sctrls,
45         LDAPControl **cctrls,
46         int *msgidp )
47 {
48         int rc;
49         BerElement      *ber;
50         ber_int_t       id;
51
52 #ifdef NEW_LOGGING
53         LDAP_LOG ( OPERATION, ENTRY, "ldap_delete_ext\n", 0,0,0 );
54 #else
55         Debug( LDAP_DEBUG_TRACE, "ldap_delete_ext\n", 0, 0, 0 );
56 #endif
57
58         assert( ld != NULL );
59         assert( LDAP_VALID( ld ) );
60         assert( dn != NULL );
61         assert( msgidp != NULL );
62
63         /* check client controls */
64         rc = ldap_int_client_controls( ld, cctrls );
65         if( rc != LDAP_SUCCESS ) return rc;
66
67         /* create a message to send */
68         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
69                 ld->ld_errno = LDAP_NO_MEMORY;
70                 return( ld->ld_errno );
71         }
72
73         LDAP_NEXT_MSGID( ld, id );
74         rc = ber_printf( ber, "{its", /* '}' */
75                 id, LDAP_REQ_DELETE, dn );
76         if ( rc == -1 )
77         {
78                 ld->ld_errno = LDAP_ENCODING_ERROR;
79                 ber_free( ber, 1 );
80                 return( ld->ld_errno );
81         }
82
83         /* Put Server Controls */
84         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
85                 ber_free( ber, 1 );
86                 return ld->ld_errno;
87         }
88
89         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
90                 ld->ld_errno = LDAP_ENCODING_ERROR;
91                 ber_free( ber, 1 );
92                 return( ld->ld_errno );
93         }
94
95         /* send the message */
96         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_DELETE, dn, ber, id );
97
98         if(*msgidp < 0)
99                 return ld->ld_errno;
100
101         return LDAP_SUCCESS;
102 }
103
104 int
105 ldap_delete_ext_s(
106         LDAP *ld,
107         LDAP_CONST char *dn,
108         LDAPControl **sctrls,
109         LDAPControl **cctrls )
110 {
111         int     msgid;
112         int rc;
113         LDAPMessage     *res;
114
115         rc = ldap_delete_ext( ld, dn, sctrls, cctrls, &msgid );
116         
117         if( rc != LDAP_SUCCESS )
118                 return( ld->ld_errno );
119
120         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
121                 return( ld->ld_errno );
122
123         return( ldap_result2error( ld, res, 1 ) );
124 }
125 /*
126  * ldap_delete - initiate an ldap (and X.500) delete operation. Parameters:
127  *
128  *      ld              LDAP descriptor
129  *      dn              DN of the object to delete
130  *
131  * Example:
132  *      msgid = ldap_delete( ld, dn );
133  */
134 int
135 ldap_delete( LDAP *ld, LDAP_CONST char *dn )
136 {
137         int msgid;
138
139         /*
140          * A delete request looks like this:
141          *      DelRequet ::= DistinguishedName,
142          */
143
144 #ifdef NEW_LOGGING
145         LDAP_LOG ( OPERATION, ENTRY, "ldap_delete\n", 0,0,0 );
146 #else
147         Debug( LDAP_DEBUG_TRACE, "ldap_delete\n", 0, 0, 0 );
148 #endif
149
150         return ldap_delete_ext( ld, dn, NULL, NULL, &msgid ) == LDAP_SUCCESS
151                 ? msgid : -1 ;
152 }
153
154
155 int
156 ldap_delete_s( LDAP *ld, LDAP_CONST char *dn )
157 {
158         return ldap_delete_ext_s( ld, dn, NULL, NULL );
159 }