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