parse.c 13.5 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 */

#include "config.h"

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

#include <stdarg.h>

#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);

41 42 43 44 45
/***********************************************************************
 *      ldap_parse_extended_resultA     (WLDAP32.@)
 *
 * See ldap_parse_extended_resultW.
 */
46
ULONG CDECL ldap_parse_extended_resultA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *result,
47 48
    PCHAR *oid, struct WLDAP32_berval **data, BOOLEAN free )
{
49
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#ifdef HAVE_LDAP
    WCHAR *oidW = NULL;

    TRACE( "(%p, %p, %p, %p, 0x%02x)\n", ld, result, oid, data, free );

    if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
    if (!result) return WLDAP32_LDAP_NO_RESULTS_RETURNED;

    ret = ldap_parse_extended_resultW( ld, result, &oidW, data, free );

    if (oid) {
        *oid = strWtoA( oidW );
        if (!*oid) ret = WLDAP32_LDAP_NO_MEMORY;
        ldap_memfreeW( oidW );
    }

#endif
    return ret;
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/***********************************************************************
 *      ldap_parse_extended_resultW     (WLDAP32.@)
 *
 * Parse the result of an extended operation. 
 *
 * PARAMS
 *  ld      [I] Pointer to an LDAP context.
 *  result  [I] Result message from an extended operation.
 *  oid     [O] OID of the extended operation.
 *  data    [O] Result data.
 *  free    [I] Free the result message?
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 *
 * NOTES
 *  Free the OID and result data with ldap_memfree. Pass a nonzero
 *  value for 'free' or call ldap_msgfree to free the result message.
 */
90
ULONG CDECL ldap_parse_extended_resultW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *result,
91 92
    PWCHAR *oid, struct WLDAP32_berval **data, BOOLEAN free )
{
93
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
94 95 96 97 98 99 100 101
#ifdef HAVE_LDAP
    char *oidU = NULL;

    TRACE( "(%p, %p, %p, %p, 0x%02x)\n", ld, result, oid, data, free );

    if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
    if (!result) return WLDAP32_LDAP_NO_RESULTS_RETURNED;

102
    ret = map_error( ldap_parse_extended_result( ld, result, &oidU, (struct berval **)data, free ) );
103 104 105 106 107 108 109 110 111 112 113

    if (oid) {
        *oid = strUtoW( oidU );
        if (!*oid) ret = WLDAP32_LDAP_NO_MEMORY;
        ldap_memfree( oidU );
    }

#endif
    return ret;
}

114 115 116 117 118
/***********************************************************************
 *      ldap_parse_referenceA     (WLDAP32.@)
 *
 * See ldap_parse_referenceW.
 */
119
ULONG CDECL ldap_parse_referenceA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *message,
120 121
    PCHAR **referrals )
{
122
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
123 124 125 126 127
#ifdef HAVE_LDAP
    WCHAR **referralsW = NULL;

    TRACE( "(%p, %p, %p)\n", ld, message, referrals );

128
    if (!ld) return ~0u;
129 130 131 132 133 134 135 136 137 138

    ret = ldap_parse_referenceW( ld, message, &referralsW );

    *referrals = strarrayWtoA( referralsW );
    ldap_value_freeW( referralsW );

#endif 
    return ret;
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/***********************************************************************
 *      ldap_parse_referenceW     (WLDAP32.@)
 *
 * Return any referrals from a result message.
 *
 * PARAMS
 *  ld         [I] Pointer to an LDAP context.
 *  result     [I] Result message.
 *  referrals  [O] Array of referral URLs.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 *
 * NOTES
 *  Free the referrals with ldap_value_free.
 */
156
ULONG CDECL ldap_parse_referenceW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *message,
157 158
    PWCHAR **referrals )
{
159
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
160
#ifdef HAVE_LDAP_PARSE_REFERENCE
161 162 163 164
    char **referralsU = NULL;

    TRACE( "(%p, %p, %p)\n", ld, message, referrals );

165
    if (!ld) return ~0u;
166
    
167
    ret = map_error( ldap_parse_reference( ld, message, &referralsU, NULL, 0 ));
168 169 170 171 172 173 174 175

    *referrals = strarrayUtoW( referralsU );
    ldap_memfree( referralsU );

#endif
    return ret;
}

