]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
70a3354345d670121ef32a1811e14392fd02c1bb
[openldap] / servers / slapd / delete.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2017 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) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34
35 #include "lutil.h"
36
37 int
38 do_delete(
39     Operation   *op,
40     SlapReply   *rs )
41 {
42         struct berval dn = BER_BVNULL;
43
44         Debug( LDAP_DEBUG_TRACE, "%s do_delete\n",
45                 op->o_log_prefix, 0, 0 );
46         /*
47          * Parse the delete request.  It looks like this:
48          *
49          *      DelRequest := DistinguishedName
50          */
51
52         if ( ber_scanf( op->o_ber, "m", &dn ) == LBER_ERROR ) {
53                 Debug( LDAP_DEBUG_ANY, "%s do_delete: ber_scanf failed\n",
54                         op->o_log_prefix, 0, 0 );
55                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
56                 return SLAPD_DISCONNECT;
57         }
58
59         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
60                 Debug( LDAP_DEBUG_ANY, "%s do_delete: get_ctrls failed\n",
61                         op->o_log_prefix, 0, 0 );
62                 goto cleanup;
63         } 
64
65         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
66                 op->o_tmpmemctx );
67         if( rs->sr_err != LDAP_SUCCESS ) {
68                 Debug( LDAP_DEBUG_ANY, "%s do_delete: invalid dn (%s)\n",
69                         op->o_log_prefix, dn.bv_val, 0 );
70                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
71                 goto cleanup;
72         }
73
74         Statslog( LDAP_DEBUG_STATS, "%s DEL dn=\"%s\"\n",
75                 op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
76
77         if( op->o_req_ndn.bv_len == 0 ) {
78                 Debug( LDAP_DEBUG_ANY, "%s do_delete: root dse!\n",
79                         op->o_log_prefix, 0, 0 );
80                 /* protocolError would likely be a more appropriate error */
81                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
82                         "cannot delete the root DSE" );
83                 goto cleanup;
84
85         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
86                 Debug( LDAP_DEBUG_ANY, "%s do_delete: subschema subentry!\n",
87                         op->o_log_prefix, 0, 0 );
88                 /* protocolError would likely be a more appropriate error */
89                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
90                         "cannot delete the root DSE" );
91                 goto cleanup;
92         }
93
94         op->o_bd = frontendDB;
95         rs->sr_err = frontendDB->be_delete( op, rs );
96         if ( rs->sr_err == SLAPD_ASYNCOP ) {
97                 /* skip cleanup */
98                 return rs->sr_err;
99         }
100
101 #ifdef LDAP_X_TXN
102         if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
103                 /* skip cleanup */
104                 return rs->sr_err;
105         }
106 #endif
107
108 cleanup:;
109         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
110         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
111         return rs->sr_err;
112 }
113
114 int
115 fe_op_delete( Operation *op, SlapReply *rs )
116 {
117         struct berval   pdn = BER_BVNULL;
118         BackendDB       *op_be, *bd = op->o_bd;
119         
120         /*
121          * We could be serving multiple database backends.  Select the
122          * appropriate one, or send a referral to our "referral server"
123          * if we don't hold it.
124          */
125         op->o_bd = select_backend( &op->o_req_ndn, 1 );
126         if ( op->o_bd == NULL ) {
127                 op->o_bd = bd;
128                 rs->sr_ref = referral_rewrite( default_referral,
129                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
130
131                 if (!rs->sr_ref) rs->sr_ref = default_referral;
132                 if ( rs->sr_ref != NULL ) {
133                         rs->sr_err = LDAP_REFERRAL;
134                         send_ldap_result( op, rs );
135
136                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
137                 } else {
138                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
139                                 "no global superior knowledge" );
140                 }
141                 goto cleanup;
142         }
143
144         /* If we've got a glued backend, check the real backend */
145         op_be = op->o_bd;
146         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
147                 op->o_bd = select_backend( &op->o_req_ndn, 0 );
148         }
149
150         /* check restrictions */
151         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
152                 send_ldap_result( op, rs );
153                 goto cleanup;
154         }
155
156         /* check for referrals */
157         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
158                 goto cleanup;
159         }
160
161         /*
162          * do the delete if 1 && (2 || 3)
163          * 1) there is a delete function implemented in this backend;
164          * 2) this backend is master for what it holds;
165          * 3) it's a replica and the dn supplied is the update_ndn.
166          */
167         if ( op->o_bd->be_delete ) {
168                 /* do the update here */
169                 int repl_user = be_isupdate( op );
170                 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
171                         struct berval   org_req_dn = BER_BVNULL;
172                         struct berval   org_req_ndn = BER_BVNULL;
173                         struct berval   org_dn = BER_BVNULL;
174                         struct berval   org_ndn = BER_BVNULL;
175                         int             org_managedsait;
176
177                         op->o_bd = op_be;
178                         op->o_bd->be_delete( op, rs );
179
180                         org_req_dn = op->o_req_dn;
181                         org_req_ndn = op->o_req_ndn;
182                         org_dn = op->o_dn;
183                         org_ndn = op->o_ndn;
184                         org_managedsait = get_manageDSAit( op );
185                         op->o_dn = op->o_bd->be_rootdn;
186                         op->o_ndn = op->o_bd->be_rootndn;
187                         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
188
189                         while ( rs->sr_err == LDAP_SUCCESS &&
190                                 op->o_delete_glue_parent )
191                         {
192                                 op->o_delete_glue_parent = 0;
193                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
194                                         slap_callback cb = { NULL, NULL, NULL, NULL };
195                                         cb.sc_response = slap_null_cb;
196                                         dnParent( &op->o_req_ndn, &pdn );
197                                         op->o_req_dn = pdn;
198                                         op->o_req_ndn = pdn;
199                                         op->o_callback = &cb;
200                                         op->o_bd->be_delete( op, rs );
201                                 } else {
202                                         break;
203                                 }
204                         }
205
206                         op->o_managedsait = org_managedsait;
207                         op->o_dn = org_dn;
208                         op->o_ndn = org_ndn;
209                         op->o_req_dn = org_req_dn;
210                         op->o_req_ndn = org_req_ndn;
211                         op->o_delete_glue_parent = 0;
212
213                 } else {
214                         BerVarray defref = op->o_bd->be_update_refs
215                                 ? op->o_bd->be_update_refs : default_referral;
216
217                         if ( defref != NULL ) {
218                                 rs->sr_ref = referral_rewrite( defref,
219                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
220                                 if (!rs->sr_ref) rs->sr_ref = defref;
221                                 rs->sr_err = LDAP_REFERRAL;
222                                 send_ldap_result( op, rs );
223
224                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
225
226                         } else {
227                                 send_ldap_error( op, rs,
228                                         LDAP_UNWILLING_TO_PERFORM,
229                                         "shadow context; no update referral" );
230                         }
231                 }
232
233         } else {
234                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
235                         "operation not supported within namingContext" );
236         }
237
238 cleanup:;
239         op->o_bd = bd;
240         return rs->sr_err;
241 }