indymilter 0.3.0

Asynchronous milter library
Documentation
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// indymilter – asynchronous milter library
// Copyright © 2021–2023 David Bürgin <dbuergin@gluet.ch>
//
// This program 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.
//
// This program 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
// this program. If not, see <https://www.gnu.org/licenses/>.

//! Milter commands.

use crate::{
    macros::MacroStage,
    message::{Byte, Message, TryFromByteError, Version},
    proto_util::{Actions, ProtoOpts, SocketInfo},
    session::State,
};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::{
    error::Error,
    ffi::CString,
    fmt::{self, Display, Formatter},
    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
    str::FromStr,
};

/// The kind of a command.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum CommandKind {
    /// The `A` command.
    Abort,
    /// The `B` command.
    BodyChunk,
    /// The `C` command.
    ConnInfo,
    /// The `D` command.
    DefMacros,
    /// The `E` command.
    BodyEnd,
    /// The `H` command.
    Helo,
    /// The `K` command.
    QuitNc,
    /// The `L` command.
    Header,
    /// The `M` command.
    Mail,
    /// The `N` command.
    Eoh,
    /// The `O` command.
    OptNeg,
    /// The `Q` command.
    Quit,
    /// The `R` command.
    Rcpt,
    /// The `T` command.
    Data,
    /// The `U` command.
    Unknown,
}

impl CommandKind {
    pub(crate) fn as_state(&self) -> Option<State> {
        match self {
            Self::Abort => Some(State::Abort),
            Self::BodyChunk => Some(State::Body),
            Self::ConnInfo => Some(State::Conn),
            Self::DefMacros => None,
            Self::BodyEnd => Some(State::Eom),
            Self::Helo => Some(State::Helo),
            Self::QuitNc => Some(State::QuitNc),
            Self::Header => Some(State::Header),
            Self::Mail => Some(State::Mail),
            Self::Eoh => Some(State::Eoh),
            Self::OptNeg => Some(State::Opts),
            Self::Quit => Some(State::Quit),
            Self::Rcpt => Some(State::Rcpt),
            Self::Data => Some(State::Data),
            Self::Unknown => Some(State::Unknown),
        }
    }
}

impl From<CommandKind> for u8 {
    fn from(kind: CommandKind) -> Self {
        match kind {
            CommandKind::Abort => b'A',
            CommandKind::BodyChunk => b'B',
            CommandKind::ConnInfo => b'C',
            CommandKind::DefMacros => b'D',
            CommandKind::BodyEnd => b'E',
            CommandKind::Helo => b'H',
            CommandKind::QuitNc => b'K',
            CommandKind::Header => b'L',
            CommandKind::Mail => b'M',
            CommandKind::Eoh => b'N',
            CommandKind::OptNeg => b'O',
            CommandKind::Quit => b'Q',
            CommandKind::Rcpt => b'R',
            CommandKind::Data => b'T',
            CommandKind::Unknown => b'U',
        }
    }
}

impl TryFrom<u8> for CommandKind {
    type Error = TryFromByteError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            b'A' => Ok(Self::Abort),
            b'B' => Ok(Self::BodyChunk),
            b'C' => Ok(Self::ConnInfo),
            b'D' => Ok(Self::DefMacros),
            b'E' => Ok(Self::BodyEnd),
            b'H' => Ok(Self::Helo),
            b'K' => Ok(Self::QuitNc),
            b'L' => Ok(Self::Header),
            b'M' => Ok(Self::Mail),
            b'N' => Ok(Self::Eoh),
            b'O' => Ok(Self::OptNeg),
            b'Q' => Ok(Self::Quit),
            b'R' => Ok(Self::Rcpt),
            b'T' => Ok(Self::Data),
            b'U' => Ok(Self::Unknown),
            value => Err(TryFromByteError(value)),
        }
    }
}

/// A command with unparsed payload buffer.
pub(crate) struct CommandMessage {
    pub kind: CommandKind,
    pub buffer: Bytes,
}

