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