pagectrl.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2024 The OpenLDAP Foundation.
  5. * Copyright 2006 Hans Leidekker
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in the file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. #include "portable.h"
  17. #include <stdio.h>
  18. #include <ac/stdlib.h>
  19. #include <ac/string.h>
  20. #include <ac/time.h>
  21. #include "ldap-int.h"
  22. /* ---------------------------------------------------------------------------
  23. ldap_create_page_control_value
  24. Create and encode the value of the paged results control (RFC 2696).
  25. ld (IN) An LDAP session handle
  26. pagesize (IN) Page size requested
  27. cookie (IN) Opaque structure used by the server to track its
  28. location in the search results. NULL on the
  29. first call.
  30. value (OUT) Control value, SHOULD be freed by calling
  31. ldap_memfree() when done.
  32. pagedResultsControl ::= SEQUENCE {
  33. controlType 1.2.840.113556.1.4.319,
  34. criticality BOOLEAN DEFAULT FALSE,
  35. controlValue searchControlValue }
  36. searchControlValue ::= SEQUENCE {
  37. size INTEGER (0..maxInt),
  38. -- requested page size from client
  39. -- result set size estimate from server
  40. cookie OCTET STRING }
  41. ---------------------------------------------------------------------------*/
  42. int
  43. ldap_create_page_control_value(
  44. LDAP *ld,
  45. ber_int_t pagesize,
  46. struct berval *cookie,
  47. struct berval *value )
  48. {
  49. BerElement *ber = NULL;
  50. ber_tag_t tag;
  51. struct berval null_cookie = { 0, NULL };
  52. if ( ld == NULL || value == NULL ||
  53. pagesize < 1 || pagesize > LDAP_MAXINT )
  54. {
  55. if ( ld )
  56. ld->ld_errno = LDAP_PARAM_ERROR;
  57. return LDAP_PARAM_ERROR;
  58. }
  59. assert( LDAP_VALID( ld ) );
  60. value->bv_val = NULL;
  61. value->bv_len = 0;
  62. ld->ld_errno = LDAP_SUCCESS;
  63. if ( cookie == NULL ) {
  64. cookie = &null_cookie;
  65. }
  66. ber = ldap_alloc_ber_with_options( ld );
  67. if ( ber == NULL ) {
  68. ld->ld_errno = LDAP_NO_MEMORY;
  69. return ld->ld_errno;
  70. }
  71. tag = ber_printf( ber, "{iO}", pagesize, cookie );
  72. if ( tag == LBER_ERROR ) {
  73. ld->ld_errno = LDAP_ENCODING_ERROR;
  74. goto done;
  75. }
  76. if ( ber_flatten2( ber, value, 1 ) == -1 ) {
  77. ld->ld_errno = LDAP_NO_MEMORY;
  78. }
  79. done:;
  80. if ( ber != NULL ) {
  81. ber_free( ber, 1 );
  82. }
  83. return ld->ld_errno;
  84. }
  85. /* ---------------------------------------------------------------------------
  86. ldap_create_page_control
  87. Create and encode a page control.
  88. ld (IN) An LDAP session handle
  89. pagesize (IN) Page size requested
  90. cookie (IN) Opaque structure used by the server to track its
  91. location in the search results. NULL on the
  92. first call.
  93. value (OUT) Control value, SHOULD be freed by calling
  94. ldap_memfree() when done.
  95. iscritical (IN) Criticality
  96. ctrlp (OUT) LDAP control, SHOULD be freed by calling
  97. ldap_control_free() when done.
  98. pagedResultsControl ::= SEQUENCE {
  99. controlType 1.2.840.113556.1.4.319,
  100. criticality BOOLEAN DEFAULT FALSE,
  101. controlValue searchControlValue }
  102. searchControlValue ::= SEQUENCE {
  103. size INTEGER (0..maxInt),
  104. -- requested page size from client
  105. -- result set size estimate from server
  106. cookie OCTET STRING }
  107. ---------------------------------------------------------------------------*/
  108. int
  109. ldap_create_page_control(
  110. LDAP *ld,
  111. ber_int_t pagesize,
  112. struct berval *cookie,
  113. int iscritical,
  114. LDAPControl **ctrlp )
  115. {
  116. struct berval value;
  117. if ( ctrlp == NULL ) {
  118. ld->ld_errno = LDAP_PARAM_ERROR;
  119. return ld->ld_errno;
  120. }
  121. ld->ld_errno = ldap_create_page_control_value( ld,
  122. pagesize, cookie, &value );
  123. if ( ld->ld_errno == LDAP_SUCCESS ) {
  124. ld->ld_errno = ldap_control_create( LDAP_CONTROL_PAGEDRESULTS,
  125. iscritical, &value, 0, ctrlp );
  126. if ( ld->ld_errno != LDAP_SUCCESS ) {
  127. LDAP_FREE( value.bv_val );
  128. }
  129. }
  130. return ld->ld_errno;
  131. }
  132. /* ---------------------------------------------------------------------------
  133. ldap_parse_pageresponse_control
  134. Decode a page control.
  135. ld (IN) An LDAP session handle
  136. ctrl (IN) The page response control
  137. count (OUT) The number of entries in the page.
  138. cookie (OUT) Opaque cookie. Use ldap_memfree() to
  139. free the bv_val member of this structure.
  140. ---------------------------------------------------------------------------*/
  141. int
  142. ldap_parse_pageresponse_control(
  143. LDAP *ld,
  144. LDAPControl *ctrl,
  145. ber_int_t *countp,
  146. struct berval *cookie )
  147. {
  148. BerElement *ber;
  149. ber_tag_t tag;
  150. ber_int_t count;
  151. if ( ld == NULL || ctrl == NULL || cookie == NULL ) {
  152. if ( ld )
  153. ld->ld_errno = LDAP_PARAM_ERROR;
  154. return LDAP_PARAM_ERROR;
  155. }
  156. /* Create a BerElement from the berval returned in the control. */
  157. ber = ber_init( &ctrl->ldctl_value );
  158. if ( ber == NULL ) {
  159. ld->ld_errno = LDAP_NO_MEMORY;
  160. return ld->ld_errno;
  161. }
  162. /* Extract the count and cookie from the control. */
  163. tag = ber_scanf( ber, "{io}", &count, cookie );
  164. ber_free( ber, 1 );
  165. if ( tag == LBER_ERROR ) {
  166. ld->ld_errno = LDAP_DECODING_ERROR;
  167. } else {
  168. ld->ld_errno = LDAP_SUCCESS;
  169. if ( countp != NULL ) {
  170. *countp = (unsigned long)count;
  171. }
  172. }
  173. return ld->ld_errno;
  174. }
  175. /* ---------------------------------------------------------------------------
  176. ldap_parse_page_control
  177. Decode a page control.
  178. ld (IN) An LDAP session handle
  179. ctrls (IN) Response controls
  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.
  183. ---------------------------------------------------------------------------*/
  184. int
  185. ldap_parse_page_control(
  186. LDAP *ld,
  187. LDAPControl **ctrls,
  188. ber_int_t *countp,
  189. struct berval **cookiep )
  190. {
  191. LDAPControl *c;
  192. struct berval cookie;
  193. if ( cookiep == NULL ) {
  194. ld->ld_errno = LDAP_PARAM_ERROR;
  195. return ld->ld_errno;
  196. }
  197. if ( ctrls == NULL ) {
  198. ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
  199. return ld->ld_errno;
  200. }
  201. c = ldap_control_find( LDAP_CONTROL_PAGEDRESULTS, ctrls, NULL );
  202. if ( c == NULL ) {
  203. /* No page control was found. */
  204. ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
  205. return ld->ld_errno;
  206. }
  207. ld->ld_errno = ldap_parse_pageresponse_control( ld, c, countp, &cookie );
  208. if ( ld->ld_errno == LDAP_SUCCESS ) {
  209. *cookiep = LDAP_MALLOC( sizeof( struct berval ) );
  210. if ( *cookiep == NULL ) {
  211. ld->ld_errno = LDAP_NO_MEMORY;
  212. } else {
  213. **cookiep = cookie;
  214. }
  215. }
  216. return ld->ld_errno;
  217. }