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