sftpd
An SFTP server for file transfer over SSH with password login and a root directory.
import sftpd
This package is an SFTP server written in Salam: a server that does one thing, file transfer over SSH. It is built on the sshd package, so it logs users in the same way and uses the same host key, but it allows only the SFTP subsystem. A client cannot run a command or open a shell, which is what you want for a drop box where partners upload and download files you trust them with.
Give it a root directory and a list of users, call Listen and then Serve. Clients are the usual ones: sftp from OpenSSH, FileZilla, or the ssh package with SftpOpen, Upload and Download.
What to know before you use it:
- Users are the
Uservalues of the sshd package, with a name, a password and a home directory below the root. Login is by password. - Set
host_key_fileto the path of an OpenSSH private key. Without it the server uses a key that is built into the library and that everyone can read, which is only fine for tests. - The root is where paths start, not a security boundary. Clients see
rootas/, but a user can create a symbolic link through SFTP that points outside it, so give accounts only to people you would trust with every file the server process can write. - Connections are served one after another, so a long transfer holds up the next client.
Defaults and a free port
A fresh configuration listens on all interfaces on port 22 and serves the current directory. The program below fills in a user, binds to port 0 on the loopback address, and reads the port the system chose with Port. Nothing connects, so it runs without a network.
import sftpd
import sshd
func main:
mut cfg := sftpd.NewConfig()
println cfg.port, cfg.root
cfg.host = "127.0.0.1"
cfg.port = 0
cfg.root = "/srv/drop"
cfg.users.push(sshd.User { name = "partner", password = "s3cret", home = "/" })
println cfg.users.len()
mut srv := sftpd.New(cfg)
println sftpd.Listen(srv)
println sftpd.Port(srv) > 0
sftpd.Close(srv)
println srv.running
endRun a drop box
For a real server set the root, the host key and the users, then check Listen before Serve. Ports below 1024, including 22, need extra rights on most systems, so the example uses 2222. The password is read from SFTP_PASSWORD, so none is written in the page.
import sftpd
import sshd
import env
func main:
mut cfg := sftpd.NewConfig()
cfg.port = 2222
cfg.root = "/srv/drop"
cfg.host_key_file = "/etc/deploy/host_key"
cfg.users.push(sshd.User { name = "partner", password = env.Get("SFTP_PASSWORD"), home = "/" })
if env.Get("SFTP_PASSWORD").len() == 0:
println "set SFTP_PASSWORD first"
ret 1
end
mut srv := sftpd.New(cfg)
if not sftpd.Listen(srv):
println "cannot listen on port", cfg.port
ret 1
end
println "sftp server on port", sftpd.Port(srv)
sftpd.Serve(srv)
ret 0
endThis runs until it is stopped, so it is only checked here. Connect with sftp -P 2222 partner@host.
Which function for which job
| Job | Use |
|---|---|
| Start from the defaults | NewConfig, then set the fields of Config |
| Create the server | New |
| Open the port | Listen, then Port to read it |
| Serve clients until stopped | Serve |
| Serve exactly one connection | ServeOnce |
| Stop and free the port | Close |
Types
Config
struct Config| Name | Type | Description |
|---|---|---|
host | str = "0.0.0.0" | The address to listen on; 0.0.0.0 means every interface. |
port | int = 22 | The port to listen on; 22 by default, and 0 lets the system choose one. |
root | str = "." | The directory the clients see as /; nothing above it can be reached. |
host_key_file | str = "" | The path of the OpenSSH private key that is the identity of the server. |
users | Vector | The users who may log in, as sshd.User values. |
import sftpd
import sshd
func main:
mut cfg := sftpd.NewConfig()
println cfg.host, cfg.port, cfg.root
println cfg.host_key_file == "", cfg.users.len()
cfg.root = "/srv/drop"
cfg.users.push(sshd.User { name = "partner", password = "s3cret", home = "/" })
println cfg.root, cfg.users.len(), cfg.users.get(0).name
endServer
struct Server| Name | Type | Description |
|---|---|---|
config | Config | The configuration the server was made with; after Listen its port is the real one. |
ssh_server | sshd.Server | The SSH server underneath, set up to allow SFTP only. |
running | bool = false | True after Listen worked and until Close is called. |
import sftpd
func main:
mut cfg := sftpd.NewConfig()
cfg.host = "127.0.0.1"
cfg.port = 0
mut srv := sftpd.New(cfg)
println srv.running
println sftpd.Listen(srv), srv.running
println sftpd.Port(srv) > 0
sftpd.Close(srv)
println srv.running
endFunctions
NewConfig
func NewConfig(): ConfigThe example for this item is shown with the ones above: sftpd-Config.salam
New
func New(cfg: Config): ServerThe example for this item is shown with the ones above: sftpd-Server.salam
Listen
func Listen(srv &: Server): boolThe example for this item is shown with the ones above: sftpd-Server.salam
Close
func Close(srv &: Server)The example for this item is shown with the ones above: sftpd-Server.salam
Port
func Port(srv: Server): intListen when the configuration said 0.The example for this item is shown with the ones above: sftpd-Server.salam
ServeOnce
func ServeOnce(srv &: Server): boolimport sftpd
func main:
mut cfg := sftpd.NewConfig()
cfg.port = 2222
cfg.root = "/srv/drop"
mut srv := sftpd.New(cfg)
if not sftpd.Listen(srv):
ret 1
end
handled := sftpd.ServeOnce(srv)
println "one connection served:", handled
sftpd.Serve(srv)
sftpd.Close(srv)
ret 0
endServe
func Serve(srv &: Server)The example for this item is shown with the ones above: sftpd-Serve.salam