summaryrefslogtreecommitdiffstats
path: root/configs/sshd.nix
blob: 39cf1079111a942d1938953654e7c80783dff63b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{ config, lib, ... }: let
  cfg.host = config.krebs.build.host;
  nets =
    lib.optional (cfg.host.nets?mycelium) cfg.host.nets.mycelium ++
    lib.optional (cfg.host.nets?retiolum) cfg.host.nets.retiolum ++
    lib.optional (cfg.host.nets?wiregrill) cfg.host.nets.wiregrill;

  cfg.ssh.port = 11423;
in lib.mkMerge [
  {
    services.openssh = {
      enable = true;
    };
  }
  (lib.mkIf (!config.hrm.nftables.enable) {
    tv.iptables.input-internet-accept-tcp = [ "ssh" ];
    tv.iptables.extra.nat.OUTPUT = [
      "-o lo -p tcp --dport ${toString cfg.ssh.port} -j REDIRECT --to-ports 22"
    ];
    tv.iptables.extra4.nat.PREROUTING =
      map
        (net: "-d ${net.ip4.addr} -p tcp --dport 22 -j ACCEPT")
        (builtins.filter (net: net.ip4 != null) nets);
    tv.iptables.extra6.nat.PREROUTING =
      map
        (net: "-d ${net.ip6.addr} -p tcp --dport 22 -j ACCEPT")
        (builtins.filter (net: net.ip6 != null) nets);
    tv.iptables.extra.nat.PREROUTING = [
      "-p tcp --dport 22 -j REDIRECT --to-ports 0"
      "-p tcp --dport ${toString cfg.ssh.port} -j REDIRECT --to-ports 22"
    ];
  })
  (lib.mkIf config.hrm.nftables.enable {
    hrm.nftables.input.internet.tcp = [ "ssh" ];
    hrm.nftables.tables.nat.chains = {
      # Port 22 is only reachable on the host's own network addresses; on
      # other addresses, port 22 goes nowhere and cfg.ssh.port leads to sshd.
      prerouting.rules = {
        ssh-own-addresses = {
          order = 900;
          daddr =
            map (net: net.ip4.addr) (builtins.filter (net: net.ip4 != null) nets) ++
            map (net: net.ip6.addr) (builtins.filter (net: net.ip6 != null) nets);
          tcp.dport = [ 22 ];
          verdict = "accept";
        };
        ssh-hide = {
          tcp.dport = [ 22 ];
          verdict = "redirect to :0";
        };
        ssh-port = {
          tcp.dport = [ cfg.ssh.port ];
          verdict = "redirect to :22";
        };
      };
      output.rules.ssh-port = {
        oifname = "lo";
        tcp.dport = [ cfg.ssh.port ];
        verdict = "redirect to :22";
      };
    };
  })
]