page.c 8.18 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 28 29 30
#ifdef HAVE_LDAP_H
#include <ldap.h>
#endif
#ifndef LDAP_MAXINT
#define LDAP_MAXINT  2147483647
#endif
31 32 33 34 35 36 37

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

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

40 41 42 43 44 45 46
WINE_DEFAULT_DEBUG_CHANNEL(wldap32);

/***********************************************************************
 *      ldap_create_page_controlA     (WLDAP32.@)
 *
 * See ldap_create_page_controlW.
 */
47
ULONG CDECL ldap_create_page_controlA( WLDAP32_LDAP *ld, ULONG pagesize,
48 49
    struct WLDAP32_berval *cookie, UCHAR critical, PLDAPControlA *control )
{
50
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
51 52 53
#ifdef HAVE_LDAP
    LDAPControlW *controlW = NULL;

54
    TRACE( "(%p, 0x%08x, %p, 0x%02x, %p)\n", ld, pagesize, cookie,
55 56
           critical, control );

57 58
    if (!ld || !control || pagesize > LDAP_MAXINT)
        return WLDAP32_LDAP_PARAM_ERROR;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    ret = ldap_create_page_controlW( ld, pagesize, cookie, critical, &controlW );
    if (ret == LDAP_SUCCESS)
    {
        *control = controlWtoA( controlW );
        ldap_control_freeW( controlW );
    }

#endif
    return ret;
}

#ifdef HAVE_LDAP

/* create a page control by hand */
static ULONG create_page_control( ULONG pagesize, struct WLDAP32_berval *cookie,
    UCHAR critical, PLDAPControlW *control )
{
    LDAPControlW *ctrl;
    BerElement *ber;
79
    ber_tag_t tag;
80 81 82 83 84 85 86 87
    struct berval *berval, null_cookie = { 0, NULL };
    INT ret, len;
    char *val;

    ber = ber_alloc_t( LBER_USE_DER );
    if (!ber) return WLDAP32_LDAP_NO_MEMORY;

    if (cookie)
88
        tag = ber_printf( ber, "{iO}", (ber_int_t)pagesize, cookie );
89
    else
90
        tag = ber_printf( ber, "{iO}", (ber_int_t)pagesize, &null_cookie );
91 92 93 94

    ret = ber_flatten( ber, &berval );
    ber_free( ber, 1 );

95 96 97
    if (tag == LBER_ERROR)
        return WLDAP32_LDAP_ENCODING_ERROR;

98 99 100 101 102 103 104 105 106 107 108 109 110
    if (ret == -1)
        return WLDAP32_LDAP_NO_MEMORY;

    /* copy the berval so it can be properly freed by the caller */
    val = HeapAlloc( GetProcessHeap(), 0, berval->bv_len );
    if (!val) return WLDAP32_LDAP_NO_MEMORY;

    len = berval->bv_len;
    memcpy( val, berval->bv_val, len );
    ber_bvfree( berval );

    ctrl = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
    if (!ctrl)
111 112
    {
        HeapFree( GetProcessHeap(), 0, val );
113
        return WLDAP32_LDAP_NO_MEMORY;
114
    }
115 116 117 118 119 120 121 122

    ctrl->ldctl_oid = strAtoW( LDAP_PAGED_RESULT_OID_STRING );
    ctrl->ldctl_value.bv_len = len;
    ctrl->ldctl_value.bv_val = val;
    ctrl->ldctl_iscritical = critical;

    *control = ctrl;

123
    return WLDAP32_LDAP_SUCCESS;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
}

#endif /* HAVE_LDAP */

/***********************************************************************
 *      ldap_create_page_controlW     (WLDAP32.@)
 *
 * Create a control for paged search results.
 *
 * PARAMS
 *  ld        [I] Pointer to an LDAP context.
 *  pagesize  [I] Number of entries to return per page.
 *  cookie    [I] Used by the server to track its location in the
 *                search results.
 *  critical  [I] Tells the server this control is critical to the
 *                search operation.
 *  control   [O] LDAPControl created.
 *
 * RETURNS
 *  Success: LDAP_SUCCESS
 *  Failure: An LDAP error code.
 */
146
ULONG CDECL ldap_create_page_controlW( WLDAP32_LDAP *ld, ULONG pagesize,
147 148 149
    struct WLDAP32_berval *cookie, UCHAR critical, PLDAPControlW *control )
{
#ifdef HAVE_LDAP
150
    TRACE( "(%p, 0x%08x, %p, 0x%02x, %p)\n", ld, pagesize, cookie,
151 152
           critical, control );

153 154 155
    if (!ld || !control || pagesize > LDAP_MAXINT)
        return WLDAP32_LDAP_PARAM_ERROR;

156 157
    return create_page_control( pagesize, cookie, critical, control );

158
#else
159
    return WLDAP32_LDAP_NOT_SUPPORTED;
160 161 162
#endif
}

163
ULONG CDECL ldap_get_next_page( WLDAP32_LDAP *ld, PLDAPSearch search, ULONG pagesize,
164 165
    ULONG *message )
{
166
    FIXME( "(%p, %p, 0x%08x, %p)\n", ld, search, pagesize, message );
167

168
    if (!ld) return ~0u;
169
    return WLDAP32_LDAP_NOT_SUPPORTED;
170 171
}

