xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-meta/delete.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: delete.c,v 1.3 2021/08/14 16:15:00 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1999-2021 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 <sys/cdefs.h>
26 __RCSID("$NetBSD: delete.c,v 1.3 2021/08/14 16:15:00 christos Exp $");
27 
28 #include "portable.h"
29 
30 #include <stdio.h>
31 
32 #include <ac/string.h>
33 #include <ac/socket.h>
34 
35 #include "slap.h"
36 #include "../back-ldap/back-ldap.h"
37 #include "back-meta.h"
38 
39 int
meta_back_delete(Operation * op,SlapReply * rs)40 meta_back_delete( Operation *op, SlapReply *rs )
41 {
42 	metainfo_t	*mi = ( metainfo_t * )op->o_bd->be_private;
43 	metatarget_t	*mt;
44 	metaconn_t	*mc = NULL;
45 	int		candidate = -1;
46 	struct berval	mdn = BER_BVNULL;
47 	dncookie	dc;
48 	int		msgid;
49 	ldap_back_send_t	retrying = LDAP_BACK_RETRYING;
50 	LDAPControl	**ctrls = NULL;
51 
52 	mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
53 	if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
54 		return rs->sr_err;
55 	}
56 
57 	assert( mc->mc_conns[ candidate ].msc_ld != NULL );
58 
59 	/*
60 	 * Rewrite the compare dn, if needed
61 	 */
62 	mt = mi->mi_targets[ candidate ];
63 	dc.target = mt;
64 	dc.conn = op->o_conn;
65 	dc.rs = rs;
66 	dc.ctx = "deleteDN";
67 
68 	if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
69 		send_ldap_result( op, rs );
70 		goto cleanup;
71 	}
72 
73 retry:;
74 	ctrls = op->o_ctrls;
75 	if ( meta_back_controls_add( op, rs, mc, candidate, &ctrls ) != LDAP_SUCCESS )
76 	{
77 		send_ldap_result( op, rs );
78 		goto cleanup;
79 	}
80 
81 	rs->sr_err = ldap_delete_ext( mc->mc_conns[ candidate ].msc_ld,
82 			mdn.bv_val, ctrls, NULL, &msgid );
83 	rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
84 		mt->mt_timeout[ SLAP_OP_DELETE ], ( LDAP_BACK_SENDRESULT | retrying ) );
85 	if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
86 		retrying &= ~LDAP_BACK_RETRYING;
87 		if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
88 			/* if the identity changed, there might be need to re-authz */
89 			(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
90 			goto retry;
91 		}
92 	}
93 
94 cleanup:;
95 	(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
96 
97 	if ( mdn.bv_val != op->o_req_dn.bv_val ) {
98 		free( mdn.bv_val );
99 		BER_BVZERO( &mdn );
100 	}
101 
102 	if ( mc ) {
103 		meta_back_release_conn( mi, mc );
104 	}
105 
106 	return rs->sr_err;
107 }
108 
109