value.c 8.41 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 31 32 33 34

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

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

WINE_DEFAULT_DEBUG_CHANNEL(wldap32);

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/***********************************************************************
 *      ldap_count_values_len     (WLDAP32.@)
 *
 * Count the number of values in an array of berval structures.
 *
 * PARAMS
 *  vals  [I] Pointer to an array of berval structures.
 *
 * RETURNS
 *  Success: The number of values counted.
 *  Failure: 0
 *
 * NOTES
 *  Call ldap_count_values_len with the result of a call to
 *  ldap_get_values_len.
 */
55
ULONG CDECL WLDAP32_ldap_count_values_len( struct WLDAP32_berval **vals )
56
{
57
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
58 59 60 61 62 63 64 65 66
#ifdef HAVE_LDAP

    TRACE( "(%p)\n", vals );
    ret = ldap_count_values_len( (struct berval **)vals );

#endif
    return ret;
}

67 68 69 70 71
/***********************************************************************
 *      ldap_count_valuesA     (WLDAP32.@)
 *
 * See ldap_count_valuesW.
 */
72
ULONG CDECL ldap_count_valuesA( PCHAR *vals )
73
{
74
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
75 76 77 78 79
#ifdef HAVE_LDAP
    WCHAR **valsW = NULL;

    TRACE( "(%p)\n", vals );

80 81 82 83
    if (!vals) return 0;

    valsW = strarrayAtoW( vals );
    if (!valsW) return WLDAP32_LDAP_NO_MEMORY;
84 85 86 87 88 89 90 91

    ret = ldap_count_valuesW( valsW );
    strarrayfreeW( valsW );

#endif
    return ret;
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/***********************************************************************
 *      ldap_count_valuesW     (WLDAP32.@)
 *
 * Count the number of values in a string array.
 *
 * PARAMS
 *  vals  [I] Pointer to an array of strings.
 *
 * RETURNS
 *  Success: The number of values counted.
 *  Failure: 0
 *
 * NOTES
 *  Call ldap_count_valuesW with the result of a call to
 *  ldap_get_valuesW.
 */
108
ULONG CDECL ldap_count_valuesW( PWCHAR *vals )
109
{
110
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
111
#ifdef HAVE_LDAP
112
    WCHAR **p = vals;
113 114 115

    TRACE( "(%p)\n", vals );

116
    if (!vals) return 0;
117

118
    ret = 0;
119
    while (*p++) ret++;
120 121 122 123 124

#endif
    return ret;
}

125 126 127 128 129
/***********************************************************************
 *      ldap_get_valuesA     (WLDAP32.@)
 *
 * See ldap_get_valuesW.
 */
130
PCHAR * CDECL ldap_get_valuesA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, PCHAR attr )
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
    PCHAR *ret = NULL;
#ifdef HAVE_LDAP
    WCHAR *attrW = NULL, **retW;

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

    if (!ld || !entry || !attr) return NULL;

    attrW = strAtoW( attr );
    if (!attrW) return NULL;

    retW = ldap_get_valuesW( ld, entry, attrW );

    ret = strarrayWtoA( retW );
    ldap_value_freeW( retW );
    strfreeW( attrW );

#endif
    return ret;
}

153
#ifdef HAVE_LDAP
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
static char *bv2str( struct berval *bv )
{
    char *str = NULL;
    unsigned int len = bv->bv_len;

    str = HeapAlloc( GetProcessHeap(), 0, len + 1 );
    if (str)
    {
        memcpy( str, bv->bv_val, len );
        str[len] = '\0';
    }
    return str;
}

static char **bv2str_array( struct berval **bv )
{
    unsigned int len = 0, i = 0;
    struct berval **p = bv;
    char **str;

    while (*p)
    {
        len++;
        p++;
    }
    str = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(char *) );
    if (!str) return NULL;

    p = bv;
    while (*p)
    {
        str[i] = bv2str( *p );
        if (!str[i])
        {
188
            while (i > 0) HeapFree( GetProcessHeap(), 0, str[--i] );
189 190 191 192 193 194 195 196 197
            HeapFree( GetProcessHeap(), 0, str );
            return NULL;
        } 
        i++;
        p++; 
    }
    str[i] = NULL;
    return str;
}
198
#endif
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/***********************************************************************
 *      ldap_get_valuesW     (WLDAP32.@)
 *
 * Retrieve string values for a given attribute.
 *
 * PARAMS
 *  ld     [I] Pointer to an LDAP context.
 *  entry  [I] Entry to retrieve values from.
 *  attr   [I] Attribute to retrieve values for.
 *
 * RETURNS
 *  Success: Pointer to a character array holding the values.
 *  Failure: NULL
 *
 * NOTES
 *  Call ldap_get_valuesW with the result of a call to
 *  ldap_first_entry or ldap_next_entry. Free the returned
 *  array with a call to ldap_value_freeW.
 */
