]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
fb1c2e4d4d4f2d8b3edb289c71c787cffdf9d7f5
[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 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
51         LDAPRequest     *lr, *nextlr;
52 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
53
54         if ( ld->ld_cldapnaddr == 0 ) {
55 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
56                 /* free LDAP structure and outstanding requests/responses */
57                 for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
58                         nextlr = lr->lr_next;
59                         ldap_free_request( ld, lr );
60                 }
61
62                 /* free and unbind from all open connections */
63                 while ( ld->ld_conns != NULL ) {
64                         ldap_free_connection( ld, ld->ld_conns, 1, close );
65                 }
66 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
67                 if ( close ) {
68                         err = ldap_send_unbind( ld, &ld->ld_sb, sctrls, cctrls );
69                         ldap_close_connection( &ld->ld_sb );
70                 }
71 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
72         } else {
73                 int     i;
74
75                 for ( i = 0; i < ld->ld_cldapnaddr; ++i ) {
76                         free( ld->ld_cldapaddrs[ i ] );
77                 }
78                 free( ld->ld_cldapaddrs );
79         }
80
81         for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
82                 next = lm->lm_next;
83                 ldap_msgfree( lm );
84         }
85
86 #ifndef LDAP_NOCACHE
87         if ( ld->ld_cache != NULL )
88                 ldap_destroy_cache( ld );
89 #endif /* !LDAP_NOCACHE */
90         if ( ld->ld_error != NULL )
91                 free( ld->ld_error );
92         if ( ld->ld_matched != NULL )
93                 free( ld->ld_matched );
94         if ( ld->ld_host != NULL )
95                 free( ld->ld_host );
96         if ( ld->ld_ufnprefix != NULL )
97                 free( ld->ld_ufnprefix );
98         if ( ld->ld_filtd != NULL )
99                 ldap_getfilter_free( ld->ld_filtd );
100         if ( ld->ld_abandoned != NULL )
101                 free( ld->ld_abandoned );
102
103 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
104         if ( ld->ld_selectinfo != NULL )
105                 ldap_free_select_info( ld->ld_selectinfo );
106 #else
107         ber_clear( &(ld->ld_ber), 1 );
108 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
109
110         if ( ld->ld_options.ldo_defbase != NULL )
111                 free( ld->ld_options.ldo_defbase );
112
113         if ( ld->ld_options.ldo_defhost != NULL )
114                 free( ld->ld_options.ldo_defhost );
115
116         ber_pvt_sb_destroy( &(ld->ld_sb) );   
117    
118         free( (char *) ld );
119    
120         WSACleanup();
121
122         return( err );
123 }
124
125 int
126 ldap_unbind_s( LDAP *ld )
127 {
128         return( ldap_unbind_ext( ld, NULL, NULL ) );
129 }
130
131
132 int
133 ldap_send_unbind(
134         LDAP *ld,
135         Sockbuf *sb,
136         LDAPControl **sctrls,
137         LDAPControl **cctrls )
138 {
139         BerElement      *ber;
140
141         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
142
143         /* create a message to send */
144         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
145                 return( ld->ld_errno );
146         }
147
148         /* fill it in */
149         if ( ber_printf( ber, "{itn", ++ld->ld_msgid,
150             LDAP_REQ_UNBIND ) == -1 ) {
151                 ld->ld_errno = LDAP_ENCODING_ERROR;
152                 ber_free( ber, 1 );
153                 return( ld->ld_errno );
154         }
155
156         /* Put Server Controls */
157         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
158                 ber_free( ber, 1 );
159                 return ld->ld_errno;
160         }
161
162         if ( ber_printf( ber, "}", ++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         /* send the message */
170         if ( ber_flush( sb, ber, 1 ) == -1 ) {
171                 ld->ld_errno = LDAP_SERVER_DOWN;
172                 ber_free( ber, 1 );
173                 return( ld->ld_errno );
174         }
175
176         return( LDAP_SUCCESS );
177 }