16 forceLocalLoginsSSL = true;
17 forceLocalDataSSL = true;
20 userlist = ["non-root-user" "other-non-root-user"];
21 rsaCertFile = "/var/vsftpd/vsftpd.pem";
24 cfg = config.services.vsftpd;
26 inherit (pkgs) vsftpd;
28 yesNoOption = nixosName: vsftpdName: default: description: {
29 cfgText = "${vsftpdName}=${if getAttr nixosName cfg then "YES" else "NO"}";
35 description = description;
43 (yesNoOption "allowWriteableChroot" "allow_writeable_chroot" false ''
44 Allow the use of writeable root inside chroot().
46 (yesNoOption "virtualUseLocalPrivs" "virtual_use_local_privs" false ''
47 If enabled, virtual users will use the same privileges as local
48 users. By default, virtual users will use the same privileges as
49 anonymous users, which tends to be more restrictive (especially
50 in terms of write access).
52 (yesNoOption "anonymousUser" "anonymous_enable" false ''
53 Whether to enable the anonymous FTP user.
55 (yesNoOption "anonymousUserNoPassword" "no_anon_password" false ''
56 Whether to disable the password for the anonymous FTP user.
58 (yesNoOption "localUsers" "local_enable" false ''
59 Whether to enable FTP for local users.
61 (yesNoOption "writeEnable" "write_enable" false ''
62 Whether any write activity is permitted to users.
64 (yesNoOption "anonymousUploadEnable" "anon_upload_enable" false ''
65 Whether any uploads are permitted to anonymous users.
67 (yesNoOption "anonymousMkdirEnable" "anon_mkdir_write_enable" false ''
68 Whether any uploads are permitted to anonymous users.
70 (yesNoOption "chrootlocalUser" "chroot_local_user" false ''
71 Whether local users are confined to their home directory.
73 (yesNoOption "userlistEnable" "userlist_enable" false ''
74 Whether users are included.
76 (yesNoOption "userlistDeny" "userlist_deny" false ''
77 Specifies whether {option}`userlistFile` is a list of user
78 names to allow or deny access.
79 The default `false` means whitelist/allow.
81 (yesNoOption "forceLocalLoginsSSL" "force_local_logins_ssl" false ''
82 Only applies if {option}`sslEnable` is true. Non anonymous (local) users
83 must use a secure SSL connection to send a password.
85 (yesNoOption "forceLocalDataSSL" "force_local_data_ssl" false ''
86 Only applies if {option}`sslEnable` is true. Non anonymous (local) users
87 must use a secure SSL connection for sending/receiving data on data connection.
89 (yesNoOption "portPromiscuous" "port_promiscuous" false ''
90 Set to YES if you want to disable the PORT security check that ensures that
91 outgoing data connections can only connect to the client. Only enable if you
92 know what you are doing!
94 (yesNoOption "ssl_tlsv1" "ssl_tlsv1" true ''
95 Only applies if {option}`ssl_enable` is activated. If
96 enabled, this option will permit TLS v1 protocol connections.
97 TLS v1 connections are preferred.
99 (yesNoOption "ssl_sslv2" "ssl_sslv2" false ''
100 Only applies if {option}`ssl_enable` is activated. If
101 enabled, this option will permit SSL v2 protocol connections.
102 TLS v1 connections are preferred.
104 (yesNoOption "ssl_sslv3" "ssl_sslv3" false ''
105 Only applies if {option}`ssl_enable` is activated. If
106 enabled, this option will permit SSL v3 protocol connections.
107 TLS v1 connections are preferred.
111 configFile = pkgs.writeText "vsftpd.conf" ''
112 ${concatMapStrings (x: "${x.cfgText}\n") optionDescription}
113 ${optionalString (cfg.rsaCertFile != null) ''
115 rsa_cert_file=${cfg.rsaCertFile}
117 ${optionalString (cfg.rsaKeyFile != null) ''
118 rsa_private_key_file=${cfg.rsaKeyFile}
120 ${optionalString (cfg.userlistFile != null) ''
121 userlist_file=${cfg.userlistFile}
127 secure_chroot_dir=/var/empty
128 ${optionalString (cfg.localRoot != null) ''
129 local_root=${cfg.localRoot}
132 ${optionalString (pkgs.stdenv.hostPlatform.system == "x86_64-linux") ''
135 anon_umask=${cfg.anonymousUmask}
136 ${optionalString cfg.anonymousUser ''
137 anon_root=${cfg.anonymousUserHome}
139 ${optionalString cfg.enableVirtualUsers ''
141 guest_username=vsftpd
143 pam_service_name=vsftpd
157 enable = mkEnableOption "vsftpd";
159 userlist = mkOption {
161 type = types.listOf types.str;
162 description = "See {option}`userlistFile`.";
165 userlistFile = mkOption {
167 default = pkgs.writeText "userlist" (concatMapStrings (x: "${x}\n") cfg.userlist);
168 defaultText = literalExpression ''pkgs.writeText "userlist" (concatMapStrings (x: "''${x}\n") cfg.userlist)'';
170 Newline separated list of names to be allowed/denied if {option}`userlistEnable`
171 is `true`. Meaning see {option}`userlistDeny`.
173 The default is a file containing the users from {option}`userlist`.
175 If explicitly set to null userlist_file will not be set in vsftpd's config file.
179 enableVirtualUsers = mkOption {
183 Whether to enable the `pam_userdb`-based
188 userDbPath = mkOption {
189 type = types.nullOr types.str;
190 example = "/etc/vsftpd/userDb";
193 Only applies if {option}`enableVirtualUsers` is true.
194 Path pointing to the `pam_userdb` user
195 database used by vsftpd to authenticate the virtual users.
197 This user list should be stored in the Berkeley DB database
200 To generate a new user database, create a text file, add
201 your users using the following format:
209 You can then install `pkgs.db` to generate
210 the Berkeley DB using
212 db_load -T -t hash -f logins.txt userDb.db
215 Caution: `pam_userdb` will automatically
216 append a `.db` suffix to the filename you
217 provide though this option. This option shouldn't include
218 this filetype suffix.
222 localRoot = mkOption {
223 type = types.nullOr types.str;
225 example = "/var/www/$USER";
227 This option represents a directory which vsftpd will try to
228 change into after a local (i.e. non- anonymous) login.
230 Failure is silently ignored.
234 anonymousUserHome = mkOption {
236 default = "/home/ftp/";
238 Directory to consider the HOME of the anonymous user.
242 rsaCertFile = mkOption {
243 type = types.nullOr types.path;
245 description = "RSA certificate file.";
248 rsaKeyFile = mkOption {
249 type = types.nullOr types.path;
251 description = "RSA private key file.";
254 anonymousUmask = mkOption {
258 description = "Anonymous write umask.";
261 extraConfig = mkOption {
264 example = "ftpd_banner=Hello";
265 description = "Extra configuration to add at the bottom of the generated configuration file.";
268 } // (listToAttrs (catAttrs "nixosOption" optionDescription));
272 ###### implementation
274 config = mkIf cfg.enable {
279 (cfg.forceLocalLoginsSSL -> cfg.rsaCertFile != null)
280 && (cfg.forceLocalDataSSL -> cfg.rsaCertFile != null);
281 message = "vsftpd: If forceLocalLoginsSSL or forceLocalDataSSL is true then a rsaCertFile must be provided!";
285 (cfg.enableVirtualUsers -> cfg.userDbPath != null) && (cfg.enableVirtualUsers -> cfg.localUsers);
286 message = "vsftpd: If enableVirtualUsers is true, you need to setup both the userDbPath and localUsers options.";
295 description = "VSFTPD user";
297 if cfg.localRoot != null then
298 cfg.localRoot # <= Necessary for virtual users.
303 // optionalAttrs cfg.anonymousUser {
306 uid = config.ids.uids.ftp;
308 description = "Anonymous FTP user";
309 home = cfg.anonymousUserHome;
313 users.groups.vsftpd = { };
314 users.groups.ftp.gid = config.ids.gids.ftp;
316 # If you really have to access root via FTP use mkOverride or userlistDeny
317 # = false and whitelist root
318 services.vsftpd.userlist = optional cfg.userlistDeny "root";
322 optional cfg.anonymousUser
323 #Type Path Mode User Gr Age Arg
324 "d '${builtins.toString cfg.anonymousUserHome}' 0555 'ftp' 'ftp' - -";
326 description = "Vsftpd Server";
328 wantedBy = [ "multi-user.target" ];
330 serviceConfig.ExecStart = "@${vsftpd}/sbin/vsftpd vsftpd ${configFile}";
331 serviceConfig.Restart = "always";
332 serviceConfig.Type = "forking";
336 security.pam.services.vsftpd.text = mkIf (cfg.enableVirtualUsers && cfg.userDbPath != null) ''
337 auth required pam_userdb.so db=${cfg.userDbPath}
338 account required pam_userdb.so db=${cfg.userDbPath}