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