-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathreport.rs
284 lines (258 loc) · 9.41 KB
/
report.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <[email protected]>
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/
use std::time::Duration;
use utils::config::{utils::ParseValue, Config};
use crate::expr::{if_block::IfBlock, tokenizer::TokenMap, Constant, ConstantValue, Variable};
use super::*;
#[derive(Clone)]
pub struct ReportConfig {
pub submitter: IfBlock,
pub analysis: ReportAnalysis,
pub dkim: Report,
pub spf: Report,
pub dmarc: Report,
pub dmarc_aggregate: AggregateReport,
pub tls: AggregateReport,
}
#[derive(Clone)]
pub struct ReportAnalysis {
pub addresses: Vec<AddressMatch>,
pub forward: bool,
pub store: Option<Duration>,
}
#[derive(Clone)]
pub enum AddressMatch {
StartsWith(String),
EndsWith(String),
Equals(String),
}
#[derive(Clone)]
pub struct AggregateReport {
pub name: IfBlock,
pub address: IfBlock,
pub org_name: IfBlock,
pub contact_info: IfBlock,
pub send: IfBlock,
pub sign: IfBlock,
pub max_size: IfBlock,
}
#[derive(Clone)]
pub struct Report {
pub name: IfBlock,
pub address: IfBlock,
pub subject: IfBlock,
pub sign: IfBlock,
pub send: IfBlock,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum AggregateFrequency {
Hourly,
Daily,
Weekly,
#[default]
Never,
}
impl ReportConfig {
pub fn parse(config: &mut Config) -> Self {
let sender_vars = TokenMap::default().with_variables(SMTP_MAIL_FROM_VARS);
let rcpt_vars = TokenMap::default().with_variables(SMTP_RCPT_TO_VARS);
Self {
submitter: IfBlock::try_parse(
config,
"report.submitter",
&TokenMap::default().with_variables(RCPT_DOMAIN_VARS),
)
.unwrap_or_else(|| {
IfBlock::new::<()>("report.submitter", [], "key_get('default', 'hostname')")
}),
analysis: ReportAnalysis {
addresses: config
.properties::<AddressMatch>("report.analysis.addresses")
.into_iter()
.map(|(_, m)| m)
.collect(),
forward: config.property("report.analysis.forward").unwrap_or(true),
store: config
.property_or_default::<Option<Duration>>("report.analysis.store", "30d")
.unwrap_or_default(),
},
dkim: Report::parse(config, "dkim", &rcpt_vars),
spf: Report::parse(config, "spf", &sender_vars),
dmarc: Report::parse(config, "dmarc", &rcpt_vars),
dmarc_aggregate: AggregateReport::parse(
config,
"dmarc",
&rcpt_vars.with_constants::<AggregateFrequency>(),
),
tls: AggregateReport::parse(
config,
"tls",
&TokenMap::default()
.with_variables(SMTP_QUEUE_HOST_VARS)
.with_constants::<AggregateFrequency>(),
),
}
}
}
impl Report {
pub fn parse(config: &mut Config, id: &str, token_map: &TokenMap) -> Self {
let mut report = Self {
name: IfBlock::new::<()>(format!("report.{id}.from-name"), [], "'Report Subsystem'"),
address: IfBlock::new::<()>(
format!("report.{id}.from-address"),
[],
format!("'noreply-{id}@' + key_get('default', 'domain')"),
),
subject: IfBlock::new::<()>(
format!("report.{id}.subject"),
[],
format!(
"'{} Authentication Failure Report'",
id.to_ascii_uppercase()
),
),
sign: IfBlock::new::<()>(
format!("report.{id}.sign"),
[],
"['rsa-' + key_get('default', 'domain'), 'ed25519-' + key_get('default', 'domain')]",
),
send: IfBlock::new::<()>(format!("report.{id}.send"), [], "[1, 1d]"),
};
for (value, key) in [
(&mut report.name, "from-name"),
(&mut report.address, "from-address"),
(&mut report.subject, "subject"),
(&mut report.sign, "sign"),
(&mut report.send, "send"),
] {
if let Some(if_block) = IfBlock::try_parse(config, ("report", id, key), token_map) {
*value = if_block;
}
}
report
}
}
impl AggregateReport {
pub fn parse(config: &mut Config, id: &str, token_map: &TokenMap) -> Self {
let rcpt_vars = TokenMap::default().with_variables(RCPT_DOMAIN_VARS);
let mut report = Self {
name: IfBlock::new::<()>(
format!("report.{id}.aggregate.from-name"),
[],
format!("'{} Aggregate Report'", id.to_ascii_uppercase()),
),
address: IfBlock::new::<()>(
format!("report.{id}.aggregate.from-address"),
[],
format!("'noreply-{id}@' + key_get('default', 'domain')"),
),
org_name: IfBlock::new::<()>(
format!("report.{id}.aggregate.org-name"),
[],
"key_get('default', 'domain')",
),
contact_info: IfBlock::empty(format!("report.{id}.aggregate.contact-info")),
send: IfBlock::new::<AggregateFrequency>(
format!("report.{id}.aggregate.send"),
[],
"daily",
),
sign: IfBlock::new::<()>(
format!("report.{id}.aggregate.sign"),
[],
"['rsa-' + key_get('default', 'domain'), 'ed25519-' + key_get('default', 'domain')]",
),
max_size: IfBlock::new::<()>(format!("report.{id}.aggregate.max-size"), [], "26214400"),
};
for (value, key, token_map) in [
(&mut report.name, "aggregate.from-name", &rcpt_vars),
(&mut report.address, "aggregate.from-address", &rcpt_vars),
(&mut report.org_name, "aggregate.org-name", &rcpt_vars),
(
&mut report.contact_info,
"aggregate.contact-info",
&rcpt_vars,
),
(&mut report.send, "aggregate.send", token_map),
(&mut report.sign, "aggregate.sign", &rcpt_vars),
(&mut report.max_size, "aggregate.max-size", &rcpt_vars),
] {
if let Some(if_block) = IfBlock::try_parse(config, ("report", id, key), token_map) {
*value = if_block;
}
}
report
}
}
impl Default for ReportConfig {
fn default() -> Self {
Self::parse(&mut Config::default())
}
}
impl ParseValue for AggregateFrequency {
fn parse_value(value: &str) -> Result<Self, String> {
match value {
"daily" | "day" => Ok(AggregateFrequency::Daily),
"hourly" | "hour" => Ok(AggregateFrequency::Hourly),
"weekly" | "week" => Ok(AggregateFrequency::Weekly),
"never" | "disable" | "false" => Ok(AggregateFrequency::Never),
_ => Err(format!("Invalid aggregate frequency value {value:?}.",)),
}
}
}
impl From<AggregateFrequency> for Constant {
fn from(value: AggregateFrequency) -> Self {
match value {
AggregateFrequency::Never => 0.into(),
AggregateFrequency::Hourly => 2.into(),
AggregateFrequency::Daily => 3.into(),
AggregateFrequency::Weekly => 4.into(),
}
}
}
impl<'x> TryFrom<Variable<'x>> for AggregateFrequency {
type Error = ();
fn try_from(value: Variable<'x>) -> Result<Self, Self::Error> {
match value {
Variable::Integer(0) => Ok(AggregateFrequency::Never),
Variable::Integer(2) => Ok(AggregateFrequency::Hourly),
Variable::Integer(3) => Ok(AggregateFrequency::Daily),
Variable::Integer(4) => Ok(AggregateFrequency::Weekly),
_ => Err(()),
}
}
}
impl ConstantValue for AggregateFrequency {
fn add_constants(token_map: &mut crate::expr::tokenizer::TokenMap) {
token_map
.add_constant("never", AggregateFrequency::Never)
.add_constant("hourly", AggregateFrequency::Hourly)
.add_constant("hour", AggregateFrequency::Hourly)
.add_constant("daily", AggregateFrequency::Daily)
.add_constant("day", AggregateFrequency::Daily)
.add_constant("weekly", AggregateFrequency::Weekly)
.add_constant("week", AggregateFrequency::Weekly)
.add_constant("never", AggregateFrequency::Never)
.add_constant("disable", AggregateFrequency::Never)
.add_constant("false", AggregateFrequency::Never);
}
}
impl ParseValue for AddressMatch {
fn parse_value(value: &str) -> Result<Self, String> {
if let Some(value) = value.strip_prefix('*').map(|v| v.trim()) {
if !value.is_empty() {
return Ok(AddressMatch::EndsWith(value.to_lowercase()));
}
} else if let Some(value) = value.strip_suffix('*').map(|v| v.trim()) {
if !value.is_empty() {
return Ok(AddressMatch::StartsWith(value.to_lowercase()));
}
} else if value.contains('@') {
return Ok(AddressMatch::Equals(value.trim().to_lowercase()));
}
Err(format!("Invalid address match value {value:?}.",))
}
}