aboutsummaryrefslogtreecommitdiff
path: root/modules/services/openssh.nix
blob: c34ebd33d821be13df8c1716eee89d559883371f (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
{
  flake.modules.nixos.openssh =
    { lib, pkgs, ... }:
    {
      services.openssh = {
        enable = true;
        ports = [ 8693 ];
        settings = {
          AllowTcpForwarding = false;
          AllowAgentForwarding = false;
          PasswordAuthentication = false;
          X11Forwarding = false;
          PermitRootLogin = "yes";
        };
      };

      networking.firewall.allowedTCPPorts = [ 8693 ];

      services.fail2ban = {
        enable = true;
        bantime = lib.mkDefault "1h";
        jails.sshd = {
          enabled = true;
          settings = {
            port = 8693;
            backend = "systemd";
            maxretry = 4;
            findtime = "10m";
          };
        };
      };

      security.pam.services.sshd.text = lib.mkDefault (
        lib.mkAfter ''
          session optional pam_exec.so ${pkgs.writeShellScript "ssh-login-notify" ''
            if [ "$PAM_TYPE" = "open_session" ]; then
              TIMESTAMP=$(${pkgs.coreutils}/bin/date "+%Y-%m-%d %H:%M:%S %Z")
              HOSTNAME=$(${pkgs.coreutils}/bin/cat /etc/hostname)

              {
                ${pkgs.coreutils}/bin/printf '%s\n' \
                  "To: leander@schererleander.de" \
                  "From: root@sachiel.schererleander.de" \
                  "Subject: SSH login: $PAM_USER from $PAM_RHOST" \
                  "Content-Type: text/html; charset=UTF-8" \
                  ""

                ${pkgs.coreutils}/bin/cat <<EOF
                <!doctype html>
                <html>
                  <head>
                    <meta name="color-scheme" content="light dark">
                    <meta name="supported-color-schemes" content="light dark">
                    <style>
                      :root { color-scheme: light dark; }
                      body { margin: 0; padding: 20px; font: 14px sans-serif; background: #fff; color: #111; }
                      main { max-width: 600px; margin: auto; }
                      h1 { margin-top: 0; font-size: 22px; }
                      table { width: 100%; border-collapse: collapse; }
                      th, td { padding: 6px; text-align: left; border-bottom: 1px solid #ddd; }
                      th { width: 80px; }
                      a { color: #06c; }

                      @media (prefers-color-scheme: dark) {
                        body { background: #121212; color: #eee; }
                        th, td { border-color: #333; }
                        a { color: #6bf; }
                      }
                    </style>
                  </head>
                  <body>
                    <main>
                      <h1>SSH Login</h1>
                      <table>
                        <tr><th>User</th><td>$PAM_USER</td></tr>
                        <tr><th>Host</th><td>$HOSTNAME</td></tr>
                        <tr><th>Time</th><td>$TIMESTAMP</td></tr>
                        <tr>
                          <th>IP</th>
                          <td>
                            <a href="https://iplookup.flagfox.net/?ip=$PAM_RHOST">$PAM_RHOST</a>
                          </td>
                        </tr>
                      </table>
                    </main>
                  </body>
                </html>
EOF
              } | /run/wrappers/bin/sendmail \
                -f root@sachiel.schererleander.de \
                leander@schererleander.de
            fi
          ''}
        ''
      );
    };
}