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