cl_crypt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2010 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /* cl_crypt.c - handles changelog encryption. */
  41. #include <errno.h>
  42. #include <sys/stat.h>
  43. #if defined( OS_solaris ) || defined( hpux )
  44. #include <sys/types.h>
  45. #include <sys/statvfs.h>
  46. #endif
  47. #if defined( linux )
  48. #include <sys/vfs.h>
  49. #endif
  50. #include "slapi-plugin.h"
  51. #include "cl5_api.h"
  52. #include "cl_crypt.h"
  53. /*
  54. * BACK_INFO_CRYPT_INIT
  55. */
  56. int
  57. clcrypt_init(const CL5DBConfig *config, void **clcrypt_handle)
  58. {
  59. int rc = 0;
  60. char *cookie = NULL;
  61. Slapi_Backend *be = NULL;
  62. back_info_crypt_init crypt_init = {0};
  63. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name, "-> clcrypt_init\n");
  64. /* Encryption is not specified */
  65. if (!config->encryptionAlgorithm || !clcrypt_handle) {
  66. goto bail;
  67. }
  68. crypt_init.dn = "cn=changelog5,cn=config";
  69. crypt_init.encryptionAlgorithm = config->encryptionAlgorithm;
  70. be = slapi_get_first_backend(&cookie);
  71. while (be) {
  72. crypt_init.be = be;
  73. rc = slapi_back_ctrl_info(be, BACK_INFO_CRYPT_INIT,
  74. (void *)&crypt_init);
  75. if (LDAP_SUCCESS == rc) {
  76. break; /* Successfully fetched */
  77. }
  78. be = slapi_get_next_backend(cookie);
  79. }
  80. slapi_ch_free((void **)&cookie);
  81. if (LDAP_SUCCESS == rc && crypt_init.state_priv) {
  82. *clcrypt_handle = crypt_init.state_priv;
  83. rc = 0;
  84. } else {
  85. rc = 1;
  86. }
  87. bail:
  88. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  89. "<- clcrypt_init : %d\n", rc);
  90. return rc;
  91. }
  92. /*
  93. * return values: 0 - success
  94. * : 1 - no encryption
  95. * : -1 - error
  96. *
  97. * output value: out: non-NULL - encryption successful
  98. * : NULL - no encryption or failure
  99. */
  100. int
  101. clcrypt_encrypt_value(void *clcrypt_handle,
  102. struct berval *in, struct berval **out)
  103. {
  104. int rc = -1;
  105. char *cookie = NULL;
  106. Slapi_Backend *be = NULL;
  107. back_info_crypt_value crypt_value = {0};
  108. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  109. "-> clcrypt_encrypt_value\n");
  110. if (NULL == out) {
  111. goto bail;
  112. }
  113. *out = NULL;
  114. if (NULL == clcrypt_handle) {
  115. rc = 1;
  116. goto bail;
  117. }
  118. crypt_value.state_priv = clcrypt_handle;
  119. crypt_value.in = in;
  120. be = slapi_get_first_backend(&cookie);
  121. while (be) {
  122. rc = slapi_back_ctrl_info(be, BACK_INFO_CRYPT_ENCRYPT_VALUE,
  123. (void *)&crypt_value);
  124. if (LDAP_SUCCESS == rc) {
  125. break; /* Successfully fetched */
  126. }
  127. be = slapi_get_next_backend(cookie);
  128. }
  129. slapi_ch_free((void **)&cookie);
  130. if (LDAP_SUCCESS == rc && crypt_value.out) {
  131. *out = crypt_value.out;
  132. rc = 0;
  133. } else {
  134. rc = -1;
  135. }
  136. bail:
  137. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  138. "<- clcrypt_encrypt_entry (returning %d)\n", rc);
  139. return rc;
  140. }
  141. /*
  142. * return values: 0 - success
  143. * : 1 - no encryption
  144. * : -1 - error
  145. *
  146. * output value: out: non-NULL - encryption successful
  147. * : NULL - no encryption or failure
  148. */
  149. int
  150. clcrypt_decrypt_value(void *clcrypt_handle,
  151. struct berval *in, struct berval **out)
  152. {
  153. int rc = -1;
  154. char *cookie = NULL;
  155. Slapi_Backend *be = NULL;
  156. back_info_crypt_value crypt_value = {0};
  157. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  158. "-> clcrypt_decrypt_value\n");
  159. if (NULL == out) {
  160. goto bail;
  161. }
  162. *out = NULL;
  163. if (NULL == clcrypt_handle) {
  164. rc = 1;
  165. goto bail;
  166. }
  167. crypt_value.state_priv = clcrypt_handle;
  168. crypt_value.in = in;
  169. be = slapi_get_first_backend(&cookie);
  170. while (be) {
  171. rc = slapi_back_ctrl_info(be, BACK_INFO_CRYPT_DECRYPT_VALUE,
  172. (void *)&crypt_value);
  173. if (LDAP_SUCCESS == rc) {
  174. break; /* Successfully fetched */
  175. }
  176. be = slapi_get_next_backend(cookie);
  177. }
  178. slapi_ch_free((void **)&cookie);
  179. if (LDAP_SUCCESS == rc && crypt_value.out) {
  180. *out = crypt_value.out;
  181. rc = 0;
  182. } else {
  183. rc = -1;
  184. }
  185. bail:
  186. slapi_log_error(SLAPI_LOG_TRACE, repl_plugin_name,
  187. "<- clcrypt_decrypt_entry (returning %d)\n", rc);
  188. return rc;
  189. }