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
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
//-
// Copyright (c) 2020, 2023, 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/>.

//! A simple AST-level Crymap client.
//!
//! **THIS IS NOT A GENERAL-PURPOSE IMAP CLIENT.** It makes many assumptions
//! that rely on implementation details specific to Crymap (see `syntax.rs` for
//! the gory details) and will only reliably work even with other Crymap
//! versions for the subset of the protocol used by the Crymap CLI. Please do
//! not request to extract this to a separate crate.
//!
//! Besides the CLI subset, this is mainly used for internal integration tests.
#![allow(dead_code)]

use std::borrow::Cow;
use std::io::{self, BufRead, Read, Write};
use std::str;

use lazy_static::lazy_static;
use regex::bytes::Regex;
use thiserror::Error;

use super::lex::{InlineSplice, LexWriter};
use super::mailbox_name::MailboxName;
use super::syntax as s;

lazy_static! {
    static ref LITERAL_AT_EOL: Regex =
        Regex::new(r#"~?\{([0-9]+)?\}\r\n$"#).unwrap();
}

#[derive(Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Io(#[from] io::Error),
    #[error("Parse error: {0}")]
    Nom(String),
    #[error("Failed to parse whole response")]
    PartialParse,
    #[error("Append rejected: {0}")]
    AppendRejected(String),
}

pub struct Client<R, W> {
    read: R,
    write: W,
    trace_stderr: Option<&'static str>,
    next_tag: u64,
}

// Custom implementation of DeflateEncoder because the one in flate2 does not
// reliably sync the output stream.
// TODO Probably related to the TODO in Cargo.toml. It looks like the flush()
// implementation only sets Sync for the first attempt and then forgets about
// it for subsequent passes.
pub struct DeflateEncoder<W> {
    inner: W,
    buf: [u8; 1024],
    compress: flate2::Compress,
}

impl<W> DeflateEncoder<W> {
    fn new(inner: W) -> Self {
        Self {
            inner,
            buf: [0u8; 1024],
            compress: flate2::Compress::new(flate2::Compression::best(), false),
        }
    }
}

impl<W: Write> Write for DeflateEncoder<W> {
    fn write(&mut self, src: &[u8]) -> io::Result<usize> {
        if src.is_empty() {
            return Ok(0);
        }

        loop {
            let before_in = self.compress.total_in();
            let before_out = self.compress.total_out();
            self.compress
                .compress(src, &mut self.buf, flate2::FlushCompress::None)
                .unwrap();
            let after_in = self.compress.total_in();
            let after_out = self.compress.total_out();

            self.inner
                .write_all(&self.buf[..(after_out - before_out) as usize])?;
            let written = (after_in - before_in) as usize;
            if written > 0 {
                return Ok(written);
            }
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        loop {
            let before_out = self.compress.total_out();
            self.compress
                .compress(&[], &mut self.buf, flate2::FlushCompress::Sync)
                .unwrap();
            let after_out = self.compress.total_out();
            if after_out == before_out {
                break;
            }

            self.inner
                .write_all(&self.buf[..(after_out - before_out) as usize])?;
        }

        self.inner.flush()
    }
}

impl<R: BufRead, W: Write> Client<R, W> {
    pub fn new(read: R, write: W, trace_stderr: Option<&'static str>) -> Self {
        Client {
            read,
            write,
            trace_stderr,
            next_tag: 0,
        }
    }

    pub fn compress(
        self,
    ) -> Client<io::BufReader<flate2::read::DeflateDecoder<R>>, DeflateEncoder<W>>
    {
        if let Some(prefix) = self.trace_stderr {
            eprintln!("{:10} WIRE <start deflate>", prefix);
        }

        Client {
            read: io::BufReader::new(flate2::read::DeflateDecoder::new(
                self.read,
            )),
            write: DeflateEncoder::new(self.write),
            trace_stderr: self.trace_stderr,
            next_tag: self.next_tag,
        }
    }

    pub fn write_raw(&mut self, bytes: &[u8]) -> Result<(), Error> {
        self.trace(true, ">>[raw]", bytes);
        self.write.write_all(bytes)?;
        self.write.flush()?;
        Ok(())
    }

