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