]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
97f073e1b9c4e0aceec34ba0283252949f192a22
[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
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 int
29 do_delete(
30     Connection  *conn,
31     Operation   *op
32 )
33 {
34         struct berval dn = { 0, NULL };
35         struct berval pdn = { 0, NULL };
36         struct berval ndn = { 0, NULL };
37         const char *text;
38         Backend *be;
39         int rc;
40         int manageDSAit;
41
42 #ifdef NEW_LOGGING
43         LDAP_LOG( OPERATION, ENTRY, 
44                 "do_delete: conn %d\n", conn->c_connid, 0, 0 );
45 #else
46         Debug( LDAP_DEBUG_TRACE, "do_delete\n", 0, 0, 0 );
47 #endif
48
49         /*
50          * Parse the delete request.  It looks like this:
51          *
52          *      DelRequest := DistinguishedName
53          */
54
55         if ( ber_scanf( op->o_ber, "m", &dn ) == LBER_ERROR ) {
56 #ifdef NEW_LOGGING
57                 LDAP_LOG( OPERATION, ERR, 
58                         "do_delete: conn: %d  ber_scanf failed\n", conn->c_connid, 0, 0 );
59 #else
60                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
61 #endif
62                 send_ldap_disconnect( conn, op,
63                         LDAP_PROTOCOL_ERROR, "decoding error" );
64                 return SLAPD_DISCONNECT;
65         }
66
67         if( ( rc = get_ctrls( conn, op, 1 ) ) != LDAP_SUCCESS ) {
68 #ifdef NEW_LOGGING
69                 LDAP_LOG( OPERATION, ERR, 
70                         "do_delete: conn %d  get_ctrls failed\n", conn->c_connid, 0, 0 );
71 #else
72                 Debug( LDAP_DEBUG_ANY, "do_delete: get_ctrls failed\n", 0, 0, 0 );
73 #endif
74                 goto cleanup;
75         } 
76
77         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
78         if( rc != LDAP_SUCCESS ) {
79 #ifdef NEW_LOGGING
80                 LDAP_LOG( OPERATION, INFO, 
81                         "do_delete: conn %d  invalid dn (%s)\n",
82                         conn->c_connid, dn.bv_val, 0 );
83 #else
84                 Debug( LDAP_DEBUG_ANY,
85                         "do_delete: invalid dn (%s)\n", dn.bv_val, 0, 0 );
86 #endif
87                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
88                     "invalid DN", NULL, NULL );
89                 goto cleanup;
90         }
91
92         if( ndn.bv_len == 0 ) {
93 #ifdef NEW_LOGGING
94                 LDAP_LOG( OPERATION, INFO, 
95                         "do_delete: conn %d: Attempt to delete root DSE.\n", 
96                         conn->c_connid, 0, 0 );
97 #else
98                 Debug( LDAP_DEBUG_ANY, "do_delete: root dse!\n", 0, 0, 0 );
99 #endif
100                 /* protocolError would likely be a more appropriate error */
101                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
102                         NULL, "cannot delete the root DSE", NULL, NULL );
103                 goto cleanup;
104
105         } else if ( bvmatch( &ndn, &global_schemandn ) ) {
106 #ifdef NEW_LOGGING
107                 LDAP_LOG( OPERATION, INFO, "do_delete: conn %d: "
108                         "Attempt to delete subschema subentry.\n", conn->c_connid, 0, 0 );
109 #else
110                 Debug( LDAP_DEBUG_ANY, "do_delete: subschema subentry!\n", 0, 0, 0 );
111 #endif
112                 /* protocolError would likely be a more appropriate error */
113                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
114                         NULL, "cannot delete the root DSE", NULL, NULL );
115                 goto cleanup;
116         }
117
118         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu DEL dn=\"%s\"\n",
119                 op->o_connid, op->o_opid, pdn.bv_val, 0, 0 );
120
121         manageDSAit = get_manageDSAit( op );
122
123         /*
124          * We could be serving multiple database backends.  Select the
125          * appropriate one, or send a referral to our "referral server"
126          * if we don't hold it.
127          */
128         if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
129                 BerVarray ref = referral_rewrite( default_referral,
130                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
131
132                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
133                         NULL, NULL, ref ? ref : default_referral, NULL );
134
135                 ber_bvarray_free( ref );
136                 goto cleanup;
137         }
138
139         /* check restrictions */
140         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
141         if( rc != LDAP_SUCCESS ) {
142                 send_ldap_result( conn, op, rc,
143                         NULL, text, NULL, NULL );
144                 goto cleanup;
145         }
146
147         /* check for referrals */
148         rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
149         if ( rc != LDAP_SUCCESS ) {
150                 goto cleanup;
151         }
152
153         /* deref suffix alias if appropriate */
154         suffix_alias( be, &ndn );
155
156         /*
157          * do the delete if 1 && (2 || 3)
158          * 1) there is a delete function implemented in this backend;
159          * 2) this backend is master for what it holds;
160          * 3) it's a replica and the dn supplied is the update_ndn.
161          */
162         if ( be->be_delete ) {
163                 /* do the update here */
164                 int repl_user = be_isupdate( be, &op->o_ndn );
165 #ifndef SLAPD_MULTIMASTER
166                 if ( !be->be_update_ndn.bv_len || repl_user )
167 #endif
168                 {
169                         if ( (*be->be_delete)( be, conn, op, &pdn, &ndn ) == 0 ) {
170 #ifdef SLAPD_MULTIMASTER
171                                 if ( !be->be_update_ndn.bv_len || !repl_user )
172 #endif
173                                 {
174                                         replog( be, op, &pdn, &ndn, NULL );
175                                 }
176                         }
177 #ifndef SLAPD_MULTIMASTER
178                 } else {
179                         BerVarray defref = be->be_update_refs
180                                 ? be->be_update_refs : default_referral;
181                         BerVarray ref = referral_rewrite( default_referral,
182                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
183
184                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
185                                 ref ? ref : defref, NULL );
186
187                         ber_bvarray_free( ref );
188 #endif
189                 }
190
191         } else {
192                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
193                         NULL, "operation not supported within namingContext", NULL, NULL );
194         }
195
196 cleanup:
197         free( pdn.bv_val );
198         free( ndn.bv_val );
199         return rc;
200 }