]> git.sur5r.net Git - openldap/blob - libraries/libldap/unbind.c
Allow ctrls argument to be NULL.
[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 /* An Unbind Request looks like this:
13  *
14  *      UnbindRequest ::= NULL
15  *
16  * and has no response.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22 #include <ac/stdlib.h>
23
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
27
28 #include "ldap-int.h"
29
30 int
31 ldap_unbind_ext(
32         LDAP *ld,
33         LDAPControl **sctrls,
34         LDAPControl **cctrls )
35 {
36         return ldap_ld_free( ld, 1, sctrls, cctrls );
37 }
38
39 int
40 ldap_unbind( LDAP *ld )
41 {
42         Debug( LDAP_DEBUG_TRACE, "ldap_unbind\n", 0, 0, 0 );
43
44         return( ldap_unbind_ext( ld, NULL, NULL ) );
45 }
46
47
48 int
49 ldap_ld_free(
50         LDAP *ld,
51         int close,
52         LDAPControl **sctrls,
53         LDAPControl **cctrls )
54 {
55         LDAPMessage     *lm, *next;
56         int             err = LDAP_SUCCESS;
57         LDAPRequest     *lr, *nextlr;
58
59         if ( ld->ld_cldapnaddr == 0 ) {
60                 /* free LDAP structure and outstanding requests/responses */
61                 for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
62                         nextlr = lr->lr_next;
63                         ldap_free_request( ld, lr );
64                 }
65
66                 /* free and unbind from all open connections */
67                 while ( ld->ld_conns != NULL ) {
68                         ldap_free_connection( ld, ld->ld_conns, 1, close );
69                 }
70         } else {
71                 int     i;
72
73                 for ( i = 0; i < ld->ld_cldapnaddr; ++i ) {
74                         LDAP_FREE( ld->ld_cldapaddrs[ i ] );
75                 }
76                 LDAP_FREE( ld->ld_cldapaddrs );
77         }
78
79         for ( lm = ld->ld_responses; lm != NULL; lm = next ) {
80                 next = lm->lm_next;
81                 ldap_msgfree( lm );
82         }
83
84 #ifndef LDAP_NOCACHE
85         if ( ld->ld_cache != NULL ) {
86                 ldap_destroy_cache( ld );
87                 ld->ld_cache = NULL;
88         }
89 #endif /* !LDAP_NOCACHE */
90
91         if ( ld->ld_error != NULL ) {
92                 LDAP_FREE( ld->ld_error );
93                 ld->ld_error = NULL;
94         }
95
96         if ( ld->ld_matched != NULL ) {
97                 LDAP_FREE( ld->ld_matched );
98                 ld->ld_matched = NULL;
99         }
100
101         if ( ld->ld_host != NULL ) {
102                 LDAP_FREE( ld->ld_host );
103                 ld->ld_host = NULL;
104         }
105
106         if ( ld->ld_ufnprefix != NULL ) {
107                 LDAP_FREE( ld->ld_ufnprefix );
108                 ld->ld_ufnprefix = NULL;
109         }
110
111         if ( ld->ld_filtd != NULL ) {
112                 ldap_getfilter_free( ld->ld_filtd );
113                 ld->ld_filtd = NULL;
114         }
115
116         if ( ld->ld_abandoned != NULL ) {
117                 LDAP_FREE( ld->ld_abandoned );
118                 ld->ld_abandoned = NULL;
119         }
120
121         if ( ld->ld_selectinfo != NULL ) {
122                 ldap_free_select_info( ld->ld_selectinfo );
123                 ld->ld_selectinfo = NULL;
124         }
125
126         if ( ld->ld_options.ldo_defbase != NULL ) {
127                 LDAP_FREE( ld->ld_options.ldo_defbase );
128                 ld->ld_options.ldo_defbase = NULL;
129         }
130
131         if ( ld->ld_options.ldo_defhost != NULL ) {
132                 LDAP_FREE( ld->ld_options.ldo_defhost );
133                 ld->ld_options.ldo_defhost = NULL;
134         }
135
136         ber_pvt_sb_destroy( &(ld->ld_sb) );   
137    
138         LDAP_FREE( (char *) ld );
139    
140         WSACleanup();
141
142         return( err );
143 }
144
145 int
146 ldap_unbind_s( LDAP *ld )
147 {
148         return( ldap_unbind_ext( ld, NULL, NULL ) );
149 }
150
151
152 int
153 ldap_send_unbind(
154         LDAP *ld,
155         Sockbuf *sb,
156         LDAPControl **sctrls,
157         LDAPControl **cctrls )
158 {
159         BerElement      *ber;
160
161         Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
162
163         /* create a message to send */
164         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
165                 return( ld->ld_errno );
166         }
167
168         /* fill it in */
169         if ( ber_printf( ber, "{itn" /*}*/, ++ld->ld_msgid,
170             LDAP_REQ_UNBIND ) == -1 ) {
171                 ld->ld_errno = LDAP_ENCODING_ERROR;
172                 ber_free( ber, 1 );
173                 return( ld->ld_errno );
174         }
175
176         /* Put Server Controls */
177         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
178                 ber_free( ber, 1 );
179                 return ld->ld_errno;
180         }
181
182         if ( ber_printf( ber, /*{*/ "}", LDAP_REQ_UNBIND ) == -1 ) {
183                 ld->ld_errno = LDAP_ENCODING_ERROR;
184                 ber_free( ber, 1 );
185                 return( ld->ld_errno );
186         }
187
188         /* send the message */
189         if ( ber_flush( sb, ber, 1 ) == -1 ) {
190                 ld->ld_errno = LDAP_SERVER_DOWN;
191                 ber_free( ber, 1 );
192                 return( ld->ld_errno );
193         }
194
195         return( LDAP_SUCCESS );
196 }