]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
Cast char* argument to hh_to_c() to Byte*
[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
24 int
25 ldap_unbind( LDAP *ld )
26 {
27         Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
28
29         return( ldap_ld_free( ld, 1 ));
30 }
31
32
33 int
34 ldap_ld_free( LDAP *ld, int close )
35 {
36         LDAPMessage     *lm, *next;
37         int             err = LDAP_SUCCESS;
38 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
39         LDAPRequest     *lr, *nextlr;
40 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
41
42         if ( ld->ld_cldapnaddr == 0 ) {
43 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
44                 /* free LDAP structure and outstanding requests/responses */
45                 for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
46                         nextlr = lr->lr_next;
47                         ldap_free_request( ld, lr );
48                 }
49
50                 /* free and unbind from all open connections */
51                 while ( ld->ld_conns != NULL ) {
52                         ldap_free_connection( ld, ld->ld_conns, 1, close );
53                 }
54 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
55                 if ( close ) {
56                         err = ldap_send_unbind( ld, &ld->ld_sb );
57                         ldap_close_connection( &ld->ld_sb );
58                 }
59 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
60         } else {
61                 int     i;
62
63                 for ( i = 0; i < ld->ld_cldapnaddr; ++i ) {
64                         free( ld->ld_cldapaddrs[ i ] );
65                 }
66                 free( ld->ld_cldapaddrs );
67         }
68
69         for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
70                 next = lm->lm_next;
71                 ldap_msgfree( lm );
72         }
73
74 #ifndef LDAP_NOCACHE
75         if ( ld->ld_cache != NULL )
76                 ldap_destroy_cache( ld );
77 #endif /* !LDAP_NOCACHE */
78         if ( ld->ld_error != NULL )
79                 free( ld->ld_error );
80         if ( ld->ld_matched != NULL )
81                 free( ld->ld_matched );
82         if ( ld->ld_host != NULL )
83                 free( ld->ld_host );
84         if ( ld->ld_ufnprefix != NULL )
85                 free( ld->ld_ufnprefix );
86         if ( ld->ld_filtd != NULL )
87                 ldap_getfilter_free( ld->ld_filtd );
88         if ( ld->ld_abandoned != NULL )
89                 free( ld->ld_abandoned );
90
91 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
92         if ( ld->ld_selectinfo != NULL )
93                 ldap_free_select_info( ld->ld_selectinfo );
94 #else
95         ber_clear( &(ld->ld_ber), 1 );
96 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
97
98         if ( ld->ld_options.ldo_defbase != NULL )
99                 free( ld->ld_options.ldo_defbase );
100
101         if ( ld->ld_options.ldo_defhost != NULL )
102                 free( ld->ld_options.ldo_defhost );
103
104         lber_pvt_sb_destroy( &(ld->ld_sb) );   
105    
106         free( (char *) ld );
107    
108         WSACleanup();
109
110         return( err );
111 }
112
113 int
114 ldap_unbind_s( LDAP *ld )
115 {
116         return( ldap_ld_free( ld, 1 ));
117 }
118
119
120 int
121 ldap_send_unbind( LDAP *ld, Sockbuf *sb )
122 {
123         BerElement      *ber;
124
125         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
126
127         /* create a message to send */
128         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
129                 return( ld->ld_errno );
130         }
131
132         /* fill it in */
133         if ( ber_printf( ber, "{itn}", ++ld->ld_msgid,
134             LDAP_REQ_UNBIND ) == -1 ) {
135                 ld->ld_errno = LDAP_ENCODING_ERROR;
136                 ber_free( ber, 1 );
137                 return( ld->ld_errno );
138         }
139
140         /* send the message */
141         if ( ber_flush( sb, ber, 1 ) == -1 ) {
142                 ld->ld_errno = LDAP_SERVER_DOWN;
143                 ber_free( ber, 1 );
144                 return( ld->ld_errno );
145         }
146
147         return( LDAP_SUCCESS );
148 }