Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/libexec/nuageinit/tests/update_sshd_config.lua
288926 views
1
#!/usr/libexec/flua
2
---
3
-- SPDX-License-Identifier: BSD-2-Clause
4
--
5
-- Copyright (c) 2026 Baptiste Daroussin <[email protected]>
6
7
local n = require("nuage")
8
9
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
10
if not root then
11
root = ""
12
end
13
14
local sshd_config = root .. "/etc/ssh/sshd_config"
15
16
local 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()
22
end
23
24
local function read_config()
25
local f = assert(io.open(sshd_config, "r"))
26
local content = f:read("*a")
27
f:close()
28
return content
29
end
30
31
-- Key not found: appended
32
setup("SomeOtherKey yes\n")
33
n.update_sshd_config("PasswordAuthentication", "yes")
34
if read_config() ~= "SomeOtherKey yes\nPasswordAuthentication yes\n" then
35
n.err("Key not found: should be appended")
36
end
37
38
-- Key with same value: no change
39
setup("PasswordAuthentication yes\n")
40
n.update_sshd_config("PasswordAuthentication", "yes")
41
if read_config() ~= "PasswordAuthentication yes\n" then
42
n.err("Same value: should not change")
43
end
44
45
-- Key with different value: changed
46
setup("PasswordAuthentication no\n")
47
n.update_sshd_config("PasswordAuthentication", "yes")
48
if read_config() ~= "PasswordAuthentication yes\n" then
49
n.err("Different value: should change")
50
end
51
52
-- Key with comment
53
setup("PasswordAuthentication no # keep this\n")
54
n.update_sshd_config("PasswordAuthentication", "yes")
55
if read_config() ~= "PasswordAuthentication yes\n" then
56
n.err("Comment stripped: '" .. read_config() .. "'")
57
end
58
59
-- Case insensitive key matching
60
setup("passwordauthentication no\n")
61
n.update_sshd_config("PasswordAuthentication", "yes")
62
if read_config() ~= "PasswordAuthentication yes\n" then
63
n.err("Case insensitive matching failed")
64
end
65
66
-- Extra spaces
67
setup(" PasswordAuthentication no \n")
68
n.update_sshd_config("PasswordAuthentication", "yes")
69
if read_config() ~= "PasswordAuthentication yes\n" then
70
n.err("Extra spaces handling failed: '" .. read_config() .. "'")
71
end
72
73
-- File does not exist: should be created with key/value
74
os.remove(sshd_config)
75
n.update_sshd_config("PasswordAuthentication", "yes")
76
if read_config() ~= "PasswordAuthentication yes\n" then
77
n.err("Missing file: should create: '" .. read_config() .. "'")
78
end
79
80
os.exit(0)
81
82