summaryrefslogtreecommitdiff
path: root/usr/src/lib/libldap4/common/unbind.c
blob: fc96ab2e3782cb2d8919463c1112adac68e495da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 *
 * Portions Copyright %G% Sun Microsystems, Inc. 
 * All Rights Reserved
 *
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 *  Copyright (c) 1990 Regents of the University of Michigan.
 *  All rights reserved.
 *
 *  unbind.c
 */

#ifndef lint 
static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
#endif

#include <stdio.h>
#include <string.h>
#ifdef MACOS
#include <stdlib.h>
#include "macos.h"
#else /* MACOS */
#if defined( DOS ) || defined( _WIN32 )
#include "msdos.h"
#ifdef NCSA
#include "externs.h"
#endif /* NCSA */
#else /* DOS */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#endif /* DOS */
#endif /* MACOS */

#include "lber.h"
#include "ldap.h"
#include "ldap-private.h"
#include "ldap-int.h"


int
ldap_unbind( LDAP *ld )
{
	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 259, "ldap_unbind\n"), 0, 0, 0 );

	return( ldap_ld_free( ld, 1 ));
}


int
ldap_ld_free( LDAP *ld, int close )
{
	LDAPMessage	*lm, *next;
	int		err = LDAP_SUCCESS;
	LDAPRequest	*lr, *nextlr;

#ifdef _REENTRANT
        LOCK_LDAP(ld);
#endif
	if ( ld->ld_sb.sb_naddr == 0 ) {
		/* free LDAP structure and outstanding requests/responses */
		for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
			nextlr = lr->lr_next;
			free_request( ld, lr );
		}

		/* free and unbind from all open connections */
		while ( ld->ld_conns != NULL ) {
			free_connection( ld, ld->ld_conns, 1, close );
		}
	} else {
		int	i;

		for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
			free( ld->ld_sb.sb_addrs[ i ] );
		}
		free( ld->ld_sb.sb_addrs );
		free( ld->ld_sb.sb_fromaddr );
#ifdef LDAP_SSL
		if (ld->ld_sb.sb_ssl){
			SSL_delete(ld->ld_sb.sb_ssl);
		}
			ld->ld_sb.sb_ssl = NULL;
			ld->ld_sb.sb_ssl_tls = 0;
#endif
	}

	if (ld->ld_sb.sb_ber.ber_buf) {
		free(ld->ld_sb.sb_ber.ber_buf);
		ld->ld_sb.sb_ber.ber_buf = NULL;
	}

#ifdef _REENTRANT
	LOCK_RESPONSE(ld);
#endif
	for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
		next = lm->lm_next;
		ldap_msgfree( lm );
	}

#ifdef _REENTRANT 
	UNLOCK_RESPONSE(ld);
#endif

#ifndef NO_CACHE
	if ( ld->ld_cache != NULL )
		ldap_destroy_cache( ld );
#endif /* !NO_CACHE */
	if ( ld->ld_error != NULL )
		free( ld->ld_error );
	if ( ld->ld_matched != NULL )
		free( ld->ld_matched );
	if ( ld->ld_host != NULL )
		free( ld->ld_host );
	if ( ld->ld_ufnprefix != NULL )
		free( ld->ld_ufnprefix );
	if ( ld->ld_filtd != NULL )
		ldap_getfilter_free( ld->ld_filtd );
	if ( ld->ld_abandoned != NULL )
		free( ld->ld_abandoned );

	if ( ld->ld_selectinfo != NULL )
		free_select_info( ld->ld_selectinfo );

	if ( ld->ld_defhost != NULL )
		free( ld->ld_defhost );

#ifdef LDAP_SSL
	if (ld->ld_ssl_key != NULL)
		free(ld->ld_ssl_key);
#endif

#undef ld_attrbuffer
	{
		/* free thread-specific attr buffers */
		int i;

		for (i = 0; i < MAX_THREAD_ID; i++)
			if (ld->ld_attrbuffer[i] != NULL) {
				free(ld->ld_attrbuffer[i]);
				ld->ld_attrbuffer[i] = NULL;
			}
	}
/* free ldapv3 stuff */
	if (ld->ld_srvctrls != NULL)
		ldap_controls_free(ld->ld_srvctrls);
	if (ld->ld_cltctrls != NULL)
		ldap_controls_free(ld->ld_cltctrls);
	
#ifdef _REENTRANT
        UNLOCK_LDAP(ld);

	if (ld->ld_lockcount != 0)
		Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 260, "Mutex problem: ld_lockcount not equal to zero when freeing context\n"), 0, 0, 0 );
#endif

	free( (char *) ld );

	return( err );
}

int
ldap_unbind_s( LDAP *ld )
{
	return( ldap_ld_free( ld, 1 ));
}


int
send_unbind( LDAP *ld, Sockbuf *sb )
{
	BerElement	*ber;

#if defined( SUN ) && defined( _REENTRANT )
        LOCK_LDAP(ld);
#endif
	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 261, "send_unbind\n"), 0, 0, 0 );

	/* create a message to send */
	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
#if defined( SUN ) && defined( _REENTRANT )
		UNLOCK_LDAP(ld);
#endif
		return( ld->ld_errno );
	}

	/* fill it in */
	if ( ber_printf( ber, "{itn}", ++ld->ld_msgid,
	    LDAP_REQ_UNBIND ) == -1 ) {
		ld->ld_errno = LDAP_ENCODING_ERROR;
		ber_free( ber, 1 );
#if defined( SUN ) && defined( _REENTRANT )
		UNLOCK_LDAP(ld);
#endif
		return( ld->ld_errno );
	}

	/* send the message */
	if ( ber_flush( sb, ber, 1 ) == -1 ) {
		ld->ld_errno = LDAP_SERVER_DOWN;
		ber_free( ber, 1 );
#if defined( SUN ) && defined( _REENTRANT )
		UNLOCK_LDAP(ld);
#endif
		return( ld->ld_errno );
	}

#if defined( SUN ) && defined( _REENTRANT )
        UNLOCK_LDAP(ld);
#endif
	return( LDAP_SUCCESS );
}