misc.c 16.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 25 26
 */

#include "config.h"

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

#include <stdarg.h>
27
#include <stdio.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41

#include "windef.h"
#include "winbase.h"
#include "winnls.h"

#ifdef HAVE_LDAP_H
#include <ldap.h>
#endif

#include "winldap_private.h"
#include "wldap32.h"

WINE_DEFAULT_DEBUG_CHANNEL(wldap32);

42 43 44 45 46 47 48 49 50 51 52 53 54
/***********************************************************************
 *      ldap_abandon     (WLDAP32.@)
 *
 * Cancel an asynchronous operation.
 *
 * PARAMS
 *  ld    [I] Pointer to an LDAP context.
 *  msgid [I] ID of the operation to cancel.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 */
55
ULONG CDECL WLDAP32_ldap_abandon( WLDAP32_LDAP *ld, ULONG msgid )
56
{
57
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
58 59
#ifdef HAVE_LDAP

60
    TRACE( "(%p, 0x%08x)\n", ld, msgid );
61 62

    if (!ld) return ~0UL;
63
    ret = ldap_abandon_ext( ld, msgid, NULL, NULL );
64 65 66 67 68

#endif
    return ret;
}

69 70 71 72 73
/***********************************************************************
 *      ldap_check_filterA     (WLDAP32.@)
 *
 * See ldap_check_filterW.
 */
74
ULONG CDECL ldap_check_filterA( WLDAP32_LDAP *ld, PCHAR filter )
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
{
    ULONG ret;
    WCHAR *filterW = NULL;

    TRACE( "(%p, %s)\n", ld, debugstr_a(filter) );

    if (!ld) return WLDAP32_LDAP_PARAM_ERROR;

    if (filter) {
        filterW = strAtoW( filter );
        if (!filterW) return WLDAP32_LDAP_NO_MEMORY;
    }

    ret = ldap_check_filterW( ld, filterW );

    strfreeW( filterW );
    return ret;
}

94 95 96 97 98 99 100 101 102 103 104 105 106
/***********************************************************************
 *      ldap_check_filterW     (WLDAP32.@)
 *
 * Check filter syntax.
 *
 * PARAMS
 *  ld     [I] Pointer to an LDAP context.
 *  filter [I] Filter string.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 */
107
ULONG CDECL ldap_check_filterW( WLDAP32_LDAP *ld, PWCHAR filter )
108 109 110 111
{
    TRACE( "(%p, %s)\n", ld, debugstr_w(filter) );

    if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
112
    return WLDAP32_LDAP_SUCCESS; /* FIXME: do some checks */
113 114
}

115 116 117
/***********************************************************************
 *      ldap_cleanup     (WLDAP32.@)
 */
118
ULONG CDECL ldap_cleanup( HANDLE instance )
119 120
{
    TRACE( "(%p)\n", instance );
121
    return WLDAP32_LDAP_SUCCESS;
122 123
}

124 125 126 127 128 129 130 131 132 133 134 135 136
/***********************************************************************
 *      ldap_conn_from_msg     (WLDAP32.@)
 *
 * Get the LDAP context for a given message.
 *
 * PARAMS
 *  ld  [I] Pointer to an LDAP context.
 *  res [I] LDAP message.
 *
 * RETURNS
 *  Success: Pointer to an LDAP context.
 *  Failure: NULL
 */
