1 { config, lib, pkgs, ... }:
7 /* minimal secure setup:
10 forceLocalLoginsSSL = true;
11 forceLocalDataSSL = true;
14 userlist = ["non-root-user" "other-non-root-user"];
15 rsaCertFile = "/var/vsftpd/vsftpd.pem";
19 cfg = config.services.vsftpd;
21 inherit (pkgs) vsftpd;
23 yesNoOption = nixosName: vsftpdName: default: description: {
24 cfgText = "${vsftpdName}=${if getAttr nixosName cfg then "YES" else "NO"}";
30 description = description;
38 (yesNoOption "allowWriteableChroot" "allow_writeable_chroot" false ''
39 Allow the use of writeable root inside chroot().
41 (yesNoOption "virtualUseLocalPrivs" "virtual_use_local_privs" false ''
42 If enabled, virtual users will use the same privileges as local
43 users. By default, virtual users will use the same privileges as
44 anonymous users, which tends to be more restrictive (especially
45 in terms of write access).
47 (yesNoOption "anonymousUser" "anonymous_enable" false ''
48 Whether to enable the anonymous FTP user.
50 (yesNoOption "anonymousUserNoPassword" "no_anon_password" false ''
51 Whether to disable the password for the anonymous FTP user.
53 (yesNoOption "localUsers" "local_enable" false ''
54 Whether to enable FTP for local users.
56 (yesNoOption "writeEnable" "write_enable" false ''
57 Whether any write activity is permitted to users.
59 (yesNoOption "anonymousUploadEnable" "anon_upload_enable" false ''
60 Whether any uploads are permitted to anonymous users.
62 (yesNoOption "anonymousMkdirEnable" "anon_mkdir_write_enable" false ''
63 Whether any uploads are permitted to anonymous users.
65 (yesNoOption "chrootlocalUser" "chroot_local_user" false ''
66 Whether local users are confined to their home directory.
68 (yesNoOption "userlistEnable" "userlist_enable" false ''
69 Whether users are included.
71 (yesNoOption "userlistDeny" "userlist_deny" false ''
72 Specifies whether {option}`userlistFile` is a list of user
73 names to allow or deny access.
74 The default `false` means whitelist/allow.
76 (yesNoOption "forceLocalLoginsSSL" "force_local_logins_ssl" false ''
77 Only applies if {option}`sslEnable` is true. Non anonymous (local) users
78 must use a secure SSL connection to send a password.
80 (yesNoOption "forceLocalDataSSL" "force_local_data_ssl" false ''
81 Only applies if {option}`sslEnable` is true. Non anonymous (local) users
82 must use a secure SSL connection for sending/receiving data on data connection.
84 (yesNoOption "portPromiscuous" "port_promiscuous" false ''
85 Set to YES if you want to disable the PORT security check that ensures that
86 outgoing data connections can only connect to the client. Only enable if you
87 know what you are doing!
89 (yesNoOption "ssl_tlsv1" "ssl_tlsv1" true ''
90 Only applies if {option}`ssl_enable` is activated. If
91 enabled, this option will permit TLS v1 protocol connections.
92 TLS v1 connections are preferred.
94 (yesNoOption "ssl_sslv2" "ssl_sslv2" false ''
95 Only applies if {option}`ssl_enable` is activated. If
96 enabled, this option will permit SSL v2 protocol connections.
97 TLS v1 connections are preferred.
99 (yesNoOption "ssl_sslv3" "ssl_sslv3" false ''
100 Only applies if {option}`ssl_enable` is activated. If
101 enabled, this option will permit SSL v3 protocol connections.
102 TLS v1 connections are preferred.
106 configFile = pkgs.writeText "vsftpd.conf"
108 ${concatMapStrings (x: "${x.cfgText}\n") optionDescription}
109 ${optionalString (cfg.rsaCertFile != null) ''
111 rsa_cert_file=${cfg.rsaCertFile}
113 ${optionalString (cfg.rsaKeyFile != null) ''
114 rsa_private_key_file=${cfg.rsaKeyFile}
116 ${optionalString (cfg.userlistFile != null) ''
117 userlist_file=${cfg.userlistFile}
123 secure_chroot_dir=/var/empty
124 ${optionalString (cfg.localRoot != null) ''
125 local_root=${cfg.localRoot}
128 ${optionalString (pkgs.stdenv.hostPlatform.system == "x86_64-linux") ''
131 anon_umask=${cfg.anonymousUmask}
132 ${optionalString cfg.anonymousUser ''
133 anon_root=${cfg.anonymousUserHome}
135 ${optionalString cfg.enableVirtualUsers ''
137 guest_username=vsftpd
139 pam_service_name=vsftpd
153 enable = mkEnableOption "vsftpd";
155 userlist = mkOption {
157 type = types.listOf types.str;
158 description = "See {option}`userlistFile`.";
161 userlistFile = mkOption {
163 default = pkgs.writeText "userlist" (concatMapStrings (x: "${x}\n") cfg.userlist);
164 defaultText = literalExpression ''pkgs.writeText "userlist" (concatMapStrings (x: "''${x}\n") cfg.userlist)'';
166 Newline separated list of names to be allowed/denied if {option}`userlistEnable`
167 is `true`. Meaning see {option}`userlistDeny`.
169 The default is a file containing the users from {option}`userlist`.
171 If explicitly set to null userlist_file will not be set in vsftpd's config file.
175 enableVirtualUsers = mkOption {
179 Whether to enable the `pam_userdb`-based
184 userDbPath = mkOption {
185 type = types.nullOr types.str;
186 example = "/etc/vsftpd/userDb";
189 Only applies if {option}`enableVirtualUsers` is true.
190 Path pointing to the `pam_userdb` user
191 database used by vsftpd to authenticate the virtual users.
193 This user list should be stored in the Berkeley DB database
196 To generate a new user database, create a text file, add
197 your users using the following format:
205 You can then install `pkgs.db` to generate
206 the Berkeley DB using
208 db_load -T -t hash -f logins.txt userDb.db
211 Caution: `pam_userdb` will automatically
212 append a `.db` suffix to the filename you
213 provide though this option. This option shouldn't include
214 this filetype suffix.
218 localRoot = mkOption {
219 type = types.nullOr types.str;
221 example = "/var/www/$USER";
223 This option represents a directory which vsftpd will try to
224 change into after a local (i.e. non- anonymous) login.
226 Failure is silently ignored.
230 anonymousUserHome = mkOption {
232 default = "/home/ftp/";
234 Directory to consider the HOME of the anonymous user.
238 rsaCertFile = mkOption {
239 type = types.nullOr types.path;
241 description = "RSA certificate file.";
244 rsaKeyFile = mkOption {
245 type = types.nullOr types.path;
247 description = "RSA private key file.";
250 anonymousUmask = mkOption {
254 description = "Anonymous write umask.";
257 extraConfig = mkOption {
260 example = "ftpd_banner=Hello";
261 description = "Extra configuration to add at the bottom of the generated configuration file.";
264 } // (listToAttrs (catAttrs "nixosOption" optionDescription));
269 ###### implementation
271 config = mkIf cfg.enable {
275 (cfg.forceLocalLoginsSSL -> cfg.rsaCertFile != null)
276 && (cfg.forceLocalDataSSL -> cfg.rsaCertFile != null);
277 message = "vsftpd: If forceLocalLoginsSSL or forceLocalDataSSL is true then a rsaCertFile must be provided!";
280 assertion = (cfg.enableVirtualUsers -> cfg.userDbPath != null)
281 && (cfg.enableVirtualUsers -> cfg.localUsers);
282 message = "vsftpd: If enableVirtualUsers is true, you need to setup both the userDbPath and localUsers options.";
289 description = "VSFTPD user";
290 home = if cfg.localRoot != null
291 then cfg.localRoot # <= Necessary for virtual users.
292 else "/homeless-shelter";
294 } // optionalAttrs cfg.anonymousUser {
295 "ftp" = { name = "ftp";
296 uid = config.ids.uids.ftp;
298 description = "Anonymous FTP user";
299 home = cfg.anonymousUserHome;
303 users.groups.vsftpd = {};
304 users.groups.ftp.gid = config.ids.gids.ftp;
306 # If you really have to access root via FTP use mkOverride or userlistDeny
307 # = false and whitelist root
308 services.vsftpd.userlist = optional cfg.userlistDeny "root";
311 tmpfiles.rules = optional cfg.anonymousUser
312 #Type Path Mode User Gr Age Arg
313 "d '${builtins.toString cfg.anonymousUserHome}' 0555 'ftp' 'ftp' - -";
315 description = "Vsftpd Server";
317 wantedBy = [ "multi-user.target" ];
319 serviceConfig.ExecStart = "@${vsftpd}/sbin/vsftpd vsftpd ${configFile}";
320 serviceConfig.Restart = "always";
321 serviceConfig.Type = "forking";
325 security.pam.services.vsftpd.text = mkIf (cfg.enableVirtualUsers && cfg.userDbPath != null)''
326 auth required pam_userdb.so db=${cfg.userDbPath}
327 account required pam_userdb.so db=${cfg.userDbPath}