impl TryFrom<Message> for CommandMessage {
    type Error = TryFromByteError;

    fn try_from(msg: Message) -> Result<Self, Self::Error> {
        let kind = msg.kind.try_into()?;

        Ok(Self {
            kind,
            buffer: msg.buffer,
        })
    }
}

/// An error that occurs during parsing of a command.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum ParseCommandError {
    UnknownCommand(u8),
    UnknownFamily(u8),
    InvalidSocketAddr,
    UnknownStage(u8),
    NoOptNegPayload,
    EmptyCString,
    NotNulTerminated,
    NoU8Found,
    NoU16Found,
    NoCStringFound,
}

impl Display for ParseCommandError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Self::UnknownCommand(byte) => write!(f, "unknown command: {:?}", Byte(byte)),
            Self::UnknownFamily(byte) => write!(f, "unknown protocol family: {:?}", Byte(byte)),
            Self::InvalidSocketAddr => write!(f, "invalid socket address"),
            Self::UnknownStage(byte) => write!(f, "unknown macro stage: {:?}", Byte(byte)),
            Self::NoOptNegPayload => write!(f, "no option negotiation payload found"),
            Self::EmptyCString => write!(f, "empty string"),
            Self::NotNulTerminated => write!(f, "not nul terminated"),
            Self::NoU8Found => write!(f, "no u8 found"),
            Self::NoU16Found => write!(f, "no u16 found"),
            Self::NoCStringFound => write!(f, "no C string found"),
        }
    }
}

impl Error for ParseCommandError {}

/// A command.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Command {
    /// The `A` command.
    Abort,
    /// The `B` command.
    BodyChunk(Bytes),
    /// The `C` command.
    ConnInfo(ConnInfoPayload),
    /// The `D` command.
    DefMacros(MacroPayload),
    /// The `E` command.
    BodyEnd(Bytes),
    /// The `H` command.
    Helo(HeloPayload),
    /// The `K` command.
    QuitNc,
    /// The `L` command.
    Header(HeaderPayload),
    /// The `M` command.
    Mail(EnvAddrPayload),
    /// The `N` command.
    Eoh,
    /// The `O` command.
    OptNeg(OptNegPayload),
    /// The `Q` command.
    Quit,
    /// The `R` command.
    Rcpt(EnvAddrPayload),
    /// The `T` command.
    Data,
    /// The `U` command.
    Unknown(UnknownPayload),
}

impl Command {
    /// Parses a command from a milter protocol message.
    pub fn parse_command(msg: Message) -> Result<Self, ParseCommandError> {
        let msg = CommandMessage::try_from(msg)
            .map_err(|e| ParseCommandError::UnknownCommand(e.byte()))?;

        Ok(match msg.kind {
            CommandKind::Abort => Self::Abort,
            CommandKind::BodyChunk => Self::BodyChunk(msg.buffer),
            CommandKind::ConnInfo => Self::ConnInfo(ConnInfoPayload::parse_buffer(msg.buffer)?),
            CommandKind::DefMacros => Self::DefMacros(MacroPayload::parse_buffer(msg.buffer)?),
            CommandKind::BodyEnd => Self::BodyEnd(msg.buffer),
            CommandKind::Helo => Self::Helo(HeloPayload::parse_buffer(msg.buffer)?),
            CommandKind::QuitNc => Self::QuitNc,
            CommandKind::Header => Self::Header(HeaderPayload::parse_buffer(msg.buffer)?),
            CommandKind::Mail => Self::Mail(EnvAddrPayload::parse_buffer(msg.buffer)?),
            CommandKind::Eoh => Self::Eoh,
            CommandKind::OptNeg => Self::OptNeg(OptNegPayload::parse_buffer(msg.buffer)?),
            CommandKind::Quit => Self::Quit,
            CommandKind::Rcpt => Self::Rcpt(EnvAddrPayload::parse_buffer(msg.buffer)?),
            CommandKind::Data => Self::Data,
            CommandKind::Unknown => Self::Unknown(UnknownPayload::parse_buffer(msg.buffer)?),
        })
    }

