xref: /freebsd/libexec/nuageinit/tests/update_sshd_config.lua (revision bac7bd5038e09d12dfdbf79a87b25443e02d0ba9)
1#!/usr/libexec/flua
2---
3-- SPDX-License-Identifier: BSD-2-Clause
4--
5-- Copyright (c) 2026 Baptiste Daroussin <bapt@FreeBSD.org>
6
7local n = require("nuage")
8
9local root = os.getenv("NUAGE_FAKE_ROOTDIR")
10if not root then
11	root = ""
12end
13
14local sshd_config = root .. "/etc/ssh/sshd_config"
15
16local function setup(content)
17	local dir = root .. "/etc/ssh"
18	n.mkdir_p(dir)
19	local f = assert(io.open(sshd_config, "w"))
20	f:write(content)
21	f:close()
22end
23
24local function read_config()
25	local f = assert(io.open(sshd_config, "r"))
26	local content = f:read("*a")
27	f:close()
28	return content
29end
30
31-- Key not found: appended
32setup("SomeOtherKey yes\n")
33n.update_sshd_config("PasswordAuthentication", "yes")
34if read_config() ~= "SomeOtherKey yes\nPasswordAuthentication yes\n" then
35	n.err("Key not found: should be appended")
36end
37
38-- Key with same value: no change
39setup("PasswordAuthentication yes\n")
40n.update_sshd_config("PasswordAuthentication", "yes")
41if read_config() ~= "PasswordAuthentication yes\n" then
42	n.err("Same value: should not change")
43end
44
45-- Key with different value: changed
46setup("PasswordAuthentication no\n")
47n.update_sshd_config("PasswordAuthentication", "yes")
48if read_config() ~= "PasswordAuthentication yes\n" then
49	n.err("Different value: should change")
50end
51
52-- Key with comment
53setup("PasswordAuthentication no # keep this\n")
54n.update_sshd_config("PasswordAuthentication", "yes")
55if read_config() ~= "PasswordAuthentication yes\n" then
56	n.err("Comment stripped: '" .. read_config() .. "'")
57end
58
59-- Case insensitive key matching
60setup("passwordauthentication no\n")
61n.update_sshd_config("PasswordAuthentication", "yes")
62if read_config() ~= "PasswordAuthentication yes\n" then
63	n.err("Case insensitive matching failed")
64end
65
66-- Extra spaces
67setup("   PasswordAuthentication   no   \n")
68n.update_sshd_config("PasswordAuthentication", "yes")
69if read_config() ~= "PasswordAuthentication yes\n" then
70	n.err("Extra spaces handling failed: '" .. read_config() .. "'")
71end
72
73os.exit(0)
74