2023-08-10 21:53:21 +00:00
|
|
|
{
|
2025-02-10 21:55:08 +00:00
|
|
|
liminix,
|
|
|
|
dropbear,
|
|
|
|
lib,
|
|
|
|
watch-ssh-keys,
|
2023-08-10 21:53:21 +00:00
|
|
|
}:
|
2024-08-23 22:13:49 +00:00
|
|
|
{
|
|
|
|
address,
|
|
|
|
allowLocalPortForward,
|
|
|
|
allowPasswordLogin,
|
|
|
|
allowPasswordLoginForRoot,
|
|
|
|
allowRemoteConnectionToForwardedPorts,
|
|
|
|
allowRemotePortForward,
|
|
|
|
allowRoot,
|
|
|
|
authorizedKeys,
|
|
|
|
port,
|
2025-02-10 21:55:08 +00:00
|
|
|
extraConfig,
|
2024-08-23 22:13:49 +00:00
|
|
|
}:
|
2023-08-10 21:53:21 +00:00
|
|
|
let
|
2024-08-23 19:35:07 +00:00
|
|
|
name = "sshd";
|
2024-08-25 15:35:50 +00:00
|
|
|
inherit (builtins) toString typeOf;
|
2023-08-10 21:53:21 +00:00
|
|
|
inherit (liminix.services) longrun;
|
2024-08-23 19:35:07 +00:00
|
|
|
inherit (lib) concatStringsSep mapAttrs mapAttrsToList;
|
2024-08-25 15:35:50 +00:00
|
|
|
keydir = "/run/${name}/authorized_keys";
|
2023-08-10 21:53:21 +00:00
|
|
|
options =
|
|
|
|
[
|
2025-02-10 21:55:08 +00:00
|
|
|
"-e" # pass environment to child
|
|
|
|
"-E" # log to stderr
|
|
|
|
"-R" # create hostkeys if needed
|
2023-08-10 21:53:21 +00:00
|
|
|
"-P /run/dropbear.pid"
|
2025-02-10 21:55:08 +00:00
|
|
|
"-F" # don't fork into background
|
|
|
|
]
|
|
|
|
++ (lib.optional (!allowRoot) "-w")
|
|
|
|
++ (lib.optional (!allowPasswordLogin) "-s")
|
|
|
|
++ (lib.optional (!allowPasswordLoginForRoot) "-g")
|
|
|
|
++ (lib.optional (!allowLocalPortForward) "-j")
|
|
|
|
++ (lib.optional (!allowRemotePortForward) "-k")
|
|
|
|
++ (lib.optional (!allowRemoteConnectionToForwardedPorts) "-a")
|
|
|
|
++ (lib.optionals (authorizedKeys != null) [
|
|
|
|
"-U"
|
|
|
|
"${keydir}/%n"
|
|
|
|
])
|
|
|
|
++ [
|
|
|
|
(if address != null then "-p ${address}:${toString port}" else "-p ${toString port}")
|
|
|
|
]
|
|
|
|
++ [ extraConfig ];
|
2024-08-25 15:35:50 +00:00
|
|
|
isKeyservice = typeOf authorizedKeys == "lambda";
|
2024-08-23 19:35:07 +00:00
|
|
|
authKeysConcat =
|
2025-02-10 21:55:08 +00:00
|
|
|
if authorizedKeys != null && !isKeyservice then
|
|
|
|
mapAttrs (n: v: concatStringsSep "\\n" v) authorizedKeys
|
|
|
|
else
|
|
|
|
{ };
|
2024-08-25 15:35:50 +00:00
|
|
|
keyservice = longrun {
|
|
|
|
name = "${name}-watch-keys";
|
|
|
|
run = ''
|
|
|
|
mkdir -p ${keydir}
|
|
|
|
exec ${watch-ssh-keys}/bin/watch-ssh-keys -d ${keydir} ${authorizedKeys "service"} ${authorizedKeys "path"}
|
|
|
|
'';
|
2025-02-10 21:55:08 +00:00
|
|
|
dependencies = [ (authorizedKeys "service") ];
|
2024-08-25 15:35:50 +00:00
|
|
|
};
|
2023-08-10 21:53:21 +00:00
|
|
|
in
|
|
|
|
longrun {
|
2024-08-23 19:35:07 +00:00
|
|
|
inherit name;
|
2024-02-13 22:12:26 +00:00
|
|
|
# we need /run/dropbear to point to hostkey storage, as that
|
|
|
|
# pathname is hardcoded into the binary.
|
2023-08-10 21:53:21 +00:00
|
|
|
# env -i clears the environment so we don't pass anything weird to
|
|
|
|
# ssh sessions
|
|
|
|
run = ''
|
2024-02-13 22:12:26 +00:00
|
|
|
ln -s $(mkstate dropbear) /run
|
2024-08-23 19:35:07 +00:00
|
|
|
mkdir -p /run/${name}/authorized_keys
|
2025-02-10 21:55:08 +00:00
|
|
|
${concatStringsSep "\n" (
|
|
|
|
mapAttrsToList (n: v: "echo -e '${v}' > /run/${name}/authorized_keys/${n} ") authKeysConcat
|
|
|
|
)}
|
2023-08-10 21:53:21 +00:00
|
|
|
. /etc/profile # sets PATH but do we need this? it's the same file as ashrc
|
|
|
|
exec env -i ENV=/etc/ashrc PATH=$PATH ${dropbear}/bin/dropbear ${concatStringsSep " " options}
|
|
|
|
'';
|
2024-08-25 15:35:50 +00:00
|
|
|
dependencies = lib.optional isKeyservice keyservice;
|
2023-08-10 21:53:21 +00:00
|
|
|
}
|