crymap 2.0.1

A simple, secure IMAP server with encrypted data at rest
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//-
// Copyright (c) 2020, 2023, 2024, 2025, Jason Lingle
//
// This file is part of Crymap.
//
// Crymap is free software: you can  redistribute it and/or modify it under the
// terms of  the GNU General Public  License as published by  the Free Software
// Foundation, either version  3 of the License, or (at  your option) any later
// version.
//
// Crymap is distributed  in the hope that  it will be useful,  but WITHOUT ANY
// WARRANTY; without  even the implied  warranty of MERCHANTABILITY  or FITNESS
// FOR  A PARTICULAR  PURPOSE.  See the  GNU General  Public  License for  more
// details.
//
// You should have received a copy of the GNU General Public License along with
// Crymap. If not, see <http://www.gnu.org/licenses/>.

use std::str::FromStr;

use lazy_static::lazy_static;
use regex::Regex;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Command {
    /// (HELO|EHLO|LHLO) origin-host ignored...
    Helo(String, String),
    /// AUTH mechanism [base64]
    Auth(String, Option<String>),
    /// MAIL FROM:<return-path> [SIZE=sz] [BODY=encoding]
    /// The final element is a list of warnings.
    MailFrom(String, Option<u64>, Vec<String>),
    /// RCPT TO:<ignored...:email>
    /// The final element is a list of warnings.
    Recipient(String, Vec<String>),
    /// DATA
    Data,
    /// BDAT length [LAST]
    BinaryData(u64, bool),
    /// RSET
    Reset,
    /// VRFY ignored...
    Verify,
    /// EXPN ignored...
    Expand,
    /// HELP ignored...
    Help,
    /// NOOP
    Noop,
    /// QUIT
    Quit,
    /// STARTTLS
    StartTls,
    /// Anything that looks like a common HTTP command.
    Http,
}

const MAX_WARNINGS: usize = 4;

static SIMPLE_COMMANDS: &[(&str, Command, bool)] = &[
    ("DATA", Command::Data, false),
    ("RSET", Command::Reset, false),
    ("VRFY ", Command::Verify, true),
    ("EXPN ", Command::Expand, true),
    ("HELP", Command::Help, true),
    ("NOOP", Command::Noop, false),
    ("QUIT", Command::Quit, false),
    ("STARTTLS", Command::StartTls, false),
    ("GET", Command::Http, true),
    ("HEAD", Command::Http, true),
    ("PUT", Command::Http, true),
    ("POST", Command::Http, true),
    ("DELETE", Command::Http, true),
    ("OPTIONS", Command::Http, true),
];

lazy_static! {
    static ref RX_HELO: Regex =
        Regex::new("^(?i)(HELO|EHLO|LHLO) ([^ ]*)").unwrap();
    static ref RX_MAIL: Regex =
        Regex::new("^(?i)MAIL FROM:<([^>]*)>(.*)$").unwrap();
    static ref RX_MAIL_IGNORED_PARM: Regex =
        Regex::new("^(?i)BODY=(7BIT|8BITMIME|BINARYMIME)|SMTPUTF8$").unwrap();
    static ref RX_MAIL_SIZE_PARM: Regex =
        Regex::new("^(?i)SIZE=([0-9]+)$").unwrap();
    static ref RX_RCPT: Regex =
        Regex::new("^(?i)RCPT TO:<(?:@[^:]+:)?([^>]+)>(.*)$").unwrap();
    static ref RX_BDAT: Regex =
        Regex::new("^(?i)BDAT ([0-9]+)( LAST)?$").unwrap();
    static ref RX_AUTH: Regex =
        Regex::new("^(?i)AUTH ([A-Z0-9-]+)(?: ([0-9A-Za-z+/=]+))?$").unwrap();
    static ref RX_KNOWN_COMMANDS: Regex = Regex::new(
        "^(?i)(DATA|RSET|VRFY|EXPN|HELP|NOOP|QUIT|\
         STARTTLS|LHLO|MAIL|RCPT|BDAT|HELO|EHLO|AUTH)( .*)?$"
    )
    .unwrap();
}

pub fn looks_like_known_command(s: &str) -> bool {
    RX_KNOWN_COMMANDS.is_match(s)
}

impl FromStr for Command {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, ()> {
        let mut warnings = Vec::<String>::new();
        let mut add_warning = |w: String| {
            if warnings.len() < MAX_WARNINGS {
                warnings.push(w);
            }
        };

        for &(prefix, ref cmd, allow_trailing_garbage) in SIMPLE_COMMANDS {
            if s.len() >= prefix.len()
                && (allow_trailing_garbage || s.len() == prefix.len())
                && s.get(0..prefix.len())
                    .is_some_and(|sp| prefix.eq_ignore_ascii_case(sp))
            {
                return Ok(cmd.clone());
            }
        }

