]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
Initial checkin of ldap_search_ext() and friends.
[openldap] / libraries / libldap / unbind.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1990 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  unbind.c
10  */
11
12 #include "portable.h"
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 int
24 ldap_unbind_ext(
25         LDAP *ld,
26         LDAPControl **sctrls,
27         LDAPControl **cctrls )
28 {
29         return ldap_ld_free( ld, 1, sctrls, cctrls );
30 }
31
32 int
33 ldap_unbind( LDAP *ld )
34 {
35         Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
36
37         return( ldap_unbind_ext( ld, NULL, NULL ) );
38 }
39
40
41 int
42 ldap_ld_free(
43         LDAP *ld,
44         int close,
45         LDAPControl **sctrls,
46         LDAPControl **cctrls )
47 {
48         LDAPMessage     *lm, *next;
49         int             err = LDAP_SUCCESS;
50 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
51         LDAPRequest     *lr, *nextlr;
52 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
53
54         if ( ld->ld_cldapnaddr == 0 ) {
55 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_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_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
67                 if ( close ) {
68                         err = ldap_send_unbind( ld, &ld->ld_sb, sctrls, cctrls );
69                         ldap_close_connection( &ld->ld_sb );
70                 }
71 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
72         } else {
73                 int     i;
74
75                 for ( i = 0; i < ld->ld_cldapnaddr; ++i ) {
76                         free( ld->ld_cldapaddrs[ i ] );
77                 }
78                 free( ld->ld_cldapaddrs );
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 LDAP_NOCACHE
87         if ( ld->ld_cache != NULL ) {
88                 ldap_destroy_cache( ld );
89                 ld->ld_cache = NULL;
90         }
91 #endif /* !LDAP_NOCACHE */
92
93         if ( ld->ld_error != NULL ) {
94                 free( ld->ld_error );
95                 ld->ld_error = NULL;
96         }
97
98         if ( ld->ld_matched != NULL ) {
99                 free( ld->ld_matched );
100                 ld->ld_matched = NULL;
101         }
102
103         if ( ld->ld_host != NULL ) {
104                 free( ld->ld_host );
105                 ld->ld_host = NULL;
106         }
107
108         if ( ld->ld_ufnprefix != NULL ) {
109                 free( ld->ld_ufnprefix );
110                 ld->ld_ufnprefix = NULL;
111         }
112
113         if ( ld->ld_filtd != NULL ) {
114                 ldap_getfilter_free( ld->ld_filtd );
115                 ld->ld_filtd = NULL;
116         }
117
118         if ( ld->ld_abandoned != NULL ) {
119                 free( ld->ld_abandoned );
120                 ld->ld_abandoned = NULL;
121         }
122
123 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
124         if ( ld->ld_selectinfo != NULL ) {
125                 ldap_free_select_info( ld->ld_selectinfo );
126                 ld->ld_selectinfo = NULL;
127         }
128 #else
129         ber_clear( &(ld->ld_ber), 1 );
130 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
131
132         if ( ld->ld_options.ldo_defbase != NULL ) {
133                 free( ld->ld_options.ldo_defbase );
134                 ld->ld_options.ldo_defbase = NULL;
135         }
136
137         if ( ld->ld_options.ldo_defhost != NULL ) {
138                 free( ld->ld_options.ldo_defhost );
139                 ld->ld_options.ldo_defhost = NULL;
140         }
141
142         ber_pvt_sb_destroy( &(ld->ld_sb) );   
143    
144         free( (char *) ld );
145    
146         WSACleanup();
147
148         return( err );
149 }
150
151 int
152 ldap_unbind_s( LDAP *ld )
153 {
154         return( ldap_unbind_ext( ld, NULL, NULL ) );
155 }
156
157
158 int
159 ldap_send_unbind(
160         LDAP *ld,
161         Sockbuf *sb,
162         LDAPControl **sctrls,
163         LDAPControl **cctrls )
164 {
165         BerElement      *ber;
166
167         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
168
169         /* create a message to send */
170         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
171                 return( ld->ld_errno );
172         }
173
174         /* fill it in */
175         if ( ber_printf( ber, "{itn", ++ld->ld_msgid,
176             LDAP_REQ_UNBIND ) == -1 ) {
177                 ld->ld_errno = LDAP_ENCODING_ERROR;
178                 ber_free( ber, 1 );
179                 return( ld->ld_errno );
180         }
181
182         /* Put Server Controls */
183         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
184                 ber_free( ber, 1 );
185                 return ld->ld_errno;
186         }
187
188         if ( ber_printf( ber, "}", ++ld->ld_msgid,
189             LDAP_REQ_UNBIND ) == -1 ) {
190                 ld->ld_errno = LDAP_ENCODING_ERROR;
191                 ber_free( ber, 1 );
192                 return( ld->ld_errno );
193         }
194
195         /* send the message */
196         if ( ber_flush( sb, ber, 1 ) == -1 ) {
197                 ld->ld_errno = LDAP_SERVER_DOWN;
198                 ber_free( ber, 1 );
199                 return( ld->ld_errno );
200         }
201
202         return( LDAP_SUCCESS );
203 }