xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-ldap/unbind.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: unbind.c,v 1.3 2021/08/14 16:14:59 christos Exp $	*/
2 
3 /* unbind.c - ldap backend unbind function */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1999-2021 The OpenLDAP Foundation.
8  * Portions Copyright 1999-2003 Howard Chu.
9  * Portions Copyright 2000-2003 Pierangelo Masarati.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted only as authorized by the OpenLDAP
14  * Public License.
15  *
16  * A copy of this license is available in the file LICENSE in the
17  * top-level directory of the distribution or, alternatively, at
18  * <http://www.OpenLDAP.org/license.html>.
19  */
20 /* ACKNOWLEDGEMENTS:
21  * This work was initially developed by the Howard Chu for inclusion
22  * in OpenLDAP Software and subsequently enhanced by Pierangelo
23  * Masarati.
24  */
25 
26 #include <sys/cdefs.h>
27 __RCSID("$NetBSD: unbind.c,v 1.3 2021/08/14 16:14:59 christos Exp $");
28 
29 #include "portable.h"
30 
31 #include <stdio.h>
32 
33 #include <ac/errno.h>
34 #include <ac/socket.h>
35 #include <ac/string.h>
36 
37 #include "slap.h"
38 #include "back-ldap.h"
39 
40 int
ldap_back_conn_destroy(Backend * be,Connection * conn)41 ldap_back_conn_destroy(
42 		Backend		*be,
43 		Connection	*conn
44 )
45 {
46 	ldapinfo_t	*li = (ldapinfo_t *) be->be_private;
47 	ldapconn_t	*lc = NULL, lc_curr;
48 
49 	Debug( LDAP_DEBUG_TRACE,
50 		"=>ldap_back_conn_destroy: fetching conn %ld\n",
51 		conn->c_connid );
52 
53 	lc_curr.lc_conn = conn;
54 
55 	ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
56 #if LDAP_BACK_PRINT_CONNTREE > 0
57 	ldap_back_print_conntree( li, ">>> ldap_back_conn_destroy" );
58 #endif /* LDAP_BACK_PRINT_CONNTREE */
59 	while ( ( lc = ldap_tavl_delete( &li->li_conninfo.lai_tree, (caddr_t)&lc_curr, ldap_back_conn_cmp ) ) != NULL )
60 	{
61 		assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
62 		Debug( LDAP_DEBUG_TRACE,
63 			"=>ldap_back_conn_destroy: destroying conn %lu "
64 			"refcnt=%d flags=0x%08x\n",
65 			lc->lc_conn->c_connid, lc->lc_refcnt, lc->lc_lcflags );
66 
67 		if ( lc->lc_refcnt > 0 ) {
68 			/* someone else might be accessing the connection;
69 			 * mark for deletion */
70 			LDAP_BACK_CONN_CACHED_CLEAR( lc );
71 			LDAP_BACK_CONN_TAINTED_SET( lc );
72 
73 		} else {
74 			ldap_back_conn_free( lc );
75 		}
76 	}
77 #if LDAP_BACK_PRINT_CONNTREE > 0
78 	ldap_back_print_conntree( li, "<<< ldap_back_conn_destroy" );
79 #endif /* LDAP_BACK_PRINT_CONNTREE */
80 	ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
81 
82 	return 0;
83 }
84