crymap 1.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//-
// Copyright (c) 2020, 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::fs;
use std::io::{self, BufRead, Read, Write};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, Weak};

use lazy_static::lazy_static;
use openssl::{
    pkey,
    ssl::{SslAcceptor, SslConnector, SslMethod, SslVerifyMode},
    x509,
};
use rayon::prelude::*;
use tempfile::TempDir;

use super::server::*;
use crate::account::account::Account;
use crate::account::model::Uid;
use crate::crypt::master_key::MasterKey;
use crate::support::{
    append_limit::APPEND_SIZE_LIMIT, error::Error, rcio::RcIo,
    system_config::SystemConfig,
};

// Similar to the IMAP integration tests, we share a system directory between
// the tests since accounts are expensive to set up, and the sharing works as
// long as the tests are run concurrently.
//
// The test system has three user accounts: dib, gäz, and zim. One extra,
// "gir", is created initially but is destroyed by one of the tests.
lazy_static! {
    static ref SYSTEM_DIR: Mutex<Weak<TempDir>> = Mutex::new(Weak::new());
}

#[derive(Clone, Debug)]
struct Setup {
    system_dir: Arc<TempDir>,
}

fn set_up() -> Setup {
    crate::init_test_log();

    let mut lock = SYSTEM_DIR.lock().unwrap();

    if let Some(system_dir) = lock.upgrade() {
        return Setup { system_dir };
    }

    let setup = set_up_new_root();
    *lock = Arc::downgrade(&setup.system_dir);
    setup
}

fn set_up_new_root() -> Setup {
    let system_dir = Arc::new(TempDir::new().unwrap());

    vec!["dib", "gäz", "zim", "gir"]
        .into_par_iter()
        .for_each(|user_name| {
            let user_dir = system_dir.path().join(user_name);
            fs::create_dir(&user_dir).unwrap();

            let account = Account::new(
                "initial-setup".to_owned(),
                user_dir,
                Some(Arc::new(MasterKey::new())),
            );
            account.provision(b"hunter2").unwrap();
        });

    Setup { system_dir }
}

lazy_static! {
    static ref CERTIFICATE_PRIVATE_KEY: pkey::PKey<pkey::Private> =
        pkey::PKey::from_rsa(openssl::rsa::Rsa::generate(2048).unwrap())
            .unwrap();
    static ref CERTIFICATE: x509::X509 = {
        let mut builder = x509::X509Builder::new().unwrap();
        builder.set_pubkey(&CERTIFICATE_PRIVATE_KEY).unwrap();
        builder
            .sign(
                &CERTIFICATE_PRIVATE_KEY,
                openssl::hash::MessageDigest::sha256(),
            )
            .unwrap();
        builder.set_version(2).unwrap();
        builder
            .set_not_before(&openssl::asn1::Asn1Time::from_unix(0).unwrap())
            .unwrap();
        builder
            .set_not_after(&openssl::asn1::Asn1Time::days_from_now(2).unwrap())
            .unwrap();
        builder.build()
    };
}

impl Setup {
    fn connect(&self, cxn_name: &'static str) -> impl Read + Write {
        let (server_io, client_io) = UnixStream::pair().unwrap();
        // We don't want the server thread to hold on to the TempDir since the
        // test process can exit before the last server thread notices the EOF
        // and terminates.
        let data_root: PathBuf = self.system_dir.path().to_owned();

        std::thread::spawn(move || {
            let server_io = RcIo::wrap(server_io);
            let mut ssl_acceptor =
                SslAcceptor::mozilla_intermediate_v5(SslMethod::tls_server())
                    .unwrap();
            ssl_acceptor
                .set_private_key(&CERTIFICATE_PRIVATE_KEY)
                .unwrap();
            ssl_acceptor.set_certificate(&CERTIFICATE).unwrap();

            let ssl_acceptor = ssl_acceptor.build();

            let mut server = Server::new(
                Box::new(io::BufReader::new(server_io.clone())),
                Box::new(io::BufWriter::new(server_io)),
                Arc::new(SystemConfig::default()),
                cxn_name.to_owned(),
                ssl_acceptor,
                data_root,
                "localhost".to_owned(),
                cxn_name.to_owned(),
            );

            match server.run() {
                Ok(()) => (),
                Err(crate::support::error::Error::Io(e))
                    if io::ErrorKind::UnexpectedEof == e.kind()
                        || Some(nix::libc::EPIPE) == e.raw_os_error() =>
                {
                    ()
                }
                Err(e) => panic!("Unexpected server error: {}", e),
            }
        });

        client_io
    }
}

