Back to index

lightning-sunbird  0.9+nobinonly
unbind.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  Copyright (c) 1990 Regents of the University of Michigan.
00039  *  All rights reserved.
00040  */
00041 
00042 /*
00043  *  unbind.c
00044  */
00045 
00046 #if 0
00047 #ifndef lint 
00048 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
00049 #endif
00050 #endif
00051 
00052 #include "ldap-int.h"
00053 
00054 int
00055 LDAP_CALL
00056 ldap_unbind( LDAP *ld )
00057 {
00058        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
00059 
00060        return( ldap_ld_free( ld, NULL, NULL, 1 ) );
00061 }
00062 
00063 
00064 int
00065 LDAP_CALL
00066 ldap_unbind_s( LDAP *ld )
00067 {
00068        return( ldap_ld_free( ld, NULL, NULL, 1 ));
00069 }
00070 
00071 
00072 int
00073 LDAP_CALL
00074 ldap_unbind_ext( LDAP *ld, LDAPControl **serverctrls,
00075     LDAPControl **clientctrls )
00076 {
00077        return( ldap_ld_free( ld, serverctrls, clientctrls, 1 ));
00078 }
00079 
00080 
00081 /*
00082  * Dispose of the LDAP session ld, including all associated connections
00083  * and resources.  If close is non-zero, an unbind() request is sent as well.
00084  */
00085 int
00086 ldap_ld_free( LDAP *ld, LDAPControl **serverctrls,
00087     LDAPControl **clientctrls, int close )
00088 {
00089        LDAPMessage   *lm, *next;
00090        int           err = LDAP_SUCCESS;
00091        LDAPRequest   *lr, *nextlr;
00092 
00093        if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00094               return( LDAP_PARAM_ERROR );
00095        }
00096 
00097        if ( ld->ld_sbp->sb_naddr == 0 ) {
00098               LDAP_MUTEX_LOCK( ld, LDAP_REQ_LOCK );
00099               /* free LDAP structure and outstanding requests/responses */
00100               for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
00101                      nextlr = lr->lr_next;
00102                      nsldapi_free_request( ld, lr, 0 );
00103               }
00104               LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK );
00105 
00106               /* free and unbind from all open connections */
00107               LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
00108               while ( ld->ld_conns != NULL ) {
00109                      nsldapi_free_connection( ld, ld->ld_conns, serverctrls,
00110                          clientctrls, 1, close );
00111               }
00112               LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00113 
00114        } else {
00115               int    i;
00116 
00117               for ( i = 0; i < ld->ld_sbp->sb_naddr; ++i ) {
00118                      NSLDAPI_FREE( ld->ld_sbp->sb_addrs[ i ] );
00119               }
00120               NSLDAPI_FREE( ld->ld_sbp->sb_addrs );
00121               NSLDAPI_FREE( ld->ld_sbp->sb_fromaddr );
00122        }
00123 
00124        LDAP_MUTEX_LOCK( ld, LDAP_RESP_LOCK );
00125        for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
00126               next = lm->lm_next;
00127               ldap_msgfree( lm );
00128        }
00129        LDAP_MUTEX_UNLOCK( ld, LDAP_RESP_LOCK );
00130 
00131        /* call cache unbind function to allow it to clean up after itself */
00132        if ( ld->ld_cache_unbind != NULL ) {
00133               LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
00134               (void)ld->ld_cache_unbind( ld, 0, 0 );
00135               LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00136        }
00137 
00138        /* call the dispose handle I/O callback if one is defined */
00139        if ( ld->ld_extdisposehandle_fn != NULL ) {
00140            /*
00141             * We always pass the session extended I/O argument to
00142             * the dispose handle callback.
00143             */
00144            ld->ld_extdisposehandle_fn( ld, ld->ld_ext_session_arg );
00145        }
00146 
00147        if ( ld->ld_error != NULL )
00148               NSLDAPI_FREE( ld->ld_error );
00149        if ( ld->ld_matched != NULL )
00150               NSLDAPI_FREE( ld->ld_matched );
00151        if ( ld->ld_host != NULL )
00152               NSLDAPI_FREE( ld->ld_host );
00153        if ( ld->ld_ufnprefix != NULL )
00154               NSLDAPI_FREE( ld->ld_ufnprefix );
00155        if ( ld->ld_filtd != NULL )
00156               ldap_getfilter_free( ld->ld_filtd );
00157        if ( ld->ld_abandoned != NULL )
00158               NSLDAPI_FREE( ld->ld_abandoned );
00159        if ( ld->ld_sbp != NULL )
00160               ber_sockbuf_free( ld->ld_sbp );
00161        if ( ld->ld_defhost != NULL )
00162               NSLDAPI_FREE( ld->ld_defhost );
00163        if ( ld->ld_servercontrols != NULL )
00164               ldap_controls_free( ld->ld_servercontrols );
00165        if ( ld->ld_clientcontrols != NULL )
00166               ldap_controls_free( ld->ld_clientcontrols );
00167        if ( ld->ld_preferred_language != NULL )
00168               NSLDAPI_FREE( ld->ld_preferred_language );
00169        nsldapi_iostatus_free( ld );
00170 
00171        /*
00172         * XXXmcs: should use cache function pointers to hook in memcache
00173         */
00174        if ( ld->ld_memcache != NULL ) {
00175               ldap_memcache_set( ld, NULL );
00176        }
00177 
00178        /* free all mutexes we have allocated */
00179        nsldapi_mutex_free_all( ld );
00180        NSLDAPI_FREE( ld->ld_mutex );
00181 
00182        NSLDAPI_FREE( (char *) ld );
00183 
00184        return( err );
00185 }
00186 
00187 
00188 
00189 int
00190 nsldapi_send_unbind( LDAP *ld, Sockbuf *sb, LDAPControl **serverctrls,
00191     LDAPControl **clientctrls )
00192 {
00193        BerElement    *ber;
00194        int           err, msgid;
00195 
00196        LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_send_unbind\n", 0, 0, 0 );
00197 
00198        /* create a message to send */
00199        if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
00200            != LDAP_SUCCESS ) {
00201               return( err );
00202        }
00203 
00204        /* fill it in */
00205        LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
00206        msgid = ++ld->ld_msgid;
00207        LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
00208 
00209        if ( ber_printf( ber, "{itn", msgid, LDAP_REQ_UNBIND ) == -1 ) {
00210               ber_free( ber, 1 );
00211               err = LDAP_ENCODING_ERROR;
00212               LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00213               return( err );
00214        }
00215 
00216        if (( err = nsldapi_put_controls( ld, serverctrls, 1, ber ))
00217            != LDAP_SUCCESS ) {
00218               ber_free( ber, 1 );
00219               return( err );
00220        }
00221 
00222        /* send the message */
00223        if ( nsldapi_ber_flush( ld, sb, ber, 1, 0 ) != 0 ) {
00224               ber_free( ber, 1 );
00225               err = LDAP_SERVER_DOWN;
00226               LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00227               return( err );
00228        }
00229 
00230        return( LDAP_SUCCESS );
00231 }