psearchctrl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /* ACKNOWLEDGEMENTS:
  16. * This work was developed by Howard Chu for inclusion in
  17. * OpenLDAP Software.
  18. */
  19. #include "portable.h"
  20. #include <stdio.h>
  21. #include <ac/stdlib.h>
  22. #include <ac/string.h>
  23. #include <ac/time.h>
  24. #include "ldap-int.h"
  25. /* Based on draft-ietf-ldapext-c-api-psearch-00 */
  26. /* ---------------------------------------------------------------------------
  27. ldap_create_persistentsearch_control_value
  28. Create and encode the value of the server-side sort control.
  29. ld (IN) An LDAP session handle, as obtained from a call to
  30. ldap_init().
  31. changetypes (IN) A bit-sensitive field that indicates which kinds of
  32. changes the client wants to be informed about. Its
  33. value should be LDAP_CHANGETYPE_ANY, or any logical-OR
  34. combination of LDAP_CHANGETYPE_ADD,
  35. LDAP_CHANGETYPE_DELETE, LDAP_CHANGETYPE_MODIFY, and
  36. LDAP_CHANGETYPE_MODDN. This field corresponds to the
  37. changeType element of the BER-encoded PersistentSearch
  38. control value itself.
  39. changesonly (IN) A Boolean field that indicates whether the client
  40. wishes to only receive searchResultEntry messages for
  41. entries that have been changed. If non-zero, only
  42. entries that result from changes are returned; other-
  43. wise, all of the static entries that match the search
  44. criteria are returned before the server begins change
  45. notification. This field corresponds to the changes-
  46. Only element of the BER-encoded PersistentSearch con-
  47. trol value itself.
  48. return_echg_ctls (IN) A Boolean field that indicates whether the server
  49. should send back an Entry Change Notification control
  50. with each searchResultEntry that is returned due to a
  51. change to an entry. If non-zero, Entry Change
  52. Notification controls are requested; if zero, they are
  53. not. This field corresponds to the returnECs element
  54. of the BER-encoded PersistentSearch control value
  55. itself.
  56. value (OUT) Contains the control value; the bv_val member of the berval structure
  57. SHOULD be freed by calling ldap_memfree() when done.
  58. ---------------------------------------------------------------------------*/
  59. int
  60. ldap_create_persistentsearch_control_value(
  61. LDAP *ld,
  62. int changetypes,
  63. int changesonly,
  64. int return_echg_ctls,
  65. struct berval *value )
  66. {
  67. int i;
  68. BerElement *ber = NULL;
  69. ber_tag_t tag;
  70. assert( ld != NULL );
  71. assert( LDAP_VALID( ld ) );
  72. if ( ld == NULL ) return LDAP_PARAM_ERROR;
  73. if ( value == NULL ) {
  74. ld->ld_errno = LDAP_PARAM_ERROR;
  75. return LDAP_PARAM_ERROR;
  76. }
  77. if (( changetypes & 0x0f ) != changetypes ) {
  78. ld->ld_errno = LDAP_PARAM_ERROR;
  79. return LDAP_PARAM_ERROR;
  80. }
  81. value->bv_val = NULL;
  82. value->bv_len = 0;
  83. ld->ld_errno = LDAP_SUCCESS;
  84. ber = ldap_alloc_ber_with_options( ld );
  85. if ( ber == NULL) {
  86. ld->ld_errno = LDAP_NO_MEMORY;
  87. return ld->ld_errno;
  88. }
  89. tag = ber_printf( ber, "{ibb}", changetypes, changesonly, return_echg_ctls );
  90. if ( tag == LBER_ERROR ) {
  91. goto error_return;
  92. }
  93. if ( ber_flatten2( ber, value, 1 ) == -1 ) {
  94. ld->ld_errno = LDAP_NO_MEMORY;
  95. }
  96. if ( 0 ) {
  97. error_return:;
  98. ld->ld_errno = LDAP_ENCODING_ERROR;
  99. }
  100. if ( ber != NULL ) {
  101. ber_free( ber, 1 );
  102. }
  103. return ld->ld_errno;
  104. }
  105. /* ---------------------------------------------------------------------------
  106. ldap_create_persistentsearch_control
  107. Create and encode the persistent search control.
  108. ld (IN) An LDAP session handle, as obtained from a call to
  109. ldap_init().
  110. changetypes (IN) A bit-sensitive field that indicates which kinds of
  111. changes the client wants to be informed about. Its
  112. value should be LDAP_CHANGETYPE_ANY, or any logical-OR
  113. combination of LDAP_CHANGETYPE_ADD,
  114. LDAP_CHANGETYPE_DELETE, LDAP_CHANGETYPE_MODIFY, and
  115. LDAP_CHANGETYPE_MODDN. This field corresponds to the
  116. changeType element of the BER-encoded PersistentSearch
  117. control value itself.
  118. changesonly (IN) A Boolean field that indicates whether the client
  119. wishes to only receive searchResultEntry messages for
  120. entries that have been changed. If non-zero, only
  121. entries that result from changes are returned; other-
  122. wise, all of the static entries that match the search
  123. criteria are returned before the server begins change
  124. notification. This field corresponds to the changes-
  125. Only element of the BER-encoded PersistentSearch con-
  126. trol value itself.
  127. return_echg_ctls (IN) A Boolean field that indicates whether the server
  128. should send back an Entry Change Notification control
  129. with each searchResultEntry that is returned due to a
  130. change to an entry. If non-zero, Entry Change
  131. Notification controls are requested; if zero, they are
  132. not. This field corresponds to the returnECs element
  133. of the BER-encoded PersistentSearch control value
  134. itself.
  135. isCritical (IN) 0 - Indicates the control is not critical to the operation.
  136. non-zero - The control is critical to the operation.
  137. ctrlp (OUT) Returns a pointer to the LDAPControl created. This control
  138. SHOULD be freed by calling ldap_control_free() when done.
  139. ---------------------------------------------------------------------------*/
  140. int
  141. ldap_create_persistentsearch_control(
  142. LDAP *ld,
  143. int changetypes,
  144. int changesonly,
  145. int return_echg_ctls,
  146. int isCritical,
  147. LDAPControl **ctrlp )
  148. {
  149. struct berval value;
  150. assert( ld != NULL );
  151. assert( LDAP_VALID( ld ) );
  152. if ( ld == NULL ) {
  153. return LDAP_PARAM_ERROR;
  154. }
  155. if ( ctrlp == NULL ) {
  156. ld->ld_errno = LDAP_PARAM_ERROR;
  157. return ld->ld_errno;
  158. }
  159. ld->ld_errno = ldap_create_persistentsearch_control_value( ld, changetypes, changesonly, return_echg_ctls, &value );
  160. if ( ld->ld_errno == LDAP_SUCCESS ) {
  161. ld->ld_errno = ldap_control_create( LDAP_CONTROL_PERSIST_REQUEST,
  162. isCritical, &value, 0, ctrlp );
  163. if ( ld->ld_errno != LDAP_SUCCESS ) {
  164. LDAP_FREE( value.bv_val );
  165. }
  166. }
  167. return ld->ld_errno;
  168. }
  169. /* ---------------------------------------------------------------------------
  170. ldap_parse_entrychange_control
  171. Decode the entry change notification control return information.
  172. ld (IN) An LDAP session handle, as obtained from a call to
  173. ldap_init().
  174. ctrl (IN) The address of the LDAP Control Structure.
  175. chgtypep (OUT) This result parameter is filled in with one of the
  176. following values to indicate the type of change that was
  177. made that caused the entry to be returned:
  178. LDAP_CONTROL_PERSIST_ENTRY_CHANGE_ADD (1),
  179. LDAP_CONTROL_PERSIST_ENTRY_CHANGE_DELETE (2),
  180. LDAP_CONTROL_PERSIST_ENTRY_CHANGE_MODIFY (4), or
  181. LDAP_CONTROL_PERSIST_ENTRY_CHANGE_RENAME (8).
  182. If this parameter is NULL, the change type information
  183. is not returned.
  184. prevdnp (OUT) This result parameter points to the DN the
  185. entry had before it was renamed and/or moved by a
  186. modifyDN operation. It is set to NULL for other types
  187. of changes. If this parameter is NULL, the previous DN
  188. information is not returned. The returned value is a
  189. pointer to the contents of the control; it is not a
  190. copy of the data.
  191. chgnumpresentp (OUT) This result parameter is filled in with a non-zero
  192. value if a change number was returned in the control
  193. (the change number is optional and servers MAY choose
  194. not to return it). If this parameter is NULL, no indication
  195. of whether the change number was present is returned.
  196. chgnump (OUT) This result parameter is filled in with the change number
  197. if one was returned in the control. If this parameter
  198. is NULL, the change number is not returned.
  199. ---------------------------------------------------------------------------*/
  200. int
  201. ldap_parse_entrychange_control(
  202. LDAP *ld,
  203. LDAPControl *ctrl,
  204. int *chgtypep,
  205. struct berval *prevdnp,
  206. int *chgnumpresentp,
  207. long *chgnump )
  208. {
  209. BerElement *ber;
  210. ber_tag_t tag, berTag;
  211. ber_len_t berLen;
  212. ber_int_t chgtype;
  213. assert( ld != NULL );
  214. assert( LDAP_VALID( ld ) );
  215. assert( ctrl != NULL );
  216. if (ld == NULL) {
  217. return LDAP_PARAM_ERROR;
  218. }
  219. if (ctrl == NULL) {
  220. ld->ld_errno = LDAP_PARAM_ERROR;
  221. return(ld->ld_errno);
  222. }
  223. if ( !ctrl->ldctl_value.bv_val ) {
  224. ld->ld_errno = LDAP_DECODING_ERROR;
  225. return(ld->ld_errno);
  226. }
  227. /* Create a BerElement from the berval returned in the control. */
  228. ber = ber_init(&ctrl->ldctl_value);
  229. if (ber == NULL) {
  230. ld->ld_errno = LDAP_NO_MEMORY;
  231. return(ld->ld_errno);
  232. }
  233. if ( prevdnp != NULL ) {
  234. BER_BVZERO( prevdnp );
  235. }
  236. if ( chgnumpresentp != NULL )
  237. *chgnumpresentp = 0;
  238. if ( chgnump != NULL )
  239. *chgnump = 0;
  240. /* Extract the change type from the control. */
  241. tag = ber_scanf(ber, "{e" /*}*/, &chgtype);
  242. if( tag != LBER_ENUMERATED ) {
  243. ber_free(ber, 1);
  244. ld->ld_errno = LDAP_DECODING_ERROR;
  245. return(ld->ld_errno);
  246. }
  247. if ( chgtypep != NULL )
  248. *chgtypep = chgtype;
  249. tag = ber_peek_tag( ber, &berLen );
  250. if ( berLen ) {
  251. if (tag == LBER_OCTETSTRING) {
  252. if (prevdnp != NULL) {
  253. tag = ber_get_stringbv( ber, prevdnp, 0 );
  254. } else {
  255. struct berval bv;
  256. tag = ber_skip_element( ber, &bv );
  257. }
  258. if ( tag == LBER_ERROR ) {
  259. ber_free(ber, 1);
  260. ld->ld_errno = LDAP_DECODING_ERROR;
  261. return(ld->ld_errno);
  262. }
  263. tag = ber_peek_tag( ber, &berLen );
  264. }
  265. if ( chgnumpresentp != NULL || chgnump != NULL ) {
  266. ber_int_t chgnum = 0;
  267. int present = 0;
  268. if (tag == LBER_INTEGER) {
  269. present = 1;
  270. tag = ber_get_int( ber, &chgnum );
  271. if ( tag == LBER_ERROR ) {
  272. ber_free(ber, 1);
  273. ld->ld_errno = LDAP_DECODING_ERROR;
  274. return(ld->ld_errno);
  275. }
  276. if ( chgnumpresentp != NULL )
  277. *chgnumpresentp = present;
  278. if ( chgnump != NULL )
  279. *chgnump = chgnum;
  280. }
  281. }
  282. }
  283. ber_free(ber,1);
  284. ld->ld_errno = LDAP_SUCCESS;
  285. return(ld->ld_errno);
  286. }