]> git.sur5r.net Git - openldap/blob - libraries/libldap/delete.c
b7dd170d0269fafb1a471221ff4c59309fc37f15
[openldap] / libraries / libldap / delete.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2003 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 #ifdef NEW_LOGGING
63         LDAP_LOG ( OPERATION, ENTRY, "ldap_delete_ext\n", 0,0,0 );
64 #else
65         Debug( LDAP_DEBUG_TRACE, "ldap_delete_ext\n", 0, 0, 0 );
66 #endif
67
68         assert( ld != NULL );
69         assert( LDAP_VALID( ld ) );
70         assert( dn != NULL );
71         assert( msgidp != NULL );
72
73         /* check client controls */
74         rc = ldap_int_client_controls( ld, cctrls );
75         if( rc != LDAP_SUCCESS ) return rc;
76
77         /* create a message to send */
78         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
79                 ld->ld_errno = LDAP_NO_MEMORY;
80                 return( ld->ld_errno );
81         }
82
83         LDAP_NEXT_MSGID( ld, id );
84         rc = ber_printf( ber, "{its", /* '}' */
85                 id, LDAP_REQ_DELETE, dn );
86         if ( rc == -1 )
87         {
88                 ld->ld_errno = LDAP_ENCODING_ERROR;
89                 ber_free( ber, 1 );
90                 return( ld->ld_errno );
91         }
92
93         /* Put Server Controls */
94         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
95                 ber_free( ber, 1 );
96                 return ld->ld_errno;
97         }
98
99         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
100                 ld->ld_errno = LDAP_ENCODING_ERROR;
101                 ber_free( ber, 1 );
102                 return( ld->ld_errno );
103         }
104
105         /* send the message */
106         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_DELETE, dn, ber, id );
107
108         if(*msgidp < 0)
109                 return ld->ld_errno;
110
111         return LDAP_SUCCESS;
112 }
113
114 int
115 ldap_delete_ext_s(
116         LDAP *ld,
117         LDAP_CONST char *dn,
118         LDAPControl **sctrls,
119         LDAPControl **cctrls )
120 {
121         int     msgid;
122         int rc;
123         LDAPMessage     *res;
124
125         rc = ldap_delete_ext( ld, dn, sctrls, cctrls, &msgid );
126         
127         if( rc != LDAP_SUCCESS )
128                 return( ld->ld_errno );
129
130         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
131                 return( ld->ld_errno );
132
133         return( ldap_result2error( ld, res, 1 ) );
134 }
135 /*
136  * ldap_delete - initiate an ldap (and X.500) delete operation. Parameters:
137  *
138  *      ld              LDAP descriptor
139  *      dn              DN of the object to delete
140  *
141  * Example:
142  *      msgid = ldap_delete( ld, dn );
143  */
144 int
145 ldap_delete( LDAP *ld, LDAP_CONST char *dn )
146 {
147         int msgid;
148
149         /*
150          * A delete request looks like this:
151          *      DelRequet ::= DistinguishedName,
152          */
153
154 #ifdef NEW_LOGGING
155         LDAP_LOG ( OPERATION, ENTRY, "ldap_delete\n", 0,0,0 );
156 #else
157         Debug( LDAP_DEBUG_TRACE, "ldap_delete\n", 0, 0, 0 );
158 #endif
159
160         return ldap_delete_ext( ld, dn, NULL, NULL, &msgid ) == LDAP_SUCCESS
161                 ? msgid : -1 ;
162 }
163
164
165 int
166 ldap_delete_s( LDAP *ld, LDAP_CONST char *dn )
167 {
168         return ldap_delete_ext_s( ld, dn, NULL, NULL );
169 }