176 177 178 179 180
/***********************************************************************
 *      ldap_parse_resultA     (WLDAP32.@)
 *
 * See ldap_parse_resultW.
 */
181
ULONG CDECL ldap_parse_resultA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *result,
182 183 184
    ULONG *retcode, PCHAR *matched, PCHAR *error, PCHAR **referrals,
    PLDAPControlA **serverctrls, BOOLEAN free )
{
185
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
186
#ifdef HAVE_LDAP
187
    WCHAR *matchedW = NULL, *errorW = NULL, **referralsW = NULL;
188 189 190 191 192
    LDAPControlW **serverctrlsW = NULL;

    TRACE( "(%p, %p, %p, %p, %p, %p, %p, 0x%02x)\n", ld, result, retcode,
           matched, error, referrals, serverctrls, free );

193
    if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
194

195
    ret = ldap_parse_resultW( ld, result, retcode, &matchedW, &errorW,
196 197
                              &referralsW, &serverctrlsW, free );

198 199
    if (matched) *matched = strWtoA( matchedW );
    if (error) *error = strWtoA( errorW );
200

201 202
    if (referrals) *referrals = strarrayWtoA( referralsW );
    if (serverctrls) *serverctrls = controlarrayWtoA( serverctrlsW );
203

204 205
    ldap_memfreeW( matchedW );
    ldap_memfreeW( errorW );
206 207 208 209 210 211 212
    ldap_value_freeW( referralsW );
    ldap_controls_freeW( serverctrlsW );

#endif
    return ret;
}

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
/***********************************************************************
 *      ldap_parse_resultW     (WLDAP32.@)
 *
 * Parse a result message. 
 *
 * PARAMS
 *  ld           [I] Pointer to an LDAP context.
 *  result       [I] Result message.
 *  retcode      [O] Return code for the server operation.
 *  matched      [O] DNs matched in the operation.
 *  error        [O] Error message for the operation.
 *  referrals    [O] Referrals found in the result message.
 *  serverctrls  [O] Controls used in the operation.
 *  free         [I] Free the result message?
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 *
 * NOTES
 *  Free the DNs and error message with ldap_memfree. Free
 *  the referrals with ldap_value_free and the controls with
 *  ldap_controls_free. Pass a nonzero value for 'free' or call
 *  ldap_msgfree to free the result message.
 */
238
ULONG CDECL ldap_parse_resultW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *result,
239 240 241
    ULONG *retcode, PWCHAR *matched, PWCHAR *error, PWCHAR **referrals,
    PLDAPControlW **serverctrls, BOOLEAN free )
{
242
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
243
#ifdef HAVE_LDAP
244
    char *matchedU = NULL, *errorU = NULL, **referralsU = NULL;
245 246 247 248 249
    LDAPControl **serverctrlsU = NULL;

    TRACE( "(%p, %p, %p, %p, %p, %p, %p, 0x%02x)\n", ld, result, retcode,
           matched, error, referrals, serverctrls, free );

250
    if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
251

252 253
    ret = map_error( ldap_parse_result( ld, result, (int *)retcode, &matchedU, &errorU,
                                        &referralsU, &serverctrlsU, free ));
254

255 256
    if (matched) *matched = strUtoW( matchedU );
    if (error) *error = strUtoW( errorU );
257

258 259
    if (referrals) *referrals = strarrayUtoW( referralsU );
    if (serverctrls) *serverctrls = controlarrayUtoW( serverctrlsU );
260 261 262

    ldap_memfree( matchedU );
    ldap_memfree( errorU );
263
    strarrayfreeU( referralsU );
264 265 266
    ldap_controls_free( serverctrlsU );

#endif
267
    return ret;
268 269
}

