From 7f35e68bfe21c61f4da9e37f127dd7abb73291fa Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 27 Feb 2024 18:33:49 +0100 Subject: Refactor --- src/dav/mod.rs | 7 + src/imap/command/anonymous.rs | 2 +- src/imap/command/authenticated.rs | 3 +- src/imap/command/mod.rs | 2 +- src/imap/command/selected.rs | 2 +- src/imap/flow.rs | 2 +- src/mail/incoming.rs | 2 +- src/mail/mailbox.rs | 2 +- src/mail/mod.rs | 2 +- src/mail/namespace.rs | 209 ++++++++++++++++ src/mail/user.rs | 500 -------------------------------------- src/main.rs | 1 + src/user.rs | 313 ++++++++++++++++++++++++ 13 files changed, 539 insertions(+), 508 deletions(-) create mode 100644 src/mail/namespace.rs delete mode 100644 src/mail/user.rs create mode 100644 src/user.rs (limited to 'src') diff --git a/src/dav/mod.rs b/src/dav/mod.rs index 709abd5..ac25f2d 100644 --- a/src/dav/mod.rs +++ b/src/dav/mod.rs @@ -106,6 +106,13 @@ async fn auth( .ok_or(anyhow!("Missing colon in Authorization, can't split decoded value into a username/password pair"))?; // Call login provider + let creds = match login.login(username, password).await { + Ok(c) => c, + Err(e) => return Ok(Response::builder() + .status(401) + .body(Full::new(Bytes::from("Wrong credentials")))?), + }; + // Call router with user diff --git a/src/imap/command/anonymous.rs b/src/imap/command/anonymous.rs index 0582b06..811d1e4 100644 --- a/src/imap/command/anonymous.rs +++ b/src/imap/command/anonymous.rs @@ -9,7 +9,7 @@ use crate::imap::command::anystate; use crate::imap::flow; use crate::imap::response::Response; use crate::login::ArcLoginProvider; -use crate::mail::user::User; +use crate::user::User; //--- dispatching diff --git a/src/imap/command/authenticated.rs b/src/imap/command/authenticated.rs index eb8833d..3d332ec 100644 --- a/src/imap/command/authenticated.rs +++ b/src/imap/command/authenticated.rs @@ -22,8 +22,9 @@ use crate::imap::response::Response; use crate::imap::Body; use crate::mail::uidindex::*; -use crate::mail::user::{User, MAILBOX_HIERARCHY_DELIMITER as MBX_HIER_DELIM_RAW}; +use crate::user::User; use crate::mail::IMF; +use crate::mail::namespace::MAILBOX_HIERARCHY_DELIMITER as MBX_HIER_DELIM_RAW; pub struct AuthenticatedContext<'a> { pub req: &'a Command<'static>, diff --git a/src/imap/command/mod.rs b/src/imap/command/mod.rs index 073040e..f201eb6 100644 --- a/src/imap/command/mod.rs +++ b/src/imap/command/mod.rs @@ -3,7 +3,7 @@ pub mod anystate; pub mod authenticated; pub mod selected; -use crate::mail::user::INBOX; +use crate::mail::namespace::INBOX; use imap_codec::imap_types::mailbox::Mailbox as MailboxCodec; /// Convert an IMAP mailbox name/identifier representation diff --git a/src/imap/command/selected.rs b/src/imap/command/selected.rs index d000905..eedfbd6 100644 --- a/src/imap/command/selected.rs +++ b/src/imap/command/selected.rs @@ -17,7 +17,7 @@ use crate::imap::command::{anystate, authenticated, MailboxName}; use crate::imap::flow; use crate::imap::mailbox_view::{MailboxView, UpdateParameters}; use crate::imap::response::Response; -use crate::mail::user::User; +use crate::user::User; pub struct SelectedContext<'a> { pub req: &'a Command<'static>, diff --git a/src/imap/flow.rs b/src/imap/flow.rs index e372d69..86eb12e 100644 --- a/src/imap/flow.rs +++ b/src/imap/flow.rs @@ -6,7 +6,7 @@ use imap_codec::imap_types::core::Tag; use tokio::sync::Notify; use crate::imap::mailbox_view::MailboxView; -use crate::mail::user::User; +use crate::user::User; #[derive(Debug)] pub enum Error { diff --git a/src/mail/incoming.rs b/src/mail/incoming.rs index 781d8dc..e2ad97d 100644 --- a/src/mail/incoming.rs +++ b/src/mail/incoming.rs @@ -16,7 +16,7 @@ use crate::login::{Credentials, PublicCredentials}; use crate::mail::mailbox::Mailbox; use crate::mail::uidindex::ImapUidvalidity; use crate::mail::unique_ident::*; -use crate::mail::user::User; +use crate::user::User; use crate::mail::IMF; use crate::storage; use crate::timestamp::now_msec; diff --git a/src/mail/mailbox.rs b/src/mail/mailbox.rs index 9190883..d1a5473 100644 --- a/src/mail/mailbox.rs +++ b/src/mail/mailbox.rs @@ -17,7 +17,7 @@ pub struct Mailbox { } impl Mailbox { - pub(super) async fn open( + pub(crate) async fn open( creds: &Credentials, id: UniqueIdent, min_uidvalidity: ImapUidvalidity, diff --git a/src/mail/mod.rs b/src/mail/mod.rs index 37578b8..03e85cd 100644 --- a/src/mail/mod.rs +++ b/src/mail/mod.rs @@ -6,7 +6,7 @@ pub mod query; pub mod snapshot; pub mod uidindex; pub mod unique_ident; -pub mod user; +pub mod namespace; // Internet Message Format // aka RFC 822 - RFC 2822 - RFC 5322 diff --git a/src/mail/namespace.rs b/src/mail/namespace.rs new file mode 100644 index 0000000..5e67173 --- /dev/null +++ b/src/mail/namespace.rs @@ -0,0 +1,209 @@ +use std::collections::{BTreeMap, HashMap}; +use std::sync::{Arc, Weak}; + +use anyhow::{anyhow, bail, Result}; +use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; +use tokio::sync::watch; + +use crate::cryptoblob::{open_deserialize, seal_serialize}; +use crate::login::Credentials; +use crate::mail::incoming::incoming_mail_watch_process; +use crate::mail::mailbox::Mailbox; +use crate::mail::uidindex::ImapUidvalidity; +use crate::mail::unique_ident::{gen_ident, UniqueIdent}; +use crate::storage; +use crate::timestamp::now_msec; + +pub const MAILBOX_HIERARCHY_DELIMITER: char = '.'; + +/// INBOX is the only mailbox that must always exist. +/// It is created automatically when the account is created. +/// IMAP allows the user to rename INBOX to something else, +/// in this case all messages from INBOX are moved to a mailbox +/// with the new name and the INBOX mailbox still exists and is empty. +/// In our implementation, we indeed move the underlying mailbox +/// to the new name (i.e. the new name has the same id as the previous +/// INBOX), and we create a new empty mailbox for INBOX. +pub const INBOX: &str = "INBOX"; + +/// For convenience purpose, we also create some special mailbox +/// that are described in RFC6154 SPECIAL-USE +/// @FIXME maybe it should be a configuration parameter +/// @FIXME maybe we should have a per-mailbox flag mechanism, either an enum or a string, so we +/// track which mailbox is used for what. +/// @FIXME Junk could be useful but we don't have any antispam solution yet so... +/// @FIXME IMAP supports virtual mailbox. \All or \Flagged are intended to be virtual mailboxes. +/// \Trash might be one, or not one. I don't know what we should do there. +pub const DRAFTS: &str = "Drafts"; +pub const ARCHIVE: &str = "Archive"; +pub const SENT: &str = "Sent"; +pub const TRASH: &str = "Trash"; + +pub(crate) const MAILBOX_LIST_PK: &str = "mailboxes"; +pub(crate) const MAILBOX_LIST_SK: &str = "list"; + +// ---- User's mailbox list (serialized in K2V) ---- + +#[derive(Serialize, Deserialize)] +pub(crate) struct MailboxList(BTreeMap); + +#[derive(Serialize, Deserialize, Clone, Copy, Debug)] +pub(crate) struct MailboxListEntry { + id_lww: (u64, Option), + uidvalidity: ImapUidvalidity, +} + +impl MailboxListEntry { + fn merge(&mut self, other: &Self) { + // Simple CRDT merge rule + if other.id_lww.0 > self.id_lww.0 + || (other.id_lww.0 == self.id_lww.0 && other.id_lww.1 > self.id_lww.1) + { + self.id_lww = other.id_lww; + } + self.uidvalidity = std::cmp::max(self.uidvalidity, other.uidvalidity); + } +} + +impl MailboxList { + pub(crate) fn new() -> Self { + Self(BTreeMap::new()) + } + + pub(crate) fn merge(&mut self, list2: Self) { + for (k, v) in list2.0.into_iter() { + if let Some(e) = self.0.get_mut(&k) { + e.merge(&v); + } else { + self.0.insert(k, v); + } + } + } + + pub(crate) fn existing_mailbox_names(&self) -> Vec { + self.0 + .iter() + .filter(|(_, v)| v.id_lww.1.is_some()) + .map(|(k, _)| k.to_string()) + .collect() + } + + pub(crate) fn has_mailbox(&self, name: &str) -> bool { + matches!( + self.0.get(name), + Some(MailboxListEntry { + id_lww: (_, Some(_)), + .. + }) + ) + } + + pub(crate) fn get_mailbox(&self, name: &str) -> Option<(ImapUidvalidity, Option)> { + self.0.get(name).map( + |MailboxListEntry { + id_lww: (_, mailbox_id), + uidvalidity, + }| (*uidvalidity, *mailbox_id), + ) + } + + /// Ensures mailbox `name` maps to id `id`. + /// If it already mapped to that, returns None. + /// If a change had to be done, returns Some(new uidvalidity in mailbox). + pub(crate) fn set_mailbox(&mut self, name: &str, id: Option) -> Option { + let (ts, id, uidvalidity) = match self.0.get_mut(name) { + None => { + if id.is_none() { + return None; + } else { + (now_msec(), id, ImapUidvalidity::new(1).unwrap()) + } + } + Some(MailboxListEntry { + id_lww, + uidvalidity, + }) => { + if id_lww.1 == id { + return None; + } else { + ( + std::cmp::max(id_lww.0 + 1, now_msec()), + id, + ImapUidvalidity::new(uidvalidity.get() + 1).unwrap(), + ) + } + } + }; + + self.0.insert( + name.into(), + MailboxListEntry { + id_lww: (ts, id), + uidvalidity, + }, + ); + Some(uidvalidity) + } + + pub(crate) fn update_uidvalidity(&mut self, name: &str, new_uidvalidity: ImapUidvalidity) { + match self.0.get_mut(name) { + None => { + self.0.insert( + name.into(), + MailboxListEntry { + id_lww: (now_msec(), None), + uidvalidity: new_uidvalidity, + }, + ); + } + Some(MailboxListEntry { uidvalidity, .. }) => { + *uidvalidity = std::cmp::max(*uidvalidity, new_uidvalidity); + } + } + } + + pub(crate) fn create_mailbox(&mut self, name: &str) -> CreatedMailbox { + if let Some(MailboxListEntry { + id_lww: (_, Some(id)), + uidvalidity, + }) = self.0.get(name) + { + return CreatedMailbox::Existed(*id, *uidvalidity); + } + + let id = gen_ident(); + let uidvalidity = self.set_mailbox(name, Some(id)).unwrap(); + CreatedMailbox::Created(id, uidvalidity) + } + + pub(crate) fn rename_mailbox(&mut self, old_name: &str, new_name: &str) -> Result<()> { + if let Some((uidvalidity, Some(mbid))) = self.get_mailbox(old_name) { + if self.has_mailbox(new_name) { + bail!( + "Cannot rename {} into {}: {} already exists", + old_name, + new_name, + new_name + ); + } + + self.set_mailbox(old_name, None); + self.set_mailbox(new_name, Some(mbid)); + self.update_uidvalidity(new_name, uidvalidity); + Ok(()) + } else { + bail!( + "Cannot rename {} into {}: {} doesn't exist", + old_name, + new_name, + old_name + ); + } + } +} + +pub(crate) enum CreatedMailbox { + Created(UniqueIdent, ImapUidvalidity), + Existed(UniqueIdent, ImapUidvalidity), +} diff --git a/src/mail/user.rs b/src/mail/user.rs deleted file mode 100644 index ad05615..0000000 --- a/src/mail/user.rs +++ /dev/null @@ -1,500 +0,0 @@ -use std::collections::{BTreeMap, HashMap}; -use std::sync::{Arc, Weak}; - -use anyhow::{anyhow, bail, Result}; -use lazy_static::lazy_static; -use serde::{Deserialize, Serialize}; -use tokio::sync::watch; - -use crate::cryptoblob::{open_deserialize, seal_serialize}; -use crate::login::Credentials; -use crate::mail::incoming::incoming_mail_watch_process; -use crate::mail::mailbox::Mailbox; -use crate::mail::uidindex::ImapUidvalidity; -use crate::mail::unique_ident::{gen_ident, UniqueIdent}; -use crate::storage; -use crate::timestamp::now_msec; - -pub const MAILBOX_HIERARCHY_DELIMITER: char = '.'; - -/// INBOX is the only mailbox that must always exist. -/// It is created automatically when the account is created. -/// IMAP allows the user to rename INBOX to something else, -/// in this case all messages from INBOX are moved to a mailbox -/// with the new name and the INBOX mailbox still exists and is empty. -/// In our implementation, we indeed move the underlying mailbox -/// to the new name (i.e. the new name has the same id as the previous -/// INBOX), and we create a new empty mailbox for INBOX. -pub const INBOX: &str = "INBOX"; - -/// For convenience purpose, we also create some special mailbox -/// that are described in RFC6154 SPECIAL-USE -/// @FIXME maybe it should be a configuration parameter -/// @FIXME maybe we should have a per-mailbox flag mechanism, either an enum or a string, so we -/// track which mailbox is used for what. -/// @FIXME Junk could be useful but we don't have any antispam solution yet so... -/// @FIXME IMAP supports virtual mailbox. \All or \Flagged are intended to be virtual mailboxes. -/// \Trash might be one, or not one. I don't know what we should do there. -pub const DRAFTS: &str = "Drafts"; -pub const ARCHIVE: &str = "Archive"; -pub const SENT: &str = "Sent"; -pub const TRASH: &str = "Trash"; - -const MAILBOX_LIST_PK: &str = "mailboxes"; -const MAILBOX_LIST_SK: &str = "list"; - -pub struct User { - pub username: String, - pub creds: Credentials, - pub storage: storage::Store, - pub mailboxes: std::sync::Mutex>>, - - tx_inbox_id: watch::Sender>, -} - -impl User { - pub async fn new(username: String, creds: Credentials) -> Result> { - let cache_key = (username.clone(), creds.storage.unique()); - - { - let cache = USER_CACHE.lock().unwrap(); - if let Some(u) = cache.get(&cache_key).and_then(Weak::upgrade) { - return Ok(u); - } - } - - let user = Self::open(username, creds).await?; - - let mut cache = USER_CACHE.lock().unwrap(); - if let Some(concurrent_user) = cache.get(&cache_key).and_then(Weak::upgrade) { - drop(user); - Ok(concurrent_user) - } else { - cache.insert(cache_key, Arc::downgrade(&user)); - Ok(user) - } - } - - /// Lists user's available mailboxes - pub async fn list_mailboxes(&self) -> Result> { - let (list, _ct) = self.load_mailbox_list().await?; - Ok(list.existing_mailbox_names()) - } - - /// Opens an existing mailbox given its IMAP name. - pub async fn open_mailbox(&self, name: &str) -> Result>> { - let (mut list, ct) = self.load_mailbox_list().await?; - - //@FIXME it could be a trace or an opentelemtry trace thing. - // Be careful to not leak sensible data - /* - eprintln!("List of mailboxes:"); - for ent in list.0.iter() { - eprintln!(" - {:?}", ent); - } - */ - - if let Some((uidvalidity, Some(mbid))) = list.get_mailbox(name) { - let mb = self.open_mailbox_by_id(mbid, uidvalidity).await?; - let mb_uidvalidity = mb.current_uid_index().await.uidvalidity; - if mb_uidvalidity > uidvalidity { - list.update_uidvalidity(name, mb_uidvalidity); - self.save_mailbox_list(&list, ct).await?; - } - Ok(Some(mb)) - } else { - Ok(None) - } - } - - /// Check whether mailbox exists - pub async fn has_mailbox(&self, name: &str) -> Result { - let (list, _ct) = self.load_mailbox_list().await?; - Ok(list.has_mailbox(name)) - } - - /// Creates a new mailbox in the user's IMAP namespace. - pub async fn create_mailbox(&self, name: &str) -> Result<()> { - if name.ends_with(MAILBOX_HIERARCHY_DELIMITER) { - bail!("Invalid mailbox name: {}", name); - } - - let (mut list, ct) = self.load_mailbox_list().await?; - match list.create_mailbox(name) { - CreatedMailbox::Created(_, _) => { - self.save_mailbox_list(&list, ct).await?; - Ok(()) - } - CreatedMailbox::Existed(_, _) => Err(anyhow!("Mailbox {} already exists", name)), - } - } - - /// Deletes a mailbox in the user's IMAP namespace. - pub async fn delete_mailbox(&self, name: &str) -> Result<()> { - if name == INBOX { - bail!("Cannot delete INBOX"); - } - - let (mut list, ct) = self.load_mailbox_list().await?; - if list.has_mailbox(name) { - //@TODO: actually delete mailbox contents - list.set_mailbox(name, None); - self.save_mailbox_list(&list, ct).await?; - Ok(()) - } else { - bail!("Mailbox {} does not exist", name); - } - } - - /// Renames a mailbox in the user's IMAP namespace. - pub async fn rename_mailbox(&self, old_name: &str, new_name: &str) -> Result<()> { - let (mut list, ct) = self.load_mailbox_list().await?; - - if old_name.ends_with(MAILBOX_HIERARCHY_DELIMITER) { - bail!("Invalid mailbox name: {}", old_name); - } - if new_name.ends_with(MAILBOX_HIERARCHY_DELIMITER) { - bail!("Invalid mailbox name: {}", new_name); - } - - if old_name == INBOX { - list.rename_mailbox(old_name, new_name)?; - if !self.ensure_inbox_exists(&mut list, &ct).await? { - self.save_mailbox_list(&list, ct).await?; - } - } else { - let names = list.existing_mailbox_names(); - - let old_name_w_delim = format!("{}{}", old_name, MAILBOX_HIERARCHY_DELIMITER); - let new_name_w_delim = format!("{}{}", new_name, MAILBOX_HIERARCHY_DELIMITER); - - if names - .iter() - .any(|x| x == new_name || x.starts_with(&new_name_w_delim)) - { - bail!("Mailbox {} already exists", new_name); - } - - for name in names.iter() { - if name == old_name { - list.rename_mailbox(name, new_name)?; - } else if let Some(tail) = name.strip_prefix(&old_name_w_delim) { - let nnew = format!("{}{}", new_name_w_delim, tail); - list.rename_mailbox(name, &nnew)?; - } - } - - self.save_mailbox_list(&list, ct).await?; - } - Ok(()) - } - - // ---- Internal user & mailbox management ---- - - async fn open(username: String, creds: Credentials) -> Result> { - let storage = creds.storage.build().await?; - - let (tx_inbox_id, rx_inbox_id) = watch::channel(None); - - let user = Arc::new(Self { - username, - creds: creds.clone(), - storage, - tx_inbox_id, - mailboxes: std::sync::Mutex::new(HashMap::new()), - }); - - // Ensure INBOX exists (done inside load_mailbox_list) - user.load_mailbox_list().await?; - - tokio::spawn(incoming_mail_watch_process( - Arc::downgrade(&user), - user.creds.clone(), - rx_inbox_id, - )); - - Ok(user) - } - - pub(super) async fn open_mailbox_by_id( - &self, - id: UniqueIdent, - min_uidvalidity: ImapUidvalidity, - ) -> Result> { - { - let cache = self.mailboxes.lock().unwrap(); - if let Some(mb) = cache.get(&id).and_then(Weak::upgrade) { - return Ok(mb); - } - } - - let mb = Arc::new(Mailbox::open(&self.creds, id, min_uidvalidity).await?); - - let mut cache = self.mailboxes.lock().unwrap(); - if let Some(concurrent_mb) = cache.get(&id).and_then(Weak::upgrade) { - drop(mb); // we worked for nothing but at least we didn't starve someone else - Ok(concurrent_mb) - } else { - cache.insert(id, Arc::downgrade(&mb)); - Ok(mb) - } - } - - // ---- Mailbox list management ---- - - async fn load_mailbox_list(&self) -> Result<(MailboxList, Option)> { - let row_ref = storage::RowRef::new(MAILBOX_LIST_PK, MAILBOX_LIST_SK); - let (mut list, row) = match self - .storage - .row_fetch(&storage::Selector::Single(&row_ref)) - .await - { - Err(storage::StorageError::NotFound) => (MailboxList::new(), None), - Err(e) => return Err(e.into()), - Ok(rv) => { - let mut list = MailboxList::new(); - let (row_ref, row_vals) = match rv.into_iter().next() { - Some(row_val) => (row_val.row_ref, row_val.value), - None => (row_ref, vec![]), - }; - - for v in row_vals { - if let storage::Alternative::Value(vbytes) = v { - let list2 = - open_deserialize::(&vbytes, &self.creds.keys.master)?; - list.merge(list2); - } - } - (list, Some(row_ref)) - } - }; - - let is_default_mbx_missing = [DRAFTS, ARCHIVE, SENT, TRASH] - .iter() - .map(|mbx| list.create_mailbox(mbx)) - .fold(false, |acc, r| { - acc || matches!(r, CreatedMailbox::Created(..)) - }); - let is_inbox_missing = self.ensure_inbox_exists(&mut list, &row).await?; - if is_default_mbx_missing && !is_inbox_missing { - // It's the only case where we created some mailboxes and not saved them - // So we save them! - self.save_mailbox_list(&list, row.clone()).await?; - } - - Ok((list, row)) - } - - async fn ensure_inbox_exists( - &self, - list: &mut MailboxList, - ct: &Option, - ) -> Result { - // If INBOX doesn't exist, create a new mailbox with that name - // and save new mailbox list. - // Also, ensure that the mpsc::watch that keeps track of the - // inbox id is up-to-date. - let saved; - let (inbox_id, inbox_uidvalidity) = match list.create_mailbox(INBOX) { - CreatedMailbox::Created(i, v) => { - self.save_mailbox_list(list, ct.clone()).await?; - saved = true; - (i, v) - } - CreatedMailbox::Existed(i, v) => { - saved = false; - (i, v) - } - }; - let inbox_id = Some((inbox_id, inbox_uidvalidity)); - if *self.tx_inbox_id.borrow() != inbox_id { - self.tx_inbox_id.send(inbox_id).unwrap(); - } - - Ok(saved) - } - - async fn save_mailbox_list( - &self, - list: &MailboxList, - ct: Option, - ) -> Result<()> { - let list_blob = seal_serialize(list, &self.creds.keys.master)?; - let rref = ct.unwrap_or(storage::RowRef::new(MAILBOX_LIST_PK, MAILBOX_LIST_SK)); - let row_val = storage::RowVal::new(rref, list_blob); - self.storage.row_insert(vec![row_val]).await?; - Ok(()) - } -} - -// ---- User's mailbox list (serialized in K2V) ---- - -#[derive(Serialize, Deserialize)] -struct MailboxList(BTreeMap); - -#[derive(Serialize, Deserialize, Clone, Copy, Debug)] -struct MailboxListEntry { - id_lww: (u64, Option), - uidvalidity: ImapUidvalidity, -} - -impl MailboxListEntry { - fn merge(&mut self, other: &Self) { - // Simple CRDT merge rule - if other.id_lww.0 > self.id_lww.0 - || (other.id_lww.0 == self.id_lww.0 && other.id_lww.1 > self.id_lww.1) - { - self.id_lww = other.id_lww; - } - self.uidvalidity = std::cmp::max(self.uidvalidity, other.uidvalidity); - } -} - -impl MailboxList { - fn new() -> Self { - Self(BTreeMap::new()) - } - - fn merge(&mut self, list2: Self) { - for (k, v) in list2.0.into_iter() { - if let Some(e) = self.0.get_mut(&k) { - e.merge(&v); - } else { - self.0.insert(k, v); - } - } - } - - fn existing_mailbox_names(&self) -> Vec { - self.0 - .iter() - .filter(|(_, v)| v.id_lww.1.is_some()) - .map(|(k, _)| k.to_string()) - .collect() - } - - fn has_mailbox(&self, name: &str) -> bool { - matches!( - self.0.get(name), - Some(MailboxListEntry { - id_lww: (_, Some(_)), - .. - }) - ) - } - - fn get_mailbox(&self, name: &str) -> Option<(ImapUidvalidity, Option)> { - self.0.get(name).map( - |MailboxListEntry { - id_lww: (_, mailbox_id), - uidvalidity, - }| (*uidvalidity, *mailbox_id), - ) - } - - /// Ensures mailbox `name` maps to id `id`. - /// If it already mapped to that, returns None. - /// If a change had to be done, returns Some(new uidvalidity in mailbox). - fn set_mailbox(&mut self, name: &str, id: Option) -> Option { - let (ts, id, uidvalidity) = match self.0.get_mut(name) { - None => { - if id.is_none() { - return None; - } else { - (now_msec(), id, ImapUidvalidity::new(1).unwrap()) - } - } - Some(MailboxListEntry { - id_lww, - uidvalidity, - }) => { - if id_lww.1 == id { - return None; - } else { - ( - std::cmp::max(id_lww.0 + 1, now_msec()), - id, - ImapUidvalidity::new(uidvalidity.get() + 1).unwrap(), - ) - } - } - }; - - self.0.insert( - name.into(), - MailboxListEntry { - id_lww: (ts, id), - uidvalidity, - }, - ); - Some(uidvalidity) - } - - fn update_uidvalidity(&mut self, name: &str, new_uidvalidity: ImapUidvalidity) { - match self.0.get_mut(name) { - None => { - self.0.insert( - name.into(), - MailboxListEntry { - id_lww: (now_msec(), None), - uidvalidity: new_uidvalidity, - }, - ); - } - Some(MailboxListEntry { uidvalidity, .. }) => { - *uidvalidity = std::cmp::max(*uidvalidity, new_uidvalidity); - } - } - } - - fn create_mailbox(&mut self, name: &str) -> CreatedMailbox { - if let Some(MailboxListEntry { - id_lww: (_, Some(id)), - uidvalidity, - }) = self.0.get(name) - { - return CreatedMailbox::Existed(*id, *uidvalidity); - } - - let id = gen_ident(); - let uidvalidity = self.set_mailbox(name, Some(id)).unwrap(); - CreatedMailbox::Created(id, uidvalidity) - } - - fn rename_mailbox(&mut self, old_name: &str, new_name: &str) -> Result<()> { - if let Some((uidvalidity, Some(mbid))) = self.get_mailbox(old_name) { - if self.has_mailbox(new_name) { - bail!( - "Cannot rename {} into {}: {} already exists", - old_name, - new_name, - new_name - ); - } - - self.set_mailbox(old_name, None); - self.set_mailbox(new_name, Some(mbid)); - self.update_uidvalidity(new_name, uidvalidity); - Ok(()) - } else { - bail!( - "Cannot rename {} into {}: {} doesn't exist", - old_name, - new_name, - old_name - ); - } - } -} - -enum CreatedMailbox { - Created(UniqueIdent, ImapUidvalidity), - Existed(UniqueIdent, ImapUidvalidity), -} - -// ---- User cache ---- - -lazy_static! { - static ref USER_CACHE: std::sync::Mutex>> = - std::sync::Mutex::new(HashMap::new()); -} diff --git a/src/main.rs b/src/main.rs index 6e3057a..5f5089f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ mod mail; mod server; mod storage; mod timestamp; +mod user; use std::io::Read; use std::path::PathBuf; diff --git a/src/user.rs b/src/user.rs new file mode 100644 index 0000000..a38b9c1 --- /dev/null +++ b/src/user.rs @@ -0,0 +1,313 @@ +use std::collections::{BTreeMap, HashMap}; +use std::sync::{Arc, Weak}; + +use anyhow::{anyhow, bail, Result}; +use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; +use tokio::sync::watch; + +use crate::cryptoblob::{open_deserialize, seal_serialize}; +use crate::login::Credentials; +use crate::mail::incoming::incoming_mail_watch_process; +use crate::mail::mailbox::Mailbox; +use crate::mail::uidindex::ImapUidvalidity; +use crate::mail::unique_ident::{gen_ident, UniqueIdent}; +use crate::storage; +use crate::timestamp::now_msec; + +use crate::mail::namespace::{MAILBOX_HIERARCHY_DELIMITER, INBOX, DRAFTS, ARCHIVE, SENT, TRASH, MAILBOX_LIST_PK, MAILBOX_LIST_SK,MailboxList,CreatedMailbox}; + +//@FIXME User should be totally rewriten +//to extract the local mailbox list +//to the mail/namespace.rs file (and mailbox list should be reworded as mail namespace) + +pub struct User { + pub username: String, + pub creds: Credentials, + pub storage: storage::Store, + pub mailboxes: std::sync::Mutex>>, + + tx_inbox_id: watch::Sender>, +} + +impl User { + pub async fn new(username: String, creds: Credentials) -> Result> { + let cache_key = (username.clone(), creds.storage.unique()); + + { + let cache = USER_CACHE.lock().unwrap(); + if let Some(u) = cache.get(&cache_key).and_then(Weak::upgrade) { + return Ok(u); + } + } + + let user = Self::open(username, creds).await?; + + let mut cache = USER_CACHE.lock().unwrap(); + if let Some(concurrent_user) = cache.get(&cache_key).and_then(Weak::upgrade) { + drop(user); + Ok(concurrent_user) + } else { + cache.insert(cache_key, Arc::downgrade(&user)); + Ok(user) + } + } + + /// Lists user's available mailboxes + pub async fn list_mailboxes(&self) -> Result> { + let (list, _ct) = self.load_mailbox_list().await?; + Ok(list.existing_mailbox_names()) + } + + /// Opens an existing mailbox given its IMAP name. + pub async fn open_mailbox(&self, name: &str) -> Result>> { + let (mut list, ct) = self.load_mailbox_list().await?; + + //@FIXME it could be a trace or an opentelemtry trace thing. + // Be careful to not leak sensible data + /* + eprintln!("List of mailboxes:"); + for ent in list.0.iter() { + eprintln!(" - {:?}", ent); + } + */ + + if let Some((uidvalidity, Some(mbid))) = list.get_mailbox(name) { + let mb = self.open_mailbox_by_id(mbid, uidvalidity).await?; + let mb_uidvalidity = mb.current_uid_index().await.uidvalidity; + if mb_uidvalidity > uidvalidity { + list.update_uidvalidity(name, mb_uidvalidity); + self.save_mailbox_list(&list, ct).await?; + } + Ok(Some(mb)) + } else { + Ok(None) + } + } + + /// Check whether mailbox exists + pub async fn has_mailbox(&self, name: &str) -> Result { + let (list, _ct) = self.load_mailbox_list().await?; + Ok(list.has_mailbox(name)) + } + + /// Creates a new mailbox in the user's IMAP namespace. + pub async fn create_mailbox(&self, name: &str) -> Result<()> { + if name.ends_with(MAILBOX_HIERARCHY_DELIMITER) { + bail!("Invalid mailbox name: {}", name); + } + + let (mut list, ct) = self.load_mailbox_list().await?; + match list.create_mailbox(name) { + CreatedMailbox::Created(_, _) => { + self.save_mailbox_list(&list, ct).await?; + Ok(()) + } + CreatedMailbox::Existed(_, _) => Err(anyhow!("Mailbox {} already exists", name)), + } + } + + /// Deletes a mailbox in the user's IMAP namespace. + pub async fn delete_mailbox(&self, name: &str) -> Result<()> { + if name == INBOX { + bail!("Cannot delete INBOX"); + } + + let (mut list, ct) = self.load_mailbox_list().await?; + if list.has_mailbox(name) { + //@TODO: actually delete mailbox contents + list.set_mailbox(name, None); + self.save_mailbox_list(&list, ct).await?; + Ok(()) + } else { + bail!("Mailbox {} does not exist", name); + } + } + + /// Renames a mailbox in the user's IMAP namespace. + pub async fn rename_mailbox(&self, old_name: &str, new_name: &str) -> Result<()> { + let (mut list, ct) = self.load_mailbox_list().await?; + + if old_name.ends_with(MAILBOX_HIERARCHY_DELIMITER) { + bail!("Invalid mailbox name: {}", old_name); + } + if new_name.ends_with(MAILBOX_HIERARCHY_DELIMITER) { + bail!("Invalid mailbox name: {}", new_name); + } + + if old_name == INBOX { + list.rename_mailbox(old_name, new_name)?; + if !self.ensure_inbox_exists(&mut list, &ct).await? { + self.save_mailbox_list(&list, ct).await?; + } + } else { + let names = list.existing_mailbox_names(); + + let old_name_w_delim = format!("{}{}", old_name, MAILBOX_HIERARCHY_DELIMITER); + let new_name_w_delim = format!("{}{}", new_name, MAILBOX_HIERARCHY_DELIMITER); + + if names + .iter() + .any(|x| x == new_name || x.starts_with(&new_name_w_delim)) + { + bail!("Mailbox {} already exists", new_name); + } + + for name in names.iter() { + if name == old_name { + list.rename_mailbox(name, new_name)?; + } else if let Some(tail) = name.strip_prefix(&old_name_w_delim) { + let nnew = format!("{}{}", new_name_w_delim, tail); + list.rename_mailbox(name, &nnew)?; + } + } + + self.save_mailbox_list(&list, ct).await?; + } + Ok(()) + } + + // ---- Internal user & mailbox management ---- + + async fn open(username: String, creds: Credentials) -> Result> { + let storage = creds.storage.build().await?; + + let (tx_inbox_id, rx_inbox_id) = watch::channel(None); + + let user = Arc::new(Self { + username, + creds: creds.clone(), + storage, + tx_inbox_id, + mailboxes: std::sync::Mutex::new(HashMap::new()), + }); + + // Ensure INBOX exists (done inside load_mailbox_list) + user.load_mailbox_list().await?; + + tokio::spawn(incoming_mail_watch_process( + Arc::downgrade(&user), + user.creds.clone(), + rx_inbox_id, + )); + + Ok(user) + } + + pub(super) async fn open_mailbox_by_id( + &self, + id: UniqueIdent, + min_uidvalidity: ImapUidvalidity, + ) -> Result> { + { + let cache = self.mailboxes.lock().unwrap(); + if let Some(mb) = cache.get(&id).and_then(Weak::upgrade) { + return Ok(mb); + } + } + + let mb = Arc::new(Mailbox::open(&self.creds, id, min_uidvalidity).await?); + + let mut cache = self.mailboxes.lock().unwrap(); + if let Some(concurrent_mb) = cache.get(&id).and_then(Weak::upgrade) { + drop(mb); // we worked for nothing but at least we didn't starve someone else + Ok(concurrent_mb) + } else { + cache.insert(id, Arc::downgrade(&mb)); + Ok(mb) + } + } + + // ---- Mailbox list management ---- + + async fn load_mailbox_list(&self) -> Result<(MailboxList, Option)> { + let row_ref = storage::RowRef::new(MAILBOX_LIST_PK, MAILBOX_LIST_SK); + let (mut list, row) = match self + .storage + .row_fetch(&storage::Selector::Single(&row_ref)) + .await + { + Err(storage::StorageError::NotFound) => (MailboxList::new(), None), + Err(e) => return Err(e.into()), + Ok(rv) => { + let mut list = MailboxList::new(); + let (row_ref, row_vals) = match rv.into_iter().next() { + Some(row_val) => (row_val.row_ref, row_val.value), + None => (row_ref, vec![]), + }; + + for v in row_vals { + if let storage::Alternative::Value(vbytes) = v { + let list2 = + open_deserialize::(&vbytes, &self.creds.keys.master)?; + list.merge(list2); + } + } + (list, Some(row_ref)) + } + }; + + let is_default_mbx_missing = [DRAFTS, ARCHIVE, SENT, TRASH] + .iter() + .map(|mbx| list.create_mailbox(mbx)) + .fold(false, |acc, r| { + acc || matches!(r, CreatedMailbox::Created(..)) + }); + let is_inbox_missing = self.ensure_inbox_exists(&mut list, &row).await?; + if is_default_mbx_missing && !is_inbox_missing { + // It's the only case where we created some mailboxes and not saved them + // So we save them! + self.save_mailbox_list(&list, row.clone()).await?; + } + + Ok((list, row)) + } + + async fn ensure_inbox_exists( + &self, + list: &mut MailboxList, + ct: &Option, + ) -> Result { + // If INBOX doesn't exist, create a new mailbox with that name + // and save new mailbox list. + // Also, ensure that the mpsc::watch that keeps track of the + // inbox id is up-to-date. + let saved; + let (inbox_id, inbox_uidvalidity) = match list.create_mailbox(INBOX) { + CreatedMailbox::Created(i, v) => { + self.save_mailbox_list(list, ct.clone()).await?; + saved = true; + (i, v) + } + CreatedMailbox::Existed(i, v) => { + saved = false; + (i, v) + } + }; + let inbox_id = Some((inbox_id, inbox_uidvalidity)); + if *self.tx_inbox_id.borrow() != inbox_id { + self.tx_inbox_id.send(inbox_id).unwrap(); + } + + Ok(saved) + } + + async fn save_mailbox_list( + &self, + list: &MailboxList, + ct: Option, + ) -> Result<()> { + let list_blob = seal_serialize(list, &self.creds.keys.master)?; + let rref = ct.unwrap_or(storage::RowRef::new(MAILBOX_LIST_PK, MAILBOX_LIST_SK)); + let row_val = storage::RowVal::new(rref, list_blob); + self.storage.row_insert(vec![row_val]).await?; + Ok(()) + } +} + +// ---- User cache ---- + +lazy_static! { + static ref USER_CACHE: std::sync::Mutex>> = + std::sync::Mutex::new(HashMap::new()); +} -- cgit v1.2.3