stctrl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 2007 Pierangelo Masarati.
  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 Pierangelo Masarati 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. #ifdef LDAP_CONTROL_X_SESSION_TRACKING
  27. /*
  28. * Client-side of <draft-wahl-ldap-session-03>
  29. */
  30. int
  31. ldap_create_session_tracking_value(
  32. LDAP *ld,
  33. char *sessionSourceIp,
  34. char *sessionSourceName,
  35. char *formatOID,
  36. struct berval *sessionTrackingIdentifier,
  37. struct berval *value )
  38. {
  39. BerElement *ber = NULL;
  40. ber_tag_t tag;
  41. struct berval ip, name, oid, id;
  42. if ( ld == NULL ||
  43. formatOID == NULL ||
  44. value == NULL )
  45. {
  46. param_error:;
  47. if ( ld ) {
  48. ld->ld_errno = LDAP_PARAM_ERROR;
  49. }
  50. return LDAP_PARAM_ERROR;
  51. }
  52. assert( LDAP_VALID( ld ) );
  53. ld->ld_errno = LDAP_SUCCESS;
  54. /* check sizes according to I.D. */
  55. if ( sessionSourceIp == NULL ) {
  56. BER_BVSTR( &ip, "" );
  57. } else {
  58. ber_str2bv( sessionSourceIp, 0, 0, &ip );
  59. /* NOTE: we're strict because we don't want
  60. * to send out bad data */
  61. if ( ip.bv_len > 128 ) goto param_error;
  62. }
  63. if ( sessionSourceName == NULL ) {
  64. BER_BVSTR( &name, "" );
  65. } else {
  66. ber_str2bv( sessionSourceName, 0, 0, &name );
  67. /* NOTE: we're strict because we don't want
  68. * to send out bad data */
  69. if ( name.bv_len > 65536 ) goto param_error;
  70. }
  71. ber_str2bv( formatOID, 0, 0, &oid );
  72. /* NOTE: we're strict because we don't want
  73. * to send out bad data */
  74. if ( oid.bv_len > 1024 ) goto param_error;
  75. if ( sessionTrackingIdentifier == NULL ||
  76. sessionTrackingIdentifier->bv_val == NULL )
  77. {
  78. BER_BVSTR( &id, "" );
  79. } else {
  80. id = *sessionTrackingIdentifier;
  81. }
  82. /* prepare value */
  83. value->bv_val = NULL;
  84. value->bv_len = 0;
  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, "{OOOO}", &ip, &name, &oid, &id );
  91. if ( tag == LBER_ERROR ) {
  92. ld->ld_errno = LDAP_ENCODING_ERROR;
  93. goto done;
  94. }
  95. if ( ber_flatten2( ber, value, 1 ) == -1 ) {
  96. ld->ld_errno = LDAP_NO_MEMORY;
  97. }
  98. done:;
  99. if ( ber != NULL ) {
  100. ber_free( ber, 1 );
  101. }
  102. return ld->ld_errno;
  103. }
  104. /*
  105. * NOTE: this API is bad; it could be much more efficient...
  106. */
  107. int
  108. ldap_create_session_tracking_control(
  109. LDAP *ld,
  110. char *sessionSourceIp,
  111. char *sessionSourceName,
  112. char *formatOID,
  113. struct berval *sessionTrackingIdentifier,
  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_session_tracking_value( ld,
  122. sessionSourceIp, sessionSourceName, formatOID,
  123. sessionTrackingIdentifier, &value );
  124. if ( ld->ld_errno == LDAP_SUCCESS ) {
  125. ld->ld_errno = ldap_control_create( LDAP_CONTROL_X_SESSION_TRACKING,
  126. 0, &value, 0, ctrlp );
  127. if ( ld->ld_errno != LDAP_SUCCESS ) {
  128. LDAP_FREE( value.bv_val );
  129. }
  130. }
  131. return ld->ld_errno;
  132. }
  133. int
  134. ldap_parse_session_tracking_control(
  135. LDAP *ld,
  136. LDAPControl *ctrl,
  137. struct berval *ip,
  138. struct berval *name,
  139. struct berval *oid,
  140. struct berval *id )
  141. {
  142. BerElement *ber;
  143. ber_tag_t tag;
  144. ber_len_t len;
  145. if ( ld == NULL ||
  146. ctrl == NULL ||
  147. ip == NULL ||
  148. name == NULL ||
  149. oid == NULL ||
  150. id == NULL )
  151. {
  152. if ( ld ) {
  153. ld->ld_errno = LDAP_PARAM_ERROR;
  154. }
  155. /* NOTE: we want the caller to get all or nothing;
  156. * we could allow some of the pointers to be NULL,
  157. * if one does not want part of the data */
  158. return LDAP_PARAM_ERROR;
  159. }
  160. BER_BVZERO( ip );
  161. BER_BVZERO( name );
  162. BER_BVZERO( oid );
  163. BER_BVZERO( id );
  164. ber = ber_init( &ctrl->ldctl_value );
  165. if ( ber == NULL ) {
  166. ld->ld_errno = LDAP_NO_MEMORY;
  167. return ld->ld_errno;
  168. }
  169. tag = ber_skip_tag( ber, &len );
  170. if ( tag != LBER_SEQUENCE ) {
  171. tag = LBER_ERROR;
  172. goto error;
  173. }
  174. /* sessionSourceIp */
  175. tag = ber_peek_tag( ber, &len );
  176. if ( tag == LBER_DEFAULT ) {
  177. tag = LBER_ERROR;
  178. goto error;
  179. }
  180. if ( len == 0 ) {
  181. tag = ber_skip_tag( ber, &len );
  182. } else {
  183. if ( len > 128 ) {
  184. /* should be LDAP_DECODING_ERROR,
  185. * but we're liberal in what we accept */
  186. }
  187. tag = ber_scanf( ber, "o", ip );
  188. }
  189. /* sessionSourceName */
  190. tag = ber_peek_tag( ber, &len );
  191. if ( tag == LBER_DEFAULT ) {
  192. tag = LBER_ERROR;
  193. goto error;
  194. }
  195. if ( len == 0 ) {
  196. tag = ber_skip_tag( ber, &len );
  197. } else {
  198. if ( len > 65536 ) {
  199. /* should be LDAP_DECODING_ERROR,
  200. * but we're liberal in what we accept */
  201. }
  202. tag = ber_scanf( ber, "o", name );
  203. }
  204. /* formatOID */
  205. tag = ber_peek_tag( ber, &len );
  206. if ( tag == LBER_DEFAULT ) {
  207. tag = LBER_ERROR;
  208. goto error;
  209. }
  210. if ( len == 0 ) {
  211. ld->ld_errno = LDAP_DECODING_ERROR;
  212. goto error;
  213. } else {
  214. if ( len > 1024 ) {
  215. /* should be LDAP_DECODING_ERROR,
  216. * but we're liberal in what we accept */
  217. }
  218. tag = ber_scanf( ber, "o", oid );
  219. }
  220. /* FIXME: should check if it is an OID... leave it to the caller */
  221. /* sessionTrackingIdentifier */
  222. tag = ber_peek_tag( ber, &len );
  223. if ( tag == LBER_DEFAULT ) {
  224. tag = LBER_ERROR;
  225. goto error;
  226. }
  227. if ( len == 0 ) {
  228. tag = ber_skip_tag( ber, &len );
  229. } else {
  230. #if 0
  231. if ( len > 65536 ) {
  232. /* should be LDAP_DECODING_ERROR,
  233. * but we're liberal in what we accept */
  234. }
  235. #endif
  236. tag = ber_scanf( ber, "o", id );
  237. }
  238. /* closure */
  239. tag = ber_skip_tag( ber, &len );
  240. if ( tag == LBER_DEFAULT && len == 0 ) {
  241. tag = 0;
  242. }
  243. error:;
  244. (void)ber_free( ber, 1 );
  245. if ( tag == LBER_ERROR ) {
  246. return LDAP_DECODING_ERROR;
  247. }
  248. return ld->ld_errno;
  249. }
  250. #endif /* LDAP_CONTROL_X_SESSION_TRACKING */