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