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