]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
544ab964af8391c7e15862dd73b7bd375528875d
[openldap] / servers / slapd / delete.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 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, "do_delete\n", 0, 0, 0 );
45
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, "ber_scanf failed\n", 0, 0, 0 );
54                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
55                 return SLAPD_DISCONNECT;
56         }
57
58         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
59                 Debug( LDAP_DEBUG_ANY, "do_delete: get_ctrls failed\n", 0, 0, 0 );
60                 goto cleanup;
61         } 
62
63         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
64                 op->o_tmpmemctx );
65         if( rs->sr_err != LDAP_SUCCESS ) {
66                 Debug( LDAP_DEBUG_ANY,
67                         "do_delete: invalid dn (%s)\n", dn.bv_val, 0, 0 );
68                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
69                 goto cleanup;
70         }
71
72         if( op->o_req_ndn.bv_len == 0 ) {
73                 Debug( LDAP_DEBUG_ANY, "do_delete: root dse!\n", 0, 0, 0 );
74                 /* protocolError would likely be a more appropriate error */
75                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
76                         "cannot delete the root DSE" );
77                 goto cleanup;
78
79         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
80                 Debug( LDAP_DEBUG_ANY, "do_delete: subschema subentry!\n", 0, 0, 0 );
81                 /* protocolError would likely be a more appropriate error */
82                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
83                         "cannot delete the root DSE" );
84                 goto cleanup;
85         }
86
87         Statslog( LDAP_DEBUG_STATS, "%s DEL dn=\"%s\"\n",
88                 op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
89
90         op->o_bd = frontendDB;
91         rs->sr_err = frontendDB->be_delete( op, rs );
92
93 cleanup:;
94         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
95         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
96         return rs->sr_err;
97 }
98
99 int
100 fe_op_delete( Operation *op, SlapReply *rs )
101 {
102         struct berval   pdn = BER_BVNULL;
103         int             manageDSAit;
104         BackendDB *op_be;
105         
106         manageDSAit = get_manageDSAit( op );
107
108         /*
109          * We could be serving multiple database backends.  Select the
110          * appropriate one, or send a referral to our "referral server"
111          * if we don't hold it.
112          */
113         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
114         if ( op->o_bd == NULL ) {
115                 rs->sr_ref = referral_rewrite( default_referral,
116                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
117
118                 if (!rs->sr_ref) rs->sr_ref = default_referral;
119                 if ( rs->sr_ref != NULL ) {
120                         rs->sr_err = LDAP_REFERRAL;
121
122                         op->o_bd = frontendDB;
123                         send_ldap_result( op, rs );
124                         op->o_bd = NULL;
125
126                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
127                 } else {
128                         op->o_bd = frontendDB;
129                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
130                                 "no global superior knowledge" );
131                         op->o_bd = NULL;
132                 }
133                 goto cleanup;
134         }
135
136         /* If we've got a glued backend, check the real backend */
137         op_be = op->o_bd;
138         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
139                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
140         }
141
142         /* check restrictions */
143         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
144                 send_ldap_result( op, rs );
145                 goto cleanup;
146         }
147
148         /* check for referrals */
149         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
150                 goto cleanup;
151         }
152
153         /*
154          * do the delete if 1 && (2 || 3)
155          * 1) there is a delete function implemented in this backend;
156          * 2) this backend is master for what it holds;
157          * 3) it's a replica and the dn supplied is the update_ndn.
158          */
159         if ( op->o_bd->be_delete ) {
160                 /* do the update here */
161                 int repl_user = be_isupdate( op );
162 #ifndef SLAPD_MULTIMASTER
163                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
164 #endif
165                 {
166                         struct berval   org_req_dn = BER_BVNULL;
167                         struct berval   org_req_ndn = BER_BVNULL;
168                         struct berval   org_dn = BER_BVNULL;
169                         struct berval   org_ndn = BER_BVNULL;
170                         int             org_managedsait;
171                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
172
173                         op->o_bd = op_be;
174
175 #ifdef SLAPD_MULTIMASTER
176                         if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
177 #endif
178                         {
179                                 cb.sc_next = op->o_callback;
180                                 op->o_callback = &cb;
181                         }
182
183                         op->o_bd->be_delete( op, rs );
184
185                         org_req_dn = op->o_req_dn;
186                         org_req_ndn = op->o_req_ndn;
187                         org_dn = op->o_dn;
188                         org_ndn = op->o_ndn;
189                         org_managedsait = get_manageDSAit( op );
190                         op->o_dn = op->o_bd->be_rootdn;
191                         op->o_ndn = op->o_bd->be_rootndn;
192                         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
193
194                         while ( rs->sr_err == LDAP_SUCCESS &&
195                                 op->o_delete_glue_parent )
196                         {
197                                 op->o_delete_glue_parent = 0;
198                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
199                                         slap_callback cb = { NULL, NULL, NULL, NULL };
200                                         cb.sc_response = slap_null_cb;
201                                         dnParent( &op->o_req_ndn, &pdn );
202                                         op->o_req_dn = pdn;
203                                         op->o_req_ndn = pdn;
204                                         op->o_callback = &cb;
205                                         op->o_bd->be_delete( op, rs );
206                                 } else {
207                                         break;
208                                 }
209                         }
210
211                         op->o_managedsait = org_managedsait;
212                         op->o_dn = org_dn;
213                         op->o_ndn = org_ndn;
214                         op->o_req_dn = org_req_dn;
215                         op->o_req_ndn = org_req_ndn;
216                         op->o_delete_glue_parent = 0;
217
218 #ifndef SLAPD_MULTIMASTER
219                 } else {
220                         BerVarray defref = op->o_bd->be_update_refs
221                                 ? op->o_bd->be_update_refs : default_referral;
222
223                         if ( defref != NULL ) {
224                                 rs->sr_ref = referral_rewrite( defref,
225                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
226                                 if (!rs->sr_ref) rs->sr_ref = defref;
227                                 rs->sr_err = LDAP_REFERRAL;
228                                 send_ldap_result( op, rs );
229
230                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
231
232                         } else {
233                                 send_ldap_error( op, rs,
234                                         LDAP_UNWILLING_TO_PERFORM,
235                                         "shadow context; no update referral" );
236                         }
237 #endif
238                 }
239
240         } else {
241                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
242                         "operation not supported within namingContext" );
243         }
244
245 cleanup:;
246         return rs->sr_err;
247 }