        if let Some(cap) = RX_HELO.captures(s) {
            Ok(Command::Helo(
                cap.get(1).unwrap().as_str().to_owned(),
                cap.get(2).unwrap().as_str().to_owned(),
            ))
        } else if let Some(cap) = RX_MAIL.captures(s) {
            let mut size = None::<u64>;
            for parm in cap
                .get(2)
                .map(|c| c.as_str())
                .unwrap_or("")
                .split(' ')
                .filter(|s| !s.is_empty())
            {
                let truncated_parm = &parm[..parm
                    .char_indices()
                    .nth(64)
                    .map(|(ix, _)| ix)
                    .unwrap_or(parm.len())];
                if let Some(cap) = RX_MAIL_SIZE_PARM.captures(parm) {
                    if let Some(s) =
                        cap.get(1).and_then(|c| c.as_str().parse::<u64>().ok())
                    {
                        size = Some(s);
                    } else {
                        add_warning(format!(
                            "Ignoring invalid MAIL FROM parameter {:?}",
                            truncated_parm,
                        ));
                    }
                } else if !RX_MAIL_IGNORED_PARM.is_match(parm) {
                    add_warning(format!(
                        "Ignoring unknown MAIL FROM parameter {:?}",
                        truncated_parm,
                    ));
                }
            }

            Ok(Command::MailFrom(
                cap.get(1).unwrap().as_str().to_owned(),
                size,
                warnings,
            ))
        } else if let Some(cap) = RX_RCPT.captures(s) {
            if let Some(extra) = cap.get(2).filter(|c| !c.as_str().is_empty()) {
                let extra = extra.as_str().trim();
                let extra = &extra[..extra
                    .char_indices()
                    .nth(64)
                    .map(|(ix, _)| ix)
                    .unwrap_or(extra.len())];
                add_warning(format!(
                    "Ignoring extraneous RCPT TO parameters: {extra:?}"
                ));
            };

            Ok(Command::Recipient(
                cap.get(1).unwrap().as_str().to_owned(),
                warnings,
            ))
        } else if let Some(cap) = RX_BDAT.captures(s) {
            cap.get(1)
                .unwrap()
                .as_str()
                .parse::<u64>()
                .map_err(|_| ())
                .map(|len| Command::BinaryData(len, cap.get(2).is_some()))
        } else if let Some(cap) = RX_AUTH.captures(s) {
            let mechanism = cap.get(1).unwrap().as_str().to_owned();
            let data = cap.get(2).map(|data| data.as_str().to_owned());
            Ok(Command::Auth(mechanism, data))
        } else {
            Err(())
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn command_parsing() {
        assert_eq!(
            Ok(Command::Helo(
                "LHLO".to_owned(),
                "foo.example.com".to_owned()
            )),
            "LHLO foo.example.com".parse()
        );
        assert_eq!(
            Ok(Command::Helo(
                "lhlo".to_owned(),
                "foo.example.com".to_owned()
            )),
            "lhlo foo.example.com some client implementation".parse()
        );

        assert_eq!(
            Ok(Command::Helo(
                "HELO".to_owned(),
                "foo.example.com".to_owned()
            )),
            "HELO foo.example.com".parse()
        );
        assert_eq!(
            Ok(Command::Helo(
                "helo".to_owned(),
                "foo.example.com".to_owned()
            )),
            "helo foo.example.com some client implementation".parse()
        );

        assert_eq!(
            Ok(Command::Helo(
                "EHLO".to_owned(),
                "foo.example.com".to_owned()
            )),
            "EHLO foo.example.com".parse()
        );
        assert_eq!(
            Ok(Command::Helo(
                "ehlo".to_owned(),
                "foo.example.com".to_owned()
            )),
            "ehlo foo.example.com some client implementation".parse()
        );

        assert_eq!(
            Ok(Command::MailFrom("foo@bar.com".to_owned(), None, vec![])),
            "MAIL FROM:<foo@bar.com>".parse()
        );
        assert_eq!(
            Ok(Command::MailFrom("foo@bar.com".to_owned(), None, vec![])),
            "MAIL FROM:<foo@bar.com> BODY=BiNaRyMiMe".parse()
        );
        assert_eq!(
            Ok(Command::MailFrom("foo@bar.com".to_owned(), None, vec![])),
            "MAIL FROM:<foo@bar.com> body=8bitmime".parse()
        );
        assert_eq!(
            Ok(Command::MailFrom("foo@bar.com".to_owned(), None, vec![])),
            "MAIL FROM:<foo@bar.com> body=7bit".parse()
        );
        assert_eq!(
            Ok(Command::MailFrom(
                "foo@bar.com".to_owned(),
                None,
                vec!["Ignoring unknown MAIL FROM parameter \"body=9BIT\""
                    .to_owned()],
            )),
            "MAIL FROM:<foo@bar.com> body=9BIT".parse()
        );
        assert_eq!(
            Ok(Command::MailFrom("foo@bar.com".to_owned(), None, vec![])),
            "MAIL FROM:<foo@bar.com> SmTpUtF8".parse(),
        );
        assert_eq!(
            Ok(Command::MailFrom(
                "foo@bar.com".to_owned(),
                None,
                vec!["Ignoring unknown MAIL FROM parameter \"smtputf8=foo\""
                    .to_owned()],
            )),
            "MAIL FROM:<foo@bar.com> smtputf8=foo".parse(),
        );
        assert_eq!(
            Ok(Command::MailFrom(
                "foo@bar.com".to_owned(),
                Some(42),
                vec![]
            )),
            "MAIL FROM:<foo@bar.com> SIZE=42".parse()
        );
        assert_eq!(
            Ok(Command::MailFrom(
                "foo@bar.com".to_owned(),
                Some(42),
                vec![]
            )),
            "MAIL FROM:<foo@bar.com> body=7bit size=42".parse()
        );
        assert_eq!(
            Ok(Command::MailFrom(
                "foo@bar.com".to_owned(),
                Some(42),
                vec![]
            )),
            "MAIL FROM:<foo@bar.com> size=42 body=7bit".parse()
        );
        assert_eq!(
            Ok(Command::MailFrom(String::new(), None, vec![])),
            "mail from:<>".parse()
        );
        assert_eq!(
            Ok(Command::MailFrom(
                "foo@bar.com".to_owned(),
                None,
                vec!["Ignoring invalid MAIL FROM parameter \"size=99999999999999999999\"".to_owned()],
            )),
            "MAIL FROM:<foo@bar.com> size=99999999999999999999".parse::<Command>()
        );
        assert_eq!(
            Ok(Command::MailFrom(
                "foo@bar.com".to_owned(),
                None,
                vec!["Ignoring unknown MAIL FROM parameter \"FOO=BAR\""
                    .to_owned()],
            )),
            "MAIL FROM:<foo@bar.com> FOO=BAR".parse::<Command>()
        );

        assert_eq!(
            Ok(Command::Recipient("userc@d.bar.org".to_owned(), vec![])),
            "RCPT TO:<userc@d.bar.org>".parse()
        );
        assert_eq!(
            Ok(Command::Recipient("userc@d.bar.org".to_owned(), vec![])),
            "rcpt to:<@hosta.int,@jkl.org:userc@d.bar.org>".parse()
        );
        assert_eq!(
            Ok(Command::Recipient(
                "userc@d.bar.org".to_owned(),
                vec!["Ignoring extraneous RCPT TO parameters: \"FOO=BAR\""
                    .to_owned()],
            )),
            "RCPT TO:<userc@d.bar.org> FOO=BAR".parse()
        );

        assert_eq!(Ok(Command::Data), "DATA".parse());
        assert_eq!(Ok(Command::Data), "data".parse());
        assert_eq!(Err(()), "DATA DATA".parse::<Command>());
        assert_eq!(Err(()), "DATABASE".parse::<Command>());

        assert_eq!(Ok(Command::BinaryData(42, false)), "BDAT 42".parse());
        assert_eq!(
            Ok(Command::BinaryData(1000, true)),
            "BDAT 1000 LAST".parse()
        );
        assert_eq!(Ok(Command::BinaryData(1, true)), "bdat 1 last".parse());

        assert_eq!(Ok(Command::Reset), "RSET".parse());
        assert_eq!(Err(()), "RSET FOO".parse::<Command>());

        assert_eq!(Ok(Command::Verify), "VRFY Smith".parse());
        assert_eq!(Ok(Command::Verify), "vrfy <foo@bar.com>".parse());
        assert_eq!(Err(()), "VRFY".parse::<Command>());

        assert_eq!(Ok(Command::Expand), "EXPN Smith".parse());
        assert_eq!(Ok(Command::Expand), "EXPN <foo@bar.com>".parse());
        assert_eq!(Err(()), "EXPN".parse::<Command>());

        assert_eq!(Ok(Command::Help), "HELP".parse());
        assert_eq!(Ok(Command::Help), "help me".parse());

        assert_eq!(Ok(Command::Noop), "NOOP".parse());
        assert_eq!(Err(()), "NOOP NOP".parse::<Command>());

        assert_eq!(Ok(Command::Quit), "QUIT".parse());
        assert_eq!(Err(()), "QUIT NOW".parse::<Command>());

        assert_eq!(Ok(Command::StartTls), "STARTTLS".parse());
        assert_eq!(Err(()), "STARTTLS 1.3".parse::<Command>());

        assert_eq!(
            Ok(Command::Auth(
                "PLAIN".to_owned(),
                Some("AGF6dXJlAGh1bnRlcjI+//=".to_owned()),
            )),
            "AUTH PLAIN AGF6dXJlAGh1bnRlcjI+//=".parse::<Command>(),
        );
        assert_eq!(
            Ok(Command::Auth("NTLM".to_owned(), None)),
            "auth NTLM".parse::<Command>(),
        );

        assert_eq!(Ok(Command::Http), "GET / HTTP/1.0".parse());
        assert_eq!(Ok(Command::Http), "HEAD /favicon HTTP/1.1".parse());
        assert_eq!(
            Ok(Command::Http),
            "PUT /../../../etc/passwd HTTP/1.0".parse()
        );
        assert_eq!(Ok(Command::Http), "POST /adminmyphp HTTP/1.2".parse());
        assert_eq!(Ok(Command::Http), "DELETE /bar HTTP/1.0".parse());
        assert_eq!(Ok(Command::Http), "OPTIONS * HTTP/1.1".parse());
    }
}