/// Read responses from `r` up to and including the final response.
///
/// This creates a `BufReader` over `r` and will lose any data which was
/// buffered after the last read line. This should be fine since we don't do
/// pipelining here.
fn read_responses(r: &mut impl Read) -> Vec<String> {
    let mut ret = Vec::<String>::new();
    let mut r = io::BufReader::new(r);

    loop {
        let mut line = String::new();
        r.read_line(&mut line).unwrap();
        println!("Read response: {:?}", line);

        if line.is_empty() {
            panic!("Unexpected EOF");
        }

        let last = " " == &line[3..4];
        ret.push(line);

        if last {
            break;
        }
    }

    ret
}

fn skip_pleasantries(cxn: &mut (impl Read + Write), name: &str) {
    read_responses(cxn);
    writeln!(cxn, "LHLO {}\r", name).unwrap();
    read_responses(cxn);
}

/// Send a command which is expected to have one response with the given
/// prefix.
fn simple_command(cxn: &mut (impl Read + Write), command: &str, prefix: &str) {
    writeln!(cxn, "{}\r", command).unwrap();
    let responses = read_responses(cxn);
    assert_eq!(1, responses.len());
    assert!(responses[0].starts_with(prefix));
}

/// Return whether the given account received the specified email.
fn received_email(setup: &Setup, account_name: &str, email: &str) -> bool {
    let account = Account::new(
        "verify".to_owned(),
        setup.system_dir.path().join(account_name),
        None,
    );
    let config = account.load_config().unwrap();
    let master_key =
        MasterKey::from_config(&config.master_key, b"hunter2").unwrap();

    let account = Account::new(
        "verify".to_owned(),
        setup.system_dir.path().join(account_name),
        Some(Arc::new(master_key)),
    );

    let mailbox = account.mailbox("INBOX", true).unwrap();
    // Messages are always delivered one at a time, so we can just do linear
    // probing until we hit a non-existent UID.
    for uid in 1.. {
        let mut r = match mailbox.open_message(Uid::u(uid)) {
            Ok((_, r)) => r,
            Err(Error::NxMessage)
            | Err(Error::UnaddressableMessage)
            | Err(Error::ExpungedMessage) => return false,
            Err(e) => panic!("Unexpected error: {}", e),
        };

        let mut data = Vec::new();
        r.read_to_end(&mut data).unwrap();

        if data.ends_with(email.as_bytes()) {
            return true;
        }
    }

    false
}

#[test]
fn first_contact() {
    let setup = set_up();
    let mut cxn = setup.connect("first_contact");

    let responses = read_responses(&mut cxn);
    assert_eq!(1, responses.len());
    assert!(
        responses[0].starts_with("220 localhost"),
        "Unexpected greeting: {}",
        responses[0]
    );

    writeln!(cxn, "QUIT\r").unwrap();

    let responses = read_responses(&mut cxn);
    assert_eq!(1, responses.len());
    assert!(
        responses[0].starts_with("221 2.0.0"),
        "Unexpected goodbye: {}",
        responses[0]
    );
}

#[test]
fn test_lhlo() {
    let setup = set_up();
    let mut cxn = setup.connect("test_lhlo");

    read_responses(&mut cxn);

    writeln!(cxn, "LHLO test_lhlo\r").unwrap();

    let responses = read_responses(&mut cxn);
    assert!(responses[0].starts_with("250-localhost "));
    assert!(responses.contains(&"250-STARTTLS\r\n".to_owned()));
    assert!(responses.last().unwrap().starts_with("250 "));
}

