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