    pub fn write_raw_censored(&mut self, bytes: &[u8]) -> Result<(), Error> {
        if let Some(prefix) = self.trace_stderr {
            eprintln!("{:10} WIRE >>[raw]<data not shown>", prefix,);
        }
        self.write.write_all(bytes)?;
        self.write.flush()?;
        Ok(())
    }

    pub fn read_line_raw(&mut self, dst: &mut Vec<u8>) -> Result<usize, Error> {
        let start = dst.len();
        let nread = self.read.read_until(b'\n', dst)?;
        self.trace(false, "<<[eol]", &dst[start..]);
        Ok(nread)
    }

    pub fn read_data_raw(
        &mut self,
        dst: &mut Vec<u8>,
        n: u32,
    ) -> Result<usize, Error> {
        let start = dst.len();
        let nread = self.read.by_ref().take(n.into()).read_to_end(dst)?;
        self.trace(true, "<<[lit]", &dst[start..]);
        if n > nread as u32 {
            return Err(Error::Io(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "Hit EOF before end of literal",
            )));
        }

        Ok(nread)
    }

    pub fn read_logical_line(
        &mut self,
        dst: &mut Vec<u8>,
    ) -> Result<(), Error> {
        loop {
            let nread = self.read_line_raw(dst)?;
            if 0 == nread || !dst.ends_with(b"\r\n") {
                return Err(Error::Io(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "Line didn't end with CRLF",
                )));
            }

            let literal_len = LITERAL_AT_EOL
                .captures(&dst[dst.len() - nread..])
                .and_then(|cap| {
                    str::from_utf8(cap.get(1).unwrap().as_bytes())
                        .expect("Matched invalid UTF-8 inside literal start?")
                        .parse::<u32>()
                        .ok()
                });

            if let Some(literal_len) = literal_len {
                self.read_data_raw(dst, literal_len)?;
            } else {
                break;
            }
        }

