home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ldapsdk.zip / libraries / libldap / unbind.c < prev    next >
C/C++ Source or Header  |  2001-07-22  |  4KB  |  194 lines

  1. /* $OpenLDAP: pkg/ldap/libraries/libldap/unbind.c,v 1.17.6.7 2001/07/21 19:01:40 kurt Exp $ */
  2. /*
  3.  * Copyright 1998-2000 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.     int rc;
  38.  
  39.     /* check client controls */
  40.     rc = ldap_int_client_controls( ld, cctrls );
  41.     if( rc != LDAP_SUCCESS ) return rc;
  42.  
  43.     return ldap_ld_free( ld, 1, sctrls, cctrls );
  44. }
  45.  
  46. int
  47. ldap_unbind_ext_s(
  48.     LDAP *ld,
  49.     LDAPControl **sctrls,
  50.     LDAPControl **cctrls )
  51. {
  52.     return ldap_unbind_ext( ld, sctrls, cctrls );
  53. }
  54.  
  55. int
  56. ldap_unbind( LDAP *ld )
  57. {
  58.     Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
  59.  
  60.     return( ldap_unbind_ext( ld, NULL, NULL ) );
  61. }
  62.  
  63.  
  64. int
  65. ldap_ld_free(
  66.     LDAP *ld,
  67.     int close,
  68.     LDAPControl **sctrls,
  69.     LDAPControl **cctrls )
  70. {
  71.     LDAPMessage    *lm, *next;
  72.     int        err = LDAP_SUCCESS;
  73.     LDAPRequest    *lr, *nextlr;
  74.  
  75.         /* free LDAP structure and outstanding requests/responses */
  76.         for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
  77.             nextlr = lr->lr_next;
  78.             ldap_free_request( ld, lr );
  79.         }
  80.  
  81.         /* free and unbind from all open connections */
  82.         while ( ld->ld_conns != NULL ) {
  83.             ldap_free_connection( ld, ld->ld_conns, 1, close );
  84.         }
  85.  
  86.     for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
  87.         next = lm->lm_next;
  88.         ldap_msgfree( lm );
  89.     }
  90.  
  91. #ifndef LDAP_NOCACHE
  92.     if ( ld->ld_cache != NULL ) {
  93.         ldap_destroy_cache( ld );
  94.         ld->ld_cache = NULL;
  95.     }
  96. #endif /* !LDAP_NOCACHE */
  97.  
  98.     if ( ld->ld_error != NULL ) {
  99.         LDAP_FREE( ld->ld_error );
  100.         ld->ld_error = NULL;
  101.     }
  102.  
  103.     if ( ld->ld_matched != NULL ) {
  104.         LDAP_FREE( ld->ld_matched );
  105.         ld->ld_matched = NULL;
  106.     }
  107.  
  108.     if ( ld->ld_abandoned != NULL ) {
  109.         LDAP_FREE( ld->ld_abandoned );
  110.         ld->ld_abandoned = NULL;
  111.     }
  112.  
  113.     if ( ld->ld_selectinfo != NULL ) {
  114.         ldap_free_select_info( ld->ld_selectinfo );
  115.         ld->ld_selectinfo = NULL;
  116.     }
  117.  
  118.     if ( ld->ld_options.ldo_defludp != NULL ) {
  119.         ldap_free_urllist( ld->ld_options.ldo_defludp );
  120.         ld->ld_options.ldo_defludp = NULL;
  121.     }
  122.  
  123.     if ( ld->ld_options.ldo_tm_api != NULL ) {
  124.         LDAP_FREE( ld->ld_options.ldo_tm_api );
  125.         ld->ld_options.ldo_tm_api = NULL;
  126.     }
  127.  
  128.     if ( ld->ld_options.ldo_tm_net != NULL ) {
  129.         LDAP_FREE( ld->ld_options.ldo_tm_net );
  130.         ld->ld_options.ldo_tm_net = NULL;
  131.     }
  132.  
  133.     ber_sockbuf_free( ld->ld_sb );   
  134.    
  135.     LDAP_FREE( (char *) ld );
  136.    
  137.     WSACleanup();
  138.  
  139.     return( err );
  140. }
  141.  
  142. int
  143. ldap_unbind_s( LDAP *ld )
  144. {
  145.     return( ldap_unbind_ext( ld, NULL, NULL ) );
  146. }
  147.  
  148.  
  149. int
  150. ldap_send_unbind(
  151.     LDAP *ld,
  152.     Sockbuf *sb,
  153.     LDAPControl **sctrls,
  154.     LDAPControl **cctrls )
  155. {
  156.     BerElement    *ber;
  157.  
  158.     Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
  159.  
  160.     /* create a message to send */
  161.     if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
  162.         return( ld->ld_errno );
  163.     }
  164.  
  165.     /* fill it in */
  166.     if ( ber_printf( ber, "{itn" /*}*/, ++ld->ld_msgid,
  167.         LDAP_REQ_UNBIND ) == -1 ) {
  168.         ld->ld_errno = LDAP_ENCODING_ERROR;
  169.         ber_free( ber, 1 );
  170.         return( ld->ld_errno );
  171.     }
  172.  
  173.     /* Put Server Controls */
  174.     if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
  175.         ber_free( ber, 1 );
  176.         return ld->ld_errno;
  177.     }
  178.  
  179.     if ( ber_printf( ber, /*{*/ "N}", LDAP_REQ_UNBIND ) == -1 ) {
  180.         ld->ld_errno = LDAP_ENCODING_ERROR;
  181.         ber_free( ber, 1 );
  182.         return( ld->ld_errno );
  183.     }
  184.  
  185.     /* send the message */
  186.     if ( ber_flush( sb, ber, 1 ) == -1 ) {
  187.         ld->ld_errno = LDAP_SERVER_DOWN;
  188.         ber_free( ber, 1 );
  189.         return( ld->ld_errno );
  190.     }
  191.  
  192.     return( LDAP_SUCCESS );
  193. }
  194.