1 /*	$NetBSD: delete.c,v 1.1.1.3 2010/12/12 15:23:10 adam Exp $	*/
2 
3 /* OpenLDAP: pkg/ldap/servers/slapd/back-meta/delete.c,v 1.37.2.10 2010/04/13 20:23:31 kurt Exp */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1999-2010 The OpenLDAP Foundation.
7  * Portions Copyright 2001-2003 Pierangelo Masarati.
8  * Portions Copyright 1999-2003 Howard Chu.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* ACKNOWLEDGEMENTS:
20  * This work was initially developed by the Howard Chu for inclusion
21  * in OpenLDAP Software and subsequently enhanced by Pierangelo
22  * Masarati.
23  */
24 
25 #include "portable.h"
26 
27 #include <stdio.h>
28 
29 #include <ac/string.h>
30 #include <ac/socket.h>
31 
32 #include "slap.h"
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
35 
36 int
37 meta_back_delete( Operation *op, SlapReply *rs )
38 {
39 	metainfo_t	*mi = ( metainfo_t * )op->o_bd->be_private;
40 	metatarget_t	*mt;
41 	metaconn_t	*mc = NULL;
42 	int		candidate = -1;
43 	struct berval	mdn = BER_BVNULL;
44 	dncookie	dc;
45 	int		msgid;
46 	ldap_back_send_t	retrying = LDAP_BACK_RETRYING;
47 	LDAPControl	**ctrls = NULL;
48 
49 	mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
50 	if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
51 		return rs->sr_err;
52 	}
53 
54 	assert( mc->mc_conns[ candidate ].msc_ld != NULL );
55 
56 	/*
57 	 * Rewrite the compare dn, if needed
58 	 */
59 	mt = mi->mi_targets[ candidate ];
60 	dc.target = mt;
61 	dc.conn = op->o_conn;
62 	dc.rs = rs;
63 	dc.ctx = "deleteDN";
64 
65 	if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
66 		send_ldap_result( op, rs );
67 		goto cleanup;
68 	}
69 
70 retry:;
71 	ctrls = op->o_ctrls;
72 	if ( meta_back_controls_add( op, rs, mc, candidate, &ctrls ) != LDAP_SUCCESS )
73 	{
74 		send_ldap_result( op, rs );
75 		goto cleanup;
76 	}
77 
78 	rs->sr_err = ldap_delete_ext( mc->mc_conns[ candidate ].msc_ld,
79 			mdn.bv_val, ctrls, NULL, &msgid );
80 	rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
81 		mt->mt_timeout[ SLAP_OP_DELETE ], ( LDAP_BACK_SENDRESULT | retrying ) );
82 	if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
83 		retrying &= ~LDAP_BACK_RETRYING;
84 		if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
85 			/* if the identity changed, there might be need to re-authz */
86 			(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
87 			goto retry;
88 		}
89 	}
90 
91 cleanup:;
92 	(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
93 
94 	if ( mdn.bv_val != op->o_req_dn.bv_val ) {
95 		free( mdn.bv_val );
96 		BER_BVZERO( &mdn );
97 	}
98 
99 	if ( mc ) {
100 		meta_back_release_conn( mi, mc );
101 	}
102 
103 	return rs->sr_err;
104 }
105 
106