270 271 272 273 274
/***********************************************************************
 *      ldap_parse_sort_controlA     (WLDAP32.@)
 *
 * See ldap_parse_sort_controlW.
 */
275
ULONG CDECL ldap_parse_sort_controlA( WLDAP32_LDAP *ld, PLDAPControlA *control,
276 277
    ULONG *result, PCHAR *attr )
{
278
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
279 280 281 282 283 284
#ifdef HAVE_LDAP
    WCHAR *attrW = NULL;
    LDAPControlW **controlW = NULL;

    TRACE( "(%p, %p, %p, %p)\n", ld, control, result, attr );

285 286
    if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
    if (!control) return WLDAP32_LDAP_CONTROL_NOT_FOUND;
287

288 289
    controlW = controlarrayAtoW( control );
    if (!controlW) return WLDAP32_LDAP_NO_MEMORY;
290 291 292 293 294 295 296 297 298 299

    ret = ldap_parse_sort_controlW( ld, controlW, result, &attrW );

    *attr = strWtoA( attrW );
    controlarrayfreeW( controlW );

#endif
    return ret;
}

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
/***********************************************************************
 *      ldap_parse_sort_controlW     (WLDAP32.@)
 *
 * Parse a sort control.
 *
 * PARAMS
 *  ld       [I] Pointer to an LDAP context.
 *  control  [I] Control obtained from a result message.
 *  result   [O] Result code.
 *  attr     [O] Failing attribute.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 *
 * NOTES
 *  If the function fails, free the failing attribute with ldap_memfree.
 */
318
ULONG CDECL ldap_parse_sort_controlW( WLDAP32_LDAP *ld, PLDAPControlW *control,
319 320
    ULONG *result, PWCHAR *attr )
{
321
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
322 323 324
#ifdef HAVE_LDAP
    char *attrU = NULL;
    LDAPControl **controlU = NULL;
325
#ifdef HAVE_LDAP_PARSE_SORT_CONTROL
326
    unsigned long res;
327 328 329 330 331
#elif defined(HAVE_LDAP_PARSE_SORTRESPONSE_CONTROL)
    ber_int_t res;
    LDAPControl *sortcontrol = NULL;
    unsigned int i;
#endif
332 333 334

    TRACE( "(%p, %p, %p, %p)\n", ld, control, result, attr );

335 336
    if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
    if (!control) return WLDAP32_LDAP_CONTROL_NOT_FOUND;
337

338 339
    controlU = controlarrayWtoU( control );
    if (!controlU) return WLDAP32_LDAP_NO_MEMORY;
340

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
#ifdef HAVE_LDAP_PARSE_SORT_CONTROL
    if (!(ret = ldap_parse_sort_control( ld, controlU, &res, &attrU )))
    {
        *result = res;
        *attr = strUtoW( attrU );
    }
#elif defined(HAVE_LDAP_PARSE_SORTRESPONSE_CONTROL)
    for (i = 0; controlU[i]; i++)
    {
        if (!strcmp( LDAP_SERVER_RESP_SORT_OID, controlU[i]->ldctl_oid ))
            sortcontrol = controlU[i];
    }
    if (!sortcontrol)
    {
        controlarrayfreeU( controlU );
        return WLDAP32_LDAP_CONTROL_NOT_FOUND;
    }
    if (!(ret = ldap_parse_sortresponse_control( ld, sortcontrol, &res, &attrU )))
    {
        *result = res;
        *attr = strUtoW( attrU );
    }
#endif
364 365 366
    controlarrayfreeU( controlU );

#endif
367
    return map_error( ret );
368 369
}

370 371 372 373 374
/***********************************************************************
 *      ldap_parse_vlv_controlA     (WLDAP32.@)
 *
 * See ldap_parse_vlv_controlW.
 */