        Ok(())
    }

    pub fn read_one_response<'a>(
        &mut self,
        dst: &'a mut Vec<u8>,
    ) -> Result<s::ResponseLine<'a>, Error> {
        let start = dst.len();
        self.read_logical_line(dst)?;
        let (remaining, r) = s::ResponseLine::parse(&dst[start..dst.len() - 2])
            .map_err(|e| Error::Nom(e.to_string()))?;

        if !remaining.is_empty() {
            return Err(Error::PartialParse);
        }

        Ok(r)
    }

    pub fn read_responses_until_tagged<'a>(
        &mut self,
        dst: &'a mut Vec<u8>,
    ) -> Result<Vec<s::ResponseLine<'a>>, Error> {
        let mut boundaries = vec![dst.len()];
        loop {
            let start = dst.len();
            self.read_logical_line(dst)?;
            boundaries.push(dst.len());

            if start == dst.len() {
                return Err(Error::Io(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "Expected continuation line, got nothing",
                )));
            }

            if b'*' != dst[start] {
                break;
            }
        }

        let dst: &'a Vec<u8> = &*dst;

        boundaries
            .windows(2)
            .map(|w| &dst[w[0]..w[1] - 2])
            .map(|line| {
                let (remaining, r) = s::ResponseLine::parse(line)
                    .map_err(|e| Error::Nom(e.to_string()))?;
                if !remaining.is_empty() {
                    return Err(Error::PartialParse);
                }

                Ok(r)
            })
            .collect::<Result<Vec<_>, Error>>()
    }

    pub fn command<'a>(
        &mut self,
        command: s::Command<'_>,
        response_buffer: &'a mut Vec<u8>,
    ) -> Result<Vec<s::ResponseLine<'a>>, Error> {
        response_buffer.clear();

        let tag = self.next_tag;
        self.next_tag += 1;

        let mut command_buffer = Vec::<u8>::new();
        {
            s::CommandLine {
                tag: Cow::Owned(format!("{}", tag)),
                cmd: command,
            }
            .write_to(&mut LexWriter::new(
                InlineSplice(&mut command_buffer),
                true,
                true,
            ))
            .unwrap();
        }

        command_buffer.extend_from_slice(b"\r\n");
        self.trace(false, ">>[cmd]", &command_buffer);
        self.write.write_all(&command_buffer)?;
        self.write.flush()?;
        self.read_responses_until_tagged(response_buffer)
    }

    pub fn start_append(
        &mut self,
        mailbox: &str,
        frag: s::AppendFragment,
        data: &[u8],
    ) -> Result<(), Error> {
        let tag = self.next_tag;
        self.next_tag += 1;

        let mut command = s::AppendCommandStart {
            tag: Cow::Owned(format!("{}", tag)),
            mailbox: MailboxName::of_wire(Cow::Borrowed(mailbox)),
            first_fragment: frag,
        };

        let mut request_buffer = Vec::<u8>::new();
        command.write_to(&mut LexWriter::new(
            InlineSplice(&mut request_buffer),
            true,
            true,
        ))?;
        write!(request_buffer, "{{{}}}\r\n", data.len())?;

        self.trace(false, ">>[app]", &request_buffer);
        self.write.write_all(&request_buffer)?;

        let mut response_buffer = Vec::new();
        self.read_logical_line(&mut response_buffer)?;
        if !response_buffer.starts_with(b"+ ") {
            return Err(Error::AppendRejected(
                String::from_utf8_lossy(&response_buffer).into_owned(),
            ));
        }

        self.trace(true, ">>[lit]", data);
        self.write.write_all(data)?;

        if command.first_fragment.utf8 {
            self.trace(true, ">>[app]", b")");
            self.write.write_all(b")")?;
        }

        self.write.flush()?;

        Ok(())
    }

    pub fn append_item(
        &mut self,
        mut frag: s::AppendFragment,
        data: &[u8],
    ) -> Result<(), Error> {
        let mut request_buffer = Vec::<u8>::new();
        frag.write_to(&mut LexWriter::new(
            InlineSplice(&mut request_buffer),
            true,
            true,
        ))?;
        write!(request_buffer, "{{{}}}\r\n", data.len())?;

        self.trace(false, ">>[app]", &request_buffer);
        self.write.write_all(&request_buffer)?;

        let mut response_buffer = Vec::new();
        self.read_logical_line(&mut response_buffer)?;
        if !response_buffer.starts_with(b"+ ") {
            return Err(Error::AppendRejected(
                String::from_utf8_lossy(&response_buffer).into_owned(),
            ));
        }

        self.trace(true, ">>[lit]", data);
        self.write.write_all(data)?;

        if frag.utf8 {
            self.trace(true, ">>[app] ", b")");
            self.write.write_all(b")")?;
        }

        self.write.flush()?;

        Ok(())
    }

    pub fn finish_append<'a>(
        &mut self,
        response_buffer: &'a mut Vec<u8>,
    ) -> Result<Vec<s::ResponseLine<'a>>, Error> {
        self.trace(false, ">>[app]", b"\r\n");
        self.write.write_all(b"\r\n")?;
        self.write.flush()?;
        self.read_responses_until_tagged(response_buffer)
    }

    fn trace(&self, truncate: bool, what: &str, data: &[u8]) {
        if let Some(prefix) = self.trace_stderr {
            if data.is_empty() {
                eprintln!("{:10} WIRE {}<empty>", prefix, what);
                return;
            }

            let (data, truncated) = if truncate {
                data.split_at(data.len().min(128))
            } else {
                (data, &[] as &[u8])
            };

            let mut start = 0;
            for split in memchr::memchr_iter(b'\n', data)
                .chain(std::iter::once(data.len() - 1))
            {
                if split < start {
                    continue;
                }

                let data = &data[start..=split];
                start = split + 1;

                let mut vis = String::new();
                for &byte in data {
                    match byte {
                        b' '..=b'~' => vis.push(byte as char),
                        b'\n' => vis.push_str("\\n"),
                        b'\r' => vis.push_str("\\r"),
                        b => vis.push_str(&format!("\\x{:02X}", b)),
                    }
                }

                eprintln!("{:10} WIRE {} {}", prefix, what, vis);
            }

            if !truncated.is_empty() {
                eprintln!(
                    "{:10} WIRE {}<{} more bytes, {} total>",
                    prefix,
                    what,
                    truncated.len(),
                    truncated.len() + data.len(),
                );
            }
        }
    }
}