bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING file */
25d624dd86700c82cd28427f3d3bebe7c8f7f459Timo Sirainen#define ERROR_NOT_ADMIN "["IMAP_RESP_CODE_NOPERM"] " \
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen "You lack administrator privileges on this mailbox."
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenstatic const struct imap_acl_letter_map imap_acl_letter_map[] = {
bd63b5b860658b01b1f46f26d406e1e4a9dc019aTimo Sirainenconst char *imap_acl_plugin_version = DOVECOT_ABI_VERSION;
c1faff067b29fb48426cb84260adba563e93189aTimo Sirainenstatic imap_client_created_func_t *next_hook_client_created;
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenstatic struct mailbox *
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenacl_mailbox_open_as_admin(struct client_command_context *cmd, const char *name)
eacce2276278ce6a8176a9a100807dba50bbfb36Timo Sirainen enum mailbox_existence existence = MAILBOX_EXISTENCE_NONE;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen if (ACL_USER_CONTEXT(cmd->client->user) == NULL) {
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen client_send_command_error(cmd, "ACLs disabled.");
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen /* Force opening the mailbox so that we can give a nicer error message
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen if mailbox isn't selectable but is listable. */
4145cbac82bfc0c8bfeceeca0ef841700117930cTimo Sirainen box = mailbox_alloc(ns->list, name, MAILBOX_FLAG_READONLY |
eacce2276278ce6a8176a9a100807dba50bbfb36Timo Sirainen if (mailbox_exists(box, TRUE, &existence) == 0 &&
eacce2276278ce6a8176a9a100807dba50bbfb36Timo Sirainen ret = acl_mailbox_right_lookup(box, ACL_STORAGE_RIGHT_ADMIN);
eacce2276278ce6a8176a9a100807dba50bbfb36Timo Sirainen /* mailbox doesn't exist / not an administrator. */
eacce2276278ce6a8176a9a100807dba50bbfb36Timo Sirainen acl_mailbox_right_lookup(box, ACL_STORAGE_RIGHT_LOOKUP) <= 0) {
e1203014de25c8c3d3975a9f4b4a04616df4bba2Timo Sirainen client_send_tagline(cmd, "NO "ERROR_NOT_ADMIN);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenstatic const struct imap_acl_letter_map *
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen unsigned int i;
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen for (i = 0; imap_acl_letter_map[i].name != NULL; i++) {
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen if (strcmp(imap_acl_letter_map[i].name, name) == 0)
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenimap_acl_write_rights_list(string_t *dest, const char *const *rights)
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen unsigned int i;
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen /* write only letters */
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenimap_acl_write_right(string_t *dest, string_t *tmp,
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen const char *const *rights = neg ? right->neg_rights : right->rights;
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen str_append(tmp, IMAP_ACL_GROUP_OVERRIDE_PREFIX);
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainenacl_rights_is_owner(struct acl_backend *backend,
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainenstatic bool have_positive_owner_rights(struct acl_backend *backend,
0b2f7be9fadfd4026a9174e51170890cde3edf48Timo Sirainenimap_acl_write_aclobj(string_t *dest, struct acl_backend *backend,
45c872f65e4f327ef166c6e2b71bb43e188ac562Timo Sirainen struct acl_object *aclobj, bool convert_owner,
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainen bool seen_owner = FALSE, seen_positive_owner = FALSE;
0b2f7be9fadfd4026a9174e51170890cde3edf48Timo Sirainen username = acl_backend_get_acl_username(backend);
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainen if (rights.id_type == ACL_ID_OWNER && convert_owner) {
0b2f7be9fadfd4026a9174e51170890cde3edf48Timo Sirainen /* oops, we have both owner and user=myself.
0b2f7be9fadfd4026a9174e51170890cde3edf48Timo Sirainen can't do the conversion, so try again. */
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen imap_acl_write_right(dest, tmp, &rights, FALSE);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen imap_acl_write_right(dest, tmp, &rights, TRUE);
45c872f65e4f327ef166c6e2b71bb43e188ac562Timo Sirainen if (!seen_positive_owner && username != NULL && add_default) {
0b2f7be9fadfd4026a9174e51170890cde3edf48Timo Sirainen /* no positive owner rights returned, write default ACLs */
0b2f7be9fadfd4026a9174e51170890cde3edf48Timo Sirainen rights.rights = acl_object_get_default_rights(aclobj);
0b2f7be9fadfd4026a9174e51170890cde3edf48Timo Sirainen imap_acl_write_right(dest, tmp, &rights, FALSE);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenstatic bool cmd_getacl(struct client_command_context *cmd)
04a7b696e5255aa956277a0f7cabee736c69ec96Timo Sirainen if (!client_read_string_args(cmd, 1, &mailbox))
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen box = acl_mailbox_open_as_admin(cmd, mailbox);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen backend = acl_mailbox_list_get_backend(ns->list);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen client_send_tagline(cmd, "OK Getacl completed.");
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen client_send_tagline(cmd, "NO "MAIL_ERRSTR_CRITICAL_MSG);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenstatic bool cmd_myrights(struct client_command_context *cmd)
04a7b696e5255aa956277a0f7cabee736c69ec96Timo Sirainen if (!client_read_string_args(cmd, 1, &mailbox))
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen if (ACL_USER_CONTEXT(cmd->client->user) == NULL) {
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen client_send_command_error(cmd, "ACLs disabled.");
4145cbac82bfc0c8bfeceeca0ef841700117930cTimo Sirainen MAILBOX_FLAG_READONLY | MAILBOX_FLAG_IGNORE_ACLS);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen if (acl_object_get_my_rights(acl_mailbox_get_aclobj(box),
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen client_send_tagline(cmd, "NO "MAIL_ERRSTR_CRITICAL_MSG);
04870054863757edf048c81dcce3c5e7dec453cdTimo Sirainen /* Post right alone doesn't give permissions to see if the mailbox
04870054863757edf048c81dcce3c5e7dec453cdTimo Sirainen exists or not. Only mail deliveries care about that. */
04870054863757edf048c81dcce3c5e7dec453cdTimo Sirainen (strcmp(*rights, MAIL_ACL_POST) == 0 && rights[1] == NULL)) {
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen client_send_tagline(cmd, "OK Myrights completed.");
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenstatic bool cmd_listrights(struct client_command_context *cmd)
04a7b696e5255aa956277a0f7cabee736c69ec96Timo Sirainen if (!client_read_string_args(cmd, 2, &mailbox, &identifier))
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen box = acl_mailbox_open_as_admin(cmd, mailbox);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen str_append(str, "\"\" l r w s t p i e k x a c d");
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen client_send_tagline(cmd, "OK Listrights completed.");
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenimap_acl_letters_parse(const char *letters, const char *const **rights_r,
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen const char **error_r)
e5b723864630e40c9028808ef417dd3d6fbf495bTimo Sirainen static const char *acl_t = MAIL_ACL_WRITE_DELETED;
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen unsigned int i;
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen for (i = 0; imap_acl_letter_map[i].name != NULL; i++) {
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen if (imap_acl_letter_map[i].letter == *letters) {
e5b723864630e40c9028808ef417dd3d6fbf495bTimo Sirainen /* Handling of obsolete rights as virtual
e5b723864630e40c9028808ef417dd3d6fbf495bTimo Sirainen rights according to RFC 4314 */
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainenstatic bool acl_anyone_allow(struct mail_user *user)
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen env = mail_user_plugin_getenv(user, "acl_anyone");
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen return env != NULL && strcmp(env, "allow") == 0;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainenimap_acl_identifier_parse(struct client_command_context *cmd,
714e2da5096fb52b8845d3c79f9bb26225a606c9Timo Sirainen *error_r = t_strdup_printf("Global ACLs can't be modified: %s",
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen if (check_anyone && !acl_anyone_allow(user)) {
6882df5fbca4a09cdaa95f54d70bb31b5920528cTimo Sirainen *error_r = "'anyone' identifier is disallowed";
6882df5fbca4a09cdaa95f54d70bb31b5920528cTimo Sirainen } else if (strcmp(id, IMAP_ACL_AUTHENTICATED) == 0) {
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen if (check_anyone && !acl_anyone_allow(user)) {
6882df5fbca4a09cdaa95f54d70bb31b5920528cTimo Sirainen *error_r = "'authenticated' identifier is disallowed";
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen rights->identifier = id + strlen(IMAP_ACL_GROUP_PREFIX);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen } else if (strncmp(id, IMAP_ACL_GROUP_OVERRIDE_PREFIX,
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen strlen(IMAP_ACL_GROUP_OVERRIDE_PREFIX)) == 0) {
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainenstatic void imap_acl_update_ensure_keep_admins(struct acl_backend *backend,
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen static const char *acl_admin = MAIL_ACL_ADMIN;
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen const char *const *rights = update->rights.rights;
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen unsigned int i;
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainen if (have_positive_owner_rights(backend, aclobj))
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainen /* adding initial rights for a user. we need to add
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainen the defaults also. don't worry about duplicates. */
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainen default_rights = acl_object_get_default_rights(aclobj);
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainen array_append(&new_rights, &default_rights[i], 1);
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen /* skip over the ADMIN removal and add the rest */
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen /* add the missing ADMIN right */
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen update->rights.rights = array_idx(&new_rights, 0);
eb4d4f557fa75aa2a47639e9deb75a21f44eb42aTimo Sirainen const char **error_r)
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL,
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenstatic bool cmd_setacl(struct client_command_context *cmd)
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen const char *mailbox, *identifier, *rights, *error;
04a7b696e5255aa956277a0f7cabee736c69ec96Timo Sirainen if (!client_read_string_args(cmd, 3, &mailbox, &identifier, &rights))
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen client_send_command_error(cmd, "Invalid arguments.");
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen if (imap_acl_identifier_parse(cmd, identifier, &update.rights,
c3d40f3092af25cad9e807a85eaad4d92aab107bTimo Sirainen if (imap_acl_letters_parse(rights, &update.rights.rights, &error) < 0) {
c3d40f3092af25cad9e807a85eaad4d92aab107bTimo Sirainen box = acl_mailbox_open_as_admin(cmd, mailbox);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen backend = acl_mailbox_list_get_backend(ns->list);
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen client_send_tagline(cmd, "NO Public namespaces have no owner");
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen update.rights.neg_rights = update.rights.rights;
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen } else if (ns->type == MAIL_NAMESPACE_TYPE_PRIVATE &&
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen acl_backend_user_name_equals(backend, r->identifier)) ||
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen /* make sure client doesn't (accidentally) remove admin
bd4de9c8152e6ea032c1cb1df8b79635ff5ddf9eTimo Sirainen privileges from its own mailboxes */
4530cfa7456c10cd03fe9120c75f8bcb2f623ba4Timo Sirainen imap_acl_update_ensure_keep_admins(backend, aclobj, &update);
eb4d4f557fa75aa2a47639e9deb75a21f44eb42aTimo Sirainen if (cmd_acl_mailbox_update(box, &update, &error) < 0)
eb4d4f557fa75aa2a47639e9deb75a21f44eb42aTimo Sirainen client_send_tagline(cmd, t_strdup_printf("NO %s", error));
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen client_send_tagline(cmd, "OK Setacl complete.");
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainenstatic bool cmd_deleteacl(struct client_command_context *cmd)
04a7b696e5255aa956277a0f7cabee736c69ec96Timo Sirainen if (!client_read_string_args(cmd, 2, &mailbox, &identifier))
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen client_send_command_error(cmd, "Invalid arguments.");
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen update.neg_modify_mode = ACL_MODIFY_MODE_CLEAR;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen if (imap_acl_identifier_parse(cmd, identifier, &update.rights,
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen box = acl_mailbox_open_as_admin(cmd, mailbox);
eb4d4f557fa75aa2a47639e9deb75a21f44eb42aTimo Sirainen if (cmd_acl_mailbox_update(box, &update, &error) < 0)
eb4d4f557fa75aa2a47639e9deb75a21f44eb42aTimo Sirainen client_send_tagline(cmd, t_strdup_printf("NO %s", error));
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen client_send_tagline(cmd, "OK Deleteacl complete.");
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic void imap_acl_client_created(struct client **client)
2c719bcb92302f45df4badb71d1d97f57235d0ccTimo Sirainen if (mail_user_is_plugin_loaded((*client)->user, imap_acl_module)) {
2c719bcb92302f45df4badb71d1d97f57235d0ccTimo Sirainen client_add_capability(*client, "RIGHTS=texk");
cd2cd224d3216a243d55c71c298a5b7684de0ac4Timo Sirainenvoid imap_acl_plugin_init(struct module *module)
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen command_register("LISTRIGHTS", cmd_listrights, 0);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen command_register("MYRIGHTS", cmd_myrights, 0);
02a54da28f376dd66d7939d8546a196a0045b486Timo Sirainen command_register("DELETEACL", cmd_deleteacl, 0);
ac0fed903142d28ae3a1d5d00d2097fdf161b138Timo Sirainen imap_client_created_hook_set(imap_acl_client_created);
b66d803de86bfb411165b3465b0d9ef64ecfe2a1Timo Sirainen imap_client_created_hook_set(next_hook_client_created);
23fdad6c7e2581921f511e24cd9371c9eaebcef9Timo Sirainenconst char *imap_acl_plugin_dependencies[] = { "acl", NULL };
8552b0cad8ffe9ccb8270577ba28b8010c89af11Timo Sirainenconst char imap_acl_plugin_binary_dependency[] = "imap";