375
INT CDECL ldap_parse_vlv_controlA( WLDAP32_LDAP *ld, PLDAPControlA *control,
376 377
    PULONG targetpos, PULONG listcount,
    struct WLDAP32_berval **context, PINT errcode )
378
{
379
    int ret = WLDAP32_LDAP_NOT_SUPPORTED;
380 381 382 383 384 385
#ifdef HAVE_LDAP
    LDAPControlW **controlW = NULL;

    TRACE( "(%p, %p, %p, %p, %p, %p)\n", ld, control, targetpos,
           listcount, context, errcode );

386
    if (!ld) return ~0u;
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

    if (control) {
        controlW = controlarrayAtoW( control );
        if (!controlW) return WLDAP32_LDAP_NO_MEMORY;
    }

    ret = ldap_parse_vlv_controlW( ld, controlW, targetpos, listcount,
                                   context, errcode );

    controlarrayfreeW( controlW );

#endif
    return ret;
}

402 403 404 405 406 407 408 409
/***********************************************************************
 *      ldap_parse_vlv_controlW     (WLDAP32.@)
 *
 * Parse a virtual list view control.
 *
 * PARAMS
 *  ld         [I] Pointer to an LDAP context.
 *  control    [I] Controls obtained from a result message.
Austin English's avatar
Austin English committed
410
 *  targetpos  [O] Position of the target in the result list.
411 412 413 414 415 416 417 418 419 420 421
 *  listcount  [O] Estimate of the number of results in the list.
 *  context    [O] Server side context.
 *  errcode    [O] Error code from the listview operation.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 *
 * NOTES
 *  Free the server context with ber_bvfree.
 */
422
INT CDECL ldap_parse_vlv_controlW( WLDAP32_LDAP *ld, PLDAPControlW *control,
423 424
    PULONG targetpos, PULONG listcount,
    struct WLDAP32_berval **context, PINT errcode )
425
{
426
    int ret = WLDAP32_LDAP_NOT_SUPPORTED;
427 428
#ifdef HAVE_LDAP
    LDAPControl **controlU = NULL;
429
#ifdef HAVE_LDAP_PARSE_VLV_CONTROL
430
    unsigned long pos, count;
431 432 433 434 435
#elif defined(HAVE_LDAP_PARSE_VLVRESPONSE_CONTROL)
    ber_int_t pos, count;
    LDAPControl *vlvcontrol = NULL;
    unsigned int i;
#endif
436 437 438 439

    TRACE( "(%p, %p, %p, %p, %p, %p)\n", ld, control, targetpos,
           listcount, context, errcode );

440
    if (!ld || !control) return ~0u;
441

442 443
    controlU = controlarrayWtoU( control );
    if (!controlU) return WLDAP32_LDAP_NO_MEMORY;
444

445 446
#ifdef HAVE_LDAP_PARSE_VLV_CONTROL
    if (!(ret = ldap_parse_vlv_control( ld, controlU, &pos, &count,
447
                                        context, errcode )))
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    {
        *targetpos = pos;
        *listcount = count;
    }
#elif defined(HAVE_LDAP_PARSE_VLVRESPONSE_CONTROL)
    for (i = 0; controlU[i]; i++)
    {
        if (!strcmp( LDAP_CONTROL_VLVRESPONSE, controlU[i]->ldctl_oid ))
            vlvcontrol = controlU[i];
    }
    if (!vlvcontrol)
    {
        controlarrayfreeU( controlU );
        return WLDAP32_LDAP_CONTROL_NOT_FOUND;
    }
    if (!(ret = ldap_parse_vlvresponse_control( ld, vlvcontrol, &pos, &count,
                                                (struct berval **)context, errcode )))
    {
        *targetpos = pos;
        *listcount = count;
    }
#endif
470 471 472
    controlarrayfreeU( controlU );

#endif
473
    return map_error( ret );
474
}