Skip to content

Commit

Permalink
nixos/sshd: fix socket activated ports when using ListenAddress
Browse files Browse the repository at this point in the history
Noticed that issue while reviewing #275633: when declaring
`ListenAddress host` without a port, all ports declared by
`Port`/`cfg.ports` will be used with `host` according to
`sshd_config(5)`.

However, if this is done and socket activation is used, only a socket
for port 22 is created instead of a sockets for each port from
`Port`/`cfg.ports`. This patch corrects that behavior.

Also added a regression test for this case.
  • Loading branch information
Ma27 committed Jan 3, 2024
1 parent cb274ae commit 7e45990
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion nixos/modules/services/networking/ssh/sshd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,11 @@ in
{ description = "SSH Socket";
wantedBy = [ "sockets.target" ];
socketConfig.ListenStream = if cfg.listenAddresses != [] then
map (l: "${l.addr}:${toString (if l.port != null then l.port else 22)}") cfg.listenAddresses
concatMap
({ addr, port }:
if port != null then [ "${addr}:${toString port}" ]
else map (p: "${addr}:${toString p}") cfg.ports)
cfg.listenAddresses
else
cfg.ports;
socketConfig.Accept = true;
Expand Down
28 changes: 27 additions & 1 deletion nixos/tests/openssh.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ in {
];
};

server-lazy-socket = {
virtualisation.vlans = [ 1 2 ];
services.openssh = {
enable = true;
startWhenNeeded = true;
ports = [ 2222 ];
listenAddresses = [ { addr = "0.0.0.0"; } ];
};
users.users.root.openssh.authorizedKeys.keys = [
snakeOilPublicKey
];
};

server-localhost-only =
{ ... }:

Expand Down Expand Up @@ -96,7 +109,9 @@ in {
};

client =
{ ... }: { };
{ ... }: {
virtualisation.vlans = [ 1 2 ];
};

};

Expand All @@ -109,6 +124,7 @@ in {
server_lazy.wait_for_unit("sshd.socket", timeout=30)
server_localhost_only_lazy.wait_for_unit("sshd.socket", timeout=30)
server_lazy_socket.wait_for_unit("sshd.socket", timeout=30)
with subtest("manual-authkey"):
client.succeed("mkdir -m 700 /root/.ssh")
Expand Down Expand Up @@ -145,6 +161,16 @@ in {
timeout=30
)
with subtest("socket activation on a non-standard port"):
client.succeed(
"cat ${snakeOilPrivateKey} > privkey.snakeoil"
)
client.succeed("chmod 600 privkey.snakeoil")
client.succeed(
"ssh -p 2222 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil root@192.168.2.4 true",
timeout=30
)
with subtest("configured-authkey"):
client.succeed(
"cat ${snakeOilPrivateKey} > privkey.snakeoil"
Expand Down

0 comments on commit 7e45990

Please sign in to comment.