xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-meta/unbind.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: unbind.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: unbind.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/errno.h>
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 
36 #include "slap.h"
37 #include "../back-ldap/back-ldap.h"
38 #include "back-meta.h"
39 
40 int
meta_back_conn_destroy(Backend * be,Connection * conn)41 meta_back_conn_destroy(
42 	Backend		*be,
43 	Connection	*conn )
44 {
45 	metainfo_t	*mi = ( metainfo_t * )be->be_private;
46 	metaconn_t	*mc,
47 			mc_curr = {{ 0 }};
48 	int		i;
49 
50 
51 	Debug( LDAP_DEBUG_TRACE,
52 		"=>meta_back_conn_destroy: fetching conn=%ld DN=\"%s\"\n",
53 		conn->c_connid,
54 		BER_BVISNULL( &conn->c_ndn ) ? "" : conn->c_ndn.bv_val );
55 
56 	mc_curr.mc_conn = conn;
57 
58 	ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
59 #if META_BACK_PRINT_CONNTREE > 0
60 	meta_back_print_conntree( mi, ">>> meta_back_conn_destroy" );
61 #endif /* META_BACK_PRINT_CONNTREE */
62 	while ( ( mc = ldap_tavl_delete( &mi->mi_conninfo.lai_tree, ( caddr_t )&mc_curr, meta_back_conn_cmp ) ) != NULL )
63 	{
64 		assert( !LDAP_BACK_PCONN_ISPRIV( mc ) );
65 		Debug( LDAP_DEBUG_TRACE,
66 			"=>meta_back_conn_destroy: destroying conn %lu "
67 			"refcnt=%d flags=0x%08x\n",
68 			mc->mc_conn->c_connid, mc->mc_refcnt, mc->msc_mscflags );
69 
70 		if ( mc->mc_refcnt > 0 ) {
71 			/* someone else might be accessing the connection;
72 			 * mark for deletion */
73 			LDAP_BACK_CONN_CACHED_CLEAR( mc );
74 			LDAP_BACK_CONN_TAINTED_SET( mc );
75 
76 		} else {
77 			meta_back_conn_free( mc );
78 		}
79 	}
80 #if META_BACK_PRINT_CONNTREE > 0
81 	meta_back_print_conntree( mi, "<<< meta_back_conn_destroy" );
82 #endif /* META_BACK_PRINT_CONNTREE */
83 	ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
84 
85 	/*
86 	 * Cleanup rewrite session
87 	 */
88 	for ( i = 0; i < mi->mi_ntargets; ++i ) {
89 		rewrite_session_delete( mi->mi_targets[ i ]->mt_rwmap.rwm_rw, conn );
90 	}
91 
92 	return 0;
93 }
94 
95