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