dds.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 2005-2024 The OpenLDAP Foundation.
  5. * Portions Copyright 2005-2006 SysNet s.n.c.
  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
  18. * in OpenLDAP Software */
  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. int
  26. ldap_parse_refresh( LDAP *ld, LDAPMessage *res, ber_int_t *newttl )
  27. {
  28. int rc;
  29. struct berval *retdata = NULL;
  30. ber_tag_t tag;
  31. BerElement *ber;
  32. assert( ld != NULL );
  33. assert( LDAP_VALID( ld ) );
  34. assert( res != NULL );
  35. assert( newttl != NULL );
  36. *newttl = 0;
  37. rc = ldap_parse_extended_result( ld, res, NULL, &retdata, 0 );
  38. if ( rc != LDAP_SUCCESS ) {
  39. return rc;
  40. }
  41. if ( ld->ld_errno != LDAP_SUCCESS ) {
  42. return ld->ld_errno;
  43. }
  44. if ( retdata == NULL ) {
  45. rc = ld->ld_errno = LDAP_DECODING_ERROR;
  46. return rc;
  47. }
  48. ber = ber_init( retdata );
  49. if ( ber == NULL ) {
  50. rc = ld->ld_errno = LDAP_NO_MEMORY;
  51. goto done;
  52. }
  53. /* check the tag */
  54. tag = ber_scanf( ber, "{i}", newttl );
  55. ber_free( ber, 1 );
  56. if ( tag != LDAP_TAG_EXOP_REFRESH_RES_TTL ) {
  57. *newttl = 0;
  58. rc = ld->ld_errno = LDAP_DECODING_ERROR;
  59. }
  60. done:;
  61. if ( retdata ) {
  62. ber_bvfree( retdata );
  63. }
  64. return rc;
  65. }
  66. int
  67. ldap_refresh(
  68. LDAP *ld,
  69. struct berval *dn,
  70. ber_int_t ttl,
  71. LDAPControl **sctrls,
  72. LDAPControl **cctrls,
  73. int *msgidp )
  74. {
  75. struct berval bv = { 0, NULL };
  76. BerElement *ber = NULL;
  77. int rc;
  78. assert( ld != NULL );
  79. assert( LDAP_VALID( ld ) );
  80. assert( dn != NULL );
  81. assert( msgidp != NULL );
  82. *msgidp = -1;
  83. ber = ber_alloc_t( LBER_USE_DER );
  84. if ( ber == NULL ) {
  85. ld->ld_errno = LDAP_NO_MEMORY;
  86. return ld->ld_errno;
  87. }
  88. ber_printf( ber, "{tOtiN}",
  89. LDAP_TAG_EXOP_REFRESH_REQ_DN, dn,
  90. LDAP_TAG_EXOP_REFRESH_REQ_TTL, ttl );
  91. rc = ber_flatten2( ber, &bv, 0 );
  92. if ( rc < 0 ) {
  93. rc = ld->ld_errno = LDAP_ENCODING_ERROR;
  94. goto done;
  95. }
  96. rc = ldap_extended_operation( ld, LDAP_EXOP_REFRESH, &bv,
  97. sctrls, cctrls, msgidp );
  98. done:;
  99. ber_free( ber, 1 );
  100. return rc;
  101. }
  102. int
  103. ldap_refresh_s(
  104. LDAP *ld,
  105. struct berval *dn,
  106. ber_int_t ttl,
  107. ber_int_t *newttl,
  108. LDAPControl **sctrls,
  109. LDAPControl **cctrls )
  110. {
  111. int rc;
  112. int msgid;
  113. LDAPMessage *res;
  114. rc = ldap_refresh( ld, dn, ttl, sctrls, cctrls, &msgid );
  115. if ( rc != LDAP_SUCCESS ) return rc;
  116. rc = ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *)NULL, &res );
  117. if( rc == -1 || !res ) return ld->ld_errno;
  118. rc = ldap_parse_refresh( ld, res, newttl );
  119. if( rc != LDAP_SUCCESS ) {
  120. ldap_msgfree( res );
  121. return rc;
  122. }
  123. return ldap_result2error( ld, res, 1 );
  124. }