]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
7bc2f42ed7423da70bd17eb2a79579432d0f053c
[openldap] / servers / slapd / delete.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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 "ldap_pvt.h"
34 #include "slap.h"
35
36 #include "lutil.h"
37
38 #ifdef LDAP_SLAPI
39 #include "slapi/slapi.h"
40 #endif
41
42 int
43 do_delete(
44     Operation   *op,
45     SlapReply   *rs )
46 {
47         struct berval dn = BER_BVNULL;
48         struct berval pdn = BER_BVNULL;
49         struct berval org_req_dn = BER_BVNULL;
50         struct berval org_req_ndn = BER_BVNULL;
51         struct berval org_dn = BER_BVNULL;
52         struct berval org_ndn = BER_BVNULL;
53         int     org_managedsait;
54         int manageDSAit;
55
56 #ifdef NEW_LOGGING
57         LDAP_LOG( OPERATION, ENTRY, 
58                 "do_delete: conn %d\n", op->o_connid, 0, 0 );
59 #else
60         Debug( LDAP_DEBUG_TRACE, "do_delete\n", 0, 0, 0 );
61 #endif
62
63         /*
64          * Parse the delete request.  It looks like this:
65          *
66          *      DelRequest := DistinguishedName
67          */
68
69         if ( ber_scanf( op->o_ber, "m", &dn ) == LBER_ERROR ) {
70 #ifdef NEW_LOGGING
71                 LDAP_LOG( OPERATION, ERR, 
72                         "do_delete: conn: %d  ber_scanf failed\n", op->o_connid, 0, 0 );
73 #else
74                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
75 #endif
76                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
77                 return SLAPD_DISCONNECT;
78         }
79
80         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
81 #ifdef NEW_LOGGING
82                 LDAP_LOG( OPERATION, ERR, 
83                         "do_delete: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
84 #else
85                 Debug( LDAP_DEBUG_ANY, "do_delete: get_ctrls failed\n", 0, 0, 0 );
86 #endif
87                 goto cleanup;
88         } 
89
90         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
91                 op->o_tmpmemctx );
92         if( rs->sr_err != LDAP_SUCCESS ) {
93 #ifdef NEW_LOGGING
94                 LDAP_LOG( OPERATION, INFO, 
95                         "do_delete: conn %d  invalid dn (%s)\n",
96                         op->o_connid, dn.bv_val, 0 );
97 #else
98                 Debug( LDAP_DEBUG_ANY,
99                         "do_delete: invalid dn (%s)\n", dn.bv_val, 0, 0 );
100 #endif
101                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
102                 goto cleanup;
103         }
104
105         if( op->o_req_ndn.bv_len == 0 ) {
106 #ifdef NEW_LOGGING
107                 LDAP_LOG( OPERATION, INFO, 
108                         "do_delete: conn %d: Attempt to delete root DSE.\n", 
109                         op->o_connid, 0, 0 );
110 #else
111                 Debug( LDAP_DEBUG_ANY, "do_delete: root dse!\n", 0, 0, 0 );
112 #endif
113                 /* protocolError would likely be a more appropriate error */
114                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
115                         "cannot delete the root DSE" );
116                 goto cleanup;
117
118         } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
119 #ifdef NEW_LOGGING
120                 LDAP_LOG( OPERATION, INFO, "do_delete: conn %d: "
121                         "Attempt to delete subschema subentry.\n", op->o_connid, 0, 0 );
122 #else
123                 Debug( LDAP_DEBUG_ANY, "do_delete: subschema subentry!\n", 0, 0, 0 );
124 #endif
125                 /* protocolError would likely be a more appropriate error */
126                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
127                         "cannot delete the root DSE" );
128                 goto cleanup;
129         }
130
131         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu DEL dn=\"%s\"\n",
132                 op->o_connid, op->o_opid, op->o_req_dn.bv_val, 0, 0 );
133
134         manageDSAit = get_manageDSAit( op );
135
136         /*
137          * We could be serving multiple database backends.  Select the
138          * appropriate one, or send a referral to our "referral server"
139          * if we don't hold it.
140          */
141         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
142         if ( op->o_bd == NULL ) {
143                 rs->sr_ref = referral_rewrite( default_referral,
144                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
145
146                 if (!rs->sr_ref) rs->sr_ref = default_referral;
147                 if ( rs->sr_ref != NULL ) {
148                         rs->sr_err = LDAP_REFERRAL;
149
150                         send_ldap_result( op, rs );
151
152                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
153                 } else {
154                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
155                                 "no global superior knowledge" );
156                 }
157                 goto cleanup;
158         }
159
160         /* check restrictions */
161         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
162                 send_ldap_result( op, rs );
163                 goto cleanup;
164         }
165
166         /* check for referrals */
167         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
168                 goto cleanup;
169         }
170
171 #if defined( LDAP_SLAPI )
172 #define pb op->o_pb
173         if ( pb ) {
174                 slapi_int_pblock_set_operation( pb, op );
175                 slapi_pblock_set( pb, SLAPI_DELETE_TARGET, (void *)dn.bv_val );
176                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
177
178                 rs->sr_err = slapi_int_call_plugins( op->o_bd,
179                         SLAPI_PLUGIN_PRE_DELETE_FN, pb );
180                 if ( rs->sr_err < 0 ) {
181                         /*
182                          * A preoperation plugin failure will abort the
183                          * entire operation.
184                          */
185 #ifdef NEW_LOGGING
186                         LDAP_LOG( OPERATION, INFO, "do_delete: "
187                                 "delete preoperation plugin failed\n", 0, 0, 0 );
188 #else
189                         Debug (LDAP_DEBUG_TRACE, "do_delete: "
190                                 "delete preoperation plugin failed.\n", 0, 0, 0);
191 #endif
192                         if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE,
193                                 (void *)&rs->sr_err ) != 0 ) ||
194                                 rs->sr_err == LDAP_SUCCESS )
195                         {
196                                 rs->sr_err = LDAP_OTHER;
197                         }
198                         goto cleanup;
199                 }
200         }
201 #endif /* defined( LDAP_SLAPI ) */
202
203         /*
204          * do the delete if 1 && (2 || 3)
205          * 1) there is a delete function implemented in this backend;
206          * 2) this backend is master for what it holds;
207          * 3) it's a replica and the dn supplied is the update_ndn.
208          */
209         if ( op->o_bd->be_delete ) {
210                 /* do the update here */
211                 int repl_user = be_isupdate( op );
212 #ifndef SLAPD_MULTIMASTER
213                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
214 #endif
215                 {
216                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
217
218                         if ( !repl_user ) {
219                                 struct berval csn = { 0 , NULL };
220                                 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
221                                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
222                         }
223
224 #ifdef SLAPD_MULTIMASTER
225                         if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
226 #endif
227                         {
228                                 cb.sc_next = op->o_callback;
229                                 op->o_callback = &cb;
230                         }
231
232                         op->o_bd->be_delete( op, rs );
233
234                         org_req_dn = op->o_req_dn;
235                         org_req_ndn = op->o_req_ndn;
236                         org_dn = op->o_dn;
237                         org_ndn = op->o_ndn;
238                         org_managedsait = get_manageDSAit( op );
239                         op->o_dn = op->o_bd->be_rootdn;
240                         op->o_ndn = op->o_bd->be_rootndn;
241                         op->o_managedsait = 1;
242
243                         while ( rs->sr_err == LDAP_SUCCESS &&
244                                 op->o_delete_glue_parent )
245                         {
246                                 op->o_delete_glue_parent = 0;
247                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
248                                         slap_callback cb = { NULL };
249                                         cb.sc_response = slap_null_cb;
250                                         dnParent( &op->o_req_ndn, &pdn );
251                                         op->o_req_dn = pdn;
252                                         op->o_req_ndn = pdn;
253                                         op->o_callback = &cb;
254                                         op->o_bd->be_delete( op, rs );
255                                 } else {
256                                         break;
257                                 }
258                         }
259
260                         op->o_managedsait = org_managedsait;
261                         op->o_dn = org_dn;
262                         op->o_ndn = org_ndn;
263                         op->o_req_dn = org_req_dn;
264                         op->o_req_ndn = org_req_ndn;
265                         op->o_delete_glue_parent = 0;
266
267 #ifndef SLAPD_MULTIMASTER
268                 } else {
269                         BerVarray defref = op->o_bd->be_update_refs
270                                 ? op->o_bd->be_update_refs : default_referral;
271
272                         if ( defref != NULL ) {
273                                 rs->sr_ref = referral_rewrite( defref,
274                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
275                                 if (!rs->sr_ref) rs->sr_ref = defref;
276                                 rs->sr_err = LDAP_REFERRAL;
277                                 send_ldap_result( op, rs );
278
279                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
280
281                         } else {
282                                 send_ldap_error( op, rs,
283                                         LDAP_UNWILLING_TO_PERFORM,
284                                         "shadow context; no update referral" );
285                         }
286 #endif
287                 }
288
289         } else {
290                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
291                         "operation not supported within namingContext" );
292         }
293
294 #if defined( LDAP_SLAPI )
295         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
296                 SLAPI_PLUGIN_POST_DELETE_FN, pb ) < 0)
297         {
298 #ifdef NEW_LOGGING
299                 LDAP_LOG( OPERATION, INFO,
300                         "do_delete: delete postoperation plugins failed\n",
301                         0, 0, 0 );
302 #else
303                 Debug(LDAP_DEBUG_TRACE,
304                         "do_delete: delete postoperation plugins failed\n",
305                         0, 0, 0 );
306 #endif
307         }
308 #endif /* defined( LDAP_SLAPI ) */
309
310 cleanup:
311         slap_graduate_commit_csn( op );
312
313         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
314         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
315         return rs->sr_err;
316 }