xref: /onnv-gate/usr/src/lib/libldap4/common/unbind.c (revision 3857:21b9b714e4ab)
1 /*
2  *
3  * Portions Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
5  *
6  */
7 
8 #pragma ident	"%Z%%M%	%I%	%E% SMI"
9 
10 /*
11  *  Copyright (c) 1990 Regents of the University of Michigan.
12  *  All rights reserved.
13  *
14  *  unbind.c
15  */
16 
17 #ifndef lint
18 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
19 #endif
20 
21 #include <stdio.h>
22 #include <string.h>
23 #ifdef MACOS
24 #include <stdlib.h>
25 #include "macos.h"
26 #else /* MACOS */
27 #if defined( DOS ) || defined( _WIN32 )
28 #include "msdos.h"
29 #ifdef NCSA
30 #include "externs.h"
31 #endif /* NCSA */
32 #else /* DOS */
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 #endif /* DOS */
37 #endif /* MACOS */
38 
39 #include "lber.h"
40 #include "ldap.h"
41 #include "ldap-private.h"
42 #include "ldap-int.h"
43 
44 
45 int
ldap_unbind(LDAP * ld)46 ldap_unbind( LDAP *ld )
47 {
48 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 259, "ldap_unbind\n"), 0, 0, 0 );
49 
50 	return( ldap_ld_free( ld, 1 ));
51 }
52 
53 
54 int
ldap_ld_free(LDAP * ld,int close)55 ldap_ld_free( LDAP *ld, int close )
56 {
57 	LDAPMessage	*lm, *next;
58 	int		err = LDAP_SUCCESS;
59 	LDAPRequest	*lr, *nextlr;
60 
61 #ifdef _REENTRANT
62         LOCK_LDAP(ld);
63 #endif
64 	if ( ld->ld_sb.sb_naddr == 0 ) {
65 		/* free LDAP structure and outstanding requests/responses */
66 		for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
67 			nextlr = lr->lr_next;
68 			free_request( ld, lr );
69 		}
70 
71 		/* free and unbind from all open connections */
72 		while ( ld->ld_conns != NULL ) {
73 			free_connection( ld, ld->ld_conns, 1, close );
74 		}
75 	} else {
76 		int	i;
77 
78 		for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
79 			free( ld->ld_sb.sb_addrs[ i ] );
80 		}
81 		free( ld->ld_sb.sb_addrs );
82 		free( ld->ld_sb.sb_fromaddr );
83 #ifdef LDAP_SSL
84 		if (ld->ld_sb.sb_ssl){
85 			SSL_delete(ld->ld_sb.sb_ssl);
86 		}
87 			ld->ld_sb.sb_ssl = NULL;
88 			ld->ld_sb.sb_ssl_tls = 0;
89 #endif
90 	}
91 
92 	if (ld->ld_sb.sb_ber.ber_buf) {
93 		free(ld->ld_sb.sb_ber.ber_buf);
94 		ld->ld_sb.sb_ber.ber_buf = NULL;
95 	}
96 
97 #ifdef _REENTRANT
98 	LOCK_RESPONSE(ld);
99 #endif
100 	for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
101 		next = lm->lm_next;
102 		ldap_msgfree( lm );
103 	}
104 
105 #ifdef _REENTRANT
106 	UNLOCK_RESPONSE(ld);
107 #endif
108 
109 #ifndef NO_CACHE
110 	if ( ld->ld_cache != NULL )
111 		ldap_destroy_cache( ld );
112 #endif /* !NO_CACHE */
113 	if ( ld->ld_error != NULL )
114 		free( ld->ld_error );
115 	if ( ld->ld_matched != NULL )
116 		free( ld->ld_matched );
117 	if ( ld->ld_host != NULL )
118 		free( ld->ld_host );
119 	if ( ld->ld_ufnprefix != NULL )
120 		free( ld->ld_ufnprefix );
121 	if ( ld->ld_filtd != NULL )
122 		ldap_getfilter_free( ld->ld_filtd );
123 	if ( ld->ld_abandoned != NULL )
124 		free( ld->ld_abandoned );
125 
126 	if ( ld->ld_selectinfo != NULL )
127 		free_select_info( ld->ld_selectinfo );
128 
129 	if ( ld->ld_defhost != NULL )
130 		free( ld->ld_defhost );
131 
132 #ifdef LDAP_SSL
133 	if (ld->ld_ssl_key != NULL)
134 		free(ld->ld_ssl_key);
135 #endif
136 
137 #undef ld_attrbuffer
138 	{
139 		/* free thread-specific attr buffers */
140 		int i;
141 
142 		for (i = 0; i < MAX_THREAD_ID; i++)
143 			if (ld->ld_attrbuffer[i] != NULL) {
144 				free(ld->ld_attrbuffer[i]);
145 				ld->ld_attrbuffer[i] = NULL;
146 			}
147 	}
148 /* free ldapv3 stuff */
149 	if (ld->ld_srvctrls != NULL)
150 		ldap_controls_free(ld->ld_srvctrls);
151 	if (ld->ld_cltctrls != NULL)
152 		ldap_controls_free(ld->ld_cltctrls);
153 
154 #ifdef _REENTRANT
155         UNLOCK_LDAP(ld);
156 
157 	if (ld->ld_lockcount != 0)
158 		Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 260, "Mutex problem: ld_lockcount not equal to zero when freeing context\n"), 0, 0, 0 );
159 #endif
160 
161 	free( (char *) ld );
162 
163 	return( err );
164 }
165 
166 int
ldap_unbind_s(LDAP * ld)167 ldap_unbind_s( LDAP *ld )
168 {
169 	return( ldap_ld_free( ld, 1 ));
170 }
171 
172 
173 int
send_unbind(LDAP * ld,Sockbuf * sb)174 send_unbind( LDAP *ld, Sockbuf *sb )
175 {
176 	BerElement	*ber;
177 
178 #if defined( SUN ) && defined( _REENTRANT )
179         LOCK_LDAP(ld);
180 #endif
181 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 261, "send_unbind\n"), 0, 0, 0 );
182 
183 	/* create a message to send */
184 	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
185 #if defined( SUN ) && defined( _REENTRANT )
186 		UNLOCK_LDAP(ld);
187 #endif
188 		return( ld->ld_errno );
189 	}
190 
191 	/* fill it in */
192 	if ( ber_printf( ber, "{itn}", ++ld->ld_msgid,
193 	    LDAP_REQ_UNBIND ) == -1 ) {
194 		ld->ld_errno = LDAP_ENCODING_ERROR;
195 		ber_free( ber, 1 );
196 #if defined( SUN ) && defined( _REENTRANT )
197 		UNLOCK_LDAP(ld);
198 #endif
199 		return( ld->ld_errno );
200 	}
201 
202 	/* send the message */
203 	if ( ber_flush( sb, ber, 1 ) == -1 ) {
204 		ld->ld_errno = LDAP_SERVER_DOWN;
205 		ber_free( ber, 1 );
206 #if defined( SUN ) && defined( _REENTRANT )
207 		UNLOCK_LDAP(ld);
208 #endif
209 		return( ld->ld_errno );
210 	}
211 
212 #if defined( SUN ) && defined( _REENTRANT )
213         UNLOCK_LDAP(ld);
214 #endif
215 	return( LDAP_SUCCESS );
216 }
217