diff --git a/crates/common/src/network/mta.rs b/crates/common/src/network/mta.rs index 50b3aefb..d0fa9630 100644 --- a/crates/common/src/network/mta.rs +++ b/crates/common/src/network/mta.rs @@ -102,6 +102,9 @@ impl Server { let directory = self .get_directory_for_cached_domain(&domain) .filter(|directory| directory.can_lookup_recipients()); + // inbuxa: #364: why the directory had no answer, logged only if the + // address ends up refused (a mailing list or catch-all may still take it) + let mut directory_miss: Option<(&'static str, String)> = None; if let Some(directory) = directory { let is_subaddressed = local_part.as_ref() != local_part_orig; let address = if is_subaddressed { @@ -115,12 +118,24 @@ impl Server { if self .assert_directory_serves(directory, &account.email) .await - .is_err() => {} + .is_err() => + { + directory_miss = Some(( + "The directory answered with an account on a domain it doesn't serve", + account.email, + )); + } Recipient::Group(group) if self .assert_directory_serves(directory, &group.email) .await - .is_err() => {} + .is_err() => + { + directory_miss = Some(( + "The directory answered with a group on a domain it doesn't serve", + group.email, + )); + } Recipient::Account(account) => { Box::pin(self.synchronize_account(account)).await?; return Ok(if is_subaddressed { @@ -137,7 +152,14 @@ impl Server { RcptResolution::Accept }); } - Recipient::Invalid => {} + Recipient::Invalid => { + directory_miss = Some(( + "The directory found no entry for the address", + directory + .recipient_query(address.as_ref()) + .unwrap_or_default(), + )); + } } } @@ -196,6 +218,16 @@ impl Server { if domain.flags & DOMAIN_FLAG_RELAY != 0 { Ok(RcptResolution::Accept) } else { + if let Some((reason, details)) = directory_miss { + trc::event!( + Smtp(trc::SmtpEvent::DirectoryNoAnswer), + SpanId = session_id, + To = rcpt.to_string(), + Domain = domain_part.to_string(), + Reason = reason, + Details = details, + ); + } Ok(RcptResolution::UnknownRecipient) } } diff --git a/crates/directory/src/backend/ldap/lookup.rs b/crates/directory/src/backend/ldap/lookup.rs index 1ce1e702..d37fda9e 100644 --- a/crates/directory/src/backend/ldap/lookup.rs +++ b/crates/directory/src/backend/ldap/lookup.rs @@ -2,6 +2,8 @@ * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL + * + * Modified by Coffey Labs in 2026 for INBUXA. */ use super::{LdapDirectory, LdapMappings}; @@ -305,3 +307,11 @@ fn result_to_trace(rs: &ResultEntry) -> trc::Value { .collect::>() .into() } + +// inbuxa: #364: the mailbox filter as it runs for an address, for the log +// line that explains a refused recipient +impl LdapDirectory { + pub fn recipient_query(&self, address: &str) -> String { + self.mappings.filter_mailbox.build(address) + } +} diff --git a/crates/directory/src/backend/sql/lookup.rs b/crates/directory/src/backend/sql/lookup.rs index 16cf4f54..5130e87d 100644 --- a/crates/directory/src/backend/sql/lookup.rs +++ b/crates/directory/src/backend/sql/lookup.rs @@ -2,6 +2,8 @@ * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL + * + * Modified by Coffey Labs in 2026 for INBUXA. */ use super::{SqlDirectory, SqlMappings}; @@ -199,3 +201,11 @@ impl SqlMappings { } } } + +// inbuxa: #364: the recipient query, for the log line that explains a +// refused recipient +impl SqlDirectory { + pub fn recipient_query(&self) -> String { + self.mappings.query_recipient.clone() + } +} diff --git a/crates/directory/src/core/dispatch.rs b/crates/directory/src/core/dispatch.rs index 8ecd887a..7e5ca08e 100644 --- a/crates/directory/src/core/dispatch.rs +++ b/crates/directory/src/core/dispatch.rs @@ -2,6 +2,8 @@ * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL + * + * Modified by Coffey Labs in 2026 for INBUXA. */ use crate::{Account, Credentials, Directory, Recipient, backend::oidc::OidcDiscovery}; @@ -29,6 +31,17 @@ impl Directory { .caused_by(trc::location!()) } + /// inbuxa: #364: what a recipient lookup asks the directory (the LDAP + /// mailbox filter for this address, or the SQL query), for the log line + /// that explains a refused recipient. + pub fn recipient_query(&self, address: &str) -> Option { + match &self { + Directory::Ldap(store) => Some(store.recipient_query(address)), + Directory::Sql(store) => Some(store.recipient_query()), + Directory::OpenId(_) | Directory::Unavailable(_) => None, + } + } + pub fn has_bearer_token_support(&self) -> bool { match &self { Directory::OpenId(_) => true, diff --git a/crates/trc/src/event/enums.rs b/crates/trc/src/event/enums.rs index dbe38696..922637eb 100644 --- a/crates/trc/src/event/enums.rs +++ b/crates/trc/src/event/enums.rs @@ -13,10 +13,11 @@ // security.legacy-protocols-changed (LP-8); 644 to 646 are the cluster // coordinator's connection events; 647 and 648 are the audit log's // (audit-hold-lock spec, AU-3, AU-8); 649 is cluster.node-id-reassigned; -// 650 to 659 are inbuxa Relay's (relay spec, RL-23, RL-T23). +// 650 to 659 are inbuxa Relay's (relay spec, RL-23, RL-T23); 660 is +// store.blob-read-through; 661 is smtp.directory-no-answer (#364). // TOTAL_EVENT_COUNT sizes the collector's per-event tables, so it is one // more than the highest id; the test below holds it to the variant list -pub const TOTAL_EVENT_COUNT: usize = 661; +pub const TOTAL_EVENT_COUNT: usize = 662; pub const TOTAL_METRIC_COUNT: usize = 369; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -766,6 +767,9 @@ pub enum SmtpEvent { MailFrom = 444, MultipleMailFrom = 456, MailboxDoesNotExist = 449, + /// inbuxa: #364: the domain's directory was asked about a recipient + /// and had no answer, so the address is refused. + DirectoryNoAnswer = 661, RelayNotAllowed = 468, RcptTo = 464, RcptToDuplicate = 465, diff --git a/crates/trc/src/event/enums_impl.rs b/crates/trc/src/event/enums_impl.rs index 2c2c15fc..cdc946b5 100644 --- a/crates/trc/src/event/enums_impl.rs +++ b/crates/trc/src/event/enums_impl.rs @@ -527,6 +527,8 @@ impl EventType { b"smtp.mail-from" => EventType::Smtp(SmtpEvent::MailFrom), b"smtp.multiple-mail-from" => EventType::Smtp(SmtpEvent::MultipleMailFrom), b"smtp.mailbox-does-not-exist" => EventType::Smtp(SmtpEvent::MailboxDoesNotExist), + // inbuxa: #364 + b"smtp.directory-no-answer" => EventType::Smtp(SmtpEvent::DirectoryNoAnswer), b"smtp.relay-not-allowed" => EventType::Smtp(SmtpEvent::RelayNotAllowed), b"smtp.rcpt-to" => EventType::Smtp(SmtpEvent::RcptTo), b"smtp.rcpt-to-duplicate" => EventType::Smtp(SmtpEvent::RcptToDuplicate), @@ -1322,6 +1324,8 @@ impl EventType { EventType::Smtp(SmtpEvent::MailFrom) => "smtp.mail-from", EventType::Smtp(SmtpEvent::MultipleMailFrom) => "smtp.multiple-mail-from", EventType::Smtp(SmtpEvent::MailboxDoesNotExist) => "smtp.mailbox-does-not-exist", + // inbuxa: #364 + EventType::Smtp(SmtpEvent::DirectoryNoAnswer) => "smtp.directory-no-answer", EventType::Smtp(SmtpEvent::RelayNotAllowed) => "smtp.relay-not-allowed", EventType::Smtp(SmtpEvent::RcptTo) => "smtp.rcpt-to", EventType::Smtp(SmtpEvent::RcptToDuplicate) => "smtp.rcpt-to-duplicate", @@ -2014,6 +2018,8 @@ impl EventType { EventType::Smtp(SmtpEvent::MailFrom) => 444, EventType::Smtp(SmtpEvent::MultipleMailFrom) => 456, EventType::Smtp(SmtpEvent::MailboxDoesNotExist) => 449, + // inbuxa: #364 + EventType::Smtp(SmtpEvent::DirectoryNoAnswer) => 661, EventType::Smtp(SmtpEvent::RelayNotAllowed) => 468, EventType::Smtp(SmtpEvent::RcptTo) => 464, EventType::Smtp(SmtpEvent::RcptToDuplicate) => 465, @@ -2724,6 +2730,8 @@ impl EventType { 444 => Some(EventType::Smtp(SmtpEvent::MailFrom)), 456 => Some(EventType::Smtp(SmtpEvent::MultipleMailFrom)), 449 => Some(EventType::Smtp(SmtpEvent::MailboxDoesNotExist)), + // inbuxa: #364 + 661 => Some(EventType::Smtp(SmtpEvent::DirectoryNoAnswer)), 468 => Some(EventType::Smtp(SmtpEvent::RelayNotAllowed)), 464 => Some(EventType::Smtp(SmtpEvent::RcptTo)), 465 => Some(EventType::Smtp(SmtpEvent::RcptToDuplicate)), @@ -3121,6 +3129,8 @@ impl EventType { EventType::Smtp(SmtpEvent::InvalidEhlo) => Level::Info, EventType::Smtp(SmtpEvent::MailFrom) => Level::Info, EventType::Smtp(SmtpEvent::MailboxDoesNotExist) => Level::Info, + // inbuxa: #364 + EventType::Smtp(SmtpEvent::DirectoryNoAnswer) => Level::Info, EventType::Smtp(SmtpEvent::RelayNotAllowed) => Level::Info, EventType::Smtp(SmtpEvent::RcptTo) => Level::Info, EventType::Smtp(SmtpEvent::RcptToGreylisted) => Level::Info, @@ -3915,6 +3925,10 @@ impl EventType { EventType::Smtp(SmtpEvent::MailFrom) => "SMTP MAIL FROM command", EventType::Smtp(SmtpEvent::MultipleMailFrom) => "Multiple MAIL FROM commands", EventType::Smtp(SmtpEvent::MailboxDoesNotExist) => "Mailbox does not exist", + // inbuxa: #364 + EventType::Smtp(SmtpEvent::DirectoryNoAnswer) => { + "Directory had no answer for a recipient" + } EventType::Smtp(SmtpEvent::RelayNotAllowed) => "Relay not allowed", EventType::Smtp(SmtpEvent::RcptTo) => "SMTP RCPT TO command", EventType::Smtp(SmtpEvent::RcptToDuplicate) => "Duplicate RCPT TO", @@ -4299,6 +4313,8 @@ impl EventType { EventType::Smtp(SmtpEvent::MailFrom) => "SMTP error", EventType::Smtp(SmtpEvent::MultipleMailFrom) => "SMTP error", EventType::Smtp(SmtpEvent::MailboxDoesNotExist) => "SMTP error", + // inbuxa: #364 + EventType::Smtp(SmtpEvent::DirectoryNoAnswer) => "SMTP error", EventType::Smtp(SmtpEvent::RelayNotAllowed) => "SMTP error", EventType::Smtp(SmtpEvent::RcptTo) => "SMTP error", EventType::Smtp(SmtpEvent::RcptToDuplicate) => "SMTP error", @@ -4903,6 +4919,8 @@ impl EventType { EventType::Smtp(SmtpEvent::MailFrom), EventType::Smtp(SmtpEvent::MultipleMailFrom), EventType::Smtp(SmtpEvent::MailboxDoesNotExist), + // inbuxa: #364 + EventType::Smtp(SmtpEvent::DirectoryNoAnswer), EventType::Smtp(SmtpEvent::RelayNotAllowed), EventType::Smtp(SmtpEvent::RcptTo), EventType::Smtp(SmtpEvent::RcptToDuplicate), diff --git a/resources/schema/schema.json.gz b/resources/schema/schema.json.gz index 99cc43db..61c87882 100644 Binary files a/resources/schema/schema.json.gz and b/resources/schema/schema.json.gz differ diff --git a/resources/schema/schema.json.sha256 b/resources/schema/schema.json.sha256 index 074b1a17..df965f57 100644 --- a/resources/schema/schema.json.sha256 +++ b/resources/schema/schema.json.sha256 @@ -1 +1 @@ -sJaguWUKSQwNzEBKsAaYBmqxPQqFJciaHs1EPsPfylI \ No newline at end of file +d5R3iz87YIYY8m_r62Kd4-dlqkkVUHt1lz7rKB9fBO0 \ No newline at end of file diff --git a/tests/src/directory/per_domain.rs b/tests/src/directory/per_domain.rs index 33345bad..6480af06 100644 --- a/tests/src/directory/per_domain.rs +++ b/tests/src/directory/per_domain.rs @@ -24,7 +24,8 @@ use registry::schema::{ }, }; use serde_json::json; -use std::net::IpAddr; +use std::{net::IpAddr, time::Duration}; +use trc::{Collector, EventType, Key, SmtpEvent, ipc::subscriber::SubscriberBuilder}; use types::id::Id; /// An SQLite directory database with its rows. @@ -154,6 +155,48 @@ async fn rcpt(address: &str) -> char { .unwrap_or('?') } +/// RCPT for an address, with the reason and details of the +/// smtp.directory-no-answer event it raised, if any (#364). +async fn rcpt_with_miss(address: &str) -> (char, Option<(String, String)>) { + let subscriber_id = format!("directory-no-answer-{address}"); + let subscriber = SubscriberBuilder::new(subscriber_id.clone()) + .set_interests([EventType::Smtp(SmtpEvent::DirectoryNoAnswer)]) + .with_lossy(false); + Collector::union_interests(subscriber.interests.clone()); + let (_, mut events) = subscriber.register(); + tokio::time::timeout(Duration::from_secs(5), async { + while !Collector::get_subscribers().contains(&subscriber_id) { + tokio::time::sleep(Duration::from_millis(10)).await; + } + }) + .await + .expect("subscriber was not registered"); + + let reply = rcpt(address).await; + let miss = tokio::time::timeout(Duration::from_secs(2), async { + loop { + for event in events.recv().await.expect("subscriber closed") { + if event.value_as_str(Key::To) == Some(address) { + return ( + event + .value_as_str(Key::Reason) + .unwrap_or_default() + .to_string(), + event + .value_as_str(Key::Details) + .unwrap_or_default() + .to_string(), + ); + } + } + } + }) + .await + .ok(); + Collector::remove_subscriber(subscriber_id); + (reply, miss) +} + /// `cargo test -p tests per_domain_directory_tests -- --ignored`. #[ignore] #[tokio::test(flavor = "multi_thread")] @@ -218,6 +261,7 @@ pub async fn per_domain_directory_tests() { "INSERT INTO emails (name, address) VALUES ('alice@a.test', 'alice@b.test')", "INSERT INTO group_members (name, member_of) VALUES ('alice@a.test', 'team@a.test')", "INSERT INTO group_members (name, member_of) VALUES ('alice@a.test', 'crew@b.test')", + "INSERT INTO emails (name, address) VALUES ('mallory@b.test', 'eve@e.test')", ], ) .await; @@ -250,6 +294,16 @@ pub async fn per_domain_directory_tests() { .registry_create_object(domain("b.test", Some(dir_b))) .await; let c = admin.registry_create_object(domain("c.test", None)).await; + // e.test: A's data, looked up by alias, so an alias of a b.test entry + // answers for an e.test address (#364, DIR-6 at RCPT) + let mut by_alias = sql_directory(&path_a, "E"); + if let structs::Directory::Sql(sql) = &mut by_alias { + sql.query_recipient = "SELECT a.name, a.secret, a.description, a.type FROM accounts a JOIN emails e ON e.name = a.name WHERE e.address = $1 AND a.active = true".into(); + } + let dir_e = admin.registry_create_object(by_alias).await; + admin + .registry_create_object(domain("e.test", Some(dir_e))) + .await; // d.test gets its broken directory after dora's internal password exists, // since passwords can't be set on a directory domain (DIR-13) let d = admin.registry_create_object(domain("d.test", None)).await; @@ -366,7 +420,33 @@ pub async fn per_domain_directory_tests() { assert!(account_of(&test, "dave@a.test").await.is_none()); assert_eq!(rcpt("dave@a.test").await, '2', "test 7"); assert!(account_of(&test, "dave@a.test").await.is_some(), "test 7"); - assert_eq!(rcpt("ghost@a.test").await, '5', "test 7"); + // #364: the refusal says why, at info level: no entry, with the query + let (reply, miss) = rcpt_with_miss("ghost@a.test").await; + assert_eq!(reply, '5', "test 7"); + let (reason, details) = miss.expect("#364: no-entry miss logged"); + assert_eq!( + reason, "The directory found no entry for the address", + "#364" + ); + assert!(details.contains("WHERE name = $1"), "#364: {details}"); + // #364: an answer for a domain the directory doesn't serve is dropped, + // and that is logged with the address it answered with + let (reply, miss) = rcpt_with_miss("eve@e.test").await; + assert_eq!(reply, '5', "#364: DIR-6 at RCPT"); + let (reason, details) = miss.expect("#364: DIR-6 miss logged"); + assert_eq!( + reason, "The directory answered with an account on a domain it doesn't serve", + "#364" + ); + assert_eq!(details, "mallory@b.test", "#364"); + assert!(account_of(&test, "mallory@b.test").await.is_none(), "#364"); + // An accepted recipient raises nothing + let (reply, miss) = rcpt_with_miss("dave@a.test").await; + assert_eq!(reply, '2', "#364"); + assert!( + miss.is_none(), + "#364: nothing logged for an accepted address" + ); // Test 11: no password change on a directory account (DIR-13) let refused = admin