Sync with trunk r63743.
[reactos.git] / dll / win32 / wldap32 / error.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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "winldap_private.h"
22
23 #include <winuser.h>
24
25 extern HINSTANCE hwldap32;
26
27 ULONG map_error( int error )
28 {
29 switch (error)
30 {
31 #ifdef HAVE_LDAP_H
32 case LDAP_SERVER_DOWN: return WLDAP32_LDAP_SERVER_DOWN;
33 case LDAP_LOCAL_ERROR: return WLDAP32_LDAP_LOCAL_ERROR;
34 case LDAP_DECODING_ERROR: return WLDAP32_LDAP_DECODING_ERROR;
35 case LDAP_TIMEOUT: return WLDAP32_LDAP_TIMEOUT;
36 case LDAP_AUTH_UNKNOWN: return WLDAP32_LDAP_AUTH_UNKNOWN;
37 case LDAP_FILTER_ERROR: return WLDAP32_LDAP_FILTER_ERROR;
38 case LDAP_USER_CANCELLED: return WLDAP32_LDAP_USER_CANCELLED;
39 case LDAP_PARAM_ERROR: return WLDAP32_LDAP_PARAM_ERROR;
40 case LDAP_NO_MEMORY: return WLDAP32_LDAP_NO_MEMORY;
41 case LDAP_CONNECT_ERROR: return WLDAP32_LDAP_CONNECT_ERROR;
42 case LDAP_NOT_SUPPORTED: return WLDAP32_LDAP_NOT_SUPPORTED;
43 case LDAP_CONTROL_NOT_FOUND: return WLDAP32_LDAP_CONTROL_NOT_FOUND;
44 case LDAP_NO_RESULTS_RETURNED: return WLDAP32_LDAP_NO_RESULTS_RETURNED;
45 case LDAP_MORE_RESULTS_TO_RETURN: return WLDAP32_LDAP_MORE_RESULTS_TO_RETURN;
46 case LDAP_CLIENT_LOOP: return WLDAP32_LDAP_CLIENT_LOOP;
47 case LDAP_REFERRAL_LIMIT_EXCEEDED: return WLDAP32_LDAP_REFERRAL_LIMIT_EXCEEDED;
48 #endif
49 default: return error;
50 }
51 }
52
53 /***********************************************************************
54 * ldap_err2stringA (WLDAP32.@)
55 *
56 * See ldap_err2stringW.
57 */
58 PCHAR CDECL ldap_err2stringA( ULONG err )
59 {
60 static char buf[256] = "";
61
62 TRACE( "(0x%08x)\n", err );
63
64 if (err <= WLDAP32_LDAP_REFERRAL_LIMIT_EXCEEDED)
65 LoadStringA( hwldap32, err, buf, 256 );
66 else
67 LoadStringA( hwldap32, WLDAP32_LDAP_LOCAL_ERROR, buf, 256 );
68
69 return buf;
70 }
71
72 /***********************************************************************
73 * ldap_err2stringW (WLDAP32.@)
74 *
75 * Convert an error code into a string describing the error.
76 *
77 * PARAMS
78 * err [I] Error code to convert.
79 *
80 * RETURNS
81 * Success: Pointer to a string containing the error description.
82 * Failure: NULL
83 *
84 * NOTES
85 * The returned string is statically allocated, you must not
86 * free this string.
87 */
88 PWCHAR CDECL ldap_err2stringW( ULONG err )
89 {
90 static WCHAR buf[256] = { 0 };
91
92 TRACE( "(0x%08x)\n", err );
93
94 if (err <= WLDAP32_LDAP_REFERRAL_LIMIT_EXCEEDED)
95 LoadStringW( hwldap32, err, buf, 256 );
96 else
97 LoadStringW( hwldap32, WLDAP32_LDAP_LOCAL_ERROR, buf, 256 );
98
99 return buf;
100 }
101
102 /***********************************************************************
103 * ldap_perror (WLDAP32.@)
104 *
105 * Print a given error string.
106 *
107 * PARAMS
108 * ld [I] Pointer to an LDAP context.
109 * msg [I] Error string.
110 *
111 * RETURNS
112 * Nothing.
113 *
114 * NOTES
115 * Like native, this function does nothing.
116 */
117 void CDECL WLDAP32_ldap_perror( WLDAP32_LDAP *ld, const PCHAR msg )
118 {
119 TRACE( "(%p, %s)\n", ld, debugstr_a(msg) );
120 }
121
122 /***********************************************************************
123 * ldap_result2error (WLDAP32.@)
124 *
125 * Parse an LDAP message and return the error obtained from it.
126 *
127 * PARAMS
128 * ld [I] Pointer to an LDAP context.
129 * res [I] Pointer to an LDAPMessage structure.
130 * free [I] Ask for the LDAPMessage structure to be freed.
131 *
132 * RETURNS
133 * Success: LDAP_SUCCESS
134 * Failure: An LDAP error code.
135 *
136 * NOTES
137 * If not asked for, use ldap_msgfree to free the LDAPMessage.
138 */
139 ULONG CDECL WLDAP32_ldap_result2error( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res, ULONG free )
140 {
141 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
142 #ifdef HAVE_LDAP
143 int error;
144
145 TRACE( "(%p, %p, 0x%08x)\n", ld, res, free );
146
147 if (!ld || !res) return ~0u;
148
149 ret = ldap_parse_result( ld, res, &error, NULL, NULL, NULL, NULL, free );
150
151 if (ret == LDAP_SUCCESS)
152 ret = error;
153 else
154 ret = ~0u;
155
156 #endif
157 return ret;
158 }
159
160 /***********************************************************************
161 * LdapGetLastError (WLDAP32.@)
162 *
163 * Return the last error set by an LDAP function call.
164 *
165 * PARAMS
166 * None.
167 *
168 * RETURNS
169 * An LDAP error code.
170 */
171 ULONG CDECL LdapGetLastError( void )
172 {
173 TRACE( "\n" );
174 return GetLastError();
175 }
176
177 static const ULONG WLDAP32_errormap[] = {
178 /* LDAP_SUCCESS */ ERROR_SUCCESS,
179 /* LDAP_OPERATIONS_ERROR */ ERROR_OPEN_FAILED,
180 /* LDAP_PROTOCOL_ERROR */ ERROR_INVALID_LEVEL,
181 /* LDAP_TIMELIMIT_EXCEEDED */ ERROR_TIMEOUT,
182 /* LDAP_SIZELIMIT_EXCEEDED */ ERROR_MORE_DATA,
183 /* LDAP_COMPARE_FALSE */ ERROR_DS_GENERIC_ERROR,
184 /* LDAP_COMPARE_TRUE */ ERROR_DS_GENERIC_ERROR,
185 /* LDAP_AUTH_METHOD_NOT_SUPPORTED */ ERROR_ACCESS_DENIED,
186 /* LDAP_STRONG_AUTH_REQUIRED */ ERROR_ACCESS_DENIED,
187 /* LDAP_REFERRAL_V2 */ ERROR_MORE_DATA,
188 /* LDAP_REFERRAL */ ERROR_MORE_DATA,
189 /* LDAP_ADMIN_LIMIT_EXCEEDED */ ERROR_NOT_ENOUGH_QUOTA,
190 /* LDAP_UNAVAILABLE_CRIT_EXTENSION */ ERROR_CAN_NOT_COMPLETE,
191 /* LDAP_CONFIDENTIALITY_REQUIRED */ ERROR_DS_GENERIC_ERROR,
192 /* LDAP_SASL_BIND_IN_PROGRESS */ ERROR_DS_GENERIC_ERROR,
193 /* 0x0f */ ERROR_DS_GENERIC_ERROR,
194 /* LDAP_NO_SUCH_ATTRIBUTE */ ERROR_INVALID_PARAMETER,
195 /* LDAP_UNDEFINED_TYPE */ ERROR_DS_GENERIC_ERROR,
196 /* LDAP_INAPPROPRIATE_MATCHING */ ERROR_INVALID_PARAMETER,
197 /* LDAP_CONSTRAINT_VIOLATION */ ERROR_INVALID_PARAMETER,
198 /* LDAP_ATTRIBUTE_OR_VALUE_EXISTS */ ERROR_ALREADY_EXISTS,
199 /* LDAP_INVALID_SYNTAX */ ERROR_INVALID_NAME,
200 /* 0x16 */ ERROR_DS_GENERIC_ERROR,
201 /* 0x17 */ ERROR_DS_GENERIC_ERROR,
202 /* 0x18 */ ERROR_DS_GENERIC_ERROR,
203 /* 0x19 */ ERROR_DS_GENERIC_ERROR,
204 /* 0x1a */ ERROR_DS_GENERIC_ERROR,
205 /* 0x1b */ ERROR_DS_GENERIC_ERROR,
206 /* 0x1c */ ERROR_DS_GENERIC_ERROR,
207 /* 0x1d */ ERROR_DS_GENERIC_ERROR,
208 /* 0x1e */ ERROR_DS_GENERIC_ERROR,
209 /* 0x1f */ ERROR_DS_GENERIC_ERROR,
210 /* LDAP_NO_SUCH_OBJECT */ ERROR_FILE_NOT_FOUND,
211 /* LDAP_ALIAS_PROBLEM */ ERROR_DS_GENERIC_ERROR,
212 /* LDAP_INVALID_DN_SYNTAX */ ERROR_INVALID_PARAMETER,
213 /* LDAP_IS_LEAF */ ERROR_DS_GENERIC_ERROR,
214 /* LDAP_ALIAS_DEREF_PROBLEM */ ERROR_DS_GENERIC_ERROR,
215 /* 0x25 */ ERROR_DS_GENERIC_ERROR,
216 /* 0x26 */ ERROR_DS_GENERIC_ERROR,
217 /* 0x27 */ ERROR_DS_GENERIC_ERROR,
218 /* 0x28 */ ERROR_DS_GENERIC_ERROR,
219 /* 0x29 */ ERROR_DS_GENERIC_ERROR,
220 /* 0x2a */ ERROR_DS_GENERIC_ERROR,
221 /* 0x2b */ ERROR_DS_GENERIC_ERROR,
222 /* 0x2c */ ERROR_DS_GENERIC_ERROR,
223 /* 0x2d */ ERROR_DS_GENERIC_ERROR,
224 /* 0x2e */ ERROR_DS_GENERIC_ERROR,
225 /* 0x2f */ ERROR_DS_GENERIC_ERROR,
226 /* LDAP_INAPPROPRIATE_AUTH */ ERROR_ACCESS_DENIED,
227 /* LDAP_INVALID_CREDENTIALS */ ERROR_WRONG_PASSWORD,
228 /* LDAP_INSUFFICIENT_RIGHTS */ ERROR_ACCESS_DENIED,
229 /* LDAP_BUSY */ ERROR_BUSY,
230 /* LDAP_UNAVAILABLE */ ERROR_DEV_NOT_EXIST,
231 /* LDAP_UNWILLING_TO_PERFORM */ ERROR_CAN_NOT_COMPLETE,
232 /* LDAP_LOOP_DETECT */ ERROR_DS_GENERIC_ERROR,
233 /* 0x37 */ ERROR_DS_GENERIC_ERROR,
234 /* 0x38 */ ERROR_DS_GENERIC_ERROR,
235 /* 0x39 */ ERROR_DS_GENERIC_ERROR,
236 /* 0x3a */ ERROR_DS_GENERIC_ERROR,
237 /* 0x3b */ ERROR_DS_GENERIC_ERROR,
238 /* LDAP_SORT_CONTROL_MISSING */ 8261,
239 /* LDAP_OFFSET_RANGE_ERROR */ 8262,
240 /* 0x3e */ ERROR_DS_GENERIC_ERROR,
241 /* 0x3f */ ERROR_DS_GENERIC_ERROR,
242 /* LDAP_NAMING_VIOLATION */ ERROR_INVALID_PARAMETER,
243 /* LDAP_OBJECT_CLASS_VIOLATION */ ERROR_INVALID_PARAMETER,
244 /* LDAP_NOT_ALLOWED_ON_NONLEAF */ ERROR_CAN_NOT_COMPLETE,
245 /* LDAP_NOT_ALLOWED_ON_RDN */ ERROR_ACCESS_DENIED,
246 /* LDAP_ALREADY_EXISTS */ ERROR_ALREADY_EXISTS,
247 /* LDAP_NO_OBJECT_CLASS_MODS */ ERROR_ACCESS_DENIED,
248 /* LDAP_RESULTS_TOO_LARGE */ ERROR_INSUFFICIENT_BUFFER,
249 /* LDAP_AFFECTS_MULTIPLE_DSAS */ ERROR_CAN_NOT_COMPLETE,
250 /* 0x48 */ ERROR_DS_GENERIC_ERROR,
251 /* 0x49 */ ERROR_DS_GENERIC_ERROR,
252 /* 0x4a */ ERROR_DS_GENERIC_ERROR,
253 /* 0x4b */ ERROR_DS_GENERIC_ERROR,
254 /* LDAP_VIRTUAL_LIST_VIEW_ERROR */ ERROR_DS_GENERIC_ERROR,
255 /* 0x4d */ ERROR_DS_GENERIC_ERROR,
256 /* 0x4e */ ERROR_DS_GENERIC_ERROR,
257 /* 0x4f */ ERROR_DS_GENERIC_ERROR,
258 /* LDAP_OTHER */ ERROR_DS_GENERIC_ERROR,
259 /* LDAP_SERVER_DOWN */ ERROR_BAD_NET_RESP,
260 /* LDAP_LOCAL_ERROR */ ERROR_DS_GENERIC_ERROR,
261 /* LDAP_ENCODING_ERROR */ ERROR_UNEXP_NET_ERR,
262 /* LDAP_DECODING_ERROR */ ERROR_UNEXP_NET_ERR,
263 /* LDAP_TIMEOUT */ ERROR_SERVICE_REQUEST_TIMEOUT,
264 /* LDAP_AUTH_UNKNOWN */ ERROR_WRONG_PASSWORD,
265 /* LDAP_FILTER_ERROR */ ERROR_INVALID_PARAMETER,
266 /* LDAP_USER_CANCELLED */ ERROR_CANCELLED,
267 /* LDAP_PARAM_ERROR */ ERROR_INVALID_PARAMETER,
268 /* LDAP_NO_MEMORY */ ERROR_NOT_ENOUGH_MEMORY,
269 /* LDAP_CONNECT_ERROR */ ERROR_CONNECTION_REFUSED,
270 /* LDAP_NOT_SUPPORTED */ ERROR_CAN_NOT_COMPLETE,
271 /* LDAP_CONTROL_NOT_FOUND */ ERROR_NOT_FOUND,
272 /* LDAP_NO_RESULTS_RETURNED */ ERROR_MORE_DATA,
273 /* LDAP_MORE_RESULTS_TO_RETURN */ ERROR_MORE_DATA,
274 /* LDAP_CLIENT_LOOP */ ERROR_DS_GENERIC_ERROR,
275 /* LDAP_REFERRAL_LIMIT_EXCEEDED */ ERROR_DS_GENERIC_ERROR
276 };
277
278 /***********************************************************************
279 * LdapMapErrorToWin32 (WLDAP32.@)
280 *
281 * Map an LDAP error code to a Win32 error code.
282 *
283 * PARAMS
284 * err [I] An LDAP error code.
285 *
286 * RETURNS
287 * A Win32 error code.
288 */
289 ULONG CDECL LdapMapErrorToWin32( ULONG err )
290 {
291 TRACE( "(0x%08x)\n", err );
292
293 if (err >= sizeof(WLDAP32_errormap)/sizeof(WLDAP32_errormap[0]))
294 return ERROR_DS_GENERIC_ERROR;
295 return WLDAP32_errormap[err];
296 }