]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
557160f1ac68aceb8f129d1541f1b2e16b76061d
[openldap] / libraries / libldap / unbind.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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         /* check client controls */
40         rc = ldap_int_client_controls( ld, cctrls );
41         if( rc != LDAP_SUCCESS ) return rc;
42
43         return ldap_ld_free( ld, 1, sctrls, cctrls );
44 }
45
46 int
47 ldap_unbind_ext_s(
48         LDAP *ld,
49         LDAPControl **sctrls,
50         LDAPControl **cctrls )
51 {
52         return ldap_unbind_ext( ld, sctrls, cctrls );
53 }
54
55 int
56 ldap_unbind( LDAP *ld )
57 {
58         Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
59
60         return( ldap_unbind_ext( ld, NULL, NULL ) );
61 }
62
63
64 int
65 ldap_ld_free(
66         LDAP *ld,
67         int close,
68         LDAPControl **sctrls,
69         LDAPControl **cctrls )
70 {
71         LDAPMessage     *lm, *next;
72         int             err = LDAP_SUCCESS;
73
74         /* free LDAP structure and outstanding requests/responses */
75         while ( ld->ld_requests != NULL ) {
76                 ldap_free_request( ld, ld->ld_requests );
77         }
78
79                 /* free and unbind from all open connections */
80                 while ( ld->ld_conns != NULL ) {
81                         ldap_free_connection( ld, ld->ld_conns, 1, close );
82                 }
83
84         for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
85                 next = lm->lm_next;
86                 ldap_msgfree( lm );
87         }
88
89 #ifndef LDAP_NOCACHE
90         if ( ld->ld_cache != NULL ) {
91                 ldap_destroy_cache( ld );
92                 ld->ld_cache = NULL;
93         }
94 #endif /* !LDAP_NOCACHE */
95
96         if ( ld->ld_error != NULL ) {
97                 LDAP_FREE( ld->ld_error );
98                 ld->ld_error = NULL;
99         }
100
101         if ( ld->ld_matched != NULL ) {
102                 LDAP_FREE( ld->ld_matched );
103                 ld->ld_matched = NULL;
104         }
105
106         if ( ld->ld_abandoned != NULL ) {
107                 LDAP_FREE( ld->ld_abandoned );
108                 ld->ld_abandoned = NULL;
109         }
110
111         if ( ld->ld_selectinfo != NULL ) {
112                 ldap_free_select_info( ld->ld_selectinfo );
113                 ld->ld_selectinfo = NULL;
114         }
115
116         if ( ld->ld_options.ldo_defludp != NULL ) {
117                 ldap_free_urllist( ld->ld_options.ldo_defludp );
118                 ld->ld_options.ldo_defludp = NULL;
119         }
120
121         if ( ld->ld_options.ldo_tm_api != NULL ) {
122                 LDAP_FREE( ld->ld_options.ldo_tm_api );
123                 ld->ld_options.ldo_tm_api = NULL;
124         }
125
126         if ( ld->ld_options.ldo_tm_net != NULL ) {
127                 LDAP_FREE( ld->ld_options.ldo_tm_net );
128                 ld->ld_options.ldo_tm_net = NULL;
129         }
130
131 #ifdef HAVE_CYRUS_SASL
132         if ( ld->ld_options.ldo_def_sasl_mech != NULL ) {
133                 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
134                 ld->ld_options.ldo_def_sasl_mech = NULL;
135         }
136
137         if ( ld->ld_options.ldo_def_sasl_realm != NULL ) {
138                 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
139                 ld->ld_options.ldo_def_sasl_realm = NULL;
140         }
141
142         if ( ld->ld_options.ldo_def_sasl_authcid != NULL ) {
143                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
144                 ld->ld_options.ldo_def_sasl_authcid = NULL;
145         }
146
147         if ( ld->ld_options.ldo_def_sasl_authzid != NULL ) {
148                 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
149                 ld->ld_options.ldo_def_sasl_authzid = NULL;
150         }
151 #endif
152
153         ber_sockbuf_free( ld->ld_sb );   
154    
155         LDAP_FREE( (char *) ld );
156    
157         WSACleanup();
158
159         return( err );
160 }
161
162 int
163 ldap_unbind_s( LDAP *ld )
164 {
165         return( ldap_unbind_ext( ld, NULL, NULL ) );
166 }
167
168
169 int
170 ldap_send_unbind(
171         LDAP *ld,
172         Sockbuf *sb,
173         LDAPControl **sctrls,
174         LDAPControl **cctrls )
175 {
176         BerElement      *ber;
177
178         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
179
180         /* create a message to send */
181         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
182                 return( ld->ld_errno );
183         }
184
185         /* fill it in */
186         if ( ber_printf( ber, "{itn" /*}*/, ++ld->ld_msgid,
187             LDAP_REQ_UNBIND ) == -1 ) {
188                 ld->ld_errno = LDAP_ENCODING_ERROR;
189                 ber_free( ber, 1 );
190                 return( ld->ld_errno );
191         }
192
193         /* Put Server Controls */
194         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
195                 ber_free( ber, 1 );
196                 return ld->ld_errno;
197         }
198
199         if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
200                 ld->ld_errno = LDAP_ENCODING_ERROR;
201                 ber_free( ber, 1 );
202                 return( ld->ld_errno );
203         }
204
205         /* send the message */
206         if ( ber_flush( sb, ber, 1 ) == -1 ) {
207                 ld->ld_errno = LDAP_SERVER_DOWN;
208                 ber_free( ber, 1 );
209                 return( ld->ld_errno );
210         }
211
212         return( LDAP_SUCCESS );
213 }