imap-acl-plugin.c revision c1faff067b29fb48426cb84260adba563e93189a
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (c) 2008-2012 Dovecot authors, see the included COPYING file */
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "imap-common.h"
8e361d2906b0e44f7175a20981f8d2280645b58bTimo Sirainen#include "str.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "imap-quote.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "imap-resp-code.h"
bdd7a96c363346f7c38f389791be1487ca08775bTimo Sirainen#include "imap-commands.h"
bdd7a96c363346f7c38f389791be1487ca08775bTimo Sirainen#include "mail-storage.h"
cc833a7a4e2258afdc834ace4bfe6579820a1df3Timo Sirainen#include "mail-namespace.h"
cc833a7a4e2258afdc834ace4bfe6579820a1df3Timo Sirainen#include "acl-api.h"
97cb20eb77d486ef67eac50567e3080faca025c1Timo Sirainen#include "acl-storage.h"
97cb20eb77d486ef67eac50567e3080faca025c1Timo Sirainen#include "acl-plugin.h"
cc833a7a4e2258afdc834ace4bfe6579820a1df3Timo Sirainen#include "imap-acl-plugin.h"
7e235b3a5f622813121cd18f351e036650aaf8f8Timo Sirainen
366eb7178f2c90d97134e0c2d1958f93fcdaba12Timo Sirainen#include <stdlib.h>
7e235b3a5f622813121cd18f351e036650aaf8f8Timo Sirainen
7e235b3a5f622813121cd18f351e036650aaf8f8Timo Sirainen#define ERROR_NOT_ADMIN "["IMAP_RESP_CODE_NOPERM"] " \
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen "You lack administrator privileges on this mailbox."
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen
863ea896fb31a16d1baec31e57650243b5547db6Timo Sirainen#define IMAP_ACL_ANYONE "anyone"
863ea896fb31a16d1baec31e57650243b5547db6Timo Sirainen#define IMAP_ACL_AUTHENTICATED "authenticated"
863ea896fb31a16d1baec31e57650243b5547db6Timo Sirainen#define IMAP_ACL_OWNER "owner"
471e447023ab73a73f0f78da2afc0c55905330ddTimo Sirainen#define IMAP_ACL_GROUP_PREFIX "$"
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen#define IMAP_ACL_GROUP_OVERRIDE_PREFIX "!$"
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen#define IMAP_ACL_GLOBAL_PREFIX "#"
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainenstruct imap_acl_letter_map {
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen char letter;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen const char *name;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen};
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainenstatic const struct imap_acl_letter_map imap_acl_letter_map[] = {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { 'l', MAIL_ACL_LOOKUP },
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { 'r', MAIL_ACL_READ },
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { 'w', MAIL_ACL_WRITE },
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { 's', MAIL_ACL_WRITE_SEEN },
bbce20cb4e5739e9a06058cf8ee1f38a7f6884f6Timo Sirainen { 't', MAIL_ACL_WRITE_DELETED },
bbce20cb4e5739e9a06058cf8ee1f38a7f6884f6Timo Sirainen { 'i', MAIL_ACL_INSERT },
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { 'p', MAIL_ACL_POST },
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { 'e', MAIL_ACL_EXPUNGE },
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { 'k', MAIL_ACL_CREATE },
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { 'x', MAIL_ACL_DELETE },
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { 'a', MAIL_ACL_ADMIN },
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen { '\0', NULL }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};
55a210942dc7da58b2fd0b11bed8da6b030af5c1Timo Sirainen
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainenconst char *imap_acl_plugin_version = DOVECOT_VERSION;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenstatic struct module *imap_acl_module;
8e361d2906b0e44f7175a20981f8d2280645b58bTimo Sirainenstatic imap_client_created_func_t *next_hook_client_created;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic struct mailbox *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenacl_mailbox_open_as_admin(struct client_command_context *cmd, const char *name)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_namespace *ns;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen struct mailbox *box;
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen enum mailbox_existence existence = MAILBOX_EXISTENCE_NONE;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen int ret;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen if (ACL_USER_CONTEXT(cmd->client->user) == NULL) {
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen client_send_command_error(cmd, "ACLs disabled.");
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen return NULL;
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen }
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen ns = client_find_namespace(cmd, &name);
88b8aea03a24ef7a9efc30399080487b7eb03537Timo Sirainen if (ns == NULL)
88b8aea03a24ef7a9efc30399080487b7eb03537Timo Sirainen return NULL;
88b8aea03a24ef7a9efc30399080487b7eb03537Timo Sirainen
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen /* Force opening the mailbox so that we can give a nicer error message
8872e5c991430f96138a46e36b7f3c2c40d8e5c2Timo Sirainen if mailbox isn't selectable but is listable. */
8872e5c991430f96138a46e36b7f3c2c40d8e5c2Timo Sirainen box = mailbox_alloc(ns->list, name, MAILBOX_FLAG_READONLY |
8872e5c991430f96138a46e36b7f3c2c40d8e5c2Timo Sirainen MAILBOX_FLAG_IGNORE_ACLS);
651fc0f1e43fef3e02e0e7b5f498973b05f641d7Timo Sirainen if (mailbox_exists(box, TRUE, &existence) == 0 &&
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen existence == MAILBOX_EXISTENCE_SELECT) {
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen ret = acl_mailbox_right_lookup(box, ACL_STORAGE_RIGHT_ADMIN);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen if (ret > 0)
d9fda7e3a0fa5551547ac3e3054b837fc77f4bfbTimo Sirainen return box;
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen }
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen
07e80e04c8876b6bf3f95266f48b41e1a681e445Timo Sirainen /* mailbox doesn't exist / not an administrator. */
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen if (existence != MAILBOX_EXISTENCE_SELECT ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen acl_mailbox_right_lookup(box, ACL_STORAGE_RIGHT_LOOKUP) <= 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_send_tagline(cmd, t_strdup_printf(
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "NO ["IMAP_RESP_CODE_NONEXISTENT"] "
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen MAIL_ERRSTR_MAILBOX_NOT_FOUND, name));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen client_send_tagline(cmd, "NO "ERROR_NOT_ADMIN);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen }
45e62043058738e294f89504c319d852e25943ccTimo Sirainen mailbox_free(&box);
45e62043058738e294f89504c319d852e25943ccTimo Sirainen return NULL;
45e62043058738e294f89504c319d852e25943ccTimo Sirainen}
45e62043058738e294f89504c319d852e25943ccTimo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenstatic const struct imap_acl_letter_map *
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenimap_acl_letter_map_find(const char *name)
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen{
d5960ce1c0adda5c9e259bc429123ebc29c60baeTimo Sirainen unsigned int i;
d5960ce1c0adda5c9e259bc429123ebc29c60baeTimo Sirainen
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen for (i = 0; imap_acl_letter_map[i].name != NULL; i++) {
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen if (strcmp(imap_acl_letter_map[i].name, name) == 0)
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen return &imap_acl_letter_map[i];
96975ce7b258b4ed09040bd1dc9a453106dee581Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void
6eb30032b4a50c383dea4c9c74342d906de6ad36Timo Sirainenimap_acl_write_rights_list(string_t *dest, const char *const *rights)
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct imap_acl_letter_map *map;
bdd7a96c363346f7c38f389791be1487ca08775bTimo Sirainen unsigned int i, orig_len = str_len(dest);
bdd7a96c363346f7c38f389791be1487ca08775bTimo Sirainen bool append_c = FALSE, append_d = FALSE;
bdd7a96c363346f7c38f389791be1487ca08775bTimo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen for (i = 0; rights[i] != NULL; i++) {
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen /* write only letters */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen map = imap_acl_letter_map_find(rights[i]);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen if (map != NULL) {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen str_append_c(dest, map->letter);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if (map->letter == 'k' || map->letter == 'x')
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen append_c = TRUE;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if (map->letter == 't' || map->letter == 'e')
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen append_d = TRUE;
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen }
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen }
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen if (append_c)
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen str_append_c(dest, 'c');
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if (append_d)
f537e7efaec891d6b3320ca94331d09ca8c4a4dbTimo Sirainen str_append_c(dest, 'd');
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (orig_len == str_len(dest))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append(dest, "\"\"");
2e99f3f3bb35715ce5e0a75a2f2a9bac3ab4224bTimo Sirainen}
2e99f3f3bb35715ce5e0a75a2f2a9bac3ab4224bTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenimap_acl_write_right(string_t *dest, string_t *tmp,
902222fb0928d1701f20a384b73f327b1d9a15ddTimo Sirainen const struct acl_rights *right, bool neg)
902222fb0928d1701f20a384b73f327b1d9a15ddTimo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *const *rights = neg ? right->neg_rights : right->rights;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_truncate(tmp, 0);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if (neg) str_append_c(tmp,'-');
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen if (right->global)
b87a4156eca6dcf6b29c504eb0cb9be2fdb11b63Timo Sirainen str_append(tmp, IMAP_ACL_GLOBAL_PREFIX);
b87a4156eca6dcf6b29c504eb0cb9be2fdb11b63Timo Sirainen switch (right->id_type) {
b87a4156eca6dcf6b29c504eb0cb9be2fdb11b63Timo Sirainen case ACL_ID_ANYONE:
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen str_append(tmp, IMAP_ACL_ANYONE);
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen break;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen case ACL_ID_AUTHENTICATED:
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen str_append(tmp, IMAP_ACL_AUTHENTICATED);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen break;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen case ACL_ID_OWNER:
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen str_append(tmp, IMAP_ACL_OWNER);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen break;
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen case ACL_ID_USER:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append(tmp, right->identifier);
break;
case ACL_ID_GROUP:
str_append(tmp, IMAP_ACL_GROUP_PREFIX);
str_append(tmp, right->identifier);
break;
case ACL_ID_GROUP_OVERRIDE:
str_append(tmp, IMAP_ACL_GROUP_OVERRIDE_PREFIX);
str_append(tmp, right->identifier);
break;
case ACL_ID_TYPE_COUNT:
i_unreached();
}
imap_quote_append(dest, str_data(tmp), str_len(tmp), FALSE);
str_append_c(dest, ' ');
imap_acl_write_rights_list(dest, rights);
}
static bool
acl_rights_is_owner(struct acl_backend *backend,
const struct acl_rights *rights)
{
switch (rights->id_type) {
case ACL_ID_OWNER:
return TRUE;
case ACL_ID_USER:
return acl_backend_user_name_equals(backend,
rights->identifier);
default:
return FALSE;
}
}
static bool have_positive_owner_rights(struct acl_backend *backend,
struct acl_object *aclobj)
{
struct acl_object_list_iter *iter;
struct acl_rights rights;
bool ret = FALSE;
iter = acl_object_list_init(aclobj);
while ((ret = acl_object_list_next(iter, &rights)) > 0) {
if (acl_rights_is_owner(backend, &rights)) {
if (rights.rights != NULL) {
ret = TRUE;
break;
}
}
}
acl_object_list_deinit(&iter);
return ret;
}
static int
imap_acl_write_aclobj(string_t *dest, struct acl_backend *backend,
struct acl_object *aclobj, bool convert_owner,
bool add_default)
{
struct acl_object_list_iter *iter;
struct acl_rights rights;
string_t *tmp;
const char *username;
unsigned int orig_len = str_len(dest);
bool seen_owner = FALSE, seen_positive_owner = FALSE;
int ret;
username = acl_backend_get_acl_username(backend);
if (username == NULL)
convert_owner = FALSE;
tmp = t_str_new(128);
iter = acl_object_list_init(aclobj);
while ((ret = acl_object_list_next(iter, &rights)) > 0) {
if (acl_rights_is_owner(backend, &rights)) {
if (rights.id_type == ACL_ID_OWNER && convert_owner) {
rights.id_type = ACL_ID_USER;
rights.identifier = username;
}
if (seen_owner && convert_owner) {
/* oops, we have both owner and user=myself.
can't do the conversion, so try again. */
str_truncate(dest, orig_len);
return imap_acl_write_aclobj(dest, backend,
aclobj, FALSE,
add_default);
}
seen_owner = TRUE;
if (rights.rights != NULL)
seen_positive_owner = TRUE;
}
if (rights.rights != NULL) {
str_append_c(dest, ' ');
imap_acl_write_right(dest, tmp, &rights, FALSE);
}
if (rights.neg_rights != NULL) {
str_append_c(dest, ' ');
imap_acl_write_right(dest, tmp, &rights, TRUE);
}
}
acl_object_list_deinit(&iter);
if (!seen_positive_owner && username != NULL && add_default) {
/* no positive owner rights returned, write default ACLs */
memset(&rights, 0, sizeof(rights));
if (!convert_owner) {
rights.id_type = ACL_ID_OWNER;
} else {
rights.id_type = ACL_ID_USER;
rights.identifier = username;
}
rights.rights = acl_object_get_default_rights(aclobj);
if (rights.rights != NULL) {
str_append_c(dest, ' ');
imap_acl_write_right(dest, tmp, &rights, FALSE);
}
}
return ret;
}
static bool cmd_getacl(struct client_command_context *cmd)
{
struct acl_backend *backend;
struct mail_namespace *ns;
struct mailbox *box;
const char *mailbox;
string_t *str;
int ret;
if (!client_read_string_args(cmd, 1, &mailbox))
return FALSE;
box = acl_mailbox_open_as_admin(cmd, mailbox);
if (box == NULL)
return TRUE;
str = t_str_new(128);
str_append(str, "* ACL ");
imap_quote_append_string(str, mailbox, FALSE);
ns = mailbox_get_namespace(box);
backend = acl_mailbox_list_get_backend(ns->list);
ret = imap_acl_write_aclobj(str, backend,
acl_mailbox_get_aclobj(box), TRUE,
ns->type == NAMESPACE_PRIVATE);
if (ret == 0) {
client_send_line(cmd->client, str_c(str));
client_send_tagline(cmd, "OK Getacl completed.");
} else {
client_send_tagline(cmd, "NO "MAIL_ERRSTR_CRITICAL_MSG);
}
mailbox_free(&box);
return TRUE;
}
static bool cmd_myrights(struct client_command_context *cmd)
{
struct mail_namespace *ns;
struct mailbox *box;
const char *mailbox;
const char *const *rights;
string_t *str;
if (!client_read_string_args(cmd, 1, &mailbox))
return FALSE;
if (ACL_USER_CONTEXT(cmd->client->user) == NULL) {
client_send_command_error(cmd, "ACLs disabled.");
return TRUE;
}
ns = client_find_namespace(cmd, &mailbox);
if (ns == NULL)
return TRUE;
box = mailbox_alloc(ns->list, mailbox,
MAILBOX_FLAG_READONLY | MAILBOX_FLAG_IGNORE_ACLS);
if (acl_object_get_my_rights(acl_mailbox_get_aclobj(box),
pool_datastack_create(), &rights) < 0) {
client_send_tagline(cmd, "NO "MAIL_ERRSTR_CRITICAL_MSG);
mailbox_free(&box);
return TRUE;
}
/* Post right alone doesn't give permissions to see if the mailbox
exists or not. Only mail deliveries care about that. */
if (*rights == NULL ||
(strcmp(*rights, MAIL_ACL_POST) == 0 && rights[1] == NULL)) {
client_send_tagline(cmd, t_strdup_printf(
"NO ["IMAP_RESP_CODE_NONEXISTENT"] "
MAIL_ERRSTR_MAILBOX_NOT_FOUND, mailbox));
mailbox_free(&box);
return TRUE;
}
str = t_str_new(128);
str_append(str, "* MYRIGHTS ");
imap_quote_append_string(str, mailbox, FALSE);
str_append_c(str,' ');
imap_acl_write_rights_list(str, rights);
client_send_line(cmd->client, str_c(str));
client_send_tagline(cmd, "OK Myrights completed.");
mailbox_free(&box);
return TRUE;
}
static bool cmd_listrights(struct client_command_context *cmd)
{
struct mailbox *box;
const char *mailbox, *identifier;
string_t *str;
if (!client_read_string_args(cmd, 2, &mailbox, &identifier))
return FALSE;
box = acl_mailbox_open_as_admin(cmd, mailbox);
if (box == NULL)
return TRUE;
str = t_str_new(128);
str_append(str, "* LISTRIGHTS ");
imap_quote_append_string(str, mailbox, FALSE);
str_append_c(str, ' ');
imap_quote_append_string(str, identifier, FALSE);
str_append_c(str, ' ');
str_append(str, "\"\" l r w s t p i e k x a c d");
client_send_line(cmd->client, str_c(str));
client_send_tagline(cmd, "OK Listrights completed.");
mailbox_free(&box);
return TRUE;
}
static int
imap_acl_letters_parse(const char *letters, const char *const **rights_r,
const char **error_r)
{
static const char *acl_k = MAIL_ACL_CREATE;
static const char *acl_x = MAIL_ACL_DELETE;
static const char *acl_e = MAIL_ACL_EXPUNGE;
static const char *acl_t = MAIL_ACL_WRITE_DELETED;
ARRAY_TYPE(const_string) rights;
unsigned int i;
t_array_init(&rights, 64);
for (; *letters != '\0'; letters++) {
for (i = 0; imap_acl_letter_map[i].name != NULL; i++) {
if (imap_acl_letter_map[i].letter == *letters) {
array_append(&rights,
&imap_acl_letter_map[i].name, 1);
break;
}
}
if (imap_acl_letter_map[i].name == NULL) {
/* Handling of obsolete rights as virtual
rights according to RFC 4314 */
switch (*letters) {
case 'c':
array_append(&rights, &acl_k, 1);
array_append(&rights, &acl_x, 1);
break;
case 'd':
array_append(&rights, &acl_e, 1);
array_append(&rights, &acl_t, 1);
break;
default:
*error_r = t_strdup_printf(
"Invalid ACL right: %c", *letters);
return -1;
}
}
}
(void)array_append_space(&rights);
*rights_r = array_idx(&rights, 0);
return 0;
}
static bool acl_anyone_allow(struct mail_user *user)
{
const char *env;
env = mail_user_plugin_getenv(user, "acl_anyone");
return env != NULL && strcmp(env, "allow") == 0;
}
static int
imap_acl_identifier_parse(struct client_command_context *cmd,
const char *id, struct acl_rights *rights,
bool check_anyone, const char **error_r)
{
struct mail_user *user = cmd->client->user;
if (strncmp(id, IMAP_ACL_GLOBAL_PREFIX,
strlen(IMAP_ACL_GLOBAL_PREFIX)) == 0) {
*error_r = t_strdup_printf("Global ACLs can't be modified: %s",
id);
return -1;
}
if (strcmp(id, IMAP_ACL_ANYONE) == 0) {
if (check_anyone && !acl_anyone_allow(user)) {
*error_r = "'anyone' identifier is disallowed";
return -1;
}
rights->id_type = ACL_ID_ANYONE;
} else if (strcmp(id, IMAP_ACL_AUTHENTICATED) == 0) {
if (check_anyone && !acl_anyone_allow(user)) {
*error_r = "'authenticated' identifier is disallowed";
return -1;
}
rights->id_type = ACL_ID_AUTHENTICATED;
} else if (strcmp(id, IMAP_ACL_OWNER) == 0)
rights->id_type = ACL_ID_OWNER;
else if (strncmp(id, IMAP_ACL_GROUP_PREFIX,
strlen(IMAP_ACL_GROUP_PREFIX)) == 0) {
rights->id_type = ACL_ID_GROUP;
rights->identifier = id + strlen(IMAP_ACL_GROUP_PREFIX);
} else if (strncmp(id, IMAP_ACL_GROUP_OVERRIDE_PREFIX,
strlen(IMAP_ACL_GROUP_OVERRIDE_PREFIX)) == 0) {
rights->id_type = ACL_ID_GROUP_OVERRIDE;
rights->identifier = id +
strlen(IMAP_ACL_GROUP_OVERRIDE_PREFIX);
} else {
rights->id_type = ACL_ID_USER;
rights->identifier = id;
}
return 0;
}
static void imap_acl_update_ensure_keep_admins(struct acl_backend *backend,
struct acl_object *aclobj,
struct acl_rights_update *update)
{
static const char *acl_admin = MAIL_ACL_ADMIN;
const char *const *rights = update->rights.rights;
const char *const *default_rights;
ARRAY_TYPE(const_string) new_rights;
unsigned int i;
t_array_init(&new_rights, 64);
for (i = 0; rights[i] != NULL; i++) {
if (strcmp(rights[i], MAIL_ACL_ADMIN) == 0)
break;
array_append(&new_rights, &rights[i], 1);
}
switch (update->modify_mode) {
case ACL_MODIFY_MODE_ADD:
if (have_positive_owner_rights(backend, aclobj))
return;
/* adding initial rights for a user. we need to add
the defaults also. don't worry about duplicates. */
for (; rights[i] != NULL; i++)
array_append(&new_rights, &rights[i], 1);
default_rights = acl_object_get_default_rights(aclobj);
for (i = 0; default_rights[i] != NULL; i++)
array_append(&new_rights, &default_rights[i], 1);
break;
case ACL_MODIFY_MODE_REMOVE:
if (rights[i] == NULL)
return;
/* skip over the ADMIN removal and add the rest */
for (i++; rights[i] != NULL; i++)
array_append(&new_rights, &rights[i], 1);
break;
case ACL_MODIFY_MODE_REPLACE:
if (rights[i] != NULL)
return;
/* add the missing ADMIN right */
array_append(&new_rights, &acl_admin, 1);
break;
default:
return;
}
(void)array_append_space(&new_rights);
update->rights.rights = array_idx(&new_rights, 0);
}
static bool cmd_setacl(struct client_command_context *cmd)
{
struct mail_namespace *ns;
struct mailbox *box;
struct acl_backend *backend;
struct acl_object *aclobj;
struct acl_rights_update update;
struct acl_rights *r;
const char *mailbox, *identifier, *rights, *error;
bool negative = FALSE;
if (!client_read_string_args(cmd, 3, &mailbox, &identifier, &rights))
return FALSE;
if (*identifier == '\0') {
client_send_command_error(cmd, "Invalid arguments.");
return TRUE;
}
memset(&update, 0, sizeof(update));
if (*identifier == '-') {
negative = TRUE;
identifier++;
}
switch (*rights) {
case '-':
update.modify_mode = ACL_MODIFY_MODE_REMOVE;
rights++;
break;
case '+':
update.modify_mode = ACL_MODIFY_MODE_ADD;
rights++;
break;
default:
update.modify_mode = ACL_MODIFY_MODE_REPLACE;
break;
}
if (imap_acl_identifier_parse(cmd, identifier, &update.rights,
TRUE, &error) < 0) {
client_send_command_error(cmd, error);
return TRUE;
}
if (imap_acl_letters_parse(rights, &update.rights.rights, &error) < 0) {
client_send_command_error(cmd, error);
return TRUE;
}
r = &update.rights;
box = acl_mailbox_open_as_admin(cmd, mailbox);
if (box == NULL)
return TRUE;
ns = mailbox_get_namespace(box);
backend = acl_mailbox_list_get_backend(ns->list);
if (ns->type == NAMESPACE_PUBLIC && r->id_type == ACL_ID_OWNER) {
client_send_tagline(cmd, "NO Public namespaces have no owner");
mailbox_free(&box);
return TRUE;
}
aclobj = acl_mailbox_get_aclobj(box);
if (negative) {
update.neg_modify_mode = update.modify_mode;
update.modify_mode = ACL_MODIFY_MODE_REMOVE;
update.rights.neg_rights = update.rights.rights;
update.rights.rights = NULL;
} else if (ns->type == NAMESPACE_PRIVATE && r->rights != NULL &&
((r->id_type == ACL_ID_USER &&
acl_backend_user_name_equals(backend, r->identifier)) ||
(r->id_type == ACL_ID_OWNER &&
strcmp(acl_backend_get_acl_username(backend),
ns->user->username) == 0))) {
/* make sure client doesn't (accidentally) remove admin
privileges from its own mailboxes */
imap_acl_update_ensure_keep_admins(backend, aclobj, &update);
}
if (acl_object_update(aclobj, &update) < 0)
client_send_tagline(cmd, "NO "MAIL_ERRSTR_CRITICAL_MSG);
else
client_send_tagline(cmd, "OK Setacl complete.");
mailbox_free(&box);
return TRUE;
}
static bool cmd_deleteacl(struct client_command_context *cmd)
{
struct mailbox *box;
struct acl_rights_update update;
const char *mailbox, *identifier, *error;
if (!client_read_string_args(cmd, 2, &mailbox, &identifier))
return FALSE;
if (*identifier == '\0') {
client_send_command_error(cmd, "Invalid arguments.");
return TRUE;
}
memset(&update, 0, sizeof(update));
if (*identifier != '-')
update.modify_mode = ACL_MODIFY_MODE_CLEAR;
else {
update.neg_modify_mode = ACL_MODIFY_MODE_CLEAR;
identifier++;
}
if (imap_acl_identifier_parse(cmd, identifier, &update.rights,
FALSE, &error) < 0) {
client_send_command_error(cmd, error);
return TRUE;
}
box = acl_mailbox_open_as_admin(cmd, mailbox);
if (box == NULL)
return TRUE;
if (acl_object_update(acl_mailbox_get_aclobj(box), &update) < 0)
client_send_tagline(cmd, "NO "MAIL_ERRSTR_CRITICAL_MSG);
else
client_send_tagline(cmd, "OK Deleteacl complete.");
mailbox_free(&box);
return TRUE;
}
static void imap_acl_client_created(struct client **client)
{
if (mail_user_is_plugin_loaded((*client)->user, imap_acl_module))
str_append((*client)->capability_string, " ACL RIGHTS=texk");
if (next_hook_client_created != NULL)
next_hook_client_created(client);
}
void imap_acl_plugin_init(struct module *module)
{
command_register("LISTRIGHTS", cmd_listrights, 0);
command_register("GETACL", cmd_getacl, 0);
command_register("MYRIGHTS", cmd_myrights, 0);
command_register("SETACL", cmd_setacl, 0);
command_register("DELETEACL", cmd_deleteacl, 0);
imap_acl_module = module;
next_hook_client_created =
imap_client_created_hook_set(imap_acl_client_created);
}
void imap_acl_plugin_deinit(void)
{
command_unregister("GETACL");
command_unregister("MYRIGHTS");
command_unregister("SETACL");
command_unregister("DELETEACL");
command_unregister("LISTRIGHTS");
imap_client_created_hook_set(next_hook_client_created);
}
const char *imap_acl_plugin_dependencies[] = { "acl", NULL };
const char imap_acl_plugin_binary_dependency[] = "imap";