From 71831dfc470e2aa9c3104294a8c2ca785a480cf8 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Tue, 28 Apr 2015 09:51:38 -0400 Subject: Implement hostname validation --- hostname-model.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hostname-model.lua b/hostname-model.lua index 8e20707..31c3e84 100644 --- a/hostname-model.lua +++ b/hostname-model.lua @@ -27,6 +27,18 @@ end mymodule.update_name = function(self, name) local success = true + -- Hostname must be less than 64 characters, characters in set 0-9a-z-, and not start/end with - + if string.len(name.value.hostname.value) == 0 or string.len(name.value.hostname.value) > 63 then + name.value.hostname.errtxt = "Illegal length" + success = false + elseif string.find(name.value.hostname.value, "[^-0-9a-zA-Z]") then + name.value.hostname.errtxt = "Contains illegal character" + success = false + elseif string.find(name.value.hostname.value, "^-") or string.find(name.value.hostname.value, "-$") then + name.value.hostname.errtxt = "Illegal start/end character" + success = false + end + if success then fs.write_file("/etc/hostname", name.value.hostname.value) modelfunctions.run_executable({"hostname", "-F", "/etc/hostname"}) -- cgit v1.2.3