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