137
WLDAP32_LDAP * CDECL ldap_conn_from_msg( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
138 139 140 141 142 143 144
{
    TRACE( "(%p, %p)\n", ld, res );

    if (!ld || !res) return NULL;
    return ld; /* FIXME: not always correct */
}

145 146 147 148 149 150 151 152 153 154 155 156 157
/***********************************************************************
 *      ldap_count_entries     (WLDAP32.@)
 *
 * Count the number of entries returned from a search.
 *
 * PARAMS
 *  ld  [I] Pointer to an LDAP context.
 *  res [I] LDAP message.
 *
 * RETURNS
 *  Success: The number of entries.
 *  Failure: ~0UL
 */
158
ULONG CDECL WLDAP32_ldap_count_entries( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
159
{
160
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
161 162 163 164 165 166 167 168 169 170 171
#ifdef HAVE_LDAP

    TRACE( "(%p, %p)\n", ld, res );

    if (!ld) return ~0UL;
    ret = ldap_count_entries( ld, res );

#endif
    return ret;
}

172 173 174 175 176 177 178 179 180 181 182 183 184
/***********************************************************************
 *      ldap_count_references     (WLDAP32.@)
 *
 * Count the number of references returned from a search.
 *
 * PARAMS
 *  ld  [I] Pointer to an LDAP context.
 *  res [I] LDAP message.
 *
 * RETURNS
 *  Success: The number of references.
 *  Failure: ~0UL
 */
185
ULONG CDECL WLDAP32_ldap_count_references( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
186
{
187
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
188
#ifdef HAVE_LDAP_COUNT_REFERENCES
189 190 191 192 193 194 195 196 197 198

    TRACE( "(%p, %p)\n", ld, res );

    if (!ld) return 0;
    ret = ldap_count_references( ld, res );

#endif
    return ret;
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static ULONG get_escape_size( PCHAR src, ULONG srclen )
{
    ULONG i, size = 0;

    if (src)
    {
        for (i = 0; i < srclen; i++)
        {
            if ((src[i] >= '0' && src[i] <= '9') ||
                (src[i] >= 'A' && src[i] <= 'Z') ||
                (src[i] >= 'a' && src[i] <= 'z'))
                size++;
            else
                size += 3;
        }
    }
    return size + 1;
}

static void escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst )
{
    ULONG i;
221
    static const char fmt[] = "\\%02X";
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    char *d = dst;

    for (i = 0; i < srclen; i++)
    {
        if ((src[i] >= '0' && src[i] <= '9') ||
            (src[i] >= 'A' && src[i] <= 'Z') ||
            (src[i] >= 'a' && src[i] <= 'z'))
            *d++ = src[i];
        else
        {
            sprintf( d, fmt, (unsigned char)src[i] );
            d += 3;
        }
    }
    *++d = 0;
}

239 240 241 242 243
/***********************************************************************
 *      ldap_escape_filter_elementA     (WLDAP32.@)
 *
 * See ldap_escape_filter_elementW.
 */
244
ULONG CDECL ldap_escape_filter_elementA( PCHAR src, ULONG srclen, PCHAR dst, ULONG dstlen )
245 246 247
{
    ULONG len;

248
    TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
249 250 251 252 253 254 255 256 257

    len = get_escape_size( src, srclen );
    if (!dst) return len;

    if (!src || dstlen < len)
        return WLDAP32_LDAP_PARAM_ERROR;
    else
    {
        escape_filter_element( src, srclen, dst );
258
        return WLDAP32_LDAP_SUCCESS;
259 260 261
    }
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
/***********************************************************************
 *      ldap_escape_filter_elementW     (WLDAP32.@)
 *
 * Escape binary data for safe passing in filters.
 *
 * PARAMS
 *  src    [I] Filter element to be escaped.
 *  srclen [I] Length in bytes of the filter element.
 *  dst    [O] Destination buffer for the escaped filter element.
 *  dstlen [I] Length in bytes of the destination buffer.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 */
277
ULONG CDECL ldap_escape_filter_elementW( PCHAR src, ULONG srclen, PWCHAR dst, ULONG dstlen )
278 279 280
{
    ULONG len;

281
    TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
282 283 284 285 286 287 288 289

    len = get_escape_size( src, srclen );
    if (!dst) return len;

    /* no matter what you throw at it, this is what native returns */
    return WLDAP32_LDAP_PARAM_ERROR;
}

290 291 292 293 294
/***********************************************************************
 *      ldap_first_attributeA     (WLDAP32.@)
 *
 * See ldap_first_attributeW.
 */
295
PCHAR CDECL ldap_first_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
296
    WLDAP32_BerElement** ptr )
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
{
    PCHAR ret = NULL;
#ifdef HAVE_LDAP
    WCHAR *retW;

    TRACE( "(%p, %p, %p)\n", ld, entry, ptr );

    if (!ld || !entry) return NULL;
    retW = ldap_first_attributeW( ld, entry, ptr );

    ret = strWtoA( retW );
    ldap_memfreeW( retW );

#endif
    return ret;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
/***********************************************************************
 *      ldap_first_attributeW     (WLDAP32.@)
 *
 * Get the first attribute for a given entry. 
 *
 * PARAMS
 *  ld    [I] Pointer to an LDAP context.
 *  entry [I] Entry to retrieve attribute for.
 *  ptr   [O] Position pointer.
 *
 * RETURNS
 *  Success: Name of the first attribute.
 *  Failure: NULL
 *
 * NOTES
 *  Use ldap_memfree to free the returned string.
 */
331
PWCHAR CDECL ldap_first_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
332
    WLDAP32_BerElement** ptr )
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
{
    PWCHAR ret = NULL;
#ifdef HAVE_LDAP
    char *retU;

    TRACE( "(%p, %p, %p)\n", ld, entry, ptr );

    if (!ld || !entry) return NULL;
    retU = ldap_first_attribute( ld, entry, ptr );

    ret = strUtoW( retU );
    ldap_memfree( retU );

#endif
    return ret;
}

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
/***********************************************************************
 *      ldap_first_entry     (WLDAP32.@)
 *
 * Get the first entry from a result message.
 *
 * PARAMS
 *  ld  [I] Pointer to an LDAP context.
 *  res [I] Search result message.
 *
 * RETURNS
 *  Success: The first entry.
 *  Failure: NULL
 *
 * NOTES
 *  The returned entry will be freed when the message is freed. 
 */
366
WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
367 368 369 370 371 372 373 374
{
#ifdef HAVE_LDAP

    TRACE( "(%p, %p)\n", ld, res );

    if (!ld || !res) return NULL;
    return ldap_first_entry( ld, res );

375
#else
376
    return NULL;
377
#endif
378 379
}

380 381 382 383 384 385 386 387 388 389 390 391 392
/***********************************************************************
 *      ldap_first_reference     (WLDAP32.@)
 *
 * Get the first reference from a result message.
 *
 * PARAMS
 *  ld  [I] Pointer to an LDAP context.
 *  res [I] Search result message.
 *
 * RETURNS
 *  Success: The first reference.
 *  Failure: NULL
 */
393
WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
394
{
395
#ifdef HAVE_LDAP_FIRST_REFERENCE
396 397 398 399 400 401

    TRACE( "(%p, %p)\n", ld, res );

    if (!ld) return NULL;
    return ldap_first_reference( ld, res );

402
#else
403
    return NULL;
404
#endif
405 406
}

407 408 409 410 411
/***********************************************************************
 *      ldap_memfreeA     (WLDAP32.@)
 *
 * See ldap_memfreeW.
 */
412
void CDECL ldap_memfreeA( PCHAR block )
413 414 415 416 417
{
    TRACE( "(%p)\n", block );
    strfreeA( block );
}

418 419 420 421 422 423 424 425
/***********************************************************************
 *      ldap_memfreeW     (WLDAP32.@)
 *
 * Free a block of memory.
 *
 * PARAMS
 *  block [I] Pointer to memory block to be freed.
 */
426
void CDECL ldap_memfreeW( PWCHAR block )
427 428 429 430 431
{
    TRACE( "(%p)\n", block );
    strfreeW( block );
}

432 433 434 435 436 437 438 439
/***********************************************************************
 *      ldap_msgfree     (WLDAP32.@)
 *
 * Free a message.
 *
 * PARAMS
 *  res [I] Message to be freed.
 */
440
ULONG CDECL WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage *res )
441
{
442
    ULONG ret = WLDAP32_LDAP_SUCCESS;
443 444 445 446 447 448 449 450 451
#ifdef HAVE_LDAP

    TRACE( "(%p)\n", res );
    ldap_msgfree( res );

#endif
    return ret;
}

452 453 454 455 456
/***********************************************************************
 *      ldap_next_attributeA     (WLDAP32.@)
 *
 * See ldap_next_attributeW.
 */
457
PCHAR CDECL ldap_next_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
458
    WLDAP32_BerElement *ptr )
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
{
    PCHAR ret = NULL;
#ifdef HAVE_LDAP
    WCHAR *retW;

    TRACE( "(%p, %p, %p)\n", ld, entry, ptr );

    if (!ld || !entry || !ptr) return NULL;
    retW = ldap_next_attributeW( ld, entry, ptr );

    ret = strWtoA( retW );
    ldap_memfreeW( retW );

#endif
    return ret;
}

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
/***********************************************************************
 *      ldap_next_attributeW     (WLDAP32.@)
 *
 * Get the next attribute for a given entry.
 *
 * PARAMS
 *  ld    [I]   Pointer to an LDAP context.
 *  entry [I]   Entry to retrieve attribute for.
 *  ptr   [I/O] Position pointer.
 *
 * RETURNS
 *  Success: The name of the next attribute.
 *  Failure: NULL
 *
 * NOTES
 *  Free the returned string after each iteration with ldap_memfree.
 *  When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
 */
494
PWCHAR CDECL ldap_next_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
495
    WLDAP32_BerElement *ptr )
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
{
    PWCHAR ret = NULL;
#ifdef HAVE_LDAP
    char *retU;

    TRACE( "(%p, %p, %p)\n", ld, entry, ptr );

    if (!ld || !entry || !ptr) return NULL;
    retU = ldap_next_attribute( ld, entry, ptr );

    ret = strUtoW( retU );
    ldap_memfree( retU );

#endif
    return ret;
}

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
/***********************************************************************
 *      ldap_next_entry     (WLDAP32.@)
 *
 * Get the next entry from a result message.
 *
 * PARAMS
 *  ld    [I] Pointer to an LDAP context.
 *  entry [I] Entry returned by a previous call.
 *
 * RETURNS
 *  Success: The next entry.
 *  Failure: NULL
 *
 * NOTES
 *  The returned entry will be freed when the message is freed.
 */
