Improve error handling for the init functions.
[wine/multimedia.git] / dlls / wldap32 / init.c
blobf82a0117586ff7f5b737220f6483739456384174
1 /*
2 * WLDAP32 - LDAP support for Wine
4 * Copyright 2005 Hans Leidekker
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.
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.
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
21 #include "config.h"
23 #include "wine/port.h"
24 #include "wine/debug.h"
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
32 #ifdef HAVE_LDAP
33 #include <ldap.h>
34 #else
35 #define LDAP_SUCCESS 0x00
36 #define LDAP_NOT_SUPPORTED 0x5c
37 #endif
39 #include "winldap_private.h"
40 #include "wldap32.h"
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 };
45 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
47 WLDAP32_LDAP *ldap_initA( PCHAR hostname, ULONG portnumber )
49 #ifdef HAVE_LDAP
50 WLDAP32_LDAP *ld = NULL;
51 WCHAR *hostnameW = NULL;
53 TRACE( "(%s, %ld)\n", debugstr_a(hostname), portnumber );
55 if (hostname) {
56 hostnameW = strAtoW( hostname );
57 if (!hostnameW) goto exit;
60 ld = ldap_initW( hostnameW, portnumber );
62 exit:
63 strfreeW( hostnameW );
64 return ld;
66 #endif
67 return NULL;
70 WLDAP32_LDAP *ldap_initW( PWCHAR hostname, ULONG portnumber )
72 #ifdef HAVE_LDAP
73 LDAP *ld = NULL;
74 char *hostnameU = NULL;
76 TRACE( "(%s, %ld)\n", debugstr_w(hostname), portnumber );
78 if (hostname) {
79 hostnameU = strWtoU( hostname );
80 if (!hostnameU) goto exit;
82 else {
83 hostnameU = strWtoU( defaulthost );
84 if (!hostnameU) goto exit;
87 ld = ldap_init( hostnameU, portnumber );
89 exit:
90 strfreeU( hostnameU );
91 return ld;
93 #endif
94 return NULL;
97 WLDAP32_LDAP *ldap_openA( PCHAR hostname, ULONG portnumber )
99 #ifdef HAVE_LDAP
100 WLDAP32_LDAP *ld = NULL;
101 WCHAR *hostnameW = NULL;
103 TRACE( "(%s, %ld)\n", debugstr_a(hostname), portnumber );
105 if (hostname) {
106 hostnameW = strAtoW( hostname );
107 if (!hostnameW) goto exit;
110 ld = ldap_openW( hostnameW, portnumber );
112 exit:
113 strfreeW( hostnameW );
114 return ld;
116 #endif
117 return NULL;
120 WLDAP32_LDAP *ldap_openW( PWCHAR hostname, ULONG portnumber )
122 #ifdef HAVE_LDAP
123 LDAP *ld = NULL;
124 char *hostnameU = NULL;
126 TRACE( "(%s, %ld)\n", debugstr_w(hostname), portnumber );
128 if (hostname) {
129 hostnameU = strWtoU( hostname );
130 if (!hostnameU) goto exit;
132 else {
133 hostnameU = strWtoU( defaulthost );
134 if (!hostnameU) goto exit;
137 ld = ldap_open( hostnameU, portnumber );
139 exit:
140 strfreeU( hostnameU );
141 return ld;
143 #endif
144 return NULL;
147 ULONG ldap_start_tls_sA( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
148 PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
150 ULONG ret = LDAP_NOT_SUPPORTED;
151 #ifdef HAVE_LDAP
152 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
153 ret = LDAP_NO_MEMORY;
155 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
157 if (!ld) return ~0UL;
159 if (serverctrls) {
160 serverctrlsW = controlarrayAtoW( serverctrls );
161 if (!serverctrlsW) goto exit;
163 if (clientctrls) {
164 clientctrlsW = controlarrayAtoW( clientctrls );
165 if (!clientctrlsW) goto exit;
168 ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
170 exit:
171 controlarrayfreeW( serverctrlsW );
172 controlarrayfreeW( clientctrlsW );
174 #endif
175 return ret;
178 ULONG ldap_start_tls_sW( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
179 PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
181 ULONG ret = LDAP_NOT_SUPPORTED;
182 #ifdef HAVE_LDAP
183 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
184 ret = LDAP_NO_MEMORY;
186 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
188 if (!ld) return ~0UL;
190 if (serverctrls) {
191 serverctrlsU = controlarrayWtoU( serverctrls );
192 if (!serverctrlsU) goto exit;
194 if (clientctrls) {
195 clientctrlsU = controlarrayWtoU( clientctrls );
196 if (!clientctrlsU) goto exit;
199 ret = ldap_start_tls_s( ld, serverctrlsU, clientctrlsU );
201 exit:
202 controlarrayfreeU( serverctrlsU );
203 controlarrayfreeU( clientctrlsU );
205 #endif
206 return ret;