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