]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
Changes for URI spport: New routines ldap_initialize and ldap_create; LDAPURLDesc...
[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_defludp != NULL ) {
128                 ldap_free_urllist( ld->ld_options.ldo_defludp );
129                 ld->ld_options.ldo_defludp = NULL;
130         }
131
132         if ( ld->ld_options.ldo_tm_api != NULL ) {
133                 LDAP_FREE( ld->ld_options.ldo_tm_api );
134                 ld->ld_options.ldo_tm_api = NULL;
135         }
136
137         if ( ld->ld_options.ldo_tm_net != NULL ) {
138                 LDAP_FREE( ld->ld_options.ldo_tm_net );
139                 ld->ld_options.ldo_tm_net = NULL;
140         }
141
142         ber_pvt_sb_destroy( &(ld->ld_sb) );   
143    
144         LDAP_FREE( (char *) ld );
145    
146         WSACleanup();
147
148         return( err );
149 }
150
151 int
152 ldap_unbind_s( LDAP *ld )
153 {
154         return( ldap_unbind_ext( ld, NULL, NULL ) );
155 }
156
157
158 int
159 ldap_send_unbind(
160         LDAP *ld,
161         Sockbuf *sb,
162         LDAPControl **sctrls,
163         LDAPControl **cctrls )
164 {
165         BerElement      *ber;
166
167         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
168
169         /* create a message to send */
170         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
171                 return( ld->ld_errno );
172         }
173
174         /* fill it in */
175         if ( ber_printf( ber, "{itn" /*}*/, ++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         /* Put Server Controls */
183         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
184                 ber_free( ber, 1 );
185                 return ld->ld_errno;
186         }
187
188         if ( ber_printf( ber, /*{*/ "}", LDAP_REQ_UNBIND ) == -1 ) {
189                 ld->ld_errno = LDAP_ENCODING_ERROR;
190                 ber_free( ber, 1 );
191                 return( ld->ld_errno );
192         }
193
194         /* send the message */
195         if ( ber_flush( sb, ber, 1 ) == -1 ) {
196                 ld->ld_errno = LDAP_SERVER_DOWN;
197                 ber_free( ber, 1 );
198                 return( ld->ld_errno );
199         }
200
201         return( LDAP_SUCCESS );
202 }