Remove redundant check.
[wine] / dlls / wldap32 / init.c
1 /*
2  * WLDAP32 - LDAP support for Wine
3  *
4  * Copyright 2005 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "wine/port.h"
24 #include "wine/debug.h"
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
31
32 #ifdef HAVE_LDAP_H
33 #include <ldap.h>
34 #else
35 #define LDAP_SUCCESS        0x00
36 #define LDAP_NOT_SUPPORTED  0x5c
37 #endif
38
39 #include "winldap_private.h"
40 #include "wldap32.h"
41
42 /* Should eventually be determined by the algorithm documented on MSDN. */
43 static const WCHAR defaulthost[] = { 'l','o','c','a','l','h','o','s','t',0 };
44
45 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
46
47 WLDAP32_LDAP *cldap_openA( PCHAR hostname, ULONG portnumber )
48 {
49     TRACE( "(%s, %ld)\n", debugstr_a(hostname), portnumber );
50
51     /* FIXME: should use UDP instead of TCP */
52     return ldap_openA( hostname, portnumber );
53 }
54
55 WLDAP32_LDAP *cldap_openW( PWCHAR hostname, ULONG portnumber )
56 {
57     TRACE( "(%s, %ld)\n", debugstr_w(hostname), portnumber );
58
59     /* FIXME: should use UDP instead of TCP */
60     return ldap_openW( hostname, portnumber );
61 }
62
63 ULONG ldap_connect( WLDAP32_LDAP *ld, LDAP_TIMEVAL *timeout )
64 {
65     TRACE( "(%p, %p)\n", ld, timeout );
66
67     if (!ld || !timeout) return WLDAP32_LDAP_PARAM_ERROR;
68     return LDAP_SUCCESS;
69 }
70
71 WLDAP32_LDAP *ldap_initA( PCHAR hostname, ULONG portnumber )
72 {
73 #ifdef HAVE_LDAP
74     WLDAP32_LDAP *ld = NULL;
75     WCHAR *hostnameW = NULL;
76
77     TRACE( "(%s, %ld)\n", debugstr_a(hostname), portnumber );
78
79     if (hostname) {
80         hostnameW = strAtoW( hostname );
81         if (!hostnameW) goto exit;
82     }
83
84     ld = ldap_initW( hostnameW, portnumber );
85
86 exit:
87     strfreeW( hostnameW );
88     return ld;
89
90 #endif
91     return NULL;
92 }
93
94 WLDAP32_LDAP *ldap_initW( PWCHAR hostname, ULONG portnumber )
95 {
96 #ifdef HAVE_LDAP
97     LDAP *ld = NULL;
98     char *hostnameU = NULL;
99
100     TRACE( "(%s, %ld)\n", debugstr_w(hostname), portnumber );
101
102     if (hostname) {
103         hostnameU = strWtoU( hostname );
104         if (!hostnameU) goto exit;
105     }
106     else {
107         hostnameU = strWtoU( defaulthost );
108         if (!hostnameU) goto exit;
109     }
110
111     ld = ldap_init( hostnameU, portnumber );
112
113 exit:
114     strfreeU( hostnameU );
115     return ld;
116
117 #endif
118     return NULL;
119 }
120
121 WLDAP32_LDAP *ldap_openA( PCHAR hostname, ULONG portnumber )
122 {
123 #ifdef HAVE_LDAP
124     WLDAP32_LDAP *ld = NULL;
125     WCHAR *hostnameW = NULL;
126
127     TRACE( "(%s, %ld)\n", debugstr_a(hostname), portnumber );
128
129     if (hostname) {
130         hostnameW = strAtoW( hostname );
131         if (!hostnameW) goto exit;
132     }
133
134     ld = ldap_openW( hostnameW, portnumber );
135
136 exit:
137     strfreeW( hostnameW );
138     return ld;
139
140 #endif
141     return NULL;
142 }
143
144 WLDAP32_LDAP *ldap_openW( PWCHAR hostname, ULONG portnumber )
145 {
146 #ifdef HAVE_LDAP
147     LDAP *ld = NULL;
148     char *hostnameU = NULL;
149
150     TRACE( "(%s, %ld)\n", debugstr_w(hostname), portnumber );
151
152     if (hostname) {
153         hostnameU = strWtoU( hostname );
154         if (!hostnameU) goto exit;
155     }
156     else {
157         hostnameU = strWtoU( defaulthost );
158         if (!hostnameU) goto exit;
159     }
160
161     ld = ldap_open( hostnameU, portnumber );
162
163 exit:
164     strfreeU( hostnameU );
165     return ld;
166
167 #endif
168     return NULL;
169 }
170
171 ULONG ldap_start_tls_sA( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
172     PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
173 {
174     ULONG ret = LDAP_NOT_SUPPORTED;
175 #ifdef HAVE_LDAP
176     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
177
178     ret = WLDAP32_LDAP_NO_MEMORY;
179
180     TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
181
182     if (!ld) return ~0UL;
183
184     if (serverctrls) {
185         serverctrlsW = controlarrayAtoW( serverctrls );
186         if (!serverctrlsW) goto exit;
187     }
188     if (clientctrls) {
189         clientctrlsW = controlarrayAtoW( clientctrls );
190         if (!clientctrlsW) goto exit;
191     }
192
193     ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
194
195 exit:
196     controlarrayfreeW( serverctrlsW );
197     controlarrayfreeW( clientctrlsW );
198
199 #endif
200     return ret;
201 }
202
203 ULONG ldap_start_tls_sW( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
204     PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
205 {
206     ULONG ret = LDAP_NOT_SUPPORTED;
207 #ifdef HAVE_LDAP
208     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
209
210     ret = WLDAP32_LDAP_NO_MEMORY;
211
212     TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
213
214     if (!ld) return ~0UL;
215
216     if (serverctrls) {
217         serverctrlsU = controlarrayWtoU( serverctrls );
218         if (!serverctrlsU) goto exit;
219     }
220     if (clientctrls) {
221         clientctrlsU = controlarrayWtoU( clientctrls );
222         if (!clientctrlsU) goto exit;
223     }
224
225     ret = ldap_start_tls_s( ld, serverctrlsU, clientctrlsU );
226
227 exit:
228     controlarrayfreeU( serverctrlsU );
229     controlarrayfreeU( clientctrlsU );
230
231 #endif
232     return ret;
233 }