2021-02-08 22:37:10 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.uptermd;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.uptermd = {
|
|
|
|
enable = mkEnableOption "uptermd";
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to open the firewall for the port in {option}`services.uptermd.port`.
|
2021-02-08 22:37:10 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 2222;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-02-08 22:37:10 +00:00
|
|
|
Port the server will listen on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
listenAddress = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "[::]";
|
|
|
|
example = "127.0.0.1";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-02-08 22:37:10 +00:00
|
|
|
Address the server will listen on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hostKey = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/upterm_host_ed25519_key";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-02-08 22:37:10 +00:00
|
|
|
Path to SSH host key. If not defined, an ed25519 keypair is generated automatically.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "--debug" ];
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-02-08 22:37:10 +00:00
|
|
|
Extra flags passed to the uptermd command.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
networking.firewall = mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = [ cfg.port ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.uptermd = {
|
|
|
|
description = "Upterm Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
|
|
|
|
path = [ pkgs.openssh ];
|
|
|
|
|
|
|
|
preStart = mkIf (cfg.hostKey == null) ''
|
|
|
|
if ! [ -f ssh_host_ed25519_key ]; then
|
|
|
|
ssh-keygen \
|
|
|
|
-t ed25519 \
|
|
|
|
-f ssh_host_ed25519_key \
|
|
|
|
-N ""
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
StateDirectory = "uptermd";
|
|
|
|
WorkingDirectory = "/var/lib/uptermd";
|
|
|
|
ExecStart = "${pkgs.upterm}/bin/uptermd --ssh-addr ${cfg.listenAddress}:${toString cfg.port} --private-key ${if cfg.hostKey == null then "ssh_host_ed25519_key" else cfg.hostKey} ${concatStringsSep " " cfg.extraFlags}";
|
|
|
|
|
|
|
|
# Hardening
|
|
|
|
AmbientCapabilities = mkIf (cfg.port < 1024) [ "CAP_NET_BIND_SERVICE" ];
|
|
|
|
CapabilityBoundingSet = mkIf (cfg.port < 1024) [ "CAP_NET_BIND_SERVICE" ];
|
|
|
|
PrivateUsers = cfg.port >= 1024;
|
2022-05-15 07:58:32 +01:00
|
|
|
DynamicUser = true;
|
2021-02-08 22:37:10 +00:00
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
2022-05-15 07:58:32 +01:00
|
|
|
ProtectProc = "invisible";
|
|
|
|
# AF_UNIX is for ssh-keygen, which relies on nscd to resolve the uid to a user
|
|
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
|
2021-02-08 22:37:10 +00:00
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = "@system-service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|