ldbm_attrcrypt_config.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* This file handles configuration information that is specific
  42. * to ldbm instance attribute encryption configuration.
  43. */
  44. /* DBDB I left in the Sun copyright statement because some of the code
  45. * in this file is derived from an older file : ldbm_index_config.c
  46. */
  47. #include "back-ldbm.h"
  48. #include "attrcrypt.h"
  49. /* Forward declarations for the callbacks */
  50. int ldbm_instance_attrcrypt_config_add_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg);
  51. int ldbm_instance_attrcrypt_config_delete_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg);
  52. /*
  53. Config entries look like this:
  54. dn: cn=<attributeName>, cn=encrypted attributes, cn=databaseName, cn=ldbm database, cn=plugins, cn=config
  55. objectclass: top
  56. objectclass: nsAttributeEncryption
  57. cn: <attributeName>
  58. nsEncryptionAlgorithm: <cipherName>
  59. */
  60. static int
  61. ldbm_attrcrypt_parse_cipher(char* cipher_display_name)
  62. {
  63. attrcrypt_cipher_entry *ce = attrcrypt_cipher_list;
  64. while (ce->cipher_number) {
  65. if (0 == strcmp(ce->cipher_display_name,cipher_display_name)) {
  66. return ce->cipher_number;
  67. }
  68. ce++;
  69. }
  70. return 0;
  71. }
  72. static int
  73. ldbm_attrcrypt_parse_entry(ldbm_instance *inst, Slapi_Entry *e,
  74. char **attribute_name,
  75. int *cipher)
  76. {
  77. Slapi_Attr *attr;
  78. const struct berval *attrValue;
  79. Slapi_Value *sval;
  80. *cipher = 0;
  81. *attribute_name = NULL;
  82. /* Get the name of the attribute to index which will be the value
  83. * of the cn attribute. */
  84. if (slapi_entry_attr_find(e, "cn", &attr) != 0) {
  85. LDAPDebug(LDAP_DEBUG_ANY, "Warning: malformed attribute encryption entry %s\n",
  86. slapi_entry_get_dn(e), 0, 0);
  87. return LDAP_OPERATIONS_ERROR;
  88. }
  89. slapi_attr_first_value(attr, &sval);
  90. attrValue = slapi_value_get_berval(sval);
  91. *attribute_name = slapi_ch_strdup(attrValue->bv_val);
  92. /* Get the list of index types from the entry. */
  93. if (0 == slapi_entry_attr_find(e, "nsEncryptionAlgorithm", &attr)) {
  94. slapi_attr_first_value(attr, &sval);
  95. if (sval) {
  96. attrValue = slapi_value_get_berval(sval);
  97. *cipher = ldbm_attrcrypt_parse_cipher(attrValue->bv_val);
  98. if (0 == *cipher)
  99. LDAPDebug(LDAP_DEBUG_ANY, "Warning: attempt to configure unrecognized cipher %s in encrypted attribute config entry %s\n",
  100. attrValue->bv_val, slapi_entry_get_dn(e), 0);
  101. }
  102. }
  103. return LDAP_SUCCESS;
  104. }
  105. static void
  106. ldbm_instance_attrcrypt_enable(struct attrinfo *ai, int cipher)
  107. {
  108. attrcrypt_private *priv = NULL;
  109. if (NULL == ai->ai_attrcrypt) {
  110. /* No existing private structure, allocate one */
  111. ai->ai_attrcrypt = (attrcrypt_private*) slapi_ch_calloc(1, sizeof(attrcrypt_private));
  112. }
  113. priv = ai->ai_attrcrypt;
  114. priv->attrcrypt_cipher = cipher;
  115. }
  116. static void
  117. ldbm_instance_attrcrypt_disable(struct attrinfo *ai)
  118. {
  119. if (NULL != ai->ai_attrcrypt) {
  120. /* Don't free the structure here, because other threads might be
  121. * concurrently referencing it.
  122. */
  123. ai->ai_attrcrypt = 0;
  124. }
  125. }
  126. /*
  127. * Config DSE callback for attribute encryption entry add.
  128. */
  129. int
  130. ldbm_instance_attrcrypt_config_add_callback(Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* eAfter, int *returncode, char *returntext, void *arg)
  131. {
  132. ldbm_instance *inst = (ldbm_instance *) arg;
  133. char *attribute_name = NULL;
  134. int cipher = 0;
  135. int ret = 0;
  136. returntext[0] = '\0';
  137. /* For add, we parse the entry, then check the attribute exists,
  138. * then check that indexing config does not preclude us encrypting it,
  139. * and finally we set the private structure in the attrinfo for the attribute.
  140. */
  141. *returncode = ldbm_attrcrypt_parse_entry(inst, e, &attribute_name , &cipher);
  142. if (*returncode == LDAP_SUCCESS) {
  143. struct attrinfo *ai = NULL;
  144. /* If the cipher was invalid, return unwilling to perform */
  145. if (0 == cipher) {
  146. returntext = "invalid cipher";
  147. *returncode = LDAP_UNWILLING_TO_PERFORM;
  148. ret = SLAPI_DSE_CALLBACK_ERROR;
  149. } else {
  150. ainfo_get(inst->inst_be, attribute_name, &ai);
  151. /* If we couldn't find a non-default attrinfo, then that means
  152. * that no indexing or encryption has yet been defined for this attribute
  153. * therefore , create a new attrinfo structure now.
  154. */
  155. if ((ai == NULL) || (0 == strcmp(LDBM_PSEUDO_ATTR_DEFAULT, ai->ai_type) )) {
  156. /* If this attribute doesn't exist in the schema, then we DO NOT fail
  157. * (this is because entensible objects and disabled schema checking allow
  158. * non-schema attributes to exist.
  159. */
  160. /* Make a new attrinfo object */
  161. attr_create_empty(inst->inst_be,attribute_name,&ai);
  162. }
  163. if (ai) {
  164. ldbm_instance_attrcrypt_enable(ai, cipher);
  165. /* Remember that we have some encryption enabled, so we can be intelligent about warning when SSL is not enabled */
  166. inst->attrcrypt_configured = 1;
  167. } else {
  168. LDAPDebug(LDAP_DEBUG_ANY, "Warning: attempt to encryption on a non-existent attribute: %s\n",
  169. attribute_name, 0, 0);
  170. returntext = "attribute does not exist";
  171. *returncode = LDAP_UNWILLING_TO_PERFORM;
  172. ret = SLAPI_DSE_CALLBACK_ERROR;
  173. }
  174. ret = SLAPI_DSE_CALLBACK_OK;
  175. }
  176. } else {
  177. ret = SLAPI_DSE_CALLBACK_ERROR;
  178. }
  179. if (attribute_name) {
  180. slapi_ch_free_string(&attribute_name);
  181. }
  182. return ret;
  183. }
  184. /*
  185. * Temp callback that gets called for each attribute encryption entry when a new
  186. * instance is starting up.
  187. */
  188. int
  189. ldbm_attrcrypt_init_entry_callback(Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg)
  190. {
  191. return ldbm_instance_attrcrypt_config_add_callback(pb,e,entryAfter,returncode,returntext,arg);
  192. }
  193. /*
  194. * Config DSE callback for attribute encryption deletes.
  195. */
  196. int
  197. ldbm_instance_attrcrypt_config_delete_callback(Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg)
  198. {
  199. ldbm_instance *inst = (ldbm_instance *) arg;
  200. char *attribute_name = NULL;
  201. int cipher = 0;
  202. int ret = SLAPI_DSE_CALLBACK_ERROR;
  203. returntext[0] = '\0';
  204. /* For add, we parse the entry, then check the attribute exists,
  205. * then check that indexing config does not preclude us encrypting it,
  206. * and finally we set the private structure in the attrinfo for the attribute.
  207. */
  208. *returncode = ldbm_attrcrypt_parse_entry(inst, e, &attribute_name , &cipher);
  209. if (*returncode == LDAP_SUCCESS) {
  210. struct attrinfo *ai = NULL;
  211. ainfo_get(inst->inst_be, attribute_name, &ai);
  212. if (ai == NULL && (0 == strcmp(LDBM_PSEUDO_ATTR_DEFAULT, ai->ai_type)) ) {
  213. LDAPDebug(LDAP_DEBUG_ANY, "Warning: attempt to delete encryption for non-existant attribute: %s\n",
  214. attribute_name, 0, 0);
  215. } else {
  216. ldbm_instance_attrcrypt_disable(ai);
  217. ret = SLAPI_DSE_CALLBACK_OK;
  218. }
  219. }
  220. if (attribute_name) {
  221. slapi_ch_free((void **)&attribute_name);
  222. }
  223. return ret;
  224. }
  225. /*
  226. * Config DSE callback for index entry changes.
  227. *
  228. * this function is huge!
  229. */
  230. int
  231. ldbm_instance_attrcrypt_config_modify_callback(Slapi_PBlock *pb, Slapi_Entry *e,
  232. Slapi_Entry *entryAfter, int *returncode, char *returntext, void *arg)
  233. {
  234. ldbm_instance *inst = (ldbm_instance *)arg;
  235. Slapi_Attr *attr;
  236. Slapi_Value *sval;
  237. const struct berval *attrValue;
  238. struct attrinfo *ainfo = NULL;
  239. LDAPMod **mods;
  240. int i = 0;
  241. int j = 0;
  242. returntext[0] = '\0';
  243. *returncode = LDAP_SUCCESS;
  244. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  245. slapi_entry_attr_find(e, "cn", &attr);
  246. slapi_attr_first_value(attr, &sval);
  247. attrValue = slapi_value_get_berval(sval);
  248. ainfo_get(inst->inst_be, attrValue->bv_val, &ainfo);
  249. if (NULL == ainfo) {
  250. return SLAPI_DSE_CALLBACK_ERROR;
  251. }
  252. for (i = 0; mods[i] != NULL; i++) {
  253. char *config_attr = (char *)mods[i]->mod_type;
  254. /* There are basically three cases in the modify:
  255. * 1. The attribute was added
  256. * 2. The attribute was deleted
  257. * 3. The attribute was modified (deleted and added).
  258. * Now, of these three, only #3 is legal.
  259. * This is because the attribute is mandatory and single-valued in the schema.
  260. * We handle this as follows: an add will always replace what's there (if anything).
  261. * a delete will remove what's there as long as it matches what's being deleted.
  262. * this is to avoid ordering problems with the adds and deletes.
  263. */
  264. if (strcasecmp(config_attr, "nsEncryptionAlgorithm") == 0) {
  265. if (SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
  266. for (j = 0; mods[i]->mod_bvalues[j] != NULL; j++) {
  267. int cipher = ldbm_attrcrypt_parse_cipher(mods[i]->mod_bvalues[j]->bv_val);
  268. if (0 == cipher) {
  269. /* Tried to configure an invalid cipher */
  270. }
  271. ldbm_instance_attrcrypt_enable(ainfo,cipher);
  272. }
  273. continue;
  274. }
  275. if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
  276. if ((mods[i]->mod_bvalues == NULL) ||
  277. (mods[i]->mod_bvalues[0] == NULL)) {
  278. /* Not legal */
  279. return SLAPI_DSE_CALLBACK_ERROR;
  280. } else {
  281. for (j = 0; mods[i]->mod_bvalues[j] != NULL; j++) {
  282. /* Code before here should ensure that we only ever delete something that was already here */
  283. ldbm_instance_attrcrypt_disable(ainfo);
  284. }
  285. }
  286. continue;
  287. }
  288. }
  289. }
  290. return SLAPI_DSE_CALLBACK_OK;
  291. }