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