Authentication hook¶
AUTH_HOOK lets uFTP ask your own program whether a user may log in and with which settings. The users can live in a database, in LDAP, behind a web API or in a file, they can change without restarting uFTP, and
the server has no database library in it. It is off by default.
AUTH_HOOK = /usr/local/sbin/uftp-auth
AUTH_HOOK_TIMEOUT_SECONDS = 5 # killed after this long (1 to 60)
AUTH_HOOK_MAX_PARALLEL = 4 # at most this many at the same time (1 to 32)
The path must be absolute, an executable file, and not writable by others (anybody who can change it chooses who logs in). If not, uFTP does not start and says why.
When uFTP calls it¶
The hook is the last step of a login, only for a name that nobody else knows:
- a name in
BLOCK_USER_nis refused, the hook is not called; - an address locked out after too many failed logins is refused first (the lock-out comes before the hook);
- PAM (if on), then the users of
uftpd.cfgandUSERS_FILE: a known name with a wrong password is refused and the hook is not called (it can not overrule a local account); - an unknown name: the hook.
So the users of the configuration, for example an administrator, work even when the database is down.
What the hook receives¶
It is started as hook auth (one argument). The name and the password arrive on standard input, two lines:
alice
the-password
They are never in the arguments or in the environment. The hook runs with the privileges of uFTP, in a process group of its own, with a clean environment (PATH=/usr/bin:/bin, LANG=C, UFTP_HOOK_VERSION=1 and
UFTP_CLIENT_IP) and no open files except standard input, output and error. It runs in a thread of its own, so the other clients are not kept waiting.
What the hook answers¶
| Exit status | Meaning |
|---|---|
0 |
Accepted. Settings on standard output, see below. |
1 |
Refused (wrong name or password). |
| anything else, or no answer in time, or it does not start | Not available. The user gets the same 530 Invalid username or password as for a wrong password: never an access. The reason is in the log. |
Settings, one key=value per line (spaces around the = do not matter; empty lines and lines that start with # are skipped; unknown keys are ignored, so new ones can be added later: check the spelling).
More than 32 lines or 8 KB, or a key given twice, refuse the login. They are all the settings that a user of uftpd.cfg can have:
| Key | Meaning |
|---|---|
home= |
Required. An absolute path to a folder that exists: the root of the user. |
owner_user=, owner_group= |
Owner of the new files (both, or none). A name that does not exist refuses the login. |
permissions= |
list,download,upload,overwrite,delete,rename,mkdir,chmod,run, or all (without run), or none (as PERMISSIONS_n). |
readonly= |
1 or true: the same as permissions=list,download (not together with permissions=). 0 or false: nothing. Any other value refuses the login. |
max_connections= |
Connections of this account (0 = no limit). |
max_download_speed=, max_upload_speed= |
KB/s of the user, as MAX_DOWNLOAD_SPEED_n. |
allow_from=, deny_from= |
From where the user may log in: addresses and networks with commas, as ALLOW_FROM_n and DENY_FROM_n. deny_from= wins. From elsewhere the answer is the one of a wrong password, and it counts as a failed login. An empty value refuses the login. |
umask= |
Octal, 000 to 077, as UMASK_n. Without it: UMASK. |
Without permissions= and readonly= the user can do everything (except run). A value that is not valid refuses the login and is written to the log: a wrong setting must never give more rights than intended.
The first line the hook writes on standard error (cut, without control characters) goes to the uFTP log next to the result.
To try AUTH_HOOK with a local user file, use the file-based hook in contrib/auth-hooks/file/. It matches usernames exactly and stores password hashes. Hook examples
Examples¶
Ready-made hooks are in contrib/auth-hooks/ of the repository, each with a README. They are short scripts (Python 3, standard library): read the one you choose and adapt it.
| Example | Users in | Tried in the uFTP tests |
|---|---|---|
file/ |
a text file, passwords as hashes, with add, remove and list |
yes, with a real server |
sqlite/ |
a SQLite database (init, add, disable, enable) |
yes, with a real server |
mysql/ |
MySQL or MariaDB (mysql client) |
the logic, with a stand-in client; not a real MySQL |
postgres/ |
PostgreSQL (psql client) |
the logic, with a stand-in client; not a real PostgreSQL |
ldap/ |
an LDAP directory (a bind with ldapwhoami) |
the logic, with a stand-in client; not a real LDAP |
api/ |
a web API (JSON over HTTPS) | with a local test server; not your API |
All of them: refuse a name that is not [A-Za-z0-9._@-] up to 64 characters before anything else (the name comes from the client), keep the password on standard input only, use the password hashes that uFTP --hash-password makes (PBKDF2-SHA256), spend the same time on a name that does not exist, and answer 3 ("not available") when the database does not answer, never 1 or 0.
Limits and safety¶
- Time: after
AUTH_HOOK_TIMEOUT_SECONDSthe whole process group getsSIGKILL; what the hook leaves running in its group is killed when it ends. - Parallel: at most
AUTH_HOOK_MAX_PARALLELhooks at the same time; with more, the login is refused at once as "not available" and the client tries again. A person who tries many names can not fill the machine with processes. - Failed logins count: a refusal, or "not available", counts as a failed login for the lock-out of the address, like a wrong password.
- No cache: every login calls the hook. If the database is slow, cache inside the hook.
- Not hidden: an unknown name (it calls the hook) takes a different time from a local name with a wrong password. The names of the local users are not secret for somebody who measures; this was already true with PAM.
- No sandbox: what the program can do is decided by the user that runs uFTP. Run uFTP without root.