error.c 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * WLDAP32 - LDAP support for Wine
 *
 * Copyright 2005 Hans Leidekker
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24
 */

#include "config.h"
#include "wine/port.h"

#include <stdarg.h>
25 26 27
#ifdef HAVE_LDAP_H
#include <ldap.h>
#endif
28 29 30

#include "windef.h"
#include "winbase.h"
31
#include "winuser.h"
32 33 34 35
#include "winnls.h"

#include "winldap_private.h"
#include "wldap32.h"
36
#include "wine/debug.h"
37 38 39

WINE_DEFAULT_DEBUG_CHANNEL(wldap32);

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
ULONG map_error( int error )
{
    switch (error)
    {
#ifdef HAVE_LDAP_H
    case LDAP_SERVER_DOWN:              return WLDAP32_LDAP_SERVER_DOWN;
    case LDAP_LOCAL_ERROR:              return WLDAP32_LDAP_LOCAL_ERROR;
    case LDAP_DECODING_ERROR:           return WLDAP32_LDAP_DECODING_ERROR;
    case LDAP_TIMEOUT:                  return WLDAP32_LDAP_TIMEOUT;
    case LDAP_AUTH_UNKNOWN:             return WLDAP32_LDAP_AUTH_UNKNOWN;
    case LDAP_FILTER_ERROR:             return WLDAP32_LDAP_FILTER_ERROR;
    case LDAP_USER_CANCELLED:           return WLDAP32_LDAP_USER_CANCELLED;
    case LDAP_PARAM_ERROR:              return WLDAP32_LDAP_PARAM_ERROR;
    case LDAP_NO_MEMORY:                return WLDAP32_LDAP_NO_MEMORY;
    case LDAP_CONNECT_ERROR:            return WLDAP32_LDAP_CONNECT_ERROR;
    case LDAP_NOT_SUPPORTED:            return WLDAP32_LDAP_NOT_SUPPORTED;
    case LDAP_CONTROL_NOT_FOUND:        return WLDAP32_LDAP_CONTROL_NOT_FOUND;
    case LDAP_NO_RESULTS_RETURNED:      return WLDAP32_LDAP_NO_RESULTS_RETURNED;
    case LDAP_MORE_RESULTS_TO_RETURN:   return WLDAP32_LDAP_MORE_RESULTS_TO_RETURN;
    case LDAP_CLIENT_LOOP:              return WLDAP32_LDAP_CLIENT_LOOP;
    case LDAP_REFERRAL_LIMIT_EXCEEDED:  return WLDAP32_LDAP_REFERRAL_LIMIT_EXCEEDED;
#endif
    default: return error;
    }
}

66 67 68 69 70
/***********************************************************************
 *      ldap_err2stringA     (WLDAP32.@)
 *
 * See ldap_err2stringW.
 */
