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 services.duplyBackup.profiles.ftp = {
34 rootDir = "/var/lib/ftp";
35 remotes = [ "eriomem" "ovh" ];
37 security.acme.certs."ftp" = config.myServices.certificates.certConfig // {
38 domain = "eldiron.immae.eu";
39 postRun = (lib.optionalString pure-ftpd-enabled ''
40 systemctl restart pure-ftpd.service
41 '') + (lib.optionalString proftpd-enabled ''
42 systemctl restart proftpd.service
44 extraDomains = { "ftp.immae.eu" = null; };
49 allowedTCPPorts = [ 21 115 ];
50 allowedTCPPortRanges = [ { from = 40000; to = 50000; } ];
55 uid = config.ids.uids.ftp; # 8
57 description = "Anonymous FTP user";
58 home = "/homeless-shelter";
59 extraGroups = [ "keys" ];
62 users.groups.ftp.gid = config.ids.gids.ftp;
64 system.activationScripts.ftp = ''
65 install -m 0755 -o ftp -g ftp -d /var/lib/ftp
66 '' + (lib.optionalString proftpd-enabled ''
67 install -m 0755 -o nobody -g nogroup -d /var/lib/proftpd/authorized_keys
70 secrets.keys."pure-ftpd-ldap" = lib.mkIf pure-ftpd-enabled {
75 LDAPServer ${config.myEnv.ftp.ldap.host}
78 LDAPBaseDN ${config.myEnv.ftp.ldap.base}
79 LDAPBindDN ${config.myEnv.ftp.ldap.dn}
80 LDAPBindPW ${config.myEnv.ftp.ldap.password}
82 LDAPForceDefaultUID False
84 LDAPForceDefaultGID False
85 LDAPFilter ${config.myEnv.ftp.ldap.pure-ftpd_filter}
89 # Pas de possibilite de donner l'Uid/Gid !
90 # Compile dans pure-ftpd directement avec immaeFtpUid / immaeFtpGid
91 LDAPHomeDir immaeFtpDirectory
94 secrets.keys."proftpd-ldap.conf" = lib.mkIf proftpd-enabled {
99 LDAPServer ldaps://${config.myEnv.ftp.ldap.host}:636/??sub
102 LDAPBindDN "${config.myEnv.ftp.ldap.dn}" "${config.myEnv.ftp.ldap.password}"
103 LDAPSearchScope subtree
107 LDAPForceDefaultUID off
108 LDAPForceDefaultGID off
109 LDAPAttr gidNumber immaeFtpGid
110 LDAPAttr uidNumber immaeFtpUid
111 LDAPAttr homeDirectory immaeFtpDirectory
112 LDAPUsers "${config.myEnv.ftp.ldap.base}" "${config.myEnv.ftp.ldap.proftpd_filter}"
113 LDAPGroups "${config.myEnv.ftp.ldap.base}"
117 services.filesWatcher.pure-ftpd = lib.mkIf pure-ftpd-enabled {
119 paths = [ config.secrets.fullPaths."pure-ftpd-ldap" ];
121 services.filesWatcher.proftpd = lib.mkIf proftpd-enabled {
123 paths = [ config.secrets.fullPaths."proftpd-ldap.conf" ];
126 systemd.services.pure-ftpd = let
127 configFile = pkgs.writeText "pure-ftpd.conf" ''
128 PassivePortRange 40000 50000
132 BrokenClientsCompatibility yes
143 LDAPConfigFile ${config.secrets.fullPaths."pure-ftpd-ldap"}
144 LimitRecursion 10000 8
145 AnonymousCanCreateDirs no
153 ProhibitDotFilesWrite no
154 ProhibitDotFilesRead no
156 AnonymousCantUpload no
160 CertFile ${config.security.acme.certs.ftp.directory}/full.pem
162 in lib.mkIf pure-ftpd-enabled {
163 description = "Pure-FTPd server";
164 wantedBy = [ "multi-user.target" ];
165 after = [ "network.target" ];
167 serviceConfig.ExecStart = "${package}/bin/pure-ftpd ${configFile}";
168 serviceConfig.Type = "forking";
169 serviceConfig.PIDFile = "/run/pure-ftpd.pid";
172 systemd.services.proftpd = let
173 configFile = pkgs.writeText "proftpd.conf" ''
175 ServerType standalone
185 # Set the user and group under which the server will run.
196 TLSProtocol TLSv1.1 TLSv1.2 TLSv1.3
198 TLSCertificateChainFile ${config.security.acme.certs.ftp.directory}/fullchain.pem
199 TLSECCertificateFile ${config.security.acme.certs.ftp.directory}/cert.pem
200 TLSECCertificateKeyFile ${config.security.acme.certs.ftp.directory}/key.pem
202 PidFile /run/proftpd/proftpd.pid
204 ScoreboardFile /run/proftpd/proftpd.scoreboard
206 PassivePorts 40000 50000
208 Include ${config.secrets.fullPaths."proftpd-ldap.conf"}
210 RequireValidShell off
212 # Bar use of SITE CHMOD by default
217 <VirtualHost 0.0.0.0>
226 SFTPHostKey /etc/ssh/ssh_host_ed25519_key
227 SFTPHostKey /etc/ssh/ssh_host_rsa_key
228 Include ${config.secrets.fullPaths."proftpd-ldap.conf"}
229 RequireValidShell off
230 SFTPAuthorizedUserKeys file:/var/lib/proftpd/authorized_keys/%u
231 SFTPAuthMethods password publickey
234 in lib.mkIf proftpd-enabled {
235 description = "ProFTPD server";
236 wantedBy = [ "multi-user.target" ];
237 after = [ "network.target" ];
239 serviceConfig.ExecStart = "${pkgs.proftpd}/bin/proftpd -c ${configFile}";
240 serviceConfig.Type = "forking";
241 serviceConfig.PIDFile = "/run/proftpd/proftpd.pid";
242 serviceConfig.RuntimeDirectory = "proftpd";
245 services.cron.systemCronJobs = lib.mkIf proftpd-enabled [
246 "*/2 * * * * nobody ${./ftp_sync.sh}"