Something went wrong. Try again.
FTP for Zig 0.16: a client, a server, and the wire protocol as a layer you can use on its own, all built on std.Io.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Who is allowed in, and what they may do once they are.//!//! FTP's login is three commands — `USER`, then `PASS` if the server asks for//! one, then `ACCT` if it asks for that — and each of them is answered with//! "yes", "no", or "not yet, send the next one". That three-way answer is what//! `Outcome` is, and it is why this is an interface rather than a table of//! passwords: a server that checks a name against LDAP, or that lets anonymous//! users in but demands a password from everybody else, is expressing itself//! entirely in which of the three it returns and when.//!//! **The password arrives in clear text.** RFC 959 has no other way to send//! one, and the fix is RFC 4217's `AUTH TLS` before the login rather than//! anything that can be done here. A server that will accept a real password//! over a plain connection is a server that will leak it; `Session` reports//! whether the connection is protected so that an implementation can refuse.
const std = @import("std");const ascii = std.ascii;
const Auth = @This();
userdata: ?*anyopaque,vtable: *const VTable,
/// What a logged-in session may do. Checked by `Session` before the file/// system is asked, so that a read-only login is read-only whatever the file/// system would have allowed.pub const Access = packed struct { /// `RETR`, `LIST`, `NLST`, `MLSD`, `MLST`, `SIZE`, `MDTM`. read: bool = false, /// `STOR`, `STOU`, `APPE`. write: bool = false, /// `DELE`, `RMD`. delete: bool = false, /// `MKD`. make_directory: bool = false, /// `RNFR` and `RNTO`. rename: bool = false,
/// Everything: what a login to one's own account gets. pub const all: Access = .{ .read = true, .write = true, .delete = true, .make_directory = true, .rename = true, };
/// Look but do not touch: what an anonymous login should get unless there /// is a specific reason otherwise. pub const read_only: Access = .{ .read = true };
/// Nothing at all. pub const none: Access = .{};};
/// The answer to one of the three login commands.pub const Outcome = union(enum) { /// Logged in. 230. accepted: Access, /// Send `PASS`. 331. need_password, /// Send `ACCT`. 332. need_account, /// No. 530. rejected,};
/// What the session knows about the connection when it asks. An/// implementation that will not accept a password in clear text refuses on/// `protected` being false.pub const Context = struct { /// The name from `USER`. Present for all three calls, because `PASS` and /// `ACCT` are meaningless without it. user: []const u8, /// Whether the control connection is encrypted. Always false today: this /// library does not implement TLS. It is here so that the interface does /// not have to change when something wraps it in one, and so that an /// implementation written now can already refuse to leak a password. protected: bool = false,};
pub const VTable = struct { /// `USER`. Returning `accepted` logs the session in on the name alone, /// which is how a server that wants no password says so — and the reason /// `Client.login` does not send one unless asked. user: *const fn (userdata: ?*anyopaque, context: Context) Outcome, /// `PASS`. password: *const fn (userdata: ?*anyopaque, context: Context, password: []const u8) Outcome, /// `ACCT`. A server with no notion of accounts returns `rejected`, which /// is what `simple` does. account: *const fn (userdata: ?*anyopaque, context: Context, account: []const u8) Outcome,};
pub fn user(a: Auth, context: Context) Outcome { return a.vtable.user(a.userdata, context);}
pub fn password(a: Auth, context: Context, secret: []const u8) Outcome { return a.vtable.password(a.userdata, context, secret);}
pub fn account(a: Auth, context: Context, name: []const u8) Outcome { return a.vtable.account(a.userdata, context, name);}
/// Accepts `anonymous` and `ftp` with any password, read-only, and rejects/// every other name.////// Read-only is the default on purpose. An anonymous server that accepts/// uploads is a server that will be found and filled with somebody else's/// files within the week; `anonymousWith` is how to ask for it anyway.pub const anonymous: Auth = .{ .userdata = null, .vtable = &anonymous_vtable };
const anonymous_vtable: VTable = .{ .user = struct { fn f(_: ?*anyopaque, context: Context) Outcome { if (!isAnonymousName(context.user)) return .rejected; // The password is asked for and then ignored, because the // convention is that it is the user's email address and because a // client that is not asked for one prints nothing to say why it // was let in. return .need_password; } }.f, .password = struct { fn f(_: ?*anyopaque, context: Context, _: []const u8) Outcome { if (!isAnonymousName(context.user)) return .rejected; return .{ .accepted = .read_only }; } }.f, .account = struct { fn f(_: ?*anyopaque, _: Context, _: []const u8) Outcome { return .rejected; } }.f,};
fn isAnonymousName(name: []const u8) bool { return ascii.eqlIgnoreCase(name, "anonymous") or ascii.eqlIgnoreCase(name, "ftp");}
/// Accepts `anonymous` and `ftp` with any password, granting `access`.////// The `Simple` it returns must outlive the sessions that use it, because the/// `Auth` holds a pointer to it.pub fn anonymousWith(state: *Simple, access: Access) Auth { state.* = .{ .access = access }; return .{ .userdata = state, .vtable = &simple_vtable };}
/// The state behind `anonymousWith` and `single`.pub const Simple = struct { access: Access = .read_only, /// The one name accepted, or null for the anonymous names. name: ?[]const u8 = null, /// The password required, or null to accept any. secret: ?[]const u8 = null,
fn matches(state: *const Simple, name: []const u8) bool { return if (state.name) |expected| std.mem.eql(u8, name, expected) else isAnonymousName(name); }};
/// Accepts exactly one name and password.////// For a server with one user, which is most servers that are not public. The/// password is compared in constant time, which matters less than it looks —/// the whole exchange is in clear text — but costs nothing and stops the/// comparison itself from being the weakest part.////// The `Simple` must outlive the sessions that use it.pub fn single(state: *Simple, name: []const u8, secret: []const u8, access: Access) Auth { state.* = .{ .access = access, .name = name, .secret = secret }; return .{ .userdata = state, .vtable = &simple_vtable };}
const simple_vtable: VTable = .{ .user = struct { fn f(userdata: ?*anyopaque, context: Context) Outcome { const state: *const Simple = @ptrCast(@alignCast(userdata)); if (!state.matches(context.user)) return .rejected; return .need_password; } }.f, .password = struct { fn f(userdata: ?*anyopaque, context: Context, secret: []const u8) Outcome { const state: *const Simple = @ptrCast(@alignCast(userdata)); if (!state.matches(context.user)) return .rejected; if (state.secret) |expected| { // Constant time in the length that matches, which is all that // can be done: the lengths themselves differ observably, and // no comparison can hide that. if (expected.len != secret.len) return .rejected; var difference: u8 = 0; for (expected, secret) |a, b| difference |= a ^ b; if (difference != 0) return .rejected; } return .{ .accepted = state.access }; } }.f, .account = struct { fn f(_: ?*anyopaque, _: Context, _: []const u8) Outcome { return .rejected; } }.f,};
/// Accepts anybody, with full access, without asking for a password.////// For a server on a socket nothing else can reach: a test, a build step, a/// loopback-only fixture. Never for one that is listening on a network.pub const open: Auth = .{ .userdata = null, .vtable = &open_vtable };
const open_vtable: VTable = .{ .user = struct { fn f(_: ?*anyopaque, _: Context) Outcome { return .{ .accepted = .all }; } }.f, .password = struct { fn f(_: ?*anyopaque, _: Context, _: []const u8) Outcome { return .{ .accepted = .all }; } }.f, .account = struct { fn f(_: ?*anyopaque, _: Context, _: []const u8) Outcome { return .{ .accepted = .all }; } }.f,};
/// Refuses everybody. What a server is configured with before it is/// configured with anything.pub const closed: Auth = .{ .userdata = null, .vtable = &closed_vtable };
const closed_vtable: VTable = .{ .user = struct { fn f(_: ?*anyopaque, _: Context) Outcome { return .rejected; } }.f, .password = struct { fn f(_: ?*anyopaque, _: Context, _: []const u8) Outcome { return .rejected; } }.f, .account = struct { fn f(_: ?*anyopaque, _: Context, _: []const u8) Outcome { return .rejected; } }.f,};
// -- tests ----------------------------------------------------------------
const testing = std.testing;
test "anonymous" { try testing.expectEqual(Outcome.need_password, anonymous.user(.{ .user = "anonymous" })); try testing.expectEqual(Outcome.need_password, anonymous.user(.{ .user = "FTP" })); try testing.expectEqual(Outcome.rejected, anonymous.user(.{ .user = "jeff" })); try testing.expectEqual( Outcome{ .accepted = .read_only }, anonymous.password(.{ .user = "anonymous" }, "nobody@example.com"), ); // A password for a name that was never accepted must not be accepted // either, however the client got here. try testing.expectEqual(Outcome.rejected, anonymous.password(.{ .user = "root" }, ""));}
test "a single account" { var state: Simple = undefined; const auth: Auth = .single(&state, "jeff", "hunter2", .all); try testing.expectEqual(Outcome.need_password, auth.user(.{ .user = "jeff" })); try testing.expectEqual(Outcome.rejected, auth.user(.{ .user = "root" })); try testing.expectEqual( Outcome{ .accepted = .all }, auth.password(.{ .user = "jeff" }, "hunter2"), ); try testing.expectEqual(Outcome.rejected, auth.password(.{ .user = "jeff" }, "hunter3")); try testing.expectEqual(Outcome.rejected, auth.password(.{ .user = "jeff" }, "hunter22")); try testing.expectEqual(Outcome.rejected, auth.password(.{ .user = "jeff" }, ""));}
test "anonymous with more than read access" { var state: Simple = undefined; const auth: Auth = .anonymousWith(&state, .all); try testing.expectEqual(Outcome.need_password, auth.user(.{ .user = "ftp" })); try testing.expectEqual(Outcome{ .accepted = .all }, auth.password(.{ .user = "ftp" }, "x"));}
test "open and closed" { try testing.expectEqual(Outcome{ .accepted = .all }, open.user(.{ .user = "anybody" })); try testing.expectEqual(Outcome.rejected, closed.user(.{ .user = "anybody" }));}