vlvctrl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  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>.
  14. */
  15. /* Portions Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
  16. *
  17. * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
  18. * TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
  19. * TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
  20. * AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
  21. * IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
  22. * OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
  23. * PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
  24. * THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
  25. *---
  26. * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
  27. * can be found in the file "build/LICENSE-2.0.1" in this distribution
  28. * of OpenLDAP Software.
  29. */
  30. #include "portable.h"
  31. #include <stdio.h>
  32. #include <ac/stdlib.h>
  33. #include <ac/string.h>
  34. #include <ac/time.h>
  35. #include "ldap-int.h"
  36. #define LDAP_VLVBYINDEX_IDENTIFIER 0xa0L
  37. #define LDAP_VLVBYVALUE_IDENTIFIER 0x81L
  38. #define LDAP_VLVCONTEXT_IDENTIFIER 0x04L
  39. /*---
  40. ldap_create_vlv_control
  41. Create and encode the Virtual List View control.
  42. ld (IN) An LDAP session handle.
  43. vlvinfop (IN) The address of an LDAPVLVInfo structure whose contents
  44. are used to construct the value of the control
  45. that is created.
  46. value (OUT) A struct berval that contains the value to be assigned to the ldctl_value member
  47. of an LDAPControl structure that contains the
  48. VirtualListViewRequest control.
  49. The bv_val member of the berval structure
  50. SHOULD be freed when it is no longer in use by
  51. calling ldap_memfree().
  52. Ber encoding
  53. VirtualListViewRequest ::= SEQUENCE {
  54. beforeCount INTEGER (0 .. maxInt),
  55. afterCount INTEGER (0 .. maxInt),
  56. CHOICE {
  57. byoffset [0] SEQUENCE, {
  58. offset INTEGER (0 .. maxInt),
  59. contentCount INTEGER (0 .. maxInt) }
  60. [1] greaterThanOrEqual assertionValue }
  61. contextID OCTET STRING OPTIONAL }
  62. Note: The first time the VLV control is created, the ldvlv_context
  63. field of the LDAPVLVInfo structure should be set to NULL.
  64. The context obtained from calling ldap_parse_vlv_control()
  65. should be used as the context in the next ldap_create_vlv_control
  66. call.
  67. ---*/
  68. int
  69. ldap_create_vlv_control_value(
  70. LDAP *ld,
  71. LDAPVLVInfo *vlvinfop,
  72. struct berval *value )
  73. {
  74. ber_tag_t tag;
  75. BerElement *ber;
  76. if ( ld == NULL || vlvinfop == NULL || value == NULL ) {
  77. if ( ld )
  78. ld->ld_errno = LDAP_PARAM_ERROR;
  79. return LDAP_PARAM_ERROR;
  80. }
  81. assert( LDAP_VALID( ld ) );
  82. value->bv_val = NULL;
  83. value->bv_len = 0;
  84. ld->ld_errno = LDAP_SUCCESS;
  85. ber = ldap_alloc_ber_with_options( ld );
  86. if ( ber == NULL ) {
  87. ld->ld_errno = LDAP_NO_MEMORY;
  88. return ld->ld_errno;
  89. }
  90. tag = ber_printf( ber, "{ii" /*}*/,
  91. vlvinfop->ldvlv_before_count,
  92. vlvinfop->ldvlv_after_count );
  93. if ( tag == LBER_ERROR ) {
  94. goto error_return;
  95. }
  96. if ( vlvinfop->ldvlv_attrvalue == NULL ) {
  97. tag = ber_printf( ber, "t{iiN}",
  98. LDAP_VLVBYINDEX_IDENTIFIER,
  99. vlvinfop->ldvlv_offset,
  100. vlvinfop->ldvlv_count );
  101. if ( tag == LBER_ERROR ) {
  102. goto error_return;
  103. }
  104. } else {
  105. tag = ber_printf( ber, "tO",
  106. LDAP_VLVBYVALUE_IDENTIFIER,
  107. vlvinfop->ldvlv_attrvalue );
  108. if ( tag == LBER_ERROR ) {
  109. goto error_return;
  110. }
  111. }
  112. if ( vlvinfop->ldvlv_context ) {
  113. tag = ber_printf( ber, "tO",
  114. LDAP_VLVCONTEXT_IDENTIFIER,
  115. vlvinfop->ldvlv_context );
  116. if ( tag == LBER_ERROR ) {
  117. goto error_return;
  118. }
  119. }
  120. tag = ber_printf( ber, /*{*/ "N}" );
  121. if ( tag == LBER_ERROR ) {
  122. goto error_return;
  123. }
  124. if ( ber_flatten2( ber, value, 1 ) == -1 ) {
  125. ld->ld_errno = LDAP_NO_MEMORY;
  126. }
  127. if ( 0 ) {
  128. error_return:;
  129. ld->ld_errno = LDAP_ENCODING_ERROR;
  130. }
  131. if ( ber != NULL ) {
  132. ber_free( ber, 1 );
  133. }
  134. return ld->ld_errno;
  135. }
  136. /*---
  137. ldap_create_vlv_control
  138. Create and encode the Virtual List View control.
  139. ld (IN) An LDAP session handle.
  140. vlvinfop (IN) The address of an LDAPVLVInfo structure whose contents
  141. are used to construct the value of the control
  142. that is created.
  143. ctrlp (OUT) A result parameter that will be assigned the address
  144. of an LDAPControl structure that contains the
  145. VirtualListViewRequest control created by this function.
  146. The memory occupied by the LDAPControl structure
  147. SHOULD be freed when it is no longer in use by
  148. calling ldap_control_free().
  149. Ber encoding
  150. VirtualListViewRequest ::= SEQUENCE {
  151. beforeCount INTEGER (0 .. maxInt),
  152. afterCount INTEGER (0 .. maxInt),
  153. CHOICE {
  154. byoffset [0] SEQUENCE, {
  155. offset INTEGER (0 .. maxInt),
  156. contentCount INTEGER (0 .. maxInt) }
  157. [1] greaterThanOrEqual assertionValue }
  158. contextID OCTET STRING OPTIONAL }
  159. Note: The first time the VLV control is created, the ldvlv_context
  160. field of the LDAPVLVInfo structure should be set to NULL.
  161. The context obtained from calling ldap_parse_vlv_control()
  162. should be used as the context in the next ldap_create_vlv_control
  163. call.
  164. ---*/
  165. int
  166. ldap_create_vlv_control(
  167. LDAP *ld,
  168. LDAPVLVInfo *vlvinfop,
  169. LDAPControl **ctrlp )
  170. {
  171. struct berval value;
  172. if ( ctrlp == NULL ) {
  173. ld->ld_errno = LDAP_PARAM_ERROR;
  174. return ld->ld_errno;
  175. }
  176. ld->ld_errno = ldap_create_vlv_control_value( ld, vlvinfop, &value );
  177. if ( ld->ld_errno == LDAP_SUCCESS ) {
  178. ld->ld_errno = ldap_control_create( LDAP_CONTROL_VLVREQUEST,
  179. 1, &value, 0, ctrlp );
  180. if ( ld->ld_errno != LDAP_SUCCESS ) {
  181. LDAP_FREE( value.bv_val );
  182. }
  183. }
  184. return ld->ld_errno;
  185. }
  186. /*---
  187. ldap_parse_vlvresponse_control
  188. Decode the Virtual List View control return information.
  189. ld (IN) An LDAP session handle.
  190. ctrl (IN) The address of the LDAPControl structure.
  191. target_posp (OUT) This result parameter is filled in with the list
  192. index of the target entry. If this parameter is
  193. NULL, the target position is not returned.
  194. list_countp (OUT) This result parameter is filled in with the server's
  195. estimate of the size of the list. If this parameter
  196. is NULL, the size is not returned.
  197. contextp (OUT) This result parameter is filled in with the address
  198. of a struct berval that contains the server-
  199. generated context identifier if one was returned by
  200. the server. If the server did not return a context
  201. identifier, this parameter will be set to NULL, even
  202. if an error occurred.
  203. The returned context SHOULD be used in the next call
  204. to create a VLV sort control. The struct berval
  205. returned SHOULD be disposed of by calling ber_bvfree()
  206. when it is no longer needed. If NULL is passed for
  207. contextp, the context identifier is not returned.
  208. errcodep (OUT) This result parameter is filled in with the VLV
  209. result code. If this parameter is NULL, the result
  210. code is not returned.
  211. Ber encoding
  212. VirtualListViewResponse ::= SEQUENCE {
  213. targetPosition INTEGER (0 .. maxInt),
  214. contentCount INTEGER (0 .. maxInt),
  215. virtualListViewResult ENUMERATED {
  216. success (0),
  217. operationsError (1),
  218. unwillingToPerform (53),
  219. insufficientAccessRights (50),
  220. busy (51),
  221. timeLimitExceeded (3),
  222. adminLimitExceeded (11),
  223. sortControlMissing (60),
  224. offsetRangeError (61),
  225. other (80) },
  226. contextID OCTET STRING OPTIONAL }
  227. ---*/
  228. int
  229. ldap_parse_vlvresponse_control(
  230. LDAP *ld,
  231. LDAPControl *ctrl,
  232. ber_int_t *target_posp,
  233. ber_int_t *list_countp,
  234. struct berval **contextp,
  235. ber_int_t *errcodep )
  236. {
  237. BerElement *ber;
  238. ber_int_t pos, count, err;
  239. ber_tag_t tag, berTag;
  240. ber_len_t berLen;
  241. assert( ld != NULL );
  242. assert( LDAP_VALID( ld ) );
  243. if (contextp) {
  244. *contextp = NULL; /* Make sure we return a NULL if error occurs. */
  245. }
  246. if (ctrl == NULL) {
  247. ld->ld_errno = LDAP_PARAM_ERROR;
  248. return(ld->ld_errno);
  249. }
  250. if (strcmp(LDAP_CONTROL_VLVRESPONSE, ctrl->ldctl_oid) != 0) {
  251. /* Not VLV Response control */
  252. ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
  253. return(ld->ld_errno);
  254. }
  255. /* Create a BerElement from the berval returned in the control. */
  256. ber = ber_init(&ctrl->ldctl_value);
  257. if (ber == NULL) {
  258. ld->ld_errno = LDAP_NO_MEMORY;
  259. return(ld->ld_errno);
  260. }
  261. /* Extract the data returned in the control. */
  262. tag = ber_scanf(ber, "{iie" /*}*/, &pos, &count, &err);
  263. if( tag == LBER_ERROR) {
  264. ber_free(ber, 1);
  265. ld->ld_errno = LDAP_DECODING_ERROR;
  266. return(ld->ld_errno);
  267. }
  268. /* Since the context is the last item encoded, if caller doesn't want
  269. it returned, don't decode it. */
  270. if (contextp) {
  271. if (LDAP_VLVCONTEXT_IDENTIFIER == ber_peek_tag(ber, &berLen)) {
  272. tag = ber_scanf(ber, "tO", &berTag, contextp);
  273. if( tag == LBER_ERROR) {
  274. ber_free(ber, 1);
  275. ld->ld_errno = LDAP_DECODING_ERROR;
  276. return(ld->ld_errno);
  277. }
  278. }
  279. }
  280. ber_free(ber, 1);
  281. /* Return data to the caller for items that were requested. */
  282. if (target_posp) *target_posp = pos;
  283. if (list_countp) *list_countp = count;
  284. if (errcodep) *errcodep = err;
  285. ld->ld_errno = LDAP_SUCCESS;
  286. return(ld->ld_errno);
  287. }