The server side of RFC 1939, with no socket, no TLS and no threads in it:
octets go in through Receive, reply octets come out through OnOutput, and
every question about storage is delegated to an iPop3ServerPolicy. Same shape
as StreamSec.Mail.SMTPEngine, and for the same reason - the framing is the
security-critical half, and a parser that cannot be driven without a socket is
one whose hostile-input cases never get tested. Pop3ServerSelfTest at the
bottom drives them.
THE DOT-STUFFER IS THE SECURITY-CRITICAL CODE, and it is on the way OUT.
SMTP's rule is about where a message ENDS on input; POP3's mirror image is
about where a multi-line response ends on output. A body line consisting of a
single '.' that is not doubled terminates the response early, and every octet
after it is read by the client AS PROTOCOL. That is response splitting, and it
is this protocol's equivalent of SMTP smuggling. Two rules follow:
- THE TERMINATOR IS EMITTED, NEVER INFERRED. If the stored message does not
end with CRLF the engine appends one, then '.' CRLF. A message with no
trailing newline cannot merge into the terminator.
- STUFFING IS CONSERVATIVE: a leading '.' is doubled after CRLF, after a
bare LF, AND after a bare CR. The strict reading - stuff only after CRLF -
is right for a strict client and leaves a LAX one, which treats a bare LF
as a line break, seeing an unstuffed '.' line and ending the message
early. The cost of the conservative rule is one octet inside a message
that was already malformed; the cost of the strict one is a
response-splitting hole against a real client. Take the octet.
THE ENGINE OWNS POP3 SEMANTICS, THE POLICY OWNS STORAGE. Message numbers are
frozen when the maildrop opens and never move; DELE only MARKS, RSET clears
the marks, and the marks are committed exactly once, in UPDATE state, reached
only by QUIT from TRANSACTION. A dropped connection therefore deletes nothing
(RFC 1939 sec.6), and that is arranged here rather than trusted to a policy.
WHAT IT WILL NOT DO. APOP is not implemented and cannot be: it is an MD5 keyed
digest over the greeting timestamp and the shared secret, so the server must
hold something the password can be recovered from or re-derived with, and
StreamSec.Mail.Users stores an Argon2id verifier by design. The same argument
rules out CRAM-MD5 and DIGEST-MD5. What is left - USER/PASS and SASL PLAIN and
LOGIN - all send the password itself, so the engine refuses every one of them
on a cleartext channel UNCONDITIONALLY, whatever the configuration says, and
CAPA does not advertise what will be refused.Feature notes: Mail subsystem notes (SMTP / POP3 / IMAP)
inherits from Exception
(no public members)
(no public members)
EVERY DECISION THE ENGINE DOES NOT MAKE ITSELF. The engine owns framing, sequencing, the frozen numbering and the deletion marks; who may sign in and what the messages are belong to the application, and they arrive here.
CALLED ON WHATEVER THREAD DROVE Receive - one connection's thread, on this family's cleartext transport - so blocking is permitted.
function Greeting(const aSession: tPop3Session): string;
function Authenticate(const aSession: tPop3Session; const aUser: string; const aPassword: iSecretKey; var aWhy: string): Boolean;
function OpenMaildrop(const aSession: tPop3Session; var aWhy: string): Boolean;
function MessageCount(const aSession: tPop3Session): Integer;
function MessageOctets(const aSession: tPop3Session; aIndex: Integer): Int64;
function MessageUidl(const aSession: tPop3Session; aIndex: Integer): string;
function ReadMessage(const aSession: tPop3Session; aIndex: Integer; out aData: tBytes): Boolean;
function ReadHead(const aSession: tPop3Session; aIndex, aBodyLines: Integer; out aData: tBytes): Boolean;
function Commit(const aSession: tPop3Session; const aDeleted: TArray<Integer>; var aWhy: string): Boolean;
procedure SessionClosed(const aSession: tPop3Session);
inherits from TPersistent
The bounds one session enforces, as a TPersistent so a component can publish it as a sub-object. 0 MEANS UNBOUNDED on every one of them; a negative normalises to 0 on the way in.
constructor Create(aOwner: TComponent); virtual;
procedure AssignTo(Dest: TPersistent); override;
property CommandLineBytes: Integer read fCommandLineBytes write SetCommandLineBytes default cPop3MaxCommandLine;
property AuthLineBytes: Integer read fAuthLineBytes write SetAuthLineBytes default cPop3MaxAuthLine;
property Errors: Integer read fErrors write SetErrors default cPop3DefaultMaxErrors;
property AuthFailures: Integer read fAuthFailures write SetAuthFailures default cPop3DefaultMaxAuthFailures;
ONE CONNECTION'S POP3 CONVERSATION.
Drive it with Start (which queues the greeting) and then Receive for every octet the peer sends. Replies leave through OnOutput, COALESCED: everything one Receive produced is emitted in a single call.
NOT THREAD-SAFE, and does not need to be: it belongs to one connection.
constructor Create(const aPolicy: iPop3ServerPolicy; aLimits: tPop3Limits = nil);
destructor Destroy; override;
procedure Start;
procedure Receive(aData: Pointer; aLen: Integer);
procedure SecureChannelEstablished;
property State: tPop3State read fState;
property Limits: tPop3Limits read fLimits;
property PendingInputLen: Integer read fInLen;
property MessageCount: Integer read fCount;
property AuthUser: string read fAuthUser;
property SecureChannel: Boolean read fSecureChannel write fSecureChannel;
property RemoteAddress: string read fRemoteAddress write fRemoteAddress;
property ServerName: string read fServerName write fServerName;
property AuthMechanisms: tPop3ServerAuths read fAuthMechanisms write fAuthMechanisms;
property OfferStartTLS: Boolean read fOfferStartTLS write fOfferStartTLS;
property OnOutput: tPop3OutputEvent read fOnOutput write fOnOutput;
property OnException: tPop3ExceptionEvent read fOnException write fOnException;
property OnStoreUnavailable: tPop3StoreUnavailableEvent read fOnStoreUnavailable write fOnStoreUnavailable;
property OnCloseRequested: TNotifyEvent read fOnCloseRequested write fOnCloseRequested;
property OnStartTLSRequested: TNotifyEvent read fOnStartTLSRequested write fOnStartTLSRequested;
RFC 1939 sec.3 states three; pssStartTLS and pssClosing are this engine's, and are appended so every existing ordinal stays put.
pssStartTLS is the window between the +OK that promises a handshake and the handshake itself. It exists to STOP THE LINE LOOP DEAD: everything the peer pipelined behind STLS has already been destroyed by then, and no further command may be read from a buffer that is about to stop being cleartext.
The SASL mechanisms this engine can run (RFC 5034). Named psa* rather than ssa*, which StreamSec.Mail.SMTPEngine already uses for its own set - a retrieval component may use both units, and Delphi would resolve a duplicate to whichever came last in the uses clause, silently. USER/PASS IS A MEMBER, not the absence of the others. It used to be modelled by emptiness, and that made the documented posture "SASL off, password login on" INEXPRESSIBLE: an operator who wanted it set [] and got a server that authenticated nobody while the port came up and logged its normal listening line. Now [psaUserPass] says exactly that, and [] still means no authentication at all - which is the family's idiom and stays fail-closed.
Reply octets. No Sender, because this is an engine rather than a component - the same shape as tSmtpOutputEvent.