529
WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
530 531 532 533 534 535 536 537
{
#ifdef HAVE_LDAP

    TRACE( "(%p, %p)\n", ld, entry );

    if (!ld || !entry) return NULL;
    return ldap_next_entry( ld, entry );

538
#else
539
    return NULL;
540
#endif
541 542
}

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
/***********************************************************************
 *      ldap_next_reference     (WLDAP32.@)
 *
 * Get the next reference from a result message.
 *
 * PARAMS
 *  ld    [I] Pointer to an LDAP context.
 *  entry [I] Entry returned by a previous call.
 *
 * RETURNS
 *  Success: The next reference.
 *  Failure: NULL
 *
 * NOTES
 *  The returned entry will be freed when the message is freed.
 */
559
WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
560
{
561
#ifdef HAVE_LDAP_NEXT_REFERENCE
562 563 564 565 566 567

    TRACE( "(%p, %p)\n", ld, entry );

    if (!ld || !entry) return NULL;
    return ldap_next_reference( ld, entry );

568
#else
569
    return NULL;
570
#endif
571 572
}

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
/***********************************************************************
 *      ldap_result     (WLDAP32.@)
 *
 * Get the result of an asynchronous operation.
 *
 * PARAMS
 *  ld      [I] Pointer to an LDAP context.
 *  msgid   [I] Message ID of the operation.
 *  all     [I] How many results should be returned?
 *  timeout [I] How long to wait for the results?
 *  res     [O] Result message for the operation.
 *
 * RETURNS
 *  Success: One of the following values:
 *
 *   LDAP_RES_ADD
 *   LDAP_RES_BIND
 *   LDAP_RES_COMPARE
 *   LDAP_RES_DELETE
 *   LDAP_RES_EXTENDED
 *   LDAP_RES_MODIFY
 *   LDAP_RES_MODRDN
 *   LDAP_RES_REFERRAL
 *   LDAP_RES_SEARCH_ENTRY
 *   LDAP_RES_SEARCH_RESULT
 *
 *  Failure: ~0UL
 *
 *  This function returns 0 when the timeout has expired.
 *
 * NOTES
 *  A NULL timeout pointer causes the function to block waiting
 *  for results to arrive. A timeout value of 0 causes the function
 *  to immediately return any available results. Free returned results
 *  with ldap_msgfree.
 */
