Skip to content

Run scripts: SITE RUN and SITE STATUS

A user with the permission run can start, in the background, one of the programs you listed in uftpd.cfg, and ask how it went. A backup of a folder, a database dump, a report. The server does not know what the program does (tar, mysqldump...): it only runs the ones you list, with limits. It is off by default: without any SITE_COMMAND_<name>, SITE RUN answers 502.

SITE_COMMAND_backup = /usr/local/sbin/uftp-backup
SITE_COMMAND_dump = /usr/local/sbin/uftp-dump
SITE_COMMAND_TIMEOUT_SECONDS = 600      # killed after this long (1 to 86400)
SITE_COMMAND_MAX_OUTPUT_MB = 1024       # each output file may be this big
#SITE_COMMAND_OUTPUT_DIR_backup = /ftpjobs   # optional server-side storage; files outside the user's FTP home cannot be fetched with RETR
SITE_COMMAND_MAX_PARALLEL = 2           # jobs at the same time in the server (and one at a time for each user)

USER_0 = alice
PASSWORD_0 = ...
HOME_0 = /srv/ftp/alice/
# Grant run explicitly because all excludes it.
PERMISSIONS_0 = list,download,upload,run

Install and configure a job

Install the script at an absolute path, owned by root and executable by uFTP, but not writable by the service user. By default the .out and .err files go in each requesting user's FTP home; this is the right setting when the user must download the result with RETR. Create that home and make it writable by the user running uFTP.

For output storage outside FTP homes, set SITE_COMMAND_OUTPUT_DIR_<name> to an existing absolute directory writable by uFTP. Those files are stored on the server but are not visible over FTP when the directory is outside that user's home; use this setting when another server-side process collects them. The setting is per command, not per FTP user.

For example, with the Debian package, install a script and (only if you want server-side output storage) create the external directory:

sudo install -o root -g root -m 755 contrib/site-commands/uftp-backup-folder /usr/local/sbin/uftp-backup-folder
sudo install -d -o uftp -g uftp -m 750 /ftpjobs

Add the command to the configuration. Set SITE_COMMAND_OUTPUT_DIR_<name> only if you want server-side output storage outside the FTP home; omit it when the user must fetch the result with RETR. Add run to the permissions of each account allowed to start it. Keep that user's other permissions; all by itself does not include run.

Installation Configuration file Check and restart
Debian or Ubuntu package /etc/uftp/uftpd.cfg sudo /usr/sbin/uFTP --check /etc/uftp/uftpd.cfg, then sudo systemctl restart uftpserver
Source, installed with the supplied systemd unit /etc/uftp/uftpd.cfg sudo /usr/local/sbin/uFTP --check /etc/uftp/uftpd.cfg, then sudo systemctl restart uftp
Source, started manually ./uftpd.cfg in the server's working directory (or the path given to --check) Check with ./uFTP --check ./uftpd.cfg, then restart the server process

With the supplied package and source systemd units, /srv/ftp is writable by default, so a user's home under /srv/ftp works for the default output. ProtectSystem=strict also applies to scripts: if a job writes temporary files or external output under /ftpjobs or another location, add those directories to the service's existing ReadWritePaths in a systemd drop-in. For a package use sudo systemctl edit uftpserver; for the source unit use sudo systemctl edit uftp. Preserve paths already listed, then run sudo systemctl daemon-reload and restart the service. This restriction also applies when the service runs as root. See systemd write access.

The package template is at packaging/debian/uftpd.cfg; the source service template is contrib/systemd/uftpd.cfg. Both include commented SITE RUN examples. If SITE_COMMAND_OUTPUT_DIR_<name> is omitted, uFTP writes the job's .out and .err files in that FTP user's home, which must be writable by the server process and is accessible to the user through FTP.

What the user does

> SITE RUN backup photos/2024
< 250 Job 7 started: backup (ask SITE STATUS 7)
> SITE STATUS 7
< 250 Job 7 backup: running for 12 s
> SITE STATUS 7
< 250 Job 7 backup: finished, exit 0, 31 s, output uftp-job-7.out (48211904 bytes), errors uftp-job-7.err (0 bytes)
> RETR uftp-job-7.out

The reply to SITE RUN is at once (the control connection has an idle limit); SITE STATUS tells how it is going. SITE STATUS without a number lists the last 10 jobs of the user. A job keeps running if the client disconnects. What the script writes on standard output goes in uftp-job-<id>.out, and on standard error in uftp-job-<id>.err (made before the reply: if they can not be made the reply is 451, never "started"), in the folder of the user: so tar czf - "$1" makes the archive that the user then downloads with RETR.

What the user can and can not choose

  • Which command, from your list. Not the program, not the names of the output files.
  • One parameter at most, up to 128 characters of A-Z a-z 0-9 . _ @ : / + = -, not starting with - (it would be an option) or /, and without ... It reaches the script as the first argument, never through a shell. The script must still treat it as hostile: resolve it inside the folder of the user and check it. The server narrows it, it does not make it safe.

How the script runs

  • fork and execve, no shell, in a process group of its own, with no open files except standard input (/dev/null), output and error, and a clean environment: PATH=/usr/bin:/bin, LANG=C, UFTP_JOB, UFTP_USER, UFTP_HOME (the folder of the user, which is also the current directory) and UFTP_CLIENT_IP.
  • With the privileges of uFTP. A uFTP that runs as root runs your scripts as root: run uFTP without root and give the script only what it needs (credentials of the database in a file that only the script can read, not in uftpd.cfg).
  • After SITE_COMMAND_TIMEOUT_SECONDS the whole process group gets SIGKILL; what the script left running is killed too. Each output file is limited by SITE_COMMAND_MAX_OUTPUT_MB.
  • Start and end of every job, with the exit status, are in the log. The last 64 jobs can be asked about until uFTP restarts (the list is in memory).
  • A stop of the server waits for the jobs in progress like for a transfer, up to SHUTDOWN_GRACE_SECONDS, then stops them.

A backup script

#!/bin/sh
# /usr/local/sbin/uftp-backup: makes an archive of a folder of the user and writes it on standard output
set -eu
cd "$UFTP_HOME"                                   # the folder of the user
folder=${1:-.}                                    # the parameter: hostile
case "$folder" in /*|*..*|-*) echo "not allowed" >&2; exit 2;; esac
[ -d "$folder" ] || { echo "no such folder" >&2; exit 2; }
exec tar czf - -- "$folder"

More examples (mysqldump, PostgreSQL, lftp, curl, rclone) are in contrib/site-commands/ of the repository.

Limits

  • No sandbox: what a script can do is decided by the user that runs uFTP and by the script.
  • No check of free disk space beyond the size limit of each file (use the quotas of the file system, see Disk quotas).
  • The numbers start from 1 when uFTP starts. The files of before are never overwritten: a job takes the next number whose two files are not there, so after a restart the jobs start, with a number after the ones of before. Use the number of the reply, not a fixed one; delete the old files when you have downloaded them.
  • Jobs do not survive a restart of uFTP. There is no SITE KILL: a job stops with its time limit.