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