71
PCHAR CDECL ldap_err2stringA( ULONG err )
72 73 74
{
    static char buf[256] = "";

75
    TRACE( "(0x%08x)\n", err );
76 77 78 79 80 81 82 83 84

    if (err <= WLDAP32_LDAP_REFERRAL_LIMIT_EXCEEDED)
        LoadStringA( hwldap32, err, buf, 256 );
    else
        LoadStringA( hwldap32, WLDAP32_LDAP_LOCAL_ERROR, buf, 256 );

    return buf;
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/***********************************************************************
 *      ldap_err2stringW     (WLDAP32.@)
 *
 * Convert an error code into a string describing the error.
 *
 * PARAMS
 *  err  [I] Error code to convert.
 *
 * RETURNS
 *  Success: Pointer to a string containing the error description.
 *  Failure: NULL
 *
 * NOTES
 *  The returned string is statically allocated, you must not
 *  free this string.
 */
101
PWCHAR CDECL ldap_err2stringW( ULONG err )
102 103 104
{
    static WCHAR buf[256] = { 0 };

105
    TRACE( "(0x%08x)\n", err );
106 107 108 109 110 111 112 113 114

    if (err <= WLDAP32_LDAP_REFERRAL_LIMIT_EXCEEDED)
        LoadStringW( hwldap32, err, buf, 256 );
    else
        LoadStringW( hwldap32, WLDAP32_LDAP_LOCAL_ERROR, buf, 256 );

    return buf;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128
/***********************************************************************
 *      ldap_perror     (WLDAP32.@)
 *
 * Print a given error string.
 *
 * PARAMS
 *  ld   [I] Pointer to an LDAP context.
 *  msg  [I] Error string.
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  Like native, this function does nothing.
129
 */
130
void CDECL WLDAP32_ldap_perror( WLDAP32_LDAP *ld, const PCHAR msg )
131 132 133 134
{
    TRACE( "(%p, %s)\n", ld, debugstr_a(msg) );
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/***********************************************************************
 *      ldap_result2error     (WLDAP32.@)
 *
 * Parse an LDAP message and return the error obtained from it.
 *
 * PARAMS
 *  ld    [I] Pointer to an LDAP context.
 *  res   [I] Pointer to an LDAPMessage structure.
 *  free  [I] Ask for the LDAPMessage structure to be freed.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 *
 * NOTES
 *  If not asked for, use ldap_msgfree to free the LDAPMessage.
 */
152
ULONG CDECL WLDAP32_ldap_result2error( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res, ULONG free )
153
{
154
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
155
#ifdef HAVE_LDAP
156
    int error;
157

158
    TRACE( "(%p, %p, 0x%08x)\n", ld, res, free );
159

160
    if (!ld || !res) return ~0u;
161 162 163 164 165 166

    ret = ldap_parse_result( ld, res, &error, NULL, NULL, NULL, NULL, free );

    if (ret == LDAP_SUCCESS)
        ret = error;
    else
167
        ret = ~0u;
168 169 170 171 172

#endif
    return ret;
}

173 174 175 176 177 178 179 180 181 182 183
/***********************************************************************
 *      LdapGetLastError     (WLDAP32.@)
 *
 * Return the last error set by an LDAP function call.
 *
 * PARAMS
 *  None.
 *
 * RETURNS
 *  An LDAP error code.
 */
184
ULONG CDECL LdapGetLastError( void )
185 186 187 188 189
{
    TRACE( "\n" );
    return GetLastError();
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
static const ULONG WLDAP32_errormap[] = {
    /* LDAP_SUCCESS */                      ERROR_SUCCESS,
    /* LDAP_OPERATIONS_ERROR */             ERROR_OPEN_FAILED,
    /* LDAP_PROTOCOL_ERROR */               ERROR_INVALID_LEVEL,
    /* LDAP_TIMELIMIT_EXCEEDED */           ERROR_TIMEOUT,
    /* LDAP_SIZELIMIT_EXCEEDED */           ERROR_MORE_DATA,
    /* LDAP_COMPARE_FALSE */                ERROR_DS_GENERIC_ERROR,
    /* LDAP_COMPARE_TRUE */                 ERROR_DS_GENERIC_ERROR,
    /* LDAP_AUTH_METHOD_NOT_SUPPORTED */    ERROR_ACCESS_DENIED,
    /* LDAP_STRONG_AUTH_REQUIRED */         ERROR_ACCESS_DENIED,
    /* LDAP_REFERRAL_V2 */                  ERROR_MORE_DATA,
    /* LDAP_REFERRAL */                     ERROR_MORE_DATA,
    /* LDAP_ADMIN_LIMIT_EXCEEDED */         ERROR_NOT_ENOUGH_QUOTA,
    /* LDAP_UNAVAILABLE_CRIT_EXTENSION */   ERROR_CAN_NOT_COMPLETE,
    /* LDAP_CONFIDENTIALITY_REQUIRED */     ERROR_DS_GENERIC_ERROR,
    /* LDAP_SASL_BIND_IN_PROGRESS */        ERROR_DS_GENERIC_ERROR,
    /* 0x0f */                              ERROR_DS_GENERIC_ERROR,
    /* LDAP_NO_SUCH_ATTRIBUTE */            ERROR_INVALID_PARAMETER,
    /* LDAP_UNDEFINED_TYPE */               ERROR_DS_GENERIC_ERROR,
    /* LDAP_INAPPROPRIATE_MATCHING */       ERROR_INVALID_PARAMETER,
    /* LDAP_CONSTRAINT_VIOLATION */         ERROR_INVALID_PARAMETER,
    /* LDAP_ATTRIBUTE_OR_VALUE_EXISTS */    ERROR_ALREADY_EXISTS,
    /* LDAP_INVALID_SYNTAX */               ERROR_INVALID_NAME,
    /* 0x16 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x17 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x18 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x19 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x1a */                              ERROR_DS_GENERIC_ERROR,
    /* 0x1b */                              ERROR_DS_GENERIC_ERROR,
    /* 0x1c */                              ERROR_DS_GENERIC_ERROR,
    /* 0x1d */                              ERROR_DS_GENERIC_ERROR,
    /* 0x1e */                              ERROR_DS_GENERIC_ERROR,
    /* 0x1f */                              ERROR_DS_GENERIC_ERROR,
    /* LDAP_NO_SUCH_OBJECT */               ERROR_FILE_NOT_FOUND,
    /* LDAP_ALIAS_PROBLEM */                ERROR_DS_GENERIC_ERROR,
    /* LDAP_INVALID_DN_SYNTAX */            ERROR_INVALID_PARAMETER,
    /* LDAP_IS_LEAF */                      ERROR_DS_GENERIC_ERROR,
    /* LDAP_ALIAS_DEREF_PROBLEM */          ERROR_DS_GENERIC_ERROR,
    /* 0x25 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x26 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x27 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x28 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x29 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x2a */                              ERROR_DS_GENERIC_ERROR,
    /* 0x2b */                              ERROR_DS_GENERIC_ERROR,
    /* 0x2c */                              ERROR_DS_GENERIC_ERROR,
    /* 0x2d */                              ERROR_DS_GENERIC_ERROR,
    /* 0x2e */                              ERROR_DS_GENERIC_ERROR,
    /* 0x2f */                              ERROR_DS_GENERIC_ERROR,
    /* LDAP_INAPPROPRIATE_AUTH */           ERROR_ACCESS_DENIED,
    /* LDAP_INVALID_CREDENTIALS */          ERROR_WRONG_PASSWORD,
    /* LDAP_INSUFFICIENT_RIGHTS */          ERROR_ACCESS_DENIED,
    /* LDAP_BUSY */                         ERROR_BUSY,
    /* LDAP_UNAVAILABLE */                  ERROR_DEV_NOT_EXIST,
    /* LDAP_UNWILLING_TO_PERFORM */         ERROR_CAN_NOT_COMPLETE,
    /* LDAP_LOOP_DETECT */                  ERROR_DS_GENERIC_ERROR,
    /* 0x37 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x38 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x39 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x3a */                              ERROR_DS_GENERIC_ERROR,
    /* 0x3b */                              ERROR_DS_GENERIC_ERROR,
    /* LDAP_SORT_CONTROL_MISSING */         8261,
    /* LDAP_OFFSET_RANGE_ERROR */           8262,
    /* 0x3e */                              ERROR_DS_GENERIC_ERROR,
    /* 0x3f */                              ERROR_DS_GENERIC_ERROR,
    /* LDAP_NAMING_VIOLATION */             ERROR_INVALID_PARAMETER,
    /* LDAP_OBJECT_CLASS_VIOLATION */       ERROR_INVALID_PARAMETER,
    /* LDAP_NOT_ALLOWED_ON_NONLEAF */       ERROR_CAN_NOT_COMPLETE,
    /* LDAP_NOT_ALLOWED_ON_RDN */           ERROR_ACCESS_DENIED,
    /* LDAP_ALREADY_EXISTS */               ERROR_ALREADY_EXISTS,
    /* LDAP_NO_OBJECT_CLASS_MODS */         ERROR_ACCESS_DENIED,
    /* LDAP_RESULTS_TOO_LARGE */            ERROR_INSUFFICIENT_BUFFER,
    /* LDAP_AFFECTS_MULTIPLE_DSAS */        ERROR_CAN_NOT_COMPLETE,
    /* 0x48 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x49 */                              ERROR_DS_GENERIC_ERROR,
    /* 0x4a */                              ERROR_DS_GENERIC_ERROR,
    /* 0x4b */                              ERROR_DS_GENERIC_ERROR,
    /* LDAP_VIRTUAL_LIST_VIEW_ERROR */      ERROR_DS_GENERIC_ERROR,
    /* 0x4d */                              ERROR_DS_GENERIC_ERROR,
    /* 0x4e */                              ERROR_DS_GENERIC_ERROR,
    /* 0x4f */                              ERROR_DS_GENERIC_ERROR,
    /* LDAP_OTHER */                        ERROR_DS_GENERIC_ERROR,
    /* LDAP_SERVER_DOWN */                  ERROR_BAD_NET_RESP,
    /* LDAP_LOCAL_ERROR */                  ERROR_DS_GENERIC_ERROR,
    /* LDAP_ENCODING_ERROR */               ERROR_UNEXP_NET_ERR,
    /* LDAP_DECODING_ERROR */               ERROR_UNEXP_NET_ERR,
    /* LDAP_TIMEOUT */                      ERROR_SERVICE_REQUEST_TIMEOUT,
    /* LDAP_AUTH_UNKNOWN */                 ERROR_WRONG_PASSWORD,
    /* LDAP_FILTER_ERROR */                 ERROR_INVALID_PARAMETER,
    /* LDAP_USER_CANCELLED */               ERROR_CANCELLED,
    /* LDAP_PARAM_ERROR */                  ERROR_INVALID_PARAMETER,
    /* LDAP_NO_MEMORY */                    ERROR_NOT_ENOUGH_MEMORY,
    /* LDAP_CONNECT_ERROR */                ERROR_CONNECTION_REFUSED,
    /* LDAP_NOT_SUPPORTED */                ERROR_CAN_NOT_COMPLETE,
    /* LDAP_CONTROL_NOT_FOUND */            ERROR_NOT_FOUND,
    /* LDAP_NO_RESULTS_RETURNED */          ERROR_MORE_DATA,
    /* LDAP_MORE_RESULTS_TO_RETURN */       ERROR_MORE_DATA,
    /* LDAP_CLIENT_LOOP */                  ERROR_DS_GENERIC_ERROR,
    /* LDAP_REFERRAL_LIMIT_EXCEEDED */      ERROR_DS_GENERIC_ERROR
};

291 292 293 294 295 296 297 298 299 300 301
/***********************************************************************
 *      LdapMapErrorToWin32     (WLDAP32.@)
 *
 * Map an LDAP error code to a Win32 error code.
 *
 * PARAMS
 *  err  [I] An LDAP error code.
 *
 * RETURNS
 *  A Win32 error code.
 */
302
ULONG CDECL LdapMapErrorToWin32( ULONG err )
303
{
304
    TRACE( "(0x%08x)\n", err );
305

306
    if (err >= sizeof(WLDAP32_errormap)/sizeof(WLDAP32_errormap[0]))
307 308 309
        return ERROR_DS_GENERIC_ERROR;
    return WLDAP32_errormap[err];
}