]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
slapi header cleanup
[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 #include "slapi.h"
28
29 int
30 do_delete(
31     Connection  *conn,
32     Operation   *op
33 )
34 {
35         struct berval dn = { 0, NULL };
36         struct berval pdn = { 0, NULL };
37         struct berval ndn = { 0, NULL };
38         const char *text;
39         Backend *be;
40         int rc;
41         int manageDSAit;
42
43         Slapi_PBlock *pb = op->o_pb;
44
45 #ifdef NEW_LOGGING
46         LDAP_LOG( OPERATION, ENTRY, 
47                 "do_delete: conn %d\n", conn->c_connid, 0, 0 );
48 #else
49         Debug( LDAP_DEBUG_TRACE, "do_delete\n", 0, 0, 0 );
50 #endif
51
52         /*
53          * Parse the delete request.  It looks like this:
54          *
55          *      DelRequest := DistinguishedName
56          */
57
58         if ( ber_scanf( op->o_ber, "m", &dn ) == LBER_ERROR ) {
59 #ifdef NEW_LOGGING
60                 LDAP_LOG( OPERATION, ERR, 
61                         "do_delete: conn: %d  ber_scanf failed\n", conn->c_connid, 0, 0 );
62 #else
63                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
64 #endif
65                 send_ldap_disconnect( conn, op,
66                         LDAP_PROTOCOL_ERROR, "decoding error" );
67                 return SLAPD_DISCONNECT;
68         }
69
70         if( ( rc = get_ctrls( conn, op, 1 ) ) != LDAP_SUCCESS ) {
71 #ifdef NEW_LOGGING
72                 LDAP_LOG( OPERATION, ERR, 
73                         "do_delete: conn %d  get_ctrls failed\n", conn->c_connid, 0, 0 );
74 #else
75                 Debug( LDAP_DEBUG_ANY, "do_delete: get_ctrls failed\n", 0, 0, 0 );
76 #endif
77                 goto cleanup;
78         } 
79
80         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
81         if( rc != LDAP_SUCCESS ) {
82 #ifdef NEW_LOGGING
83                 LDAP_LOG( OPERATION, INFO, 
84                         "do_delete: conn %d  invalid dn (%s)\n",
85                         conn->c_connid, dn.bv_val, 0 );
86 #else
87                 Debug( LDAP_DEBUG_ANY,
88                         "do_delete: invalid dn (%s)\n", dn.bv_val, 0, 0 );
89 #endif
90                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
91                     "invalid DN", NULL, NULL );
92                 goto cleanup;
93         }
94
95         if( 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                         conn->c_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_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
105                         NULL, "cannot delete the root DSE", NULL, NULL );
106                 goto cleanup;
107
108         } else if ( bvmatch( &ndn, &global_schemandn ) ) {
109 #ifdef NEW_LOGGING
110                 LDAP_LOG( OPERATION, INFO, "do_delete: conn %d: "
111                         "Attempt to delete subschema subentry.\n", conn->c_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_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
117                         NULL, "cannot delete the root DSE", NULL, NULL );
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, pdn.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 ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
132                 BerVarray ref = referral_rewrite( default_referral,
133                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
134
135                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
136                         NULL, NULL, ref ? ref : default_referral, NULL );
137
138                 ber_bvarray_free( ref );
139                 goto cleanup;
140         }
141
142         /* check restrictions */
143         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
144         if( rc != LDAP_SUCCESS ) {
145                 send_ldap_result( conn, op, rc,
146                         NULL, text, NULL, NULL );
147                 goto cleanup;
148         }
149
150         /* check for referrals */
151         rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
152         if ( rc != LDAP_SUCCESS ) {
153                 goto cleanup;
154         }
155
156         /* deref suffix alias if appropriate */
157         suffix_alias( be, &ndn );
158
159 #if defined( LDAP_SLAPI )
160         slapi_x_backend_set_pb( pb, be );
161         slapi_x_connection_set_pb( pb, conn );
162         slapi_x_operation_set_pb( pb, op );
163         slapi_pblock_set( pb, SLAPI_DELETE_TARGET, (void *)dn.bv_val );
164         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
165
166         rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_DELETE_FN, pb );
167         if ( rc != 0 ) {
168                 /*
169                  * A preoperation plugin failure will abort the
170                  * entire operation.
171                  */
172 #ifdef NEW_LOGGING
173                 LDAP_LOG( OPERATION, INFO, "do_delete: delete preoperation plugin "
174                                 "failed\n", 0, 0, 0 );
175 #else
176                 Debug (LDAP_DEBUG_TRACE, "do_delete: delete preoperation plugin failed.\n",
177                                 0, 0, 0);
178 #endif
179                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
180                         rc = LDAP_OTHER;
181                 goto cleanup;
182         }
183 #endif /* defined( LDAP_SLAPI ) */
184
185         /*
186          * do the delete if 1 && (2 || 3)
187          * 1) there is a delete function implemented in this backend;
188          * 2) this backend is master for what it holds;
189          * 3) it's a replica and the dn supplied is the update_ndn.
190          */
191         if ( be->be_delete ) {
192                 /* do the update here */
193                 int repl_user = be_isupdate( be, &op->o_ndn );
194 #ifndef SLAPD_MULTIMASTER
195                 if ( !be->be_update_ndn.bv_len || repl_user )
196 #endif
197                 {
198                         if ( (*be->be_delete)( be, conn, op, &pdn, &ndn ) == 0 ) {
199 #ifdef SLAPD_MULTIMASTER
200                                 if ( !be->be_update_ndn.bv_len || !repl_user )
201 #endif
202                                 {
203                                         replog( be, op, &pdn, &ndn, NULL );
204                                 }
205                         }
206 #ifndef SLAPD_MULTIMASTER
207                 } else {
208                         BerVarray defref = be->be_update_refs
209                                 ? be->be_update_refs : default_referral;
210                         BerVarray ref = referral_rewrite( default_referral,
211                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
212
213                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
214                                 ref ? ref : defref, NULL );
215
216                         ber_bvarray_free( ref );
217 #endif
218                 }
219
220         } else {
221                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
222                         NULL, "operation not supported within namingContext", NULL, NULL );
223         }
224
225 #if defined( LDAP_SLAPI )
226         if ( doPluginFNs( be, SLAPI_PLUGIN_POST_DELETE_FN, pb ) != 0) {
227 #ifdef NEW_LOGGING
228                 LDAP_LOG( OPERATION, INFO, "do_delete: delete postoperation plugins "
229                                 "failed\n", 0, 0, 0 );
230 #else
231                 Debug(LDAP_DEBUG_TRACE, "do_delete: delete postoperation plugins "
232                                 "failed.\n", 0, 0, 0);
233 #endif
234         }
235 #endif /* defined( LDAP_SLAPI ) */
236
237 cleanup:
238         free( pdn.bv_val );
239         free( ndn.bv_val );
240         return rc;
241 }