#[test]
fn misc_commands() {
    let setup = set_up();
    let mut cxn = setup.connect("misc_commands");
    skip_pleasantries(&mut cxn, "misc_commands");

    writeln!(cxn, "HELP ME\r").unwrap();
    let responses = read_responses(&mut cxn);
    assert!(responses.last().unwrap().starts_with("214 2.0.0"));

    simple_command(&mut cxn, "VRFY <gäz@localhost>", "252 2.7.0");
    simple_command(&mut cxn, "EXPN <list@localhost>", "550 5.3.3");
    simple_command(&mut cxn, "NOOP", "250 2.0.0");
}

#[test]
fn data_delivery() {
    let setup = set_up();
    let mut cxn = setup.connect("data_delivery");
    skip_pleasantries(&mut cxn, "data_delivery");

    let email_a = "Subject: Email A\r\n\r\nContent A\r\n";
    simple_command(&mut cxn, "MAIL FROM:<tallest@irk>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "RCPT TO:<gäz@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "DATA", "354 ");

    writeln!(cxn, "{}.\r", email_a).unwrap();
    let responses = read_responses(&mut cxn);
    assert_eq!(2, responses.len());
    assert!(responses[0].starts_with("250-2.0.0"));
    assert!(responses[1].starts_with("250 2.0.0"));

    let email_b = "Subject: Email B\r\n\r\nContent B\r\n";
    simple_command(&mut cxn, "MAIL FROM:<>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<zim@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "DATA", "354 ");

    writeln!(cxn, "{}.\r", email_b).unwrap();
    let responses = read_responses(&mut cxn);
    assert_eq!(1, responses.len());
    assert!(responses[0].starts_with("250 2.0.0"));

    assert!(received_email(&setup, "dib", email_a));
    assert!(!received_email(&setup, "dib", email_b));
    assert!(received_email(&setup, "gäz", email_a));
    assert!(!received_email(&setup, "gäz", email_b));
    assert!(!received_email(&setup, "zim", email_a));
    assert!(received_email(&setup, "zim", email_b));
}

#[test]
fn bdat_delivery() {
    let setup = set_up();
    let mut cxn = setup.connect("bdat_delivery");
    skip_pleasantries(&mut cxn, "bdat_delivery");

    // Must preserve bare line endings, not do anything with leading ., and
    // must tolerate unterminated final line.
    let email_binary = "Subject: Binary email\r\n\r\n\r\r\n\n\r\n.\r\n.a\r\nx";
    simple_command(&mut cxn, "MAIL FROM:<> BODY=BINARYMIME", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "250 2.1.5");
    let mut count_sent = 0;
    for chunk in email_binary.as_bytes().chunks(8) {
        count_sent += chunk.len();
        writeln!(
            cxn,
            "BDAT {}{}\r",
            chunk.len(),
            if count_sent == email_binary.len() {
                " LAST"
            } else {
                ""
            }
        )
        .unwrap();
        cxn.write_all(chunk).unwrap();

        let responses = read_responses(&mut cxn);
        assert_eq!(1, responses.len());
        assert!(responses[0].starts_with("250 2.0.0"));
    }

    assert!(received_email(&setup, "dib", email_binary));

    // Send another email to ensure the state machine isn't broken
    let email_followup = "Subject: Followup Email\r\n\r\nbinary followup\r\n";
    simple_command(&mut cxn, "MAIL FROM:<>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<gäz@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "DATA", "354 ");
    writeln!(cxn, "{}.\r", email_followup).unwrap();
    let responses = read_responses(&mut cxn);
    assert_eq!(1, responses.len());
    assert!(responses[0].starts_with("250 2.0.0"));

    assert!(!received_email(&setup, "dib", email_followup));
    assert!(received_email(&setup, "gäz", email_followup));
}

// This specifically tests that spilled buffers are reset properly when
// delivering to multiple accounts.
#[test]
fn large_delivery() {
    let setup = set_up();
    let mut cxn = setup.connect("large_delivery");
    skip_pleasantries(&mut cxn, "large_delivery");

    let large_content =
        format!("Subject: Large\r\n\r\n{}\r\n", "x".repeat(1024 * 1024));

    simple_command(&mut cxn, "MAIL FROM:<tallest@irk>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "RCPT TO:<gäz@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "DATA", "354 ");

    writeln!(cxn, "{}.\r", large_content).unwrap();
    let responses = read_responses(&mut cxn);
    assert_eq!(2, responses.len());
    assert!(responses[0].starts_with("250-2.0.0"));
    assert!(responses[1].starts_with("250 2.0.0"));

    assert!(received_email(&setup, "dib", &large_content));
    assert!(received_email(&setup, "gäz", &large_content));
}

#[test]
fn huge_message_rejected() {
    let setup = set_up();
    let mut cxn = setup.connect("huge_delivery");
    skip_pleasantries(&mut cxn, "huge_delivery");

    let large_content = format!(
        "Subject: Oversize\r\n\r\n{}\r\n",
        "x".repeat(APPEND_SIZE_LIMIT as usize)
    );

    simple_command(&mut cxn, "MAIL FROM:<tallest@irk>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "RCPT TO:<gäz@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "DATA", "354 ");

    writeln!(cxn, "{}.\r", large_content).unwrap();
    let responses = read_responses(&mut cxn);
    assert_eq!(2, responses.len());
    assert!(responses[0].starts_with("552-5.2.3"));
    assert!(responses[1].starts_with("552 5.2.3"));

    assert!(!received_email(&setup, "dib", &large_content));
    assert!(!received_email(&setup, "gäz", &large_content));
}

#[test]
fn huge_mail_from_size_rejected() {
    let setup = set_up();
    let mut cxn = setup.connect("huge_mail_from");
    skip_pleasantries(&mut cxn, "huge_mail_from");

    simple_command(&mut cxn, "MAIL FROM:<> SIZE=1222333444", "552 5.2.3");
}

#[test]
fn failed_delivery() {
    let setup = set_up();
    let mut cxn = setup.connect("failed_delivery");
    skip_pleasantries(&mut cxn, "failed_delivery");

    let failed_email = "Subject: Failed to deliver to gir\r\n\r\ngir\r\n";

    simple_command(&mut cxn, "MAIL FROM:<tallest@irk>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<gir@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "RCPT TO:<zim@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "DATA", "354 ");

    // Now, between verifying gir@localhost above and actually receiving the
    // content, something comes along and removes the gir account.
    fs::remove_dir_all(setup.system_dir.path().join("gir")).unwrap();

    writeln!(cxn, "{}.\r", failed_email).unwrap();
    let responses = read_responses(&mut cxn);
    // We still get both responses; the first one indicates failure
    assert_eq!(2, responses.len());
    assert!(responses[0].starts_with("450-"));
    assert!(responses[1].starts_with("250 2.0.0"));

    // The email was still delivered to zim, though
    assert!(received_email(&setup, "zim", failed_email));
}

#[test]
fn failed_rcpt_to() {
    let setup = set_up();
    let mut cxn = setup.connect("failed_rcpt_to");
    skip_pleasantries(&mut cxn, "failed_rcpt_to");

    simple_command(&mut cxn, "MAIL FROM:<>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<nobody@localhost>", "550 5.1.1");
    simple_command(&mut cxn, "RCPT TO:<..@localhost>", "550 5.1.1");
}

#[test]
fn out_of_order_commands() {
    let setup = set_up();
    let mut cxn = setup.connect("out_of_order_commands");
    read_responses(&mut cxn); // Skip greeting

    // Things that shouldn't work before LHLO
    simple_command(&mut cxn, "MAIL FROM:<>", "503 5.5.1");
    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "503 5.5.1");
    simple_command(&mut cxn, "DATA", "503 5.5.1");
    // 5 = "foo\r\n".len()
    simple_command(&mut cxn, "BDAT 5\r\nfoo", "503 5.5.1");

    writeln!(cxn, "LHLO out_of_order_commands\r").unwrap();
    let responses = read_responses(&mut cxn);
    assert!(responses.last().unwrap().starts_with("250 "));

    // LHLO not allowed after LHLO
    simple_command(&mut cxn, "LHLO out_of_order_commands", "503 5.5.1");

    // Things that shouldn't work before MAIL FROM
    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "503 5.5.1");
    simple_command(&mut cxn, "DATA", "503 5.5.1");
    simple_command(&mut cxn, "BDAT 5\r\nfoo", "503 5.5.1");

    simple_command(&mut cxn, "MAIL FROM:<>", "250 2.0.0");
    simple_command(&mut cxn, "MAIL FROM:<>", "503 5.5.1");

    // DATA and BDAT don't work without recipients
    simple_command(&mut cxn, "DATA", "503 5.5.1");
    simple_command(&mut cxn, "BDAT 5\r\nfoo", "503 5.5.1");

    // Finish up and send an email to ensure that the data we sent through BDAT
    // didn't stay in the buffer.
    let ooo_email = "Subject: Out of order\r\n\r\n";
    // If any BDAT chunks were improperly saved, they manifest as a prefix on
    // the email we're sending now
    let unexpected_email = format!("foo\r\n{}", ooo_email);

    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "DATA", "354 ");
    writeln!(cxn, "{}.\r", ooo_email).unwrap();
    let responses = read_responses(&mut cxn);
    assert_eq!(1, responses.len());
    assert!(responses[0].starts_with("250 2.0.0"));

    assert!(received_email(&setup, "dib", ooo_email));
    assert!(!received_email(&setup, "dib", &unexpected_email));

    simple_command(&mut cxn, "MAIL FROM:<>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "BDAT 5\r\nfoo", "250 2.0.0");
    // Things not allowed after BDAT
    simple_command(&mut cxn, "RCPT TO:<gäz@localhost>", "503 5.5.1");
    simple_command(&mut cxn, "DATA", "503 5.5.1");

    // RSET resets everything
    simple_command(&mut cxn, "RSET", "250 2.0.0");
    simple_command(&mut cxn, "MAIL FROM:<>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "DATA", "354 ");
    writeln!(cxn, "{}.\r", ooo_email).unwrap();
    let responses = read_responses(&mut cxn);
    assert_eq!(1, responses.len());
    assert!(responses[0].starts_with("250 2.0.0"));

    // Ensure the BDAT above wasn't saved.
    assert!(!received_email(&setup, "dib", &unexpected_email));
}

#[test]
fn start_tls() {
    let setup = set_up();
    let mut cxn = setup.connect("starttls");
    skip_pleasantries(&mut cxn, "starttls");

    simple_command(&mut cxn, "STARTTLS", "220 2.0.0");

    let mut connector = SslConnector::builder(SslMethod::tls()).unwrap();
    connector.set_verify(SslVerifyMode::NONE);

    let mut cxn = connector
        .build()
        .connect("localhost", cxn)
        .map_err(|_| "SSL handshake failed")
        .unwrap();
    skip_pleasantries(&mut cxn, "starttls");

    let tls_email = "Subject: TLS\r\n\r\nThis message was sent over TLS.\r\n";
    simple_command(&mut cxn, "MAIL FROM:<>", "250 2.0.0");
    simple_command(&mut cxn, "RCPT TO:<dib@localhost>", "250 2.1.5");
    simple_command(&mut cxn, "DATA", "354 ");
    writeln!(cxn, "{}.\r", tls_email).unwrap();
    let responses = read_responses(&mut cxn);
    assert_eq!(1, responses.len());
    assert!(responses[0].starts_with("250 2.0.0"));

    assert!(received_email(&setup, "dib", tls_email));
}