]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
0d16da652b8705c864955075af38d719b1b1f094
[openldap] / libraries / libldap / unbind.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  unbind.c
11  */
12
13 /* An Unbind Request looks like this:
14  *
15  *      UnbindRequest ::= NULL
16  *
17  * and has no response.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23 #include <ac/stdlib.h>
24
25 #include <ac/socket.h>
26 #include <ac/string.h>
27 #include <ac/time.h>
28
29 #include "ldap-int.h"
30
31 int
32 ldap_unbind_ext(
33         LDAP *ld,
34         LDAPControl **sctrls,
35         LDAPControl **cctrls )
36 {
37         int rc;
38
39         assert( ld != NULL );
40         assert( LDAP_VALID( ld ) );
41
42         /* check client controls */
43         rc = ldap_int_client_controls( ld, cctrls );
44         if( rc != LDAP_SUCCESS ) return rc;
45
46         return ldap_ld_free( ld, 1, sctrls, cctrls );
47 }
48
49 int
50 ldap_unbind_ext_s(
51         LDAP *ld,
52         LDAPControl **sctrls,
53         LDAPControl **cctrls )
54 {
55         return ldap_unbind_ext( ld, sctrls, cctrls );
56 }
57
58 int
59 ldap_unbind( LDAP *ld )
60 {
61         Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
62
63         return( ldap_unbind_ext( ld, NULL, NULL ) );
64 }
65
66
67 int
68 ldap_ld_free(
69         LDAP *ld,
70         int close,
71         LDAPControl **sctrls,
72         LDAPControl **cctrls )
73 {
74         LDAPMessage     *lm, *next;
75         int             err = LDAP_SUCCESS;
76
77         /* free LDAP structure and outstanding requests/responses */
78         while ( ld->ld_requests != NULL ) {
79                 ldap_free_request( ld, ld->ld_requests );
80         }
81
82         /* free and unbind from all open connections */
83         while ( ld->ld_conns != NULL ) {
84                 ldap_free_connection( ld, ld->ld_conns, 1, close );
85         }
86
87         for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
88                 next = lm->lm_next;
89                 ldap_msgfree( lm );
90         }
91
92 #ifndef LDAP_NOCACHE
93         if ( ld->ld_cache != NULL ) {
94                 ldap_destroy_cache( ld );
95                 ld->ld_cache = NULL;
96         }
97 #endif /* !LDAP_NOCACHE */
98
99         if ( ld->ld_error != NULL ) {
100                 LDAP_FREE( ld->ld_error );
101                 ld->ld_error = NULL;
102         }
103
104         if ( ld->ld_matched != NULL ) {
105                 LDAP_FREE( ld->ld_matched );
106                 ld->ld_matched = NULL;
107         }
108
109         if ( ld->ld_abandoned != NULL ) {
110                 LDAP_FREE( ld->ld_abandoned );
111                 ld->ld_abandoned = NULL;
112         }
113
114         if ( ld->ld_selectinfo != NULL ) {
115                 ldap_free_select_info( ld->ld_selectinfo );
116                 ld->ld_selectinfo = NULL;
117         }
118
119         if ( ld->ld_options.ldo_defludp != NULL ) {
120                 ldap_free_urllist( ld->ld_options.ldo_defludp );
121                 ld->ld_options.ldo_defludp = NULL;
122         }
123
124         if ( ld->ld_options.ldo_tm_api != NULL ) {
125                 LDAP_FREE( ld->ld_options.ldo_tm_api );
126                 ld->ld_options.ldo_tm_api = NULL;
127         }
128
129         if ( ld->ld_options.ldo_tm_net != NULL ) {
130                 LDAP_FREE( ld->ld_options.ldo_tm_net );
131                 ld->ld_options.ldo_tm_net = NULL;
132         }
133
134 #ifdef HAVE_CYRUS_SASL
135         if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
136                 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
137                 ld->ld_options.ldo_def_sasl_mech = NULL;
138         }
139
140         if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
141                 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
142                 ld->ld_options.ldo_def_sasl_realm = NULL;
143         }
144
145         if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
146                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
147                 ld->ld_options.ldo_def_sasl_authcid = NULL;
148         }
149
150         if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
151                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
152                 ld->ld_options.ldo_def_sasl_authzid = NULL;
153         }
154 #endif
155
156         ber_sockbuf_free( ld->ld_sb );   
157    
158         LDAP_FREE( (char *) ld );
159    
160         WSACleanup();
161
162         return( err );
163 }
164
165 int
166 ldap_unbind_s( LDAP *ld )
167 {
168         return( ldap_unbind_ext( ld, NULL, NULL ) );
169 }
170
171
172 int
173 ldap_send_unbind(
174         LDAP *ld,
175         Sockbuf *sb,
176         LDAPControl **sctrls,
177         LDAPControl **cctrls )
178 {
179         BerElement      *ber;
180
181         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
182
183 #ifdef LDAP_CONNECTIONLESS
184         if (LDAP_IS_UDP(ld))
185                 return LDAP_SUCCESS;
186 #endif
187         /* create a message to send */
188         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
189                 return( ld->ld_errno );
190         }
191
192         /* fill it in */
193         if ( ber_printf( ber, "{itn" /*}*/, ++ld->ld_msgid,
194             LDAP_REQ_UNBIND ) == -1 ) {
195                 ld->ld_errno = LDAP_ENCODING_ERROR;
196                 ber_free( ber, 1 );
197                 return( ld->ld_errno );
198         }
199
200         /* Put Server Controls */
201         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
202                 ber_free( ber, 1 );
203                 return ld->ld_errno;
204         }
205
206         if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
207                 ld->ld_errno = LDAP_ENCODING_ERROR;
208                 ber_free( ber, 1 );
209                 return( ld->ld_errno );
210         }
211
212         /* send the message */
213         if ( ber_flush( sb, ber, 1 ) == -1 ) {
214                 ld->ld_errno = LDAP_SERVER_DOWN;
215                 ber_free( ber, 1 );
216                 return( ld->ld_errno );
217         }
218
219         return( LDAP_SUCCESS );
220 }