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