    /// Converts this command to a milter protocol message.
    pub fn into_message(self) -> Message {
        match self {
            Self::Abort => Message::new(CommandKind::Abort, Bytes::new()),
            Self::BodyChunk(chunk) => Message::new(CommandKind::BodyChunk, chunk),
            Self::ConnInfo(ConnInfoPayload { hostname, socket_info }) => {
                let mut buf = BytesMut::with_capacity(64);

                buf.put(hostname.to_bytes_with_nul());

                match socket_info {
                    SocketInfo::Unknown => buf.put_u8(b'U'),
                    SocketInfo::Inet(addr) => {
                        buf.put_u8(match addr {
                            SocketAddr::V4(_) => b'4',
                            SocketAddr::V6(_) => b'6',
                        });

                        buf.put_u16(addr.port());

                        let ip = CString::new(addr.ip().to_string()).unwrap();
                        buf.put(ip.to_bytes_with_nul());
                    }
                    SocketInfo::Unix(path) => {
                        buf.put_u8(b'L');
                        buf.put_u16(0);
                        buf.put(path.to_bytes_with_nul());
                    }
                }

                Message::new(CommandKind::ConnInfo, buf)
            }
            Self::DefMacros(MacroPayload { stage, macros }) => {
                let mut buf = BytesMut::new();

                buf.put_u8(stage.into());
                for m in macros {
                    buf.put(m.to_bytes_with_nul());
                }

                Message::new(CommandKind::DefMacros, buf)
            }
            Self::BodyEnd(chunk) => Message::new(CommandKind::BodyEnd, chunk),
            Self::Helo(HeloPayload { hostname }) => {
                let hostname = hostname.to_bytes_with_nul();

                Message::new(CommandKind::Helo, Bytes::copy_from_slice(hostname))
            }
            Self::QuitNc => Message::new(CommandKind::QuitNc, Bytes::new()),
            Self::Header(HeaderPayload { name, value }) => {
                let name = name.to_bytes_with_nul();
                let value = value.to_bytes_with_nul();

                let mut buf = BytesMut::with_capacity(name.len() + value.len());

                buf.put(name);
                buf.put(value);

                Message::new(CommandKind::Header, buf)
            }
            Self::Mail(EnvAddrPayload { args }) => {
                let mut buf = BytesMut::new();

                for arg in args {
                    buf.put(arg.to_bytes_with_nul());
                }

                Message::new(CommandKind::Mail, buf)
            }
            Self::Eoh => Message::new(CommandKind::Eoh, Bytes::new()),
            Self::OptNeg(OptNegPayload { version, actions, opts }) => {
                let mut buf = BytesMut::with_capacity(12);

                buf.put_u32(version);
                buf.put_u32(actions.bits());
                buf.put_u32(opts.bits());

                Message::new(CommandKind::OptNeg, buf)
            }
            Self::Quit => Message::new(CommandKind::Quit, Bytes::new()),
            Self::Rcpt(EnvAddrPayload { args }) => {
                let mut buf = BytesMut::new();

                for arg in args {
                    buf.put(arg.to_bytes_with_nul());
                }

                Message::new(CommandKind::Rcpt, buf)
            }
            Self::Data => Message::new(CommandKind::Data, Bytes::new()),
            Self::Unknown(UnknownPayload { arg }) => {
                let arg = arg.to_bytes_with_nul();

                Message::new(CommandKind::Unknown, Bytes::copy_from_slice(arg))
            }
        }
    }
}

enum Family {
    Unknown,
    Ipv4,
    Ipv6,
    Unix,
}

