summaryrefslogtreecommitdiff
path: root/sys/auth/openssh.nix
blob: 07e69775d92c34c77503f804c7bebf16cdcd66aa (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
{ config, lib, pkgs, ... }:
with lib; let
  cfg = config.local.auth.openssh;
  withOath = config.local.auth.oath.enable;
  withPassword = config.local.auth.openssh.passwordAuthentication;

  port = if cfg.shiftPortNumber then 2234 else 22;
  restrict = cfg.restrictListen;

  exemptList = optionals config.local.net.fail2ban.enable config.services.fail2ban.ignoreIP;
in
{
  options.local.auth.openssh = {
    enable = mkEnableOption "openssh";
    tunnel.enable = mkEnableOption "ssh tunnel user";

    #TODO: Desfasar ecdsa, inseguro
    hostKeys = listToAttrs (map
      (name: {
        inherit name;

        value = mkOption {
          type = types.bool;
          default = false;
        };
      }) [ "ecdsa" "ed25519" "rsa" ]);

    restrictListen = mkOption {
      default = null;

      type = with types; nullOr (submodule {
        options = {
          addresses = mkOption {
            type = listOf str;
          };

          interface = mkOption {
            type = nullOr str;
            default = null;
          };

          vsockCid = mkOption {
            type = nullOr ints.u32;
            default = null;
          };
        };
      });
    };

    passwordAuthentication = mkOption {
      type = types.bool;
      default = false;
    };

    shiftPortNumber = mkOption {
      type = types.bool;
      default = true;
    };

    withDeployKeys = mkOption {
      type = types.bool;
      default = false;
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [
      {
        assertion = cfg.tunnel.enable -> withOath;
        message = "SSH tunnel requires oath";
      }
      {
        assertion = restrict != null -> (restrict.vsockCid != null -> (restrict.interface == null && restrict.addresses == [ ]));
        message = "SSH vsock restrict requires disabling inet";
      }
      {
        assertion = restrict != null -> (restrict.vsockCid != null -> config.services.openssh.startWhenNeeded);
        message = "SSH vsock restrict requires socket activation";
      }
      {
        assertion = restrict != null -> (restrict.vsockCid != null -> config.local.virt.enable);
        message = "SSH vsock restrict requires nixvirt";
      }
      {
        assertion = any (key: key) (attrValues cfg.hostKeys);
        message = "No OpenSSH host keys were enabled";
      }
    ];

    local.boot.impermanence.files =
      flatten (map (key: [ key.path "${key.path}.pub" ]) config.services.openssh.hostKeys);

    networking.firewall = {
      interfaces = optionalAttrs (restrict != null && restrict.interface != null) {
        ${restrict.interface}.allowedTCPPorts = [ port ];
      };

      allowedTCPPorts = optional (restrict == null || restrict.interface == null) port;
    };

    services.openssh = {
      enable = true;

      ports = optional (restrict != null -> restrict.addresses != [ ]) port;
      startWhenNeeded = mkDefault (!config.services.fail2ban.enable);

      extraConfig = optionalString (exemptList != [ ]) ''
        PerSourcePenaltyExemptList ${concatStringsSep "," exemptList}
      '' + optionalString cfg.tunnel.enable ''
        # User 'tunnel' has no password. Use PAM OATH
        # and connect with -N, forward with -R.
        Match User tunnel
           AllowTcpForwarding remote
           AllowStreamLocalForwarding no
           X11Forwarding no
           PermitTunnel no
           GatewayPorts no
           AllowAgentForwarding no
           PermitOpen none
           PermitListen 60220 60221 60222 60223 60224 60225 60226 60227 60228 60229

           Banner ${pkgs.writeText "tunnel-banner" ''
             This is a reverse tunnel
           ''}
      '';

      hostKeys = map
        (name: {
          path = "/etc/ssh/ssh_host_${name}_key";
          type = name;
        } // optionalAttrs (name == "rsa") {
          bits = 4096;
        })
        (attrNames (filterAttrs (name: enable: enable) cfg.hostKeys));

      settings = {
        X11Forwarding = config.local.seat.enable && config.local.seat.graphical;
        PermitRootLogin = "prohibit-password";
        PasswordAuthentication = withOath || withPassword; # Necesario para oath, no reemplaza a oath
      };

      listenAddresses = mkIf (restrict != null)
        (map (addr: { inherit addr; }) restrict.addresses);
    };

    systemd.sockets = mkIf (restrict != null && restrict.vsockCid != null) {
      sshd =
        let
          kernelMod = "modprobe@${if restrict.vsockCid == 2 then "vhost_" else ""}vsock.service";
        in
        {
          after = [ kernelMod ];
          wants = [ kernelMod ];

          socketConfig.ListenStream = mkForce [ "vsock:${toString restrict.vsockCid}:${toString port}" ];
        };
    };

    users.users = {
      root = mkIf cfg.withDeployKeys {
        openssh.authorizedKeys.keyFiles = [ ./ssh-key.pub ];
      };

      tunnel = mkIf cfg.tunnel.enable {
        uid = 1100;
        group = "nogroup";
        isSystemUser = true;

        # Requiere oath
        password = "tunnel";

        home = "/var/empty";
        shell = "${pkgs.coreutils}/bin/true";
      };
    };
  };
}