// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What a server said it can do, in the reply to `FEAT`. //! //! RFC 2389 §3 exists because RFC 959 gave a client no way to ask. Before it, //! finding out whether a server had `SIZE` meant sending `SIZE` and seeing //! whether the answer was a 500 — which works, but costs a round trip per //! command and leaves a "command not understood" in the server's log for every //! client that was merely curious. `FEAT` is one round trip for all of it. //! //! A server that does not answer `FEAT` is not broken, it is merely old: //! `Client` treats a 500 or 502 here as "none of the extensions" and carries //! on with the RFC 959 commands, which every server has. const std = @import("std"); const Io = std.Io; const ascii = std.ascii; const Features = @This(); /// `MLST` and `MLSD`, the machine-readable listings of RFC 3659 §7. One /// feature line covers both. mlst: bool = false, /// `SIZE` (RFC 3659 §4). size: bool = false, /// `MDTM` (RFC 3659 §3). mdtm: bool = false, /// `REST STREAM`: a transfer may be restarted at a byte offset (RFC 3659 §5). rest_stream: bool = false, /// `TVFS`: pathnames behave as RFC 3659 §6 describes, so a client may build /// them itself rather than sending one `CWD` per component. tvfs: bool = false, /// `UTF8`: pathnames are UTF-8 (RFC 2640). Almost always true in fact and /// rarely advertised. utf8: bool = false, /// `EPSV` and `EPRT`, the address-family-independent data connection commands /// of RFC 2428. Advertised as two separate lines. epsv: bool = false, eprt: bool = false, /// `AUTH TLS`, the start of an FTPS session (RFC 4217). This library does not /// implement TLS, but it reports what the server offered so that a caller /// which does can act on it — and so that a caller which does not can refuse /// to send a password to a server that would have protected it. auth_tls: bool = false, /// `PBSZ` and `PROT`, which follow `AUTH` (RFC 2228 §3). pbsz: bool = false, prot: bool = false, /// `LANG`: the server can reply in more than one language (RFC 2640 §5). lang: bool = false, /// The parameters of the `MLST` line, which are the facts the server can /// report, with a `*` after each one it will send unless told otherwise: /// `type*;size*;modify*;perm*;unique;`. Borrows from the reply text it was /// parsed out of. mlst_facts: []const u8 = "", /// Reads the text of a `FEAT` reply — everything `Reply.receive` gathered, /// including its first and last lines. /// /// RFC 2389 §3.2 has each feature on a line of its own beginning with a /// space, between a first line and a last line that are prose. Rather than /// trusting that shape, every line is examined and the prose simply fails to /// match any feature name: it costs nothing and it tolerates the servers that /// forget the leading space. pub fn parse(text: []const u8) Features { var features: Features = .{}; var lines = std.mem.splitScalar(u8, text, '\n'); while (lines.next()) |raw| { const line = std.mem.trim(u8, raw, " \t\r"); const end = std.mem.findScalar(u8, line, ' ') orelse line.len; const name = line[0..end]; const parameters = std.mem.trim(u8, line[end..], " "); if (ascii.eqlIgnoreCase(name, "MLST")) { features.mlst = true; features.mlst_facts = parameters; } else if (ascii.eqlIgnoreCase(name, "SIZE")) { features.size = true; } else if (ascii.eqlIgnoreCase(name, "MDTM")) { features.mdtm = true; } else if (ascii.eqlIgnoreCase(name, "REST")) { // `REST` alone means nothing; RFC 3659 §5.1 has the parameter // name the transfer mode it can restart, and `STREAM` is the only // one this library or anything else implements. features.rest_stream = ascii.eqlIgnoreCase(parameters, "STREAM"); } else if (ascii.eqlIgnoreCase(name, "TVFS")) { features.tvfs = true; } else if (ascii.eqlIgnoreCase(name, "UTF8")) { features.utf8 = true; } else if (ascii.eqlIgnoreCase(name, "EPSV")) { features.epsv = true; } else if (ascii.eqlIgnoreCase(name, "EPRT")) { features.eprt = true; } else if (ascii.eqlIgnoreCase(name, "AUTH")) { // The parameter is a list of mechanisms, in any order and any // case, separated by spaces or semicolons depending on the // server. var mechanisms = std.mem.splitAny(u8, parameters, " ;,"); while (mechanisms.next()) |mechanism| { if (ascii.eqlIgnoreCase(mechanism, "TLS") or ascii.eqlIgnoreCase(mechanism, "TLS-C") or ascii.eqlIgnoreCase(mechanism, "SSL")) { features.auth_tls = true; } } } else if (ascii.eqlIgnoreCase(name, "PBSZ")) { features.pbsz = true; } else if (ascii.eqlIgnoreCase(name, "PROT")) { features.prot = true; } else if (ascii.eqlIgnoreCase(name, "LANG")) { features.lang = true; } } return features; } /// Whether `text` names a feature the server listed, for the features this /// struct has no field for. `text` is matched against the whole first word of /// a line, so `has(text, "SITE")` does not match `SITE CHMOD`. pub fn has(text: []const u8, name: []const u8) bool { var lines = std.mem.splitScalar(u8, text, '\n'); while (lines.next()) |raw| { const line = std.mem.trim(u8, raw, " \t\r"); const end = std.mem.findScalar(u8, line, ' ') orelse line.len; if (ascii.eqlIgnoreCase(line[0..end], name)) return true; } return false; } /// Writes the text of a `FEAT` reply for a server that has these features: /// a first line, one indented line per feature, and a last line. /// /// Pass the result to `Reply.write` with code 211, which turns the newlines /// into the multi-line framing. pub fn write(f: Features, w: *Io.Writer) Io.Writer.Error!void { try w.writeAll("Features:"); // In the order RFC 2389 §3.2 shows, which is no order at all; keeping it // stable matters only so that two runs of a server produce the same bytes. if (f.auth_tls) try w.writeAll("\n AUTH TLS"); if (f.eprt) try w.writeAll("\n EPRT"); if (f.epsv) try w.writeAll("\n EPSV"); if (f.lang) try w.writeAll("\n LANG EN*"); if (f.mdtm) try w.writeAll("\n MDTM"); if (f.mlst) try w.print("\n MLST {s}", .{f.mlst_facts}); if (f.pbsz) try w.writeAll("\n PBSZ"); if (f.prot) try w.writeAll("\n PROT"); if (f.rest_stream) try w.writeAll("\n REST STREAM"); if (f.size) try w.writeAll("\n SIZE"); if (f.tvfs) try w.writeAll("\n TVFS"); if (f.utf8) try w.writeAll("\n UTF8"); try w.writeAll("\nEnd"); } // -- tests ---------------------------------------------------------------- const testing = std.testing; test "a FEAT reply from proftpd" { const text = \\Features: \\ MDTM \\ MFF modify;UNIX.group;UNIX.mode; \\ MFMT \\ MLST modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.mode*;UNIX.owner*; \\ REST STREAM \\ SIZE \\ TVFS \\ UTF8 \\End ; const f: Features = .parse(text); try testing.expect(f.mdtm); try testing.expect(f.mlst); try testing.expect(f.rest_stream); try testing.expect(f.size); try testing.expect(f.tvfs); try testing.expect(f.utf8); try testing.expect(!f.epsv); try testing.expect(!f.auth_tls); try testing.expectEqualStrings( "modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.mode*;UNIX.owner*;", f.mlst_facts, ); // Features with no field of their own are still findable. try testing.expect(has(text, "MFMT")); try testing.expect(has(text, "mff")); try testing.expect(!has(text, "MLSD")); } test "a FEAT reply offering TLS" { const f: Features = .parse("Features:\n AUTH TLS\n PBSZ\n PROT\n EPSV\n EPRT\nEnd"); try testing.expect(f.auth_tls); try testing.expect(f.pbsz and f.prot); try testing.expect(f.epsv and f.eprt); } test "AUTH mechanisms are listed in several ways" { try testing.expect(Features.parse(" AUTH TLS").auth_tls); try testing.expect(Features.parse(" AUTH SSL;TLS;TLS-C").auth_tls); try testing.expect(Features.parse(" AUTH GSSAPI TLS").auth_tls); try testing.expect(!Features.parse(" AUTH GSSAPI").auth_tls); } test "REST without STREAM is not restartable" { try testing.expect(!Features.parse(" REST").rest_stream); try testing.expect(!Features.parse(" REST BLOCK").rest_stream); try testing.expect(Features.parse(" rest stream").rest_stream); } test "an empty FEAT reply" { const f: Features = .parse("Features:\nEnd"); try testing.expectEqual(Features{}, f); } test "FEAT round trip" { const original: Features = .{ .mlst = true, .mlst_facts = "type*;size*;modify*;perm*;", .size = true, .mdtm = true, .rest_stream = true, .tvfs = true, .utf8 = true, .epsv = true, .eprt = true, }; var buffer: [512]u8 = undefined; var w: Io.Writer = .fixed(&buffer); try original.write(&w); const parsed: Features = .parse(w.buffered()); // The fact list is the one field that is a slice, and it borrows from the // text just written rather than from the original, so it is compared by // content and then set aside. try testing.expectEqualStrings(original.mlst_facts, parsed.mlst_facts); var without_facts = parsed; without_facts.mlst_facts = original.mlst_facts; try testing.expectEqual(original, without_facts); }