impl TryFrom<u8> for Family {
    type Error = TryFromByteError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            b'U' => Ok(Self::Unknown),
            b'4' => Ok(Self::Ipv4),
            b'6' => Ok(Self::Ipv6),
            b'L' => Ok(Self::Unix),
            value => Err(TryFromByteError(value)),
        }
    }
}

/// A [`ConnInfo`][Command::ConnInfo] command payload.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ConnInfoPayload {
    pub hostname: CString,
    pub socket_info: SocketInfo,
}

impl ConnInfoPayload {
    /// Parses a payload from the given buffer.
    pub fn parse_buffer(mut buf: Bytes) -> Result<Self, ParseCommandError> {
        let hostname = get_c_string(&mut buf)?;

        let family = get_u8(&mut buf)?;
        let family = Family::try_from(family)
            .map_err(|e| ParseCommandError::UnknownFamily(e.byte()))?;

        let socket_info = match family {
            Family::Unknown => SocketInfo::Unknown,
            Family::Ipv4 => {
                let addr = parse_socket_addr::<Ipv4Addr>(buf)?;

                SocketInfo::Inet(addr)
            }
            Family::Ipv6 => {
                let addr = parse_socket_addr::<Ipv6Addr>(buf)?;

                SocketInfo::Inet(addr)
            }
            Family::Unix => {
                let _unused = get_u16(&mut buf)?;

                ensure_nul_terminated(&buf)?;

                let path = get_c_string(&mut buf)?;

                SocketInfo::Unix(path)
            }
        };

        Ok(Self {
            hostname,
            socket_info,
        })
    }
}

fn parse_socket_addr<T>(mut buf: Bytes) -> Result<SocketAddr, ParseCommandError>
where
    T: FromStr + Into<IpAddr>,
{
    let port = get_u16(&mut buf)?;

    ensure_nul_terminated(&buf)?;

    let addr = get_c_string(&mut buf)?;
    let addr = addr
        .into_string()
        .map_err(|_| ParseCommandError::InvalidSocketAddr)?
        .parse::<T>()
        .map_err(|_| ParseCommandError::InvalidSocketAddr)?;

    Ok(SocketAddr::from((addr, port)))
}

/// A [`DefMacros`][Command::DefMacros] command payload.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct MacroPayload {
    pub stage: MacroStage,
    pub macros: Vec<CString>,  // key/value pairs, non-empty
}

impl MacroPayload {
    /// Parses a payload from the given buffer.
    pub fn parse_buffer(mut buf: Bytes) -> Result<Self, ParseCommandError> {
        let stage = get_u8(&mut buf)?;
        let stage = MacroStage::try_from(stage)
            .map_err(|e| ParseCommandError::UnknownStage(e.byte()))?;

        let mut macros = vec![get_c_string(&mut buf)?];
        while let Ok(s) = get_c_string(&mut buf) {
            macros.push(s);
        }

        Ok(Self { stage, macros })
    }
}

/// A [`Helo`][Command::Helo] command payload.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct HeloPayload {
    pub hostname: CString,
}

impl HeloPayload {
    /// Parses a payload from the given buffer.
    pub fn parse_buffer(mut buf: Bytes) -> Result<Self, ParseCommandError> {
        ensure_nul_terminated(&buf)?;

        let hostname = get_c_string(&mut buf)?;

        Ok(Self { hostname })
    }
}

/// A [`Header`][Command::Header] command payload.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct HeaderPayload {
    pub name: CString,  // non-empty
    pub value: CString,
}

impl HeaderPayload {
    /// Parses a payload from the given buffer.
    pub fn parse_buffer(mut buf: Bytes) -> Result<Self, ParseCommandError> {
        ensure_nul_terminated(&buf)?;

        let name = get_c_string(&mut buf)?;
        if name.as_bytes().is_empty() {
            return Err(ParseCommandError::EmptyCString);
        }

        let value = get_c_string(&mut buf)?;

        Ok(Self { name, value })
    }
}

/// An envelope address payload.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct EnvAddrPayload {
    pub args: Vec<CString>,  // non-empty
}