219
PWCHAR * CDECL ldap_get_valuesW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, PWCHAR attr )
220 221 222 223
{
    PWCHAR *ret = NULL;
#ifdef HAVE_LDAP
    char *attrU = NULL, **retU;
224
    struct berval **bv;
225 226 227 228 229 230 231 232

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

    if (!ld || !entry || !attr) return NULL;

    attrU = strWtoU( attr );
    if (!attrU) return NULL;

233
    bv = ldap_get_values_len( ld, entry, attrU );
234

235
    retU = bv2str_array( bv );
236
    ret = strarrayUtoW( retU );
237 238 239

    ldap_value_free_len( bv );
    strarrayfreeU( retU );
240 241 242 243 244 245
    strfreeU( attrU );

#endif
    return ret;
}

246 247 248 249 250
/***********************************************************************
 *      ldap_get_values_lenA     (WLDAP32.@)
 *
 * See ldap_get_values_lenW.
 */
251
struct WLDAP32_berval ** CDECL ldap_get_values_lenA( WLDAP32_LDAP *ld,
252
    WLDAP32_LDAPMessage *message, PCHAR attr )
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
{
#ifdef HAVE_LDAP
    WCHAR *attrW = NULL;
    struct WLDAP32_berval **ret;

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

    if (!ld || !message || !attr) return NULL;

    attrW = strAtoW( attr );
    if (!attrW) return NULL;

    ret = ldap_get_values_lenW( ld, message, attrW );

    strfreeW( attrW );
    return ret;

270
#else
271
    return NULL;
272
#endif
273 274
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
/***********************************************************************
 *      ldap_get_values_lenW     (WLDAP32.@)
 *
 * Retrieve binary values for a given attribute.
 *
 * PARAMS
 *  ld      [I] Pointer to an LDAP context.
 *  message [I] Entry to retrieve values from.
 *  attr    [I] Attribute to retrieve values for.
 *
 * RETURNS
 *  Success: Pointer to a berval array holding the values.
 *  Failure: NULL
 *
 * NOTES
 *  Call ldap_get_values_lenW with the result of a call to
 *  ldap_first_entry or ldap_next_entry. Free the returned
 *  array with a call to ldap_value_free_len.
 */
294
struct WLDAP32_berval ** CDECL ldap_get_values_lenW( WLDAP32_LDAP *ld,
295
    WLDAP32_LDAPMessage *message, PWCHAR attr )
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
{
#ifdef HAVE_LDAP
    char *attrU = NULL;
    struct berval **ret;

    TRACE( "(%p, %p, %s)\n", ld, message, debugstr_w(attr) );

    if (!ld || !message || !attr) return NULL;

    attrU = strWtoU( attr );
    if (!attrU) return NULL;

    ret = ldap_get_values_len( ld, message, attrU );

    strfreeU( attrU );
    return (struct WLDAP32_berval **)ret;

313
#else
314
    return NULL;
315
#endif
316 317
}

318 319 320 321 322 323 324 325 326 327 328 329
/***********************************************************************
 *      ldap_value_free_len     (WLDAP32.@)
 *
 * Free an array of berval structures.
 *
 * PARAMS
 *  vals  [I] Array of berval structures.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 */
330
ULONG CDECL WLDAP32_ldap_value_free_len( struct WLDAP32_berval **vals )
331 332 333 334 335 336 337
{
#ifdef HAVE_LDAP

    TRACE( "(%p)\n", vals );
    ldap_value_free_len( (struct berval **)vals );

#endif
338
    return WLDAP32_LDAP_SUCCESS;
339 340
}

341 342 343 344 345
/***********************************************************************
 *      ldap_value_freeA     (WLDAP32.@)
 *
 * See ldap_value_freeW.
 */
346
ULONG CDECL ldap_value_freeA( PCHAR *vals )
347 348 349 350
{
    TRACE( "(%p)\n", vals );

    strarrayfreeA( vals );
351
    return WLDAP32_LDAP_SUCCESS;
352 353
}

354 355 356 357 358 359 360 361 362 363 364 365
/***********************************************************************
 *      ldap_value_freeW     (WLDAP32.@)
 *
 * Free an array of string values.
 *
 * PARAMS
 *  vals  [I] Array of string values.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 */
366
ULONG CDECL ldap_value_freeW( PWCHAR *vals )
367 368 369 370
{
    TRACE( "(%p)\n", vals );

    strarrayfreeW( vals );
371
    return WLDAP32_LDAP_SUCCESS;
372
}