2009-10-12 17:36:19 +01:00
|
|
|
{ config, pkgs, ... }:
|
2008-02-07 12:41:18 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
with pkgs.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 = [
|
|
|
|
(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
|
|
|
'')
|
|
|
|
(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
|
|
|
'')
|
|
|
|
(yesNoOption "ssl_tlsv1" "ssl_tlsv1" true '' '')
|
|
|
|
(yesNoOption "ssl_sslv2" "ssl_sslv2" false '' '')
|
|
|
|
(yesNoOption "ssl_sslv3" "ssl_sslv3" false '' '')
|
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
|
|
|
''}
|
|
|
|
${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
|
2013-11-07 15:18:24 +00:00
|
|
|
syslog_enable=YES
|
2013-11-07 15:35:54 +00:00
|
|
|
${optionalString (pkgs.stdenv.system == "x86_64-linux") ''
|
|
|
|
seccomp_sandbox=NO
|
|
|
|
''}
|
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
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "Whether to enable the vsftpd FTP server.";
|
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 {
|
|
|
|
default = pkgs.writeText "userlist" (concatMapStrings (x: "${x}\n") cfg.userlist);
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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 {
|
|
|
|
|
2013-11-07 15:14:59 +00:00
|
|
|
assertions = singleton
|
|
|
|
{ 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!";
|
2013-11-07 15:14:59 +00:00
|
|
|
};
|
2013-10-12 00:04:59 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
users.extraUsers =
|
|
|
|
[ { name = "vsftpd";
|
2009-05-29 15:25:56 +01:00
|
|
|
uid = config.ids.uids.vsftpd;
|
2009-03-06 12:26:43 +00:00
|
|
|
description = "VSFTPD user";
|
|
|
|
home = "/homeless-shelter";
|
|
|
|
}
|
2013-11-07 15:14:59 +00:00
|
|
|
] ++ optional cfg.anonymousUser
|
2009-03-06 12:26:43 +00:00
|
|
|
{ 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
|
|
|
};
|
|
|
|
|
2013-11-07 15:14:59 +00:00
|
|
|
users.extraGroups.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 [];
|
|
|
|
|
2013-11-07 15:14:59 +00:00
|
|
|
systemd.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
|
|
|
|
|
|
|
preStart =
|
2013-11-07 15:14:59 +00:00
|
|
|
optionalString cfg.anonymousUser
|
|
|
|
''
|
2011-07-28 10:42:20 +01:00
|
|
|
mkdir -p -m 555 ${cfg.anonymousUserHome}
|
2010-05-08 20:12:36 +01:00
|
|
|
chown -R ftp:ftp ${cfg.anonymousUserHome}
|
2013-11-07 15:14:59 +00:00
|
|
|
'';
|
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
|
|
|
};
|
2009-03-06 12:26:43 +00:00
|
|
|
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2008-02-07 12:41:18 +00:00
|
|
|
}
|