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