172
ULONG CDECL ldap_get_next_page_s( WLDAP32_LDAP *ld, PLDAPSearch search,
173 174 175
    struct l_timeval *timeout, ULONG pagesize, ULONG *count,
    WLDAP32_LDAPMessage **results )
{
176
    FIXME( "(%p, %p, %p, 0x%08x, %p, %p)\n", ld, search, timeout,
177 178
           pagesize, count, results );

179
    if (!ld) return ~0u;
180
    return WLDAP32_LDAP_NOT_SUPPORTED;
181 182
}

183
ULONG CDECL ldap_get_paged_count( WLDAP32_LDAP *ld, PLDAPSearch search,
184 185
    ULONG *count, WLDAP32_LDAPMessage *results )
{
186
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
187 188 189 190 191 192 193 194 195 196
#ifdef HAVE_LDAP
    FIXME( "(%p, %p, %p, %p)\n", ld, search, count, results );

    if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
    /* FIXME: save the cookie from the server here */

#endif
    return ret;
}

197 198 199
/***********************************************************************
 *      ldap_parse_page_controlA      (WLDAP32.@)
 */
200
ULONG CDECL ldap_parse_page_controlA( WLDAP32_LDAP *ld, PLDAPControlA *ctrls,
201 202
    ULONG *count, struct WLDAP32_berval **cookie )
{
203
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
#ifdef HAVE_LDAP
    LDAPControlW **ctrlsW = NULL;

    TRACE( "(%p, %p, %p, %p)\n", ld, ctrls, count, cookie );

    if (!ld || !ctrls || !count || !cookie)
        return WLDAP32_LDAP_PARAM_ERROR;

    ctrlsW = controlarrayAtoW( ctrls );
    if (!ctrlsW) return WLDAP32_LDAP_NO_MEMORY;

    ret = ldap_parse_page_controlW( ld, ctrlsW, count, cookie );
    controlarrayfreeW( ctrlsW );
 
#endif
    return ret;
}

222 223 224
/***********************************************************************
 *      ldap_parse_page_controlW      (WLDAP32.@)
 */
225
ULONG CDECL ldap_parse_page_controlW( WLDAP32_LDAP *ld, PLDAPControlW *ctrls,
226 227
    ULONG *count, struct WLDAP32_berval **cookie )
{
228
    ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
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
#ifdef HAVE_LDAP
    LDAPControlW *control = NULL;
    BerElement *ber;
    ber_tag_t tag;
    ULONG i;

    TRACE( "(%p, %p, %p, %p)\n", ld, ctrls, count, cookie );

    if (!ld || !ctrls || !count || !cookie)
        return WLDAP32_LDAP_PARAM_ERROR;

    for (i = 0; ctrls[i]; i++)
    {
        if (!lstrcmpW( LDAP_PAGED_RESULT_OID_STRING_W, ctrls[i]->ldctl_oid ))
            control = ctrls[i];
    }

    if (!control)
        return WLDAP32_LDAP_CONTROL_NOT_FOUND; 
            
    ber = ber_init( &((LDAPControl *)control)->ldctl_value );
    if (!ber)
        return WLDAP32_LDAP_NO_MEMORY;

    tag = ber_scanf( ber, "{iO}", count, cookie );
    if ( tag == LBER_ERROR )
        ret = WLDAP32_LDAP_DECODING_ERROR;
    else
257
        ret = WLDAP32_LDAP_SUCCESS;
258 259 260 261 262 263 264

    ber_free( ber, 1 );
    
#endif
    return ret;
}

265
ULONG CDECL ldap_search_abandon_page( WLDAP32_LDAP *ld, PLDAPSearch search )
266 267 268
{
    FIXME( "(%p, %p)\n", ld, search );

269
    if (!ld) return ~0u;
270
    return WLDAP32_LDAP_SUCCESS;
271 272
}

273
PLDAPSearch CDECL ldap_search_init_pageA( WLDAP32_LDAP *ld, PCHAR dn, ULONG scope,
274 275 276
    PCHAR filter, PCHAR attrs[], ULONG attrsonly, PLDAPControlA *serverctrls,
    PLDAPControlA *clientctrls, ULONG timelimit, ULONG sizelimit, PLDAPSortKeyA *sortkeys )
{
277
    FIXME( "(%p, %s, 0x%08x, %s, %p, 0x%08x)\n", ld, debugstr_a(dn),
278 279 280 281
           scope, debugstr_a(filter), attrs, attrsonly );
    return NULL;
}

282
PLDAPSearch CDECL ldap_search_init_pageW( WLDAP32_LDAP *ld, PWCHAR dn, ULONG scope,
283 284 285
    PWCHAR filter, PWCHAR attrs[], ULONG attrsonly, PLDAPControlW *serverctrls,
    PLDAPControlW *clientctrls, ULONG timelimit, ULONG sizelimit, PLDAPSortKeyW *sortkeys )
{
286
    FIXME( "(%p, %s, 0x%08x, %s, %p, 0x%08x)\n", ld, debugstr_w(dn),
287 288 289
           scope, debugstr_w(filter), attrs, attrsonly );
    return NULL;
}