2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2008-02-07 12:41:18 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2008-02-07 12:41:18 +00:00
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
let
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2013-10-12 00:04:59 +01: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 17:36:19 +01:00
|
|
|
cfg = config.services.vsftpd;
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
inherit (pkgs) vsftpd;
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2013-10-12 00:04:59 +01: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-12 00:04:59 +01:00
|
|
|
name = nixosName;
|
|
|
|
value = mkOption {
|
|
|
|
inherit description default;
|
|
|
|
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-12 00:04:59 +01:00
|
|
|
(yesNoOption "anonymousUser" "anonymous_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether to enable the anonymous FTP user.
|
2013-10-12 00:04:59 +01: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-12 00:04:59 +01:00
|
|
|
(yesNoOption "localUsers" "local_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether to enable FTP for local users.
|
2013-10-12 00:04:59 +01:00
|
|
|
'')
|
|
|
|
(yesNoOption "writeEnable" "write_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether any write activity is permitted to users.
|
2013-10-12 00:04:59 +01: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-12 00:04:59 +01: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-12 00:04:59 +01: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-12 00:04:59 +01:00
|
|
|
'')
|
|
|
|
(yesNoOption "userlistEnable" "userlist_enable" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Whether users are included.
|
2013-10-12 00:04:59 +01:00
|
|
|
'')
|
|
|
|
(yesNoOption "userlistDeny" "userlist_deny" false ''
|
|
|
|
Specifies whether <option>userlistFile</option> is a list of user
|
|
|
|
names to allow or deny access.
|
|
|
|
The default <literal>false</literal> means whitelist/allow.
|
|
|
|
'')
|
2013-10-20 18:16:12 +01:00
|
|
|
(yesNoOption "forceLocalLoginsSSL" "force_local_logins_ssl" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Only applies if <option>sslEnable</option> is true. Non anonymous (local) users
|
|
|
|
must use a secure SSL connection to send a password.
|
2013-10-12 00:04:59 +01:00
|
|
|
'')
|
2013-10-20 18:16:12 +01:00
|
|
|
(yesNoOption "forceLocalDataSSL" "force_local_data_ssl" false ''
|
2013-11-07 15:14:59 +00:00
|
|
|
Only applies if <option>sslEnable</option> is true. Non anonymous (local) users
|
|
|
|
must use a secure SSL connection for sending/receiving data on data connection.
|
2013-10-12 00:04:59 +01: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 ''
|
|
|
|
Only applies if <option>ssl_enable</option> is activated. If
|
|
|
|
enabled, this option will permit TLS v1 protocol connections.
|
|
|
|
TLS v1 connections are preferred.
|
|
|
|
'')
|
|
|
|
(yesNoOption "ssl_sslv2" "ssl_sslv2" false ''
|
|
|
|
Only applies if <option>ssl_enable</option> is activated. If
|
|
|
|
enabled, this option will permit SSL v2 protocol connections.
|
|
|
|
TLS v1 connections are preferred.
|
|
|
|
'')
|
|
|
|
(yesNoOption "ssl_sslv3" "ssl_sslv3" false ''
|
|
|
|
Only applies if <option>ssl_enable</option> is activated. If
|
|
|
|
enabled, this option will permit SSL v3 protocol connections.
|
|
|
|
TLS v1 connections are preferred.
|
|
|
|
'')
|
2013-11-07 15:14:59 +00:00
|
|
|
];
|
2013-10-12 00:04:59 +01: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-12 00:04:59 +01: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
|
2013-11-07 15:14:59 +00:00
|
|
|
listen=YES
|
|
|
|
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 20:11:29 +01: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 17:36:19 +01:00
|
|
|
in
|
2009-04-08 15:01:16 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
services.vsftpd = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2019-11-10 11:41:07 +00:00
|
|
|
enable = mkEnableOption "vsftpd";
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2013-10-12 00:04:59 +01:00
|
|
|
userlist = mkOption {
|
|
|
|
default = [];
|
2013-11-07 15:14:59 +00:00
|
|
|
description = "See <option>userlistFile</option>.";
|
2009-10-12 17:36:19 +01:00
|
|
|
};
|
|
|
|
|
2013-10-12 00:04:59 +01:00
|
|
|
userlistFile = mkOption {
|
2016-01-17 18:34:55 +00:00
|
|
|
type = types.path;
|
2013-10-12 00:04:59 +01:00
|
|
|
default = pkgs.writeText "userlist" (concatMapStrings (x: "${x}\n") cfg.userlist);
|
2016-01-17 18:34:55 +00:00
|
|
|
defaultText = "pkgs.writeText \"userlist\" (concatMapStrings (x: \"\${x}\n\") cfg.userlist)";
|
2013-10-12 00:04:59 +01:00
|
|
|
description = ''
|
|
|
|
Newline separated list of names to be allowed/denied if <option>userlistEnable</option>
|
|
|
|
is <literal>true</literal>. Meaning see <option>userlistDeny</option>.
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2013-10-12 00:04:59 +01:00
|
|
|
The default is a file containing the users from <option>userlist</option>.
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2013-10-12 00:04:59 +01:00
|
|
|
If explicitely set to null userlist_file will not be set in vsftpd's config file.
|
|
|
|
'';
|
2009-10-12 17:36:19 +01:00
|
|
|
};
|
|
|
|
|
2019-11-10 11:41:07 +00:00
|
|
|
enableVirtualUsers = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable the <literal>pam_userdb</literal>-based
|
|
|
|
virtual user system
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
userDbPath = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
example = "/etc/vsftpd/userDb";
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Only applies if <option>enableVirtualUsers</option> is true.
|
|
|
|
Path pointing to the <literal>pam_userdb</literal> user
|
|
|
|
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:
|
|
|
|
<programlisting>
|
|
|
|
user1
|
|
|
|
password1
|
|
|
|
user2
|
|
|
|
password2
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
You can then install <literal>pkgs.db</literal> to generate
|
|
|
|
the Berkeley DB using
|
|
|
|
<programlisting>
|
|
|
|
db_load -T -t hash -f logins.txt userDb.db
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
Caution: <literal>pam_userdb</literal> will automatically
|
|
|
|
append a <literal>.db</literal> suffix to the filename you
|
|
|
|
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 17:39:08 +01:00
|
|
|
anonymousUserHome = mkOption {
|
2013-11-07 15:14:59 +00:00
|
|
|
type = types.path;
|
2013-10-20 17:39:08 +01: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 17:39:08 +01: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 21:48:27 +01: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 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
###### implementation
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2009-10-12 17:36:19 +01: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-12 00:04:59 +01: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)
|
|
|
|
&& (cfg.enableVirtualUsers -> cfg.localUsers != null);
|
|
|
|
message = "vsftpd: If enableVirtualUsers is true, you need to setup both the userDbPath and localUsers options.";
|
|
|
|
}];
|
2013-10-12 00:04:59 +01:00
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.users = {
|
|
|
|
"vsftpd" = {
|
|
|
|
uid = config.ids.uids.vsftpd;
|
|
|
|
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 15:25:56 +01:00
|
|
|
uid = config.ids.uids.ftp;
|
2009-03-06 12:26:43 +00:00
|
|
|
group = "ftp";
|
2009-10-12 17:36:19 +01:00
|
|
|
description = "Anonymous FTP user";
|
|
|
|
home = cfg.anonymousUserHome;
|
2009-03-06 12:26:43 +00:00
|
|
|
};
|
2019-09-14 18:51:29 +01:00
|
|
|
};
|
2009-03-06 12:26:43 +00:00
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.groups.ftp.gid = config.ids.gids.ftp;
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2013-10-12 00:04:59 +01:00
|
|
|
# If you really have to access root via FTP use mkOverride or userlistDeny
|
|
|
|
# = false and whitelist root
|
|
|
|
services.vsftpd.userlist = if cfg.userlistDeny then ["root"] else [];
|
|
|
|
|
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 17:36:19 +01:00
|
|
|
|
2013-11-07 15:14:59 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2009-10-12 17:36:19 +01: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 17:36:19 +01: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
|
|
|
}
|