]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
Cleanup
[openldap] / servers / slapd / delete.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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         slap_graduate_commit_csn( op );
95
96         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
97         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
98         return rs->sr_err;
99 }
100
101 int
102 fe_op_delete( Operation *op, SlapReply *rs )
103 {
104         struct berval   pdn = BER_BVNULL;
105         int             manageDSAit;
106         BackendDB *op_be;
107         
108         manageDSAit = get_manageDSAit( op );
109
110         /*
111          * We could be serving multiple database backends.  Select the
112          * appropriate one, or send a referral to our "referral server"
113          * if we don't hold it.
114          */
115         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
116         if ( op->o_bd == NULL ) {
117                 rs->sr_ref = referral_rewrite( default_referral,
118                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
119
120                 if (!rs->sr_ref) rs->sr_ref = default_referral;
121                 if ( rs->sr_ref != NULL ) {
122                         rs->sr_err = LDAP_REFERRAL;
123
124                         op->o_bd = frontendDB;
125                         send_ldap_result( op, rs );
126                         op->o_bd = NULL;
127
128                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
129                 } else {
130                         op->o_bd = frontendDB;
131                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
132                                 "no global superior knowledge" );
133                         op->o_bd = NULL;
134                 }
135                 goto cleanup;
136         }
137
138         /* If we've got a glued backend, check the real backend */
139         op_be = op->o_bd;
140         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
141                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
142         }
143
144         /* check restrictions */
145         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
146                 send_ldap_result( op, rs );
147                 goto cleanup;
148         }
149
150         /* check for referrals */
151         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
152                 goto cleanup;
153         }
154
155         /*
156          * do the delete if 1 && (2 || 3)
157          * 1) there is a delete function implemented in this backend;
158          * 2) this backend is master for what it holds;
159          * 3) it's a replica and the dn supplied is the update_ndn.
160          */
161         if ( op->o_bd->be_delete ) {
162                 /* do the update here */
163                 int repl_user = be_isupdate( op );
164 #ifndef SLAPD_MULTIMASTER
165                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
166 #endif
167                 {
168                         struct berval   org_req_dn = BER_BVNULL;
169                         struct berval   org_req_ndn = BER_BVNULL;
170                         struct berval   org_dn = BER_BVNULL;
171                         struct berval   org_ndn = BER_BVNULL;
172                         int             org_managedsait;
173                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
174
175                         op->o_bd = op_be;
176
177                         if ( !repl_user ) {
178                                 struct berval csn = BER_BVNULL;
179                                 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
180                                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
181                         }
182
183 #ifdef SLAPD_MULTIMASTER
184                         if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
185 #endif
186                         {
187                                 cb.sc_next = op->o_callback;
188                                 op->o_callback = &cb;
189                         }
190
191                         op->o_bd->be_delete( op, rs );
192
193                         org_req_dn = op->o_req_dn;
194                         org_req_ndn = op->o_req_ndn;
195                         org_dn = op->o_dn;
196                         org_ndn = op->o_ndn;
197                         org_managedsait = get_manageDSAit( op );
198                         op->o_dn = op->o_bd->be_rootdn;
199                         op->o_ndn = op->o_bd->be_rootndn;
200                         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
201
202                         while ( rs->sr_err == LDAP_SUCCESS &&
203                                 op->o_delete_glue_parent )
204                         {
205                                 op->o_delete_glue_parent = 0;
206                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
207                                         slap_callback cb = { NULL, NULL, NULL, NULL };
208                                         cb.sc_response = slap_null_cb;
209                                         dnParent( &op->o_req_ndn, &pdn );
210                                         op->o_req_dn = pdn;
211                                         op->o_req_ndn = pdn;
212                                         op->o_callback = &cb;
213                                         op->o_bd->be_delete( op, rs );
214                                 } else {
215                                         break;
216                                 }
217                         }
218
219                         op->o_managedsait = org_managedsait;
220                         op->o_dn = org_dn;
221                         op->o_ndn = org_ndn;
222                         op->o_req_dn = org_req_dn;
223                         op->o_req_ndn = org_req_ndn;
224                         op->o_delete_glue_parent = 0;
225
226 #ifndef SLAPD_MULTIMASTER
227                 } else {
228                         BerVarray defref = op->o_bd->be_update_refs
229                                 ? op->o_bd->be_update_refs : default_referral;
230
231                         if ( defref != NULL ) {
232                                 rs->sr_ref = referral_rewrite( defref,
233                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
234                                 if (!rs->sr_ref) rs->sr_ref = defref;
235                                 rs->sr_err = LDAP_REFERRAL;
236                                 send_ldap_result( op, rs );
237
238                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
239
240                         } else {
241                                 send_ldap_error( op, rs,
242                                         LDAP_UNWILLING_TO_PERFORM,
243                                         "shadow context; no update referral" );
244                         }
245 #endif
246                 }
247
248         } else {
249                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
250                         "operation not supported within namingContext" );
251         }
252
253 cleanup:;
254         return rs->sr_err;
255 }