netpkt/smtp.rs#
Source: lib/netpkt/smtp.rs
smtp -- the Simple Mail Transfer Protocol (RFC 5321) with the service extensions of EHLO that
JFNetworker's servers speak: SIZE (RFC 1870), 8BITMIME (RFC 6152), PIPELINING (RFC 2920),
ENHANCEDSTATUSCODES (RFC 2034 with the codes of RFC 3463) and AUTH with PLAIN and LOGIN
(RFC 4954, 4616); no STARTTLS
Command { verb, arg } Command::parse(line) (the verb in capitals, the argument as
typed), Command::new(verb, arg), to_line() ("EHLO pc1\r\n"; CR, LF
and NUL of the fields left out)
Path { addr, params } parse_path(arg, "FROM" | "TO"): the address of MAIL FROM:<..> or
RCPT TO:<..> (empty for the null reverse path "<>") and the
parameters after it (SIZE=1234, BODY=8BITMIME); fmt_path (without
CR, LF, NUL, angle brackets in the address and blanks in parameters)
Reply { code, lines } Reply::new(code, text), Reply::enh(code, "2.1.0", text) (the
enhanced code before the text), to_bytes(): "250 text" or the
multi-line form "250-first" ... "250 last" of 4.2.1; text(),
status() (the enhanced code), is_positive / is_intermediate /
is_transient / is_permanent
ReplyReader the replies of a server from its bytes in any pieces
Ehlo { domain, keywords } of_reply(&Reply): what a 250 to EHLO offers; has("PIPELINING"),
param("SIZE") -> "10240000", size_limit()
Stream the server's side of a connection: push(bytes), line() while
commands come, begin_data(max) after 354, data() until the line
with the dot (dot-stuffing undone; beyond max the bytes are
counted but not kept)
stuff(message) the bytes after DATA: CRLF line ends, a dot doubled at the start
of a line (4.5.2), ".\r\n" at the end
auth_plain(user, pass) / plain_of(b64) the initial response of AUTH PLAIN and back
LOGIN_USER / LOGIN_PASS the base64 challenges of AUTH LOGIN ("Username:", "Password:")
received(...) the value of a Received field (4.4)
PORT_SMTP (25), PORT_SUBMISSION (587, RFC 6409), verb_known
Lines end in CRLF; a server here also takes a lone LF. Reply texts are the server's own
words: clients go by the codes (and the enhanced codes when ENHANCEDSTATUSCODES was offered).
PORT_SMTP: u16 = 25
/// message submission (RFC 6409): clients hand in their mail here, after AUTH
PORT_SUBMISSION: u16 = 587
/// the base64 challenges of AUTH LOGIN: "Username:" and "Password:"
LOGIN_USER: &str = "VXNlcm5hbWU6"
LOGIN_PASS: &str = "UGFzc3dvcmQ6"
fn verb_known(v: &str) -> boolthe commands of RFC 5321 and the extensions a dissector namesstruct Commanda command line of a clientverb: String-- in capitalsarg: String-- everything after the first space
impl Command
fn new(verb: &str, arg: &str) -> Commandfn parse(line: &str) -> Option<Command>a line without its line end; None for an empty onefn to_line(&self) -> Stringthe line on the wire, CRLF included; CR, LF and NUL of verb and argument are left out (they would end the line and start another command)struct Paththe address and parameters of MAIL FROM or RCPT TOaddr: String-- without the angle brackets; empty: the null reverse path "<>"params: Vec<(String, String)>-- "SIZE=1234" -> ("SIZE", "1234"), "BODY=8BITMIME"; keywords in capitals
impl Path
fn param(&self, k: &str) -> Option<&str>a parameter's valuefn size(&self) -> Option<u64>SIZE=nfn parse_path(arg: &str, key: &str) -> Option<Path>"FROM:<anna@a.lab> SIZE=345" (the argument of MAIL;key"FROM", for RCPT "TO"): None when the keyword, the colon or the brackets are missing (a source route "<@a,@b:c@d>" keeps its mailbox, 4.1.2)fn fmt_path(key: &str, addr: &str, params: &[(&str, &str)]) -> String"FROM:<addr> SIZE=n" with the parameters given; the address loses CR, LF, NUL and angle brackets, the parameters those and blanks, so that neither ends the path, adds a parameter or starts another commandstruct Replya reply of a servercode: u16lines: Vec<String>-- the text of each line (without the code)
impl Reply
fn new(code: u16, text: &str) -> Replyfn enh(code: u16, status: &str, text: &str) -> Replya reply whose lines start with an enhanced status code ("2.1.0 Sender fine")fn text(&self) -> &strthe text of the first line (with its enhanced code)fn joined(&self) -> Stringall lines as one text ("a b c")fn status(&self) -> Option<&str>the enhanced status code at the start of the first line ("5.1.1")fn is_positive(&self) -> bool2yz: donefn is_intermediate(&self) -> bool3yz: go on (354 after DATA, 334 in AUTH)fn is_transient(&self) -> bool4yz: try again laterfn is_permanent(&self) -> bool5yz: will not workfn to_bytes(&self) -> Vec<u8>"code text" lines as they travel (4.2.1)fn line(&self) -> String"550 5.1.1 text" as one line (logs, bounces)struct ReplyReaderthe replies of a server, as its bytes arrive
impl ReplyReader
fn new() -> ReplyReaderfn push(&mut self, b: &[u8]) -> Vec<Reply>the replies that are complete now; a line that is no reply line is skippedstruct Ehlowhat a server offers in its answer to EHLO (4.1.1.1)domain: String-- the name it gives for itselfkeywords: Vec<String>-- "SIZE 10240000", "8BITMIME", "AUTH PLAIN LOGIN" ...
impl Ehlo
fn of_reply(r: &Reply) -> Ehlothe 250 reply to EHLO: its first line names the server, every further line is a keyword with its parametersfn has(&self, kw: &str) -> boolthe server offers the keywordfn param(&self, kw: &str) -> Option<&str>the parameters after a keyword ("SIZE" -> "10240000", "AUTH" -> "PLAIN LOGIN")fn size_limit(&self) -> Option<u64>the size limit SIZE names (0: none given)fn auth(&self, mech: &str) -> boolAUTH offers this mechanismfn to_reply(&self, greeting: &str) -> Replythe reply a server sends: its name and greeting, then the keywordsstruct Dataa message after DATAbody: Vec<u8>-- the message with CRLF line ends, dot-stuffing undone (empty when it was too big)size: u64-- its size in bytes, also when it was too bigover: bool-- more than the limit came
struct Streamthe server's side of a connection: command lines, then the message after 354
impl Stream
fn new() -> Streamfn push(&mut self, b: &[u8])fn pending(&self) -> usizebytes not taken yetfn in_data(&self) -> boola message is being readfn line(&mut self) -> Option<String>the next command line (its line end removed); None until a line is complete or while a message is readfn begin_data(&mut self, max: u64)the message starts after the 354; more thanmaxbytes are counted, not kept (0: no limit)fn data(&mut self) -> Option<Data>the message once its line with the dot came (the bytes after it stay for the next commands); None while it goes onfn stuff(message: &[u8]) -> Vec<u8>the bytes after DATA: CRLF line ends, a dot doubled at the start of a line, the line with the dot at the end (4.5.2)fn auth_plain(user: &str, pass: &str) -> Stringthe initial response of AUTH PLAIN (RFC 4616): base64 of "\0user\0password"fn plain_of(b64: &str) -> Option<(String, String)>the user and password of an AUTH PLAIN response (the authorization identity ignored)fn received(helo: &str, peer: &str, by: &str, with: &str, id: &str, rcpt: Option<&str>, date: &str) -> Stringthe value of a Received field (4.4): "from HELO (NAME [ADDR]) by HOST with PROTO id ID for <RCPT>; DATE"