txn.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 2006-2024 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 program was originally developed by Kurt D. Zeilenga for inclusion
  17. * in OpenLDAP Software.
  18. */
  19. /*
  20. * LDAPv3 Transactions (draft-zeilenga-ldap-txn)
  21. */
  22. #include "portable.h"
  23. #include <stdio.h>
  24. #include <ac/stdlib.h>
  25. #include <ac/socket.h>
  26. #include <ac/string.h>
  27. #include <ac/time.h>
  28. #include "ldap-int.h"
  29. #include "ldap_log.h"
  30. int
  31. ldap_txn_start(
  32. LDAP *ld,
  33. LDAPControl **sctrls,
  34. LDAPControl **cctrls,
  35. int *msgidp )
  36. {
  37. return ldap_extended_operation( ld, LDAP_EXOP_TXN_START,
  38. NULL, sctrls, cctrls, msgidp );
  39. }
  40. int
  41. ldap_txn_start_s(
  42. LDAP *ld,
  43. LDAPControl **sctrls,
  44. LDAPControl **cctrls,
  45. struct berval **txnid )
  46. {
  47. assert( txnid != NULL );
  48. return ldap_extended_operation_s( ld, LDAP_EXOP_TXN_START,
  49. NULL, sctrls, cctrls, NULL, txnid );
  50. }
  51. int
  52. ldap_txn_end(
  53. LDAP *ld,
  54. int commit,
  55. struct berval *txnid,
  56. LDAPControl **sctrls,
  57. LDAPControl **cctrls,
  58. int *msgidp )
  59. {
  60. int rc;
  61. BerElement *txnber = NULL;
  62. struct berval txnval;
  63. assert( txnid != NULL );
  64. txnber = ber_alloc_t( LBER_USE_DER );
  65. if( commit ) {
  66. ber_printf( txnber, "{ON}", txnid );
  67. } else {
  68. ber_printf( txnber, "{bON}", commit, txnid );
  69. }
  70. ber_flatten2( txnber, &txnval, 0 );
  71. rc = ldap_extended_operation( ld, LDAP_EXOP_TXN_END,
  72. &txnval, sctrls, cctrls, msgidp );
  73. ber_free( txnber, 1 );
  74. return rc;
  75. }
  76. int
  77. ldap_txn_end_s(
  78. LDAP *ld,
  79. int commit,
  80. struct berval *txnid,
  81. LDAPControl **sctrls,
  82. LDAPControl **cctrls,
  83. int *retidp )
  84. {
  85. int rc;
  86. BerElement *txnber = NULL;
  87. struct berval txnval;
  88. struct berval *retdata = NULL;
  89. if ( retidp != NULL ) *retidp = -1;
  90. txnber = ber_alloc_t( LBER_USE_DER );
  91. if( commit ) {
  92. ber_printf( txnber, "{ON}", txnid );
  93. } else {
  94. ber_printf( txnber, "{bON}", commit, txnid );
  95. }
  96. ber_flatten2( txnber, &txnval, 0 );
  97. rc = ldap_extended_operation_s( ld, LDAP_EXOP_TXN_END,
  98. &txnval, sctrls, cctrls, NULL, &retdata );
  99. ber_free( txnber, 1 );
  100. /* parse retdata */
  101. if( retdata != NULL ) {
  102. BerElement *ber;
  103. ber_tag_t tag;
  104. ber_int_t retid;
  105. if( retidp == NULL ) goto done;
  106. ber = ber_init( retdata );
  107. if( ber == NULL ) {
  108. rc = ld->ld_errno = LDAP_NO_MEMORY;
  109. goto done;
  110. }
  111. tag = ber_scanf( ber, "i", &retid );
  112. ber_free( ber, 1 );
  113. if ( tag != LBER_INTEGER ) {
  114. rc = ld->ld_errno = LDAP_DECODING_ERROR;
  115. goto done;
  116. }
  117. *retidp = (int) retid;
  118. done:
  119. ber_bvfree( retdata );
  120. }
  121. return rc;
  122. }