2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2008-02-07 12:41:18 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2008-02-07 12:41:18 +00:00
|
|
|
|
2011-09-14 18:20:50 +00:00
|
|
|
let
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2013-10-11 23:04:59 +00:00
|
|
|
/* minimal secure setup:
|
|
|
|
|
|
|
|
enable = true;
|
|
|
|
forceLocalLoginsSSL = true;
|
|
|
|
forceLocalDataSSL = true;
|
|
|
|
userlistDeny = false;
|
|
|
|
localUsers = true;
|
|
|
|
userlist = ["non-root-user" "other-non-root-user"];
|
|
|
|
rsaCertFile = "/var/vsftpd/vsftpd.pem";
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
cfg = config.services.vsftpd;
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
inherit (pkgs) vsftpd;
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2013-10-11 23:04:59 +00:00
|
|
|
yesNoOption = nixosName: vsftpdName: default: description: {
|
|
|
|
cfgText = "${vsftpdName}=${if getAttr nixosName cfg then "YES" else "NO"}";
|
|
|
|
|
|
|
|
nixosOption = {
|
2013-11-07 15:14:59 +00:00
|
|
|
type = types.bool;
|
2013-10-11 23:04:59 +00:00
|
|
|
name = nixosName;
|
|
|
|
value = mkOption {
|
2022-08-21 09:30:49 +00:00
|
|
|
description = description;
|
|
|
|
inherit default;
|
2013-10-11 23:04:59 +00:00
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
optionDescription = [
|
2019-11-10 11:41:07 +00:00
|
|
|
(yesNoOption "allowWriteableChroot" "allow_writeable_chroot" false ''
|
|
|
|
Allow the use of writeable root inside chroot().
|
|
|
|
'')
|
|
|
|
(yesNoOption "virtualUseLocalPrivs" "virtual_use_local_privs" false ''
|
|
|
|
If enabled, virtual users will use the same privileges as local
|
|
|
|
users. By default, virtual users will use the same privileges as
|
|
|
|
anonymous users, which tends to be more restrictive (especially
|
|
|
|
in terms of write access).
|
|
|
|
'')
|
2013-10-11 23:04:59 +00:00
|
|
|
(yesNoOption "anonymousUser" "anonymous_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether to enable the anonymous FTP user.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
2017-03-19 01:52:06 +00:00
|
|
|
(yesNoOption "anonymousUserNoPassword" "no_anon_password" false ''
|
|
|
|
Whether to disable the password for the anonymous FTP user.
|
|
|
|
'')
|
2013-10-11 23:04:59 +00:00
|
|
|
(yesNoOption "localUsers" "local_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether to enable FTP for local users.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
|
|
|
(yesNoOption "writeEnable" "write_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether any write activity is permitted to users.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
|
|
|
(yesNoOption "anonymousUploadEnable" "anon_upload_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether any uploads are permitted to anonymous users.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
|
|
|
(yesNoOption "anonymousMkdirEnable" "anon_mkdir_write_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether any uploads are permitted to anonymous users.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
|
|
|
(yesNoOption "chrootlocalUser" "chroot_local_user" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether local users are confined to their home directory.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
|
|
|
(yesNoOption "userlistEnable" "userlist_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether users are included.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
|
|
|
(yesNoOption "userlistDeny" "userlist_deny" false ''
|
2022-08-21 09:30:49 +00:00
|
|
|
Specifies whether {option}`userlistFile` is a list of user
|
2013-10-11 23:04:59 +00:00
|
|
|
names to allow or deny access.
|
2022-08-21 09:30:49 +00:00
|
|
|
The default `false` means whitelist/allow.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
2013-10-20 17:16:12 +00:00
|
|
|
(yesNoOption "forceLocalLoginsSSL" "force_local_logins_ssl" false ''
|
2022-08-21 09:30:49 +00:00
|
|
|
Only applies if {option}`sslEnable` is true. Non anonymous (local) users
|
2013-11-07 15:14:59 +00:00
|
|
|
must use a secure SSL connection to send a password.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
2013-10-20 17:16:12 +00:00
|
|
|
(yesNoOption "forceLocalDataSSL" "force_local_data_ssl" false ''
|
2022-08-21 09:30:49 +00:00
|
|
|
Only applies if {option}`sslEnable` is true. Non anonymous (local) users
|
2013-11-07 15:14:59 +00:00
|
|
|
must use a secure SSL connection for sending/receiving data on data connection.
|
2013-10-11 23:04:59 +00:00
|
|
|
'')
|
2014-12-09 09:58:34 +00:00
|
|
|
(yesNoOption "portPromiscuous" "port_promiscuous" false ''
|
|
|
|
Set to YES if you want to disable the PORT security check that ensures that
|
|
|
|
outgoing data connections can only connect to the client. Only enable if you
|
|
|
|
know what you are doing!
|
|
|
|
'')
|
2019-11-10 11:41:07 +00:00
|
|
|
(yesNoOption "ssl_tlsv1" "ssl_tlsv1" true ''
|
2022-08-21 09:30:49 +00:00
|
|
|
Only applies if {option}`ssl_enable` is activated. If
|
2019-11-10 11:41:07 +00:00
|
|
|
enabled, this option will permit TLS v1 protocol connections.
|
|
|
|
TLS v1 connections are preferred.
|
|
|
|
'')
|
|
|
|
(yesNoOption "ssl_sslv2" "ssl_sslv2" false ''
|
2022-08-21 09:30:49 +00:00
|
|
|
Only applies if {option}`ssl_enable` is activated. If
|
2019-11-10 11:41:07 +00:00
|
|
|
enabled, this option will permit SSL v2 protocol connections.
|
|
|
|
TLS v1 connections are preferred.
|
|
|
|
'')
|
|
|
|
(yesNoOption "ssl_sslv3" "ssl_sslv3" false ''
|
2022-08-21 09:30:49 +00:00
|
|
|
Only applies if {option}`ssl_enable` is activated. If
|
2019-11-10 11:41:07 +00:00
|
|
|
enabled, this option will permit SSL v3 protocol connections.
|
|
|
|
TLS v1 connections are preferred.
|
|
|
|
'')
|
2013-11-07 15:14:59 +00:00
|
|
|
];
|
2013-10-11 23:04:59 +00:00
|
|
|
|
2013-11-07 15:14:59 +00:00
|
|
|
configFile = pkgs.writeText "vsftpd.conf"
|
|
|
|
''
|
|
|
|
${concatMapStrings (x: "${x.cfgText}\n") optionDescription}
|
|
|
|
${optionalString (cfg.rsaCertFile != null) ''
|
2013-10-27 20:09:46 +00:00
|
|
|
ssl_enable=YES
|
2013-10-11 23:04:59 +00:00
|
|
|
rsa_cert_file=${cfg.rsaCertFile}
|
2013-11-07 15:14:59 +00:00
|
|
|
''}
|
2016-03-09 01:22:19 +00:00
|
|
|
${optionalString (cfg.rsaKeyFile != null) ''
|
|
|
|
rsa_private_key_file=${cfg.rsaKeyFile}
|
|
|
|
''}
|
2013-11-07 15:14:59 +00:00
|
|
|
${optionalString (cfg.userlistFile != null) ''
|
|
|
|
userlist_file=${cfg.userlistFile}
|
|
|
|
''}
|
2013-11-07 15:18:24 +00:00
|
|
|
background=YES
|
2020-05-25 18:14:20 +00:00
|
|
|
listen=NO
|
|
|
|
listen_ipv6=YES
|
2013-11-07 15:14:59 +00:00
|
|
|
nopriv_user=vsftpd
|
|
|
|
secure_chroot_dir=/var/empty
|
2019-11-10 11:41:07 +00:00
|
|
|
${optionalString (cfg.localRoot != null) ''
|
|
|
|
local_root=${cfg.localRoot}
|
|
|
|
''}
|
2013-11-07 15:18:24 +00:00
|
|
|
syslog_enable=YES
|
2018-08-20 19:11:29 +00:00
|
|
|
${optionalString (pkgs.stdenv.hostPlatform.system == "x86_64-linux") ''
|
2013-11-07 15:35:54 +00:00
|
|
|
seccomp_sandbox=NO
|
|
|
|
''}
|
2014-02-10 21:23:59 +00:00
|
|
|
anon_umask=${cfg.anonymousUmask}
|
2016-11-03 04:06:47 +00:00
|
|
|
${optionalString cfg.anonymousUser ''
|
|
|
|
anon_root=${cfg.anonymousUserHome}
|
|
|
|
''}
|
2019-11-10 11:41:07 +00:00
|
|
|
${optionalString cfg.enableVirtualUsers ''
|
|
|
|
guest_enable=YES
|
|
|
|
guest_username=vsftpd
|
|
|
|
''}
|
2020-03-11 20:15:47 +00:00
|
|
|
pam_service_name=vsftpd
|
2016-11-03 04:06:47 +00:00
|
|
|
${cfg.extraConfig}
|
2013-11-07 15:14:59 +00:00
|
|
|
'';
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
in
|
2009-04-08 14:01:16 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
services.vsftpd = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2019-11-10 11:41:07 +00:00
|
|
|
enable = mkEnableOption "vsftpd";
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2013-10-11 23:04:59 +00:00
|
|
|
userlist = mkOption {
|
|
|
|
default = [];
|
2022-02-28 21:18:28 +00:00
|
|
|
type = types.listOf types.str;
|
2013-11-07 15:14:59 +00:00
|
|
|
description = "See {option}`userlistFile`.";
|
2009-10-12 16:36:19 +00:00
|
|
|
};
|
|
|
|
|
2013-10-11 23:04:59 +00:00
|
|
|
userlistFile = mkOption {
|
2016-01-17 18:34:55 +00:00
|
|
|
type = types.path;
|
2013-10-11 23:04:59 +00:00
|
|
|
default = pkgs.writeText "userlist" (concatMapStrings (x: "${x}\n") cfg.userlist);
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression ''pkgs.writeText "userlist" (concatMapStrings (x: "''${x}\n") cfg.userlist)'';
|
2013-10-11 23:04:59 +00:00
|
|
|
description = ''
|
|
|
|
Newline separated list of names to be allowed/denied if {option}`userlistEnable`
|
|
|
|
is `true`. Meaning see {option}`userlistDeny`.
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2013-10-11 23:04:59 +00:00
|
|
|
The default is a file containing the users from {option}`userlist`.
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2022-12-18 00:31:14 +00:00
|
|
|
If explicitly set to null userlist_file will not be set in vsftpd's config file.
|
2013-10-11 23:04:59 +00:00
|
|
|
'';
|
2009-10-12 16:36:19 +00:00
|
|
|
};
|
|
|
|
|
2019-11-10 11:41:07 +00:00
|
|
|
enableVirtualUsers = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable the `pam_userdb`-based
|
|
|
|
virtual user system
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
userDbPath = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
example = "/etc/vsftpd/userDb";
|
|
|
|
default = null;
|
2022-08-21 09:30:49 +00:00
|
|
|
description = ''
|
|
|
|
Only applies if {option}`enableVirtualUsers` is true.
|
|
|
|
Path pointing to the `pam_userdb` user
|
2019-11-10 11:41:07 +00:00
|
|
|
database used by vsftpd to authenticate the virtual users.
|
|
|
|
|
|
|
|
This user list should be stored in the Berkeley DB database
|
|
|
|
format.
|
|
|
|
|
|
|
|
To generate a new user database, create a text file, add
|
|
|
|
your users using the following format:
|
2022-08-21 09:30:49 +00:00
|
|
|
```
|
2019-11-10 11:41:07 +00:00
|
|
|
user1
|
|
|
|
password1
|
|
|
|
user2
|
|
|
|
password2
|
2022-08-21 09:30:49 +00:00
|
|
|
```
|
2019-11-10 11:41:07 +00:00
|
|
|
|
2022-08-21 09:30:49 +00:00
|
|
|
You can then install `pkgs.db` to generate
|
2019-11-10 11:41:07 +00:00
|
|
|
the Berkeley DB using
|
2022-08-21 09:30:49 +00:00
|
|
|
```
|
2019-11-10 11:41:07 +00:00
|
|
|
db_load -T -t hash -f logins.txt userDb.db
|
2022-08-21 09:30:49 +00:00
|
|
|
```
|
2019-11-10 11:41:07 +00:00
|
|
|
|
2022-08-21 09:30:49 +00:00
|
|
|
Caution: `pam_userdb` will automatically
|
|
|
|
append a `.db` suffix to the filename you
|
2019-11-10 11:41:07 +00:00
|
|
|
provide though this option. This option shouldn't include
|
|
|
|
this filetype suffix.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
localRoot = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "/var/www/$USER";
|
|
|
|
description = ''
|
|
|
|
This option represents a directory which vsftpd will try to
|
|
|
|
change into after a local (i.e. non- anonymous) login.
|
|
|
|
|
|
|
|
Failure is silently ignored.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-10-20 16:39:08 +00:00
|
|
|
anonymousUserHome = mkOption {
|
2013-11-07 15:14:59 +00:00
|
|
|
type = types.path;
|
2013-10-20 16:39:08 +00:00
|
|
|
default = "/home/ftp/";
|
2013-11-07 15:14:59 +00:00
|
|
|
description = ''
|
|
|
|
Directory to consider the HOME of the anonymous user.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
rsaCertFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = "RSA certificate file.";
|
2013-10-20 16:39:08 +00:00
|
|
|
};
|
|
|
|
|
2016-03-09 01:22:19 +00:00
|
|
|
rsaKeyFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = "RSA private key file.";
|
|
|
|
};
|
|
|
|
|
2014-02-10 21:23:59 +00:00
|
|
|
anonymousUmask = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.str;
|
2014-02-10 21:23:59 +00:00
|
|
|
default = "077";
|
|
|
|
example = "002";
|
|
|
|
description = "Anonymous write umask.";
|
|
|
|
};
|
|
|
|
|
2016-11-03 04:06:47 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = "ftpd_banner=Hello";
|
|
|
|
description = "Extra configuration to add at the bottom of the generated configuration file.";
|
|
|
|
};
|
|
|
|
|
2013-11-07 15:14:59 +00:00
|
|
|
} // (listToAttrs (catAttrs "nixosOption" optionDescription));
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
###### implementation
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2019-11-10 11:41:07 +00:00
|
|
|
assertions = [
|
2013-11-07 15:14:59 +00:00
|
|
|
{ assertion =
|
2013-10-11 23:04:59 +00:00
|
|
|
(cfg.forceLocalLoginsSSL -> cfg.rsaCertFile != null)
|
|
|
|
&& (cfg.forceLocalDataSSL -> cfg.rsaCertFile != null);
|
|
|
|
message = "vsftpd: If forceLocalLoginsSSL or forceLocalDataSSL is true then a rsaCertFile must be provided!";
|
2019-11-10 11:41:07 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = (cfg.enableVirtualUsers -> cfg.userDbPath != null)
|
2024-05-02 21:24:52 +00:00
|
|
|
&& (cfg.enableVirtualUsers -> cfg.localUsers);
|
2019-11-10 11:41:07 +00:00
|
|
|
message = "vsftpd: If enableVirtualUsers is true, you need to setup both the userDbPath and localUsers options.";
|
|
|
|
}];
|
2013-10-11 23:04:59 +00:00
|
|
|
|
2019-09-14 17:51:29 +00:00
|
|
|
users.users = {
|
|
|
|
"vsftpd" = {
|
2021-09-14 12:00:00 +00:00
|
|
|
group = "vsftpd";
|
|
|
|
isSystemUser = true;
|
2019-09-14 17:51:29 +00:00
|
|
|
description = "VSFTPD user";
|
|
|
|
home = if cfg.localRoot != null
|
|
|
|
then cfg.localRoot # <= Necessary for virtual users.
|
|
|
|
else "/homeless-shelter";
|
|
|
|
};
|
|
|
|
} // optionalAttrs cfg.anonymousUser {
|
|
|
|
"ftp" = { name = "ftp";
|
2009-05-29 14:25:56 +00:00
|
|
|
uid = config.ids.uids.ftp;
|
2009-03-06 12:26:43 +00:00
|
|
|
group = "ftp";
|
2009-10-12 16:36:19 +00:00
|
|
|
description = "Anonymous FTP user";
|
|
|
|
home = cfg.anonymousUserHome;
|
2009-03-06 12:26:43 +00:00
|
|
|
};
|
2019-09-14 17:51:29 +00:00
|
|
|
};
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2021-09-14 12:00:00 +00:00
|
|
|
users.groups.vsftpd = {};
|
2018-06-29 23:58:35 +00:00
|
|
|
users.groups.ftp.gid = config.ids.gids.ftp;
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2013-10-11 23:04:59 +00:00
|
|
|
# If you really have to access root via FTP use mkOverride or userlistDeny
|
|
|
|
# = false and whitelist root
|
2023-06-25 09:47:43 +00:00
|
|
|
services.vsftpd.userlist = optional cfg.userlistDeny "root";
|
2013-10-11 23:04:59 +00:00
|
|
|
|
2019-11-10 11:41:07 +00:00
|
|
|
systemd = {
|
|
|
|
tmpfiles.rules = optional cfg.anonymousUser
|
|
|
|
#Type Path Mode User Gr Age Arg
|
|
|
|
"d '${builtins.toString cfg.anonymousUserHome}' 0555 'ftp' 'ftp' - -";
|
|
|
|
services.vsftpd = {
|
|
|
|
description = "Vsftpd Server";
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2013-11-07 15:14:59 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2013-11-07 15:14:59 +00:00
|
|
|
serviceConfig.ExecStart = "@${vsftpd}/sbin/vsftpd vsftpd ${configFile}";
|
|
|
|
serviceConfig.Restart = "always";
|
2013-11-07 15:18:24 +00:00
|
|
|
serviceConfig.Type = "forking";
|
2009-10-12 16:36:19 +00:00
|
|
|
};
|
2019-11-10 11:41:07 +00:00
|
|
|
};
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2019-11-10 11:41:07 +00:00
|
|
|
security.pam.services.vsftpd.text = mkIf (cfg.enableVirtualUsers && cfg.userDbPath != null)''
|
|
|
|
auth required pam_userdb.so db=${cfg.userDbPath}
|
|
|
|
account required pam_userdb.so db=${cfg.userDbPath}
|
|
|
|
'';
|
2009-03-06 12:26:43 +00:00
|
|
|
};
|
2008-02-07 12:41:18 +00:00
|
|
|
}
|