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