1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 * Copyright 1998-2006 The OpenLDAP Foundation.
4 * Copyright 2006 Hans Leidekker
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (C) The Internet Society (1999)
16 * ASN.1 fragments are from RFC 2696; see RFC for full legal notices.
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
28 /* ---------------------------------------------------------------------------
29 ldap_create_page_control_value
31 Create and encode the value of the paged results control (RFC 2696).
33 ld (IN) An LDAP session handle
34 pagesize (IN) Page size requested
35 cookie (IN) Opaque structure used by the server to track its
36 location in the search results. NULL on the
38 value (OUT) Control value, SHOULD be freed by calling
39 ldap_memfree() when done.
41 pagedResultsControl ::= SEQUENCE {
42 controlType 1.2.840.113556.1.4.319,
43 criticality BOOLEAN DEFAULT FALSE,
44 controlValue searchControlValue }
46 searchControlValue ::= SEQUENCE {
47 size INTEGER (0..maxInt),
48 -- requested page size from client
49 -- result set size estimate from server
52 ---------------------------------------------------------------------------*/
55 ldap_create_page_control_value(
58 struct berval *cookie,
59 struct berval *value )
61 BerElement *ber = NULL;
63 struct berval null_cookie = { 0, NULL };
65 if ( ld == NULL || value == NULL ||
66 pagesize < 1 || pagesize > LDAP_MAXINT )
68 ld->ld_errno = LDAP_PARAM_ERROR;
72 assert( LDAP_VALID( ld ) );
77 if ( cookie == NULL ) {
78 cookie = &null_cookie;
81 ber = ldap_alloc_ber_with_options( ld );
83 ld->ld_errno = LDAP_NO_MEMORY;
87 tag = ber_printf( ber, "{iO}", pagesize, cookie );
88 if ( tag == LBER_ERROR ) {
89 ld->ld_errno = LDAP_ENCODING_ERROR;
93 if ( ber_flatten2( ber, value, 1 ) == -1 ) {
94 ld->ld_errno = LDAP_NO_MEMORY;
106 /* ---------------------------------------------------------------------------
107 ldap_create_page_control
109 Create and encode a page control.
111 ld (IN) An LDAP session handle
112 pagesize (IN) Page size requested
113 cookie (IN) Opaque structure used by the server to track its
114 location in the search results. NULL on the
116 value (OUT) Control value, SHOULD be freed by calling
117 ldap_memfree() when done.
118 iscritical (IN) Criticality
119 ctrlp (OUT) LDAP control, SHOULD be freed by calling
120 ldap_control_free() when done.
122 pagedResultsControl ::= SEQUENCE {
123 controlType 1.2.840.113556.1.4.319,
124 criticality BOOLEAN DEFAULT FALSE,
125 controlValue searchControlValue }
127 searchControlValue ::= SEQUENCE {
128 size INTEGER (0..maxInt),
129 -- requested page size from client
130 -- result set size estimate from server
131 cookie OCTET STRING }
133 ---------------------------------------------------------------------------*/
136 ldap_create_page_control(
139 struct berval *cookie,
141 LDAPControl **ctrlp )
146 if ( ctrlp == NULL ) {
147 ld->ld_errno = LDAP_PARAM_ERROR;
151 ld->ld_errno = ldap_create_page_control_value( ld,
152 pagesize, cookie, &value );
153 if ( ld->ld_errno == LDAP_SUCCESS ) {
154 if ((ber = ldap_alloc_ber_with_options(ld)) == NULL) {
155 ld->ld_errno = LDAP_NO_MEMORY;
156 return LDAP_NO_MEMORY;
159 ld->ld_errno = ldap_create_control( LDAP_CONTROL_PAGEDRESULTS,
160 ber, iscritical, ctrlp );
161 if ( ld->ld_errno == LDAP_SUCCESS ) {
162 (*ctrlp)->ldctl_value = value;
164 LDAP_FREE( value.bv_val );
173 /* ---------------------------------------------------------------------------
174 ldap_parse_pageresponse_control
176 Decode a page control.
178 ld (IN) An LDAP session handle
179 ctrl (IN) The page response control
180 count (OUT) The number of entries in the page.
181 cookie (OUT) Opaque cookie. Use ldap_memfree() to
182 free the bv_val member of this structure.
184 ---------------------------------------------------------------------------*/
187 ldap_parse_pageresponse_control(
191 struct berval *cookie )
197 if ( ld == NULL || ctrl == NULL || cookie == NULL ) {
198 ld->ld_errno = LDAP_PARAM_ERROR;
202 /* Create a BerElement from the berval returned in the control. */
203 ber = ber_init( &ctrl->ldctl_value );
206 ld->ld_errno = LDAP_NO_MEMORY;
210 /* Extract the count and cookie from the control. */
211 tag = ber_scanf( ber, "{io}", &count, cookie );
214 if ( tag == LBER_ERROR ) {
215 ld->ld_errno = LDAP_DECODING_ERROR;
217 ld->ld_errno = LDAP_SUCCESS;
219 if ( countp != NULL ) {
220 *countp = (unsigned long)count;
227 /* ---------------------------------------------------------------------------
228 ldap_parse_page_control
230 Decode a page control.
232 ld (IN) An LDAP session handle
233 ctrls (IN) Response controls
234 count (OUT) The number of entries in the page.
235 cookie (OUT) Opaque cookie. Use ldap_memfree() to
236 free the bv_val member of this structure.
238 ---------------------------------------------------------------------------*/
241 ldap_parse_page_control(
245 struct berval **cookiep )
248 struct berval cookie;
250 if ( cookiep == NULL ) {
251 ld->ld_errno = LDAP_PARAM_ERROR;
255 if ( ctrls == NULL ) {
256 ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
260 c = ldap_find_control( LDAP_CONTROL_PAGEDRESULTS, ctrls );
262 /* No page control was found. */
263 ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
267 ld->ld_errno = ldap_parse_pageresponse_control( ld, c, countp, &cookie );
268 if ( ld->ld_errno == LDAP_SUCCESS ) {
269 *cookiep = LDAP_MALLOC( sizeof( struct berval * ) );
270 if ( *cookiep == NULL ) {
271 ld->ld_errno = LDAP_NO_MEMORY;