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