cl_crypt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2010 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. /* cl_crypt.c - handles changelog encryption. */
  12. #include <errno.h>
  13. #include <sys/stat.h>
  14. #if defined( OS_solaris ) || defined( hpux )
  15. #include <sys/types.h>
  16. #include <sys/statvfs.h>
  17. #endif
  18. #if defined( linux )
  19. #include <sys/vfs.h>
  20. #endif
  21. #include "slapi-plugin.h"
  22. #include "cl5_api.h"
  23. #include "cl_crypt.h"
  24. /*
  25. * BACK_INFO_CRYPT_INIT
  26. */
  27. int
  28. clcrypt_init(const CL5DBConfig *config, void **clcrypt_handle)
  29. {
  30. int rc = 0;
  31. char *cookie = NULL;
  32. Slapi_Backend *be = NULL;
  33. back_info_crypt_init crypt_init = {0};
  34. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name, "-> clcrypt_init\n");
  35. /* Encryption is not specified */
  36. if (!config->encryptionAlgorithm || !clcrypt_handle) {
  37. goto bail;
  38. }
  39. crypt_init.dn = "cn=changelog5,cn=config";
  40. crypt_init.encryptionAlgorithm = config->encryptionAlgorithm;
  41. be = slapi_get_first_backend(&cookie);
  42. while (be) {
  43. crypt_init.be = be;
  44. rc = slapi_back_ctrl_info(be, BACK_INFO_CRYPT_INIT,
  45. (void *)&crypt_init);
  46. if (LDAP_SUCCESS == rc) {
  47. break; /* Successfully fetched */
  48. }
  49. be = slapi_get_next_backend(cookie);
  50. }
  51. slapi_ch_free((void **)&cookie);
  52. if (LDAP_SUCCESS == rc && crypt_init.state_priv) {
  53. *clcrypt_handle = crypt_init.state_priv;
  54. rc = 0;
  55. } else {
  56. rc = 1;
  57. }
  58. bail:
  59. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  60. "<- clcrypt_init : %d\n", rc);
  61. return rc;
  62. }
  63. /*
  64. * return values: 0 - success
  65. * : 1 - no encryption
  66. * : -1 - error
  67. *
  68. * output value: out: non-NULL - encryption successful
  69. * : NULL - no encryption or failure
  70. */
  71. int
  72. clcrypt_encrypt_value(void *clcrypt_handle,
  73. struct berval *in, struct berval **out)
  74. {
  75. int rc = -1;
  76. char *cookie = NULL;
  77. Slapi_Backend *be = NULL;
  78. back_info_crypt_value crypt_value = {0};
  79. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  80. "-> clcrypt_encrypt_value\n");
  81. if (NULL == out) {
  82. goto bail;
  83. }
  84. *out = NULL;
  85. if (NULL == clcrypt_handle) {
  86. rc = 1;
  87. goto bail;
  88. }
  89. crypt_value.state_priv = clcrypt_handle;
  90. crypt_value.in = in;
  91. be = slapi_get_first_backend(&cookie);
  92. while (be) {
  93. rc = slapi_back_ctrl_info(be, BACK_INFO_CRYPT_ENCRYPT_VALUE,
  94. (void *)&crypt_value);
  95. if (LDAP_SUCCESS == rc) {
  96. break; /* Successfully fetched */
  97. }
  98. be = slapi_get_next_backend(cookie);
  99. }
  100. slapi_ch_free((void **)&cookie);
  101. if (LDAP_SUCCESS == rc && crypt_value.out) {
  102. *out = crypt_value.out;
  103. rc = 0;
  104. } else {
  105. rc = -1;
  106. }
  107. bail:
  108. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  109. "<- clcrypt_encrypt_entry (returning %d)\n", rc);
  110. return rc;
  111. }
  112. /*
  113. * return values: 0 - success
  114. * : 1 - no encryption
  115. * : -1 - error
  116. *
  117. * output value: out: non-NULL - encryption successful
  118. * : NULL - no encryption or failure
  119. */
  120. int
  121. clcrypt_decrypt_value(void *clcrypt_handle,
  122. struct berval *in, struct berval **out)
  123. {
  124. int rc = -1;
  125. char *cookie = NULL;
  126. Slapi_Backend *be = NULL;
  127. back_info_crypt_value crypt_value = {0};
  128. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  129. "-> clcrypt_decrypt_value\n");
  130. if (NULL == out) {
  131. goto bail;
  132. }
  133. *out = NULL;
  134. if (NULL == clcrypt_handle) {
  135. rc = 1;
  136. goto bail;
  137. }
  138. crypt_value.state_priv = clcrypt_handle;
  139. crypt_value.in = in;
  140. be = slapi_get_first_backend(&cookie);
  141. while (be) {
  142. rc = slapi_back_ctrl_info(be, BACK_INFO_CRYPT_DECRYPT_VALUE,
  143. (void *)&crypt_value);
  144. if (LDAP_SUCCESS == rc) {
  145. break; /* Successfully fetched */
  146. }
  147. be = slapi_get_next_backend(cookie);
  148. }
  149. slapi_ch_free((void **)&cookie);
  150. if (LDAP_SUCCESS == rc && crypt_value.out) {
  151. *out = crypt_value.out;
  152. rc = 0;
  153. } else {
  154. rc = -1;
  155. }
  156. bail:
  157. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  158. "<- clcrypt_decrypt_entry (returning %d)\n", rc);
  159. return rc;
  160. }