]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
Context CSN Patch (3)
[openldap] / servers / slapd / delete.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "ldap_pvt.h"
26 #include "slap.h"
27
28 #ifdef LDAP_SYNC
29 #include "lutil.h"
30 #endif
31
32 #ifdef LDAP_SLAPI
33 #include "slapi.h"
34 #endif
35
36 int
37 do_delete(
38     Operation   *op,
39     SlapReply   *rs
40 )
41 {
42         struct berval dn = { 0, NULL };
43         int manageDSAit;
44
45 #ifdef LDAP_SLAPI
46         Slapi_PBlock *pb = op->o_pb;
47 #endif
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, op->o_tmpmemctx );
84         if( rs->sr_err != LDAP_SUCCESS ) {
85 #ifdef NEW_LOGGING
86                 LDAP_LOG( OPERATION, INFO, 
87                         "do_delete: conn %d  invalid dn (%s)\n",
88                         op->o_connid, dn.bv_val, 0 );
89 #else
90                 Debug( LDAP_DEBUG_ANY,
91                         "do_delete: invalid dn (%s)\n", dn.bv_val, 0, 0 );
92 #endif
93                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
94                 goto cleanup;
95         }
96
97         if( op->o_req_ndn.bv_len == 0 ) {
98 #ifdef NEW_LOGGING
99                 LDAP_LOG( OPERATION, INFO, 
100                         "do_delete: conn %d: Attempt to delete root DSE.\n", 
101                         op->o_connid, 0, 0 );
102 #else
103                 Debug( LDAP_DEBUG_ANY, "do_delete: root dse!\n", 0, 0, 0 );
104 #endif
105                 /* protocolError would likely be a more appropriate error */
106                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
107                         "cannot delete the root DSE" );
108                 goto cleanup;
109
110         } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
111 #ifdef NEW_LOGGING
112                 LDAP_LOG( OPERATION, INFO, "do_delete: conn %d: "
113                         "Attempt to delete subschema subentry.\n", op->o_connid, 0, 0 );
114 #else
115                 Debug( LDAP_DEBUG_ANY, "do_delete: subschema subentry!\n", 0, 0, 0 );
116 #endif
117                 /* protocolError would likely be a more appropriate error */
118                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
119                         "cannot delete the root DSE" );
120                 goto cleanup;
121         }
122
123         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu DEL dn=\"%s\"\n",
124                 op->o_connid, op->o_opid, op->o_req_dn.bv_val, 0, 0 );
125
126         manageDSAit = get_manageDSAit( op );
127
128         /*
129          * We could be serving multiple database backends.  Select the
130          * appropriate one, or send a referral to our "referral server"
131          * if we don't hold it.
132          */
133         if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
134                 rs->sr_ref = referral_rewrite( default_referral,
135                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
136
137                 if (!rs->sr_ref) rs->sr_ref = default_referral;
138                 if ( rs->sr_ref != NULL ) {
139                         rs->sr_err = LDAP_REFERRAL;
140
141                         send_ldap_result( op, rs );
142
143                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
144                 } else {
145                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
146                                         "referral missing" );
147                 }
148                 goto cleanup;
149         }
150
151         /* check restrictions */
152         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
153                 send_ldap_result( op, rs );
154                 goto cleanup;
155         }
156
157         /* check for referrals */
158         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
159                 goto cleanup;
160         }
161
162 #if defined( LDAP_SLAPI )
163         slapi_x_pblock_set_operation( pb, op );
164         slapi_pblock_set( pb, SLAPI_DELETE_TARGET, (void *)dn.bv_val );
165         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
166
167         rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_DELETE_FN, pb );
168         if ( rs->sr_err < 0 ) {
169                 /*
170                  * A preoperation plugin failure will abort the
171                  * entire operation.
172                  */
173 #ifdef NEW_LOGGING
174                 LDAP_LOG( OPERATION, INFO, "do_delete: delete preoperation plugin "
175                                 "failed\n", 0, 0, 0 );
176 #else
177                 Debug (LDAP_DEBUG_TRACE, "do_delete: delete preoperation plugin failed.\n",
178                                 0, 0, 0);
179 #endif
180                 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 )  ||
181                      rs->sr_err == LDAP_SUCCESS ) {
182                         rs->sr_err = LDAP_OTHER;
183                 }
184                 goto cleanup;
185         }
186 #endif /* defined( LDAP_SLAPI ) */
187
188         /*
189          * do the delete if 1 && (2 || 3)
190          * 1) there is a delete function implemented in this backend;
191          * 2) this backend is master for what it holds;
192          * 3) it's a replica and the dn supplied is the update_ndn.
193          */
194         if ( op->o_bd->be_delete ) {
195                 /* do the update here */
196                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
197 #if defined(LDAP_SYNCREPL) && !defined(SLAPD_MULTIMASTER)
198                 if ( !op->o_bd->syncinfo && ( !op->o_bd->be_update_ndn.bv_len || repl_user ))
199 #elif defined(LDAP_SYNCREPL) && defined(SLAPD_MULTIMASTER)
200                 if ( !op->o_bd->syncinfo )  /* LDAP_SYNCREPL overrides MM */
201 #elif !defined(LDAP_SYNCREPL) && !defined(SLAPD_MULTIMASTER)
202                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user )
203 #endif
204                 {
205
206 #ifdef LDAP_SYNC
207                         if ( !repl_user ) {
208                                 struct berval csn = { 0 , NULL };
209                                 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
210                                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
211                         }
212 #endif
213
214                         if ( (op->o_bd->be_delete)( op, rs ) == 0 ) {
215 #ifdef SLAPD_MULTIMASTER
216                                 if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
217 #endif
218                                 {
219                                         replog( op );
220                                 }
221                         }
222 #if defined(LDAP_SYNCREPL) || !defined(SLAPD_MULTIMASTER)
223                 } else {
224                         BerVarray defref = NULL;
225 #ifdef LDAP_SYNCREPL
226                         if ( op->o_bd->syncinfo ) {
227                                 defref = op->o_bd->syncinfo->provideruri_bv;
228                         } else
229 #endif
230                         {
231                                 defref = op->o_bd->be_update_refs
232                                                 ? op->o_bd->be_update_refs : default_referral;
233                         }
234                         if ( defref != NULL ) {
235                                 rs->sr_ref = referral_rewrite( defref,
236                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
237                                 if (!rs->sr_ref) rs->sr_ref = defref;
238                                 rs->sr_err = LDAP_REFERRAL;
239                                 send_ldap_result( op, rs );
240
241                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
242                         } else {
243                                 send_ldap_error( op, rs,
244                                                 LDAP_UNWILLING_TO_PERFORM,
245                                                 "referral missing" );
246                         }
247 #endif
248                 }
249
250         } else {
251                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
252                         "operation not supported within namingContext" );
253         }
254
255 #if defined( LDAP_SLAPI )
256         if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_DELETE_FN, pb ) < 0) {
257 #ifdef NEW_LOGGING
258                 LDAP_LOG( OPERATION, INFO, "do_delete: delete postoperation plugins "
259                                 "failed\n", 0, 0, 0 );
260 #else
261                 Debug(LDAP_DEBUG_TRACE, "do_delete: delete postoperation plugins "
262                                 "failed.\n", 0, 0, 0);
263 #endif
264         }
265 #endif /* defined( LDAP_SLAPI ) */
266
267 cleanup:
268
269 #ifdef LDAP_SYNC
270         slap_graduate_commit_csn( op );
271 #endif
272
273         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
274         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
275         return rs->sr_err;
276 }