web: Improve hostname validation

This commit is contained in:
Manuel Munz 2011-02-12 11:09:13 +00:00
parent 2b72c2093f
commit 408a58f6f6
4 changed files with 6 additions and 15 deletions

View file

@ -141,8 +141,8 @@ var cbi_validators = {
}, },
'hostname': function(v) 'hostname': function(v)
{ { if ( v.length <= 24 )
return (v.match(/^[a-zA-Z_][a-zA-Z0-9_\-.]*$/) != null); return (v.match(/^[a-zA-Z0-9][a-zA-Z0-9\-.]*[a-zA-Z0-9]$/) != null);
}, },
'wpakey': function(v) 'wpakey': function(v)

View file

@ -127,10 +127,9 @@ function macaddr(val)
end end
function hostname(val) function hostname(val)
if val and val:match("[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*") then if val and (#val < 25) and val.match(val, "^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") then
return true -- XXX: ToDo: need better solution return true
end end
return false return false
end end

View file

@ -75,7 +75,7 @@ s:taboption("general", DummyValue, "_uptime", translate("Uptime")).value =
luci.tools.webadmin.date_format(tonumber(uptime)) luci.tools.webadmin.date_format(tonumber(uptime))
hn = s:taboption("general", Value, "hostname", translate("Hostname")) hn = s:taboption("general", Value, "hostname", translate("Hostname"))
hn.datatype = "hostname"
function hn.write(self, section, value) function hn.write(self, section, value)
Value.write(self, section, value) Value.write(self, section, value)
luci.sys.hostname(value) luci.sys.hostname(value)

View file

@ -38,15 +38,7 @@ b.anonymous = true
hn = b:option(Value, "hostname", translate("Hostname")) hn = b:option(Value, "hostname", translate("Hostname"))
hn.rmempty = false hn.rmempty = false
function hn.validate(self, value) hn.datatype = "hostname"
if value == nil then
return
elseif (#value > 24) or string.match(value, "[^%w%.%-]") or string.match(value, "^[%-%.]") or string.match(value, "[%-%.]$") then
return nil, translate("Hostname may contain up to 24 alphanumeric characters. Minus and period are also allowed, but not in the beginning or the end of the hostname.")
else
return value
end
end
loc = b:option(Value, "location", translate("Location")) loc = b:option(Value, "location", translate("Location"))
loc.rmempty = false loc.rmempty = false