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