2009-10-12 18:36:19 +02:00
|
|
|
{ config, pkgs, ... }:
|
2008-02-07 13:41:18 +01:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
with pkgs.lib;
|
2008-02-07 13:41:18 +01:00
|
|
|
|
2011-09-14 20:20:50 +02:00
|
|
|
let
|
2009-03-06 13:26:43 +01:00
|
|
|
|
2013-10-12 01:04:59 +02: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 18:36:19 +02:00
|
|
|
cfg = config.services.vsftpd;
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
inherit (pkgs) vsftpd;
|
2009-03-06 13:26:43 +01:00
|
|
|
|
2013-10-12 01:04:59 +02:00
|
|
|
yesNoOption = nixosName: vsftpdName: default: description: {
|
|
|
|
cfgText = "${vsftpdName}=${if getAttr nixosName cfg then "YES" else "NO"}";
|
|
|
|
|
|
|
|
nixosOption = {
|
2013-11-07 16:14:59 +01:00
|
|
|
type = types.bool;
|
2013-10-12 01:04:59 +02:00
|
|
|
name = nixosName;
|
|
|
|
value = mkOption {
|
|
|
|
inherit description default;
|
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
optionDescription = [
|
|
|
|
(yesNoOption "anonymousUser" "anonymous_enable" false ''
|
2013-11-07 16:14:59 +01:00
|
|
|
Whether to enable the anonymous FTP user.
|
2013-10-12 01:04:59 +02:00
|
|
|
'')
|
|
|
|
(yesNoOption "localUsers" "local_enable" false ''
|
2013-11-07 16:14:59 +01:00
|
|
|
Whether to enable FTP for local users.
|
2013-10-12 01:04:59 +02:00
|
|
|
'')
|
|
|
|
(yesNoOption "writeEnable" "write_enable" false ''
|
2013-11-07 16:14:59 +01:00
|
|
|
Whether any write activity is permitted to users.
|
2013-10-12 01:04:59 +02:00
|
|
|
'')
|
|
|
|
(yesNoOption "anonymousUploadEnable" "anon_upload_enable" false ''
|
2013-11-07 16:14:59 +01:00
|
|
|
Whether any uploads are permitted to anonymous users.
|
2013-10-12 01:04:59 +02:00
|
|
|
'')
|
|
|
|
(yesNoOption "anonymousMkdirEnable" "anon_mkdir_write_enable" false ''
|
2013-11-07 16:14:59 +01:00
|
|
|
Whether any uploads are permitted to anonymous users.
|
2013-10-12 01:04:59 +02:00
|
|
|
'')
|
|
|
|
(yesNoOption "chrootlocalUser" "chroot_local_user" false ''
|
2013-11-07 16:14:59 +01:00
|
|
|
Whether local users are confined to their home directory.
|
2013-10-12 01:04:59 +02:00
|
|
|
'')
|
|
|
|
(yesNoOption "userlistEnable" "userlist_enable" false ''
|
2013-11-07 16:14:59 +01:00
|
|
|
Whether users are included.
|
2013-10-12 01:04:59 +02: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 19:16:12 +02:00
|
|
|
(yesNoOption "forceLocalLoginsSSL" "force_local_logins_ssl" false ''
|
2013-11-07 16:14:59 +01: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 01:04:59 +02:00
|
|
|
'')
|
2013-10-20 19:16:12 +02:00
|
|
|
(yesNoOption "forceLocalDataSSL" "force_local_data_ssl" false ''
|
2013-11-07 16:14:59 +01: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 01:04:59 +02:00
|
|
|
'')
|
|
|
|
(yesNoOption "ssl_tlsv1" "ssl_tlsv1" true '' '')
|
|
|
|
(yesNoOption "ssl_sslv2" "ssl_sslv2" false '' '')
|
|
|
|
(yesNoOption "ssl_sslv3" "ssl_sslv3" false '' '')
|
2013-11-07 16:14:59 +01:00
|
|
|
];
|
2013-10-12 01:04:59 +02:00
|
|
|
|
2013-11-07 16:14:59 +01:00
|
|
|
configFile = pkgs.writeText "vsftpd.conf"
|
|
|
|
''
|
|
|
|
${concatMapStrings (x: "${x.cfgText}\n") optionDescription}
|
|
|
|
${optionalString (cfg.rsaCertFile != null) ''
|
2013-10-27 21:09:46 +01:00
|
|
|
ssl_enable=YES
|
2013-10-12 01:04:59 +02:00
|
|
|
rsa_cert_file=${cfg.rsaCertFile}
|
2013-11-07 16:14:59 +01:00
|
|
|
''}
|
|
|
|
${optionalString (cfg.userlistFile != null) ''
|
|
|
|
userlist_file=${cfg.userlistFile}
|
|
|
|
''}
|
|
|
|
background=NO
|
|
|
|
listen=YES
|
|
|
|
nopriv_user=vsftpd
|
|
|
|
secure_chroot_dir=/var/empty
|
|
|
|
'';
|
2009-03-06 13:26:43 +01:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
in
|
2009-04-08 16:01:16 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
services.vsftpd = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "Whether to enable the vsftpd FTP server.";
|
2009-03-06 13:26:43 +01:00
|
|
|
};
|
|
|
|
|
2013-10-12 01:04:59 +02:00
|
|
|
userlist = mkOption {
|
|
|
|
default = [];
|
2013-11-07 16:14:59 +01:00
|
|
|
description = "See <option>userlistFile</option>.";
|
2009-10-12 18:36:19 +02:00
|
|
|
};
|
|
|
|
|
2013-10-12 01:04:59 +02: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 18:36:19 +02:00
|
|
|
|
2013-10-12 01:04:59 +02:00
|
|
|
The default is a file containing the users from <option>userlist</option>.
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2013-10-12 01:04:59 +02:00
|
|
|
If explicitely set to null userlist_file will not be set in vsftpd's config file.
|
|
|
|
'';
|
2009-10-12 18:36:19 +02:00
|
|
|
};
|
|
|
|
|
2013-10-20 18:39:08 +02:00
|
|
|
anonymousUserHome = mkOption {
|
2013-11-07 16:14:59 +01:00
|
|
|
type = types.path;
|
2013-10-20 18:39:08 +02:00
|
|
|
default = "/home/ftp/";
|
2013-11-07 16:14:59 +01: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 18:39:08 +02:00
|
|
|
};
|
|
|
|
|
2013-11-07 16:14:59 +01:00
|
|
|
} // (listToAttrs (catAttrs "nixosOption" optionDescription));
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-03-06 13:26:43 +01:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
###### implementation
|
2009-03-06 13:26:43 +01:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2013-11-07 16:14:59 +01:00
|
|
|
assertions = singleton
|
|
|
|
{ assertion =
|
2013-10-12 01:04:59 +02: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 16:14:59 +01:00
|
|
|
};
|
2013-10-12 01:04:59 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
users.extraUsers =
|
|
|
|
[ { name = "vsftpd";
|
2009-05-29 16:25:56 +02:00
|
|
|
uid = config.ids.uids.vsftpd;
|
2009-03-06 13:26:43 +01:00
|
|
|
description = "VSFTPD user";
|
|
|
|
home = "/homeless-shelter";
|
|
|
|
}
|
2013-11-07 16:14:59 +01:00
|
|
|
] ++ optional cfg.anonymousUser
|
2009-03-06 13:26:43 +01:00
|
|
|
{ name = "ftp";
|
2009-05-29 16:25:56 +02:00
|
|
|
uid = config.ids.uids.ftp;
|
2009-03-06 13:26:43 +01:00
|
|
|
group = "ftp";
|
2009-10-12 18:36:19 +02:00
|
|
|
description = "Anonymous FTP user";
|
|
|
|
home = cfg.anonymousUserHome;
|
2009-03-06 13:26:43 +01:00
|
|
|
};
|
|
|
|
|
2013-11-07 16:14:59 +01:00
|
|
|
users.extraGroups.ftp.gid = config.ids.gids.ftp;
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2013-10-12 01:04:59 +02: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 16:14:59 +01:00
|
|
|
systemd.services.vsftpd =
|
|
|
|
{ description = "Vsftpd Server";
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2013-11-07 16:14:59 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
preStart =
|
2013-11-07 16:14:59 +01:00
|
|
|
optionalString cfg.anonymousUser
|
|
|
|
''
|
2011-07-28 11:42:20 +02:00
|
|
|
mkdir -p -m 555 ${cfg.anonymousUserHome}
|
2010-05-08 21:12:36 +02:00
|
|
|
chown -R ftp:ftp ${cfg.anonymousUserHome}
|
2013-11-07 16:14:59 +01:00
|
|
|
'';
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2013-11-07 16:14:59 +01:00
|
|
|
serviceConfig.ExecStart = "@${vsftpd}/sbin/vsftpd vsftpd ${configFile}";
|
|
|
|
serviceConfig.Restart = "always";
|
2009-10-12 18:36:19 +02:00
|
|
|
};
|
2009-03-06 13:26:43 +01:00
|
|
|
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2008-02-07 13:41:18 +01:00
|
|
|
}
|