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