1 { lib, pkgs, config, ... }:
3 package = pkgs.pure-ftpd.override { ldapFtpId = "immaeFtp"; };
4 pure-ftpd-enabled = config.myServices.ftp.pure-ftpd.enable;
5 proftpd-enabled = config.myServices.ftp.proftpd.enable;
9 myServices.ftp.enable = lib.mkOption {
10 type = lib.types.bool;
13 Whether to enable ftp.
16 myServices.ftp.pure-ftpd.enable = lib.mkOption {
17 type = lib.types.bool;
20 Whether to enable pure-ftpd.
23 myServices.ftp.proftpd.enable = lib.mkOption {
24 type = lib.types.bool;
27 Whether to enable proftpd.
32 config = lib.mkIf config.myServices.ftp.enable {
33 security.acme.certs."ftp" = config.myServices.certificates.certConfig // {
34 domain = "eldiron.immae.eu";
35 postRun = (lib.optionalString pure-ftpd-enabled ''
36 systemctl restart pure-ftpd.service
37 '') + (lib.optionalString proftpd-enabled ''
38 systemctl restart proftpd.service
40 extraDomains = { "ftp.immae.eu" = null; };
45 allowedTCPPorts = [ 21 115 ];
46 allowedTCPPortRanges = [ { from = 40000; to = 50000; } ];
51 uid = config.ids.uids.ftp; # 8
53 description = "Anonymous FTP user";
54 home = "/homeless-shelter";
55 extraGroups = [ "keys" ];
58 users.groups.ftp.gid = config.ids.gids.ftp;
60 system.activationScripts.ftp = ''
61 install -m 0755 -o ftp -g ftp -d /var/lib/ftp
62 '' + (lib.optionalString proftpd-enabled ''
63 install -m 0755 -o nobody -g nogroup -d /var/lib/proftpd/authorized_keys
66 secrets.keys."pure-ftpd-ldap" = lib.mkIf pure-ftpd-enabled {
71 LDAPServer ${config.myEnv.ftp.ldap.host}
74 LDAPBaseDN ${config.myEnv.ftp.ldap.base}
75 LDAPBindDN ${config.myEnv.ftp.ldap.dn}
76 LDAPBindPW ${config.myEnv.ftp.ldap.password}
78 LDAPForceDefaultUID False
80 LDAPForceDefaultGID False
81 LDAPFilter ${config.myEnv.ftp.ldap.pure-ftpd_filter}
85 # Pas de possibilite de donner l'Uid/Gid !
86 # Compile dans pure-ftpd directement avec immaeFtpUid / immaeFtpGid
87 LDAPHomeDir immaeFtpDirectory
90 secrets.keys."proftpd-ldap.conf" = lib.mkIf proftpd-enabled {
95 LDAPServer ldaps://${config.myEnv.ftp.ldap.host}:636/??sub
98 LDAPBindDN "${config.myEnv.ftp.ldap.dn}" "${config.myEnv.ftp.ldap.password}"
99 LDAPSearchScope subtree
103 LDAPForceDefaultUID off
104 LDAPForceDefaultGID off
105 LDAPAttr gidNumber immaeFtpGid
106 LDAPAttr uidNumber immaeFtpUid
107 LDAPAttr homeDirectory immaeFtpDirectory
108 LDAPUsers "${config.myEnv.ftp.ldap.base}" "${config.myEnv.ftp.ldap.proftpd_filter}"
109 LDAPGroups "${config.myEnv.ftp.ldap.base}"
113 services.filesWatcher.pure-ftpd = lib.mkIf pure-ftpd-enabled {
115 paths = [ config.secrets.fullPaths."pure-ftpd-ldap" ];
117 services.filesWatcher.proftpd = lib.mkIf proftpd-enabled {
119 paths = [ config.secrets.fullPaths."proftpd-ldap.conf" ];
122 systemd.services.pure-ftpd = let
123 configFile = pkgs.writeText "pure-ftpd.conf" ''
124 PassivePortRange 40000 50000
128 BrokenClientsCompatibility yes
139 LDAPConfigFile ${config.secrets.fullPaths."pure-ftpd-ldap"}
140 LimitRecursion 10000 8
141 AnonymousCanCreateDirs no
149 ProhibitDotFilesWrite no
150 ProhibitDotFilesRead no
152 AnonymousCantUpload no
156 CertFile ${config.security.acme.certs.ftp.directory}/full.pem
158 in lib.mkIf pure-ftpd-enabled {
159 description = "Pure-FTPd server";
160 wantedBy = [ "multi-user.target" ];
161 after = [ "network.target" ];
163 serviceConfig.ExecStart = "${package}/bin/pure-ftpd ${configFile}";
164 serviceConfig.Type = "forking";
165 serviceConfig.PIDFile = "/run/pure-ftpd.pid";
168 systemd.services.proftpd = let
169 configFile = pkgs.writeText "proftpd.conf" ''
171 ServerType standalone
181 # Set the user and group under which the server will run.
192 TLSProtocol TLSv1.1 TLSv1.2 TLSv1.3
194 TLSCertificateChainFile ${config.security.acme.certs.ftp.directory}/fullchain.pem
195 TLSECCertificateFile ${config.security.acme.certs.ftp.directory}/cert.pem
196 TLSECCertificateKeyFile ${config.security.acme.certs.ftp.directory}/key.pem
198 PidFile /run/proftpd/proftpd.pid
200 ScoreboardFile /run/proftpd/proftpd.scoreboard
202 PassivePorts 40000 50000
204 Include ${config.secrets.fullPaths."proftpd-ldap.conf"}
206 RequireValidShell off
208 # Bar use of SITE CHMOD by default
213 <VirtualHost 0.0.0.0>
222 SFTPHostKey /etc/ssh/ssh_host_ed25519_key
223 SFTPHostKey /etc/ssh/ssh_host_rsa_key
224 Include ${config.secrets.fullPaths."proftpd-ldap.conf"}
225 RequireValidShell off
226 SFTPAuthorizedUserKeys file:/var/lib/proftpd/authorized_keys/%u
227 SFTPAuthMethods password publickey
229 SFTPOptions IgnoreSFTPSetOwners
232 in lib.mkIf proftpd-enabled {
233 description = "ProFTPD server";
234 wantedBy = [ "multi-user.target" ];
235 after = [ "network.target" ];
237 serviceConfig.ExecStart = "${pkgs.proftpd}/bin/proftpd -c ${configFile}";
238 serviceConfig.Type = "forking";
239 serviceConfig.PIDFile = "/run/proftpd/proftpd.pid";
240 serviceConfig.RuntimeDirectory = "proftpd";
243 services.cron.systemCronJobs = lib.mkIf proftpd-enabled [
244 "*/2 * * * * nobody ${./ftp_sync.sh}"