mail-crypt-acl-plugin.c revision eb318ea05532d2e54ed3bfc89bc15dcf1adae838
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2017 Dovecot authors, see the included COPYING file */
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen MODULE_CONTEXT(obj, mail_crypt_acl_mailbox_list_module)
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(mail_crypt_acl_mailbox_list_module,
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainenvoid mail_crypt_acl_plugin_init(struct module *module);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainenmail_crypt_acl_has_user_read_right(struct acl_object *aclobj,
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen const char **error_r)
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen ret = str_array_find(rights.rights, MAIL_ACL_READ) ? 1 : 0;
e106213aebb4170904697814c168548e1ba97de9Timo Sirainenstatic int mail_crypt_acl_has_nonuser_read_right(struct acl_object *aclobj,
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen const char **error_r)
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen str_array_find(rights.rights, MAIL_ACL_READ)) {
e106213aebb4170904697814c168548e1ba97de9Timo Sirainenmail_crypt_acl_unset_private_keys(struct mailbox *src_box,
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen const char **error_r)
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen *error_r = t_strdup_printf("mail-crypt-acl-plugin: "
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen "mailbox_open(%s) failed: %s",
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen if (mail_crypt_box_get_pvt_digests(src_box, pool_datastack_create(),
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen *error_r = t_strdup_printf("mail-crypt-acl-plugin: "
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen "Failed to lookup public key digests: %s",
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen const char *const *hash;
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen const char *ptr;
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen /* if the id contains username part, skip to key public id */
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen if ((ret = mail_crypt_box_unset_shared_key(t, ptr, dest_user,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen } else if (mailbox_transaction_commit(&t) < 0) {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen *error_r = t_strdup_printf("mail-crypt-acl-plugin: "
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen "mailbox_transaction_commit(%s) failed: %s",
e106213aebb4170904697814c168548e1ba97de9Timo Sirainenmail_crypt_acl_user_create(struct mail_user *user, const char *dest_username,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen struct mail_storage_service_user **dest_service_user_r,
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen const char **error_r)
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen const struct mail_storage_service_input *old_input;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen service_ctx = mail_storage_service_user_get_service_ctx(user->_service_user);
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen old_input = mail_storage_service_user_get_input(user->_service_user);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen if ((cur_ioloop_ctx = io_loop_get_current_context(current_ioloop)) != NULL)
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen input.flags_override_add = MAIL_STORAGE_SERVICE_FLAG_NO_PLUGINS |
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen ret = mail_storage_service_lookup_next(service_ctx, &input,
e106213aebb4170904697814c168548e1ba97de9Timo Sirainenmail_crypt_acl_update_private_key(struct mailbox *src_box,
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen const char **error_r)
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen return mail_crypt_acl_unset_private_keys(src_box,
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen /* get public key from target user */
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen if ((ret = mail_crypt_user_get_public_key(dest_user,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen *error_r = t_strdup_printf("User %s has no active public key",
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen } else if (ret < 0) {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen } else if (ret == 0) {
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen /* perform insecure sharing */
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen /* get private keys from box */
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen if (mail_crypt_box_get_private_keys(src_box, &keys, error_r) < 0 ||
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen *error_r = mailbox_get_last_error(src_box, NULL);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainenstatic int mail_crypt_acl_object_update(struct acl_object *aclobj,
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen MAIL_CRYPT_ACL_LIST_CONTEXT(aclobj->backend->list);
e106213aebb4170904697814c168548e1ba97de9Timo Sirainen struct mail_storage_service_user *dest_service_user;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen if (mlist->acl_vprev.object_update(aclobj, update) < 0)
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen mail_crypt_acl_secure_sharing_enabled(aclobj->backend->list->ns->user);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen const char *box_name = mailbox_list_get_vname(aclobj->backend->list,
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen struct mailbox *box = mailbox_alloc(aclobj->backend->list, box_name, 0);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen /* setting rights for specific user: we can encrypt the
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen mailbox key for the user. */
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen ret = mail_crypt_acl_has_user_read_right(aclobj, username, &error);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen "mail_crypt_acl_has_user_read_right(%s) failed: %s",
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen ret = mail_crypt_acl_user_create(aclobj->backend->list->ns->user,
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen /* to make sure we get correct logging context */
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen mail_storage_service_io_deactivate_user(dest_service_user);
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen aclobj->backend->list->ns->user->_service_user
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen "Cannot initialize destination user %s: %s",
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch "mailbox_open(%s) failed: %s",
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch } else if ((ret = mail_crypt_acl_update_private_key(box, dest_user,
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch "acl_update_private_key(%s, %s) failed: %s",
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch /* logging context swap again */
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch aclobj->backend->list->ns->user->_service_user
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch mail_storage_service_io_activate_user(dest_service_user);
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch mail_storage_service_user_unref(&dest_service_user);
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch if ((cur_ioloop_ctx = io_loop_get_current_context(current_ioloop)) != NULL)
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch aclobj->backend->list->ns->user->_service_user
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch /* we should be the one doing this? ignore */
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch "Secure key sharing is enabled -"
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch "Remove or set plugin { %s = no }",
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch /* the mailbox key needs to be stored unencrypted. for groups
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch we could in theory use per-group encrypted keys, which the
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch users belonging to the group would able to decrypt with
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch their private key, but that becomes quite complicated. */
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch if ((ret = mail_crypt_acl_has_nonuser_read_right(aclobj, &error)) < 0) {
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch "mailbox_open(%s) failed: %s",
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch } else if ((ret = mail_crypt_acl_update_private_key(box,
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch "acl_update_private_key(%s, %s) failed: %s",
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainenmail_crypt_acl_mail_namespace_storage_added(struct mail_namespace *ns)
15581297511b658a29c707c6031a258bab7bf1a5Stephan Bosch struct acl_mailbox_list *alist = ACL_LIST_CONTEXT(ns->list);
void mail_crypt_acl_plugin_deinit(void)