609
ULONG CDECL WLDAP32_ldap_result( WLDAP32_LDAP *ld, ULONG msgid, ULONG all,
Hans Leidekker's avatar
Hans Leidekker committed
610 611
    struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
{
612
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
Hans Leidekker's avatar
Hans Leidekker committed
613 614
#ifdef HAVE_LDAP

615
    TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld, msgid, all, timeout, res );
Hans Leidekker's avatar
Hans Leidekker committed
616

617
    if (!ld || !res || msgid == ~0UL) return ~0UL;
Hans Leidekker's avatar
Hans Leidekker committed
618 619 620 621 622 623
    ret = ldap_result( ld, msgid, all, (struct timeval *)timeout, res );

#endif
    return ret;
}

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
/***********************************************************************
 *      LdapUnicodeToUTF8     (WLDAP32.@)
 *
 * Convert a wide character string to a UTF8 string.
 *
 * PARAMS
 *  src    [I] Wide character string to convert.
 *  srclen [I] Size of string to convert, in characters.
 *  dst    [O] Pointer to a buffer that receives the converted string.
 *  dstlen [I] Size of the destination buffer in characters. 
 *
 * RETURNS
 *  The number of characters written into the destination buffer.
 *
 * NOTES
 *  Set dstlen to zero to ask for the required buffer size.
 */
641
int CDECL LdapUnicodeToUTF8( LPCWSTR src, int srclen, LPSTR dst, int dstlen )
642 643 644 645
{
    return WideCharToMultiByte( CP_UTF8, 0, src, srclen, dst, dstlen, NULL, NULL );
}

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
/***********************************************************************
 *      LdapUTF8ToUnicode     (WLDAP32.@)
 *
 * Convert a UTF8 string to a wide character string.
 *
 * PARAMS
 *  src    [I] UTF8 string to convert.
 *  srclen [I] Size of string to convert, in characters.
 *  dst    [O] Pointer to a buffer that receives the converted string.
 *  dstlen [I] Size of the destination buffer in characters. 
 *
 * RETURNS
 *  The number of characters written into the destination buffer.
 *
 * NOTES
 *  Set dstlen to zero to ask for the required buffer size.
 */
663
int CDECL LdapUTF8ToUnicode( LPCSTR src, int srclen, LPWSTR dst, int dstlen )
664 665 666
{
    return MultiByteToWideChar( CP_UTF8, 0, src, srclen, dst, dstlen );
}