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