msctrl.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * Portions Copyright 2018 Howard Chu.
  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. /* ACKNOWLEDGEMENTS:
  17. * This work was developed by Howard Chu for inclusion in
  18. * OpenLDAP Software.
  19. */
  20. #include "portable.h"
  21. #include <stdio.h>
  22. #include <ac/stdlib.h>
  23. #include <ac/string.h>
  24. #include <ac/time.h>
  25. #include "ldap-int.h"
  26. /* MS Active Directory controls - not implemented in slapd(8) */
  27. #ifdef LDAP_CONTROL_X_DIRSYNC
  28. int
  29. ldap_create_dirsync_value(
  30. LDAP *ld,
  31. int flags,
  32. int maxAttrCount,
  33. struct berval *cookie,
  34. struct berval *value )
  35. {
  36. BerElement *ber = NULL;
  37. ber_tag_t tag;
  38. if ( ld == NULL || cookie == NULL ||
  39. value == NULL )
  40. {
  41. if ( ld ) {
  42. ld->ld_errno = LDAP_PARAM_ERROR;
  43. }
  44. return LDAP_PARAM_ERROR;
  45. }
  46. assert( LDAP_VALID( ld ) );
  47. ld->ld_errno = LDAP_SUCCESS;
  48. /* maxAttrCount less than 0x100000 is treated as 0x100000 by server */
  49. /* prepare value */
  50. value->bv_val = NULL;
  51. value->bv_len = 0;
  52. ber = ldap_alloc_ber_with_options( ld );
  53. if ( ber == NULL ) {
  54. ld->ld_errno = LDAP_NO_MEMORY;
  55. return ld->ld_errno;
  56. }
  57. tag = ber_printf( ber, "{iiO}", flags, maxAttrCount, cookie );
  58. if ( tag == LBER_ERROR ) {
  59. ld->ld_errno = LDAP_ENCODING_ERROR;
  60. goto done;
  61. }
  62. if ( ber_flatten2( ber, value, 1 ) == -1 ) {
  63. ld->ld_errno = LDAP_NO_MEMORY;
  64. }
  65. done:;
  66. if ( ber != NULL ) {
  67. ber_free( ber, 1 );
  68. }
  69. return ld->ld_errno;
  70. }
  71. int
  72. ldap_create_dirsync_control(
  73. LDAP *ld,
  74. int flags,
  75. int maxAttrCount,
  76. struct berval *cookie,
  77. LDAPControl **ctrlp )
  78. {
  79. struct berval value;
  80. if ( ctrlp == NULL ) {
  81. ld->ld_errno = LDAP_PARAM_ERROR;
  82. return ld->ld_errno;
  83. }
  84. ld->ld_errno = ldap_create_dirsync_value( ld,
  85. flags, maxAttrCount, cookie, &value );
  86. if ( ld->ld_errno == LDAP_SUCCESS ) {
  87. ld->ld_errno = ldap_control_create( LDAP_CONTROL_X_DIRSYNC,
  88. 1, &value, 0, ctrlp );
  89. if ( ld->ld_errno != LDAP_SUCCESS ) {
  90. LDAP_FREE( value.bv_val );
  91. }
  92. }
  93. return ld->ld_errno;
  94. }
  95. int
  96. ldap_parse_dirsync_control(
  97. LDAP *ld,
  98. LDAPControl *ctrl,
  99. int *continueFlag,
  100. struct berval *cookie )
  101. {
  102. BerElement *ber;
  103. ber_tag_t tag;
  104. ber_len_t len;
  105. int unused;
  106. if ( ld == NULL ||
  107. ctrl == NULL ||
  108. continueFlag == NULL ||
  109. cookie == NULL )
  110. {
  111. if ( ld ) {
  112. ld->ld_errno = LDAP_PARAM_ERROR;
  113. }
  114. /* NOTE: we want the caller to get all or nothing;
  115. * we could allow some of the pointers to be NULL,
  116. * if one does not want part of the data */
  117. return LDAP_PARAM_ERROR;
  118. }
  119. *continueFlag = 0;
  120. BER_BVZERO( cookie );
  121. ber = ber_init( &ctrl->ldctl_value );
  122. if ( ber == NULL ) {
  123. ld->ld_errno = LDAP_NO_MEMORY;
  124. return ld->ld_errno;
  125. }
  126. tag = ber_scanf( ber, "{iio}", continueFlag, &unused, cookie );
  127. if ( tag == LBER_DEFAULT )
  128. tag = LBER_ERROR;
  129. (void)ber_free( ber, 1 );
  130. if ( tag == LBER_ERROR ) {
  131. return LDAP_DECODING_ERROR;
  132. }
  133. return ld->ld_errno;
  134. }
  135. #endif /* LDAP_CONTROL_X_DIRSYNC */
  136. #ifdef LDAP_CONTROL_X_SHOW_DELETED
  137. int
  138. ldap_create_show_deleted_control( LDAP *ld,
  139. LDAPControl **ctrlp )
  140. {
  141. assert( ld != NULL );
  142. assert( LDAP_VALID( ld ) );
  143. assert( ctrlp != NULL );
  144. ld->ld_errno = ldap_control_create( LDAP_CONTROL_X_SHOW_DELETED,
  145. 0, NULL, 0, ctrlp );
  146. return ld->ld_errno;
  147. }
  148. #endif /* LDAP_CONTROL_X_SHOW_DELETED */
  149. #ifdef LDAP_CONTROL_X_EXTENDED_DN
  150. int
  151. ldap_create_extended_dn_value(
  152. LDAP *ld,
  153. int flag,
  154. struct berval *value )
  155. {
  156. BerElement *ber = NULL;
  157. ber_tag_t tag;
  158. if ( ld == NULL ||
  159. value == NULL )
  160. {
  161. if ( ld ) {
  162. ld->ld_errno = LDAP_PARAM_ERROR;
  163. }
  164. return LDAP_PARAM_ERROR;
  165. }
  166. assert( LDAP_VALID( ld ) );
  167. ld->ld_errno = LDAP_SUCCESS;
  168. /* prepare value */
  169. value->bv_val = NULL;
  170. value->bv_len = 0;
  171. ber = ldap_alloc_ber_with_options( ld );
  172. if ( ber == NULL ) {
  173. ld->ld_errno = LDAP_NO_MEMORY;
  174. return ld->ld_errno;
  175. }
  176. tag = ber_printf( ber, "{i}", flag );
  177. if ( tag == LBER_ERROR ) {
  178. ld->ld_errno = LDAP_ENCODING_ERROR;
  179. goto done;
  180. }
  181. if ( ber_flatten2( ber, value, 1 ) == -1 ) {
  182. ld->ld_errno = LDAP_NO_MEMORY;
  183. }
  184. done:;
  185. if ( ber != NULL ) {
  186. ber_free( ber, 1 );
  187. }
  188. return ld->ld_errno;
  189. }
  190. int
  191. ldap_create_extended_dn_control(
  192. LDAP *ld,
  193. int flag,
  194. LDAPControl **ctrlp )
  195. {
  196. struct berval value;
  197. if ( ctrlp == NULL ) {
  198. ld->ld_errno = LDAP_PARAM_ERROR;
  199. return ld->ld_errno;
  200. }
  201. ld->ld_errno = ldap_create_extended_dn_value( ld, flag, &value );
  202. if ( ld->ld_errno == LDAP_SUCCESS ) {
  203. ld->ld_errno = ldap_control_create( LDAP_CONTROL_X_EXTENDED_DN,
  204. 0, &value, 0, ctrlp );
  205. if ( ld->ld_errno != LDAP_SUCCESS ) {
  206. LDAP_FREE( value.bv_val );
  207. }
  208. }
  209. return ld->ld_errno;
  210. }
  211. #endif /* LDAP_CONTROL_X_EXTENDED_DN */
  212. #ifdef LDAP_CONTROL_X_SERVER_NOTIFICATION
  213. int
  214. ldap_create_server_notification_control( LDAP *ld,
  215. LDAPControl **ctrlp )
  216. {
  217. assert( ld != NULL );
  218. assert( LDAP_VALID( ld ) );
  219. assert( ctrlp != NULL );
  220. ld->ld_errno = ldap_control_create( LDAP_CONTROL_X_SERVER_NOTIFICATION,
  221. 0, NULL, 0, ctrlp );
  222. return ld->ld_errno;
  223. }
  224. #endif /* LDAP_CONTROL_X_SERVER_NOTIFICATION */