2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1990 Regents of the University of Michigan.
17 #include <ac/socket.h>
18 #include <ac/string.h>
25 ldap_unbind( LDAP *ld )
27 Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
29 return( ldap_ld_free( ld, 1 ));
34 ldap_ld_free( LDAP *ld, int close )
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 */
42 if ( ld->ld_sb.sb_naddr == 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 ) {
47 ldap_free_request( ld, lr );
50 /* free and unbind from all open connections */
51 while ( ld->ld_conns != NULL ) {
52 ldap_free_connection( ld, ld->ld_conns, 1, close );
54 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
56 err = ldap_send_unbind( ld, &ld->ld_sb );
57 ldap_close_connection( &ld->ld_sb );
59 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
63 for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
64 free( ld->ld_sb.sb_addrs[ i ] );
66 free( ld->ld_sb.sb_addrs );
67 free( ld->ld_sb.sb_fromaddr );
70 for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
76 if ( ld->ld_cache != NULL )
77 ldap_destroy_cache( ld );
78 #endif /* !LDAP_NOCACHE */
79 if ( ld->ld_error != NULL )
81 if ( ld->ld_matched != NULL )
82 free( ld->ld_matched );
83 if ( ld->ld_host != NULL )
85 if ( ld->ld_ufnprefix != NULL )
86 free( ld->ld_ufnprefix );
87 if ( ld->ld_filtd != NULL )
88 ldap_getfilter_free( ld->ld_filtd );
89 #ifndef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
90 if ( ld->ld_sb.sb_ber.ber_buf != NULL )
91 free( ld->ld_sb.sb_ber.ber_buf );
92 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
93 if ( ld->ld_abandoned != NULL )
94 free( ld->ld_abandoned );
96 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
97 if ( ld->ld_selectinfo != NULL )
98 ldap_free_select_info( ld->ld_selectinfo );
99 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
101 if ( ld->ld_options.ldo_defbase != NULL )
102 free( ld->ld_options.ldo_defbase );
104 if ( ld->ld_options.ldo_defhost != NULL )
105 free( ld->ld_options.ldo_defhost );
115 ldap_unbind_s( LDAP *ld )
117 return( ldap_ld_free( ld, 1 ));
122 ldap_send_unbind( LDAP *ld, Sockbuf *sb )
126 Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
128 /* create a message to send */
129 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
130 return( ld->ld_errno );
134 if ( ber_printf( ber, "{itn}", ++ld->ld_msgid,
135 LDAP_REQ_UNBIND ) == -1 ) {
136 ld->ld_errno = LDAP_ENCODING_ERROR;
138 return( ld->ld_errno );
141 /* send the message */
142 if ( ber_flush( sb, ber, 1 ) == -1 ) {
143 ld->ld_errno = LDAP_SERVER_DOWN;
145 return( ld->ld_errno );
148 return( LDAP_SUCCESS );