impl EnvAddrPayload {
    /// Parses a payload from the given buffer.
    pub fn parse_buffer(mut buf: Bytes) -> Result<Self, ParseCommandError> {
        let mut args = vec![get_c_string(&mut buf)?];

        while let Ok(s) = get_c_string(&mut buf) {
            args.push(s);
        }

        Ok(Self { args })
    }
}

/// An [`OptNeg`][Command::OptNeg] command payload.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct OptNegPayload {
    pub version: Version,
    pub actions: Actions,
    pub opts: ProtoOpts,
}

impl OptNegPayload {
    /// Parses a payload from the given buffer.
    pub fn parse_buffer(mut buf: Bytes) -> Result<Self, ParseCommandError> {
        if buf.remaining() < 12 {
            return Err(ParseCommandError::NoOptNegPayload);
        }

        let version = buf.get_u32();
        let actions = Actions::from_bits_truncate(buf.get_u32());
        let opts = ProtoOpts::from_bits_truncate(buf.get_u32());

        Ok(Self {
            version,
            actions,
            opts,
        })
    }
}

/// An [`Unknown`][Command::Unknown] command payload.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct UnknownPayload {
    pub arg: CString,
}

impl UnknownPayload {
    /// Parses a payload from the given buffer.
    pub fn parse_buffer(mut buf: Bytes) -> Result<Self, ParseCommandError> {
        let arg = get_c_string(&mut buf)?;

        Ok(Self { arg })
    }
}

fn ensure_nul_terminated(bytes: &[u8]) -> Result<(), ParseCommandError> {
    if !bytes.ends_with(&[0]) {
        return Err(ParseCommandError::NotNulTerminated);
    }
    Ok(())
}

fn get_u8(buf: &mut Bytes) -> Result<u8, ParseCommandError> {
    if !buf.has_remaining() {
        return Err(ParseCommandError::NoU8Found);
    }
    Ok(buf.get_u8())
}

fn get_u16(buf: &mut Bytes) -> Result<u16, ParseCommandError> {
    if buf.remaining() < 2 {
        return Err(ParseCommandError::NoU16Found);
    }
    Ok(buf.get_u16())
}

fn get_c_string(buf: &mut Bytes) -> Result<CString, ParseCommandError> {
    super::get_c_string(buf).map_err(|_| ParseCommandError::NoCStringFound)
}

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

    #[test]
    fn parse_command_ok() {
        let msg = Message::new(b'L', Bytes::from_static(b"name\0value\0"));

        assert_eq!(
            Command::parse_command(msg),
            Ok(Command::Header(HeaderPayload {
                name: c_str!("name").into(),
                value: c_str!("value").into(),
            }))
        );
    }

    #[test]
    fn header_payload() {
        assert_eq!(
            HeaderPayload::parse_buffer(Bytes::from_static(b"name\0value\0")),
            Ok(HeaderPayload {
                name: c_str!("name").into(),
                value: c_str!("value").into(),
            })
        );
        assert!(HeaderPayload::parse_buffer(Bytes::new()).is_err());
        assert!(HeaderPayload::parse_buffer(Bytes::from_static(b"name")).is_err());
    }

    #[test]
    fn helo_payload() {
        assert_eq!(
            HeloPayload::parse_buffer(Bytes::from_static(b"hello\0")),
            Ok(HeloPayload {
                hostname: c_str!("hello").into()
            })
        );
        assert!(HeloPayload::parse_buffer(Bytes::new()).is_err());
        assert!(HeloPayload::parse_buffer(Bytes::from_static(b"hello")).is_err());

        // undocumented:
        assert!(HeloPayload::parse_buffer(Bytes::from_static(b"hello\0excess")).is_err());
        assert_eq!(
            HeloPayload::parse_buffer(Bytes::from_static(b"hello\0excess\0")),
            Ok(HeloPayload {
                hostname: c_str!("hello").into()
            })
        );
    }
}