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