]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
Sync with HEAD
[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 {
48         struct berval dn = { 0, NULL };
49         struct berval pdn = { 0, NULL };
50         struct berval org_req_dn = { 0, NULL };
51         struct berval org_req_ndn = { 0, NULL };
52         struct berval org_dn = { 0, NULL };
53         struct berval org_ndn = { 0, NULL };
54         int     org_managedsait;
55         int manageDSAit;
56
57 #ifdef NEW_LOGGING
58         LDAP_LOG( OPERATION, ENTRY, 
59                 "do_delete: conn %d\n", op->o_connid, 0, 0 );
60 #else
61         Debug( LDAP_DEBUG_TRACE, "do_delete\n", 0, 0, 0 );
62 #endif
63
64         /*
65          * Parse the delete request.  It looks like this:
66          *
67          *      DelRequest := DistinguishedName
68          */
69
70         if ( ber_scanf( op->o_ber, "m", &dn ) == LBER_ERROR ) {
71 #ifdef NEW_LOGGING
72                 LDAP_LOG( OPERATION, ERR, 
73                         "do_delete: conn: %d  ber_scanf failed\n", op->o_connid, 0, 0 );
74 #else
75                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
76 #endif
77                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
78                 return SLAPD_DISCONNECT;
79         }
80
81         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
82 #ifdef NEW_LOGGING
83                 LDAP_LOG( OPERATION, ERR, 
84                         "do_delete: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
85 #else
86                 Debug( LDAP_DEBUG_ANY, "do_delete: get_ctrls failed\n", 0, 0, 0 );
87 #endif
88                 goto cleanup;
89         } 
90
91         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, 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         if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
142                 rs->sr_ref = referral_rewrite( default_referral,
143                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
144
145                 if (!rs->sr_ref) rs->sr_ref = default_referral;
146                 if ( rs->sr_ref != NULL ) {
147                         rs->sr_err = LDAP_REFERRAL;
148
149                         send_ldap_result( op, rs );
150
151                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
152                 } else {
153                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
154                                         "referral missing" );
155                 }
156                 goto cleanup;
157         }
158
159         /* check restrictions */
160         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
161                 send_ldap_result( op, rs );
162                 goto cleanup;
163         }
164
165         /* check for referrals */
166         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
167                 goto cleanup;
168         }
169
170 #if defined( LDAP_SLAPI )
171 #define pb op->o_pb
172         if ( pb ) {
173                 slapi_int_pblock_set_operation( pb, op );
174                 slapi_pblock_set( pb, SLAPI_DELETE_TARGET, (void *)dn.bv_val );
175                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
176
177                 rs->sr_err = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_DELETE_FN, pb );
178                 if ( rs->sr_err < 0 ) {
179                         /*
180                          * A preoperation plugin failure will abort the
181                          * entire operation.
182                          */
183 #ifdef NEW_LOGGING
184                         LDAP_LOG( OPERATION, INFO, "do_delete: delete preoperation plugin "
185                                         "failed\n", 0, 0, 0 );
186 #else
187                         Debug (LDAP_DEBUG_TRACE, "do_delete: delete preoperation plugin failed.\n",
188                                         0, 0, 0);
189 #endif
190                         if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 )  ||
191                                  rs->sr_err == LDAP_SUCCESS ) {
192                                 rs->sr_err = LDAP_OTHER;
193                         }
194                         goto cleanup;
195                 }
196         }
197 #endif /* defined( LDAP_SLAPI ) */
198
199         /*
200          * do the delete if 1 && (2 || 3)
201          * 1) there is a delete function implemented in this backend;
202          * 2) this backend is master for what it holds;
203          * 3) it's a replica and the dn supplied is the update_ndn.
204          */
205         if ( op->o_bd->be_delete ) {
206                 /* do the update here */
207                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
208 #ifndef SLAPD_MULTIMASTER
209                 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ) &&
210                         ( !op->o_bd->be_update_ndn.bv_len || repl_user ))
211 #else
212                 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ))
213 #endif
214                 {
215                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
216
217                         if ( !repl_user ) {
218                                 struct berval csn = { 0 , NULL };
219                                 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
220                                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
221                         }
222
223 #ifdef SLAPD_MULTIMASTER
224                         if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
225 #endif
226                         {
227                                 cb.sc_next = op->o_callback;
228                                 op->o_callback = &cb;
229                         }
230
231                         op->o_bd->be_delete( op, rs );
232
233                         org_req_dn = op->o_req_dn;
234                         org_req_ndn = op->o_req_ndn;
235                         org_dn = op->o_dn;
236                         org_ndn = op->o_ndn;
237                         org_managedsait = get_manageDSAit( op );
238                         op->o_dn = op->o_bd->be_rootdn;
239                         op->o_ndn = op->o_bd->be_rootndn;
240                         op->o_managedsait = 1;
241
242                         while ( rs->sr_err == LDAP_SUCCESS &&
243                                         op->o_delete_glue_parent ) {
244                                 op->o_delete_glue_parent = 0;
245                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
246                                         slap_callback cb = { NULL };
247                                         cb.sc_response = slap_null_cb;
248                                         dnParent( &op->o_req_ndn, &pdn );
249                                         op->o_req_dn = pdn;
250                                         op->o_req_ndn = pdn;
251                                         op->o_callback = &cb;
252                                         op->o_bd->be_delete( op, rs );
253                                 } else {
254                                         break;
255                                 }
256                         }
257
258                         op->o_managedsait = org_managedsait;
259                         op->o_dn = org_dn;
260                         op->o_ndn = org_ndn;
261                         op->o_req_dn = org_req_dn;
262                         op->o_req_ndn = org_req_ndn;
263                         op->o_delete_glue_parent = 0;
264
265 #ifndef SLAPD_MULTIMASTER
266                 } else {
267                         BerVarray defref = NULL;
268                         if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
269                                 syncinfo_t *si;
270                                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
271                                         struct berval tmpbv;
272                                         ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
273                                         ber_bvarray_add( &defref, &tmpbv );
274                                 }
275                         } else {
276                                 defref = op->o_bd->be_update_refs
277                                         ? op->o_bd->be_update_refs : default_referral;
278                         }
279                         if ( defref != NULL ) {
280                                 rs->sr_ref = referral_rewrite( defref,
281                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
282                                 if (!rs->sr_ref) rs->sr_ref = defref;
283                                 rs->sr_err = LDAP_REFERRAL;
284                                 send_ldap_result( op, rs );
285
286                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
287                         } else {
288                                 send_ldap_error( op, rs,
289                                                 LDAP_UNWILLING_TO_PERFORM,
290                                                 "referral missing" );
291                         }
292 #endif
293                 }
294
295         } else {
296                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
297                         "operation not supported within namingContext" );
298         }
299
300 #if defined( LDAP_SLAPI )
301         if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_DELETE_FN, pb ) < 0) {
302 #ifdef NEW_LOGGING
303                 LDAP_LOG( OPERATION, INFO, "do_delete: delete postoperation plugins "
304                                 "failed\n", 0, 0, 0 );
305 #else
306                 Debug(LDAP_DEBUG_TRACE, "do_delete: delete postoperation plugins "
307                                 "failed.\n", 0, 0, 0);
308 #endif
309         }
310 #endif /* defined( LDAP_SLAPI ) */
311
312 cleanup:
313
314         slap_graduate_commit_csn( op );
315
316         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
317         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
318         return rs->sr_err;
319 }