libs/web: rework hostname match to not allow arbritary sequences of numbers and dots
[project/luci.git] / libs / web / luasrc / cbi / datatypes.lua
index 6640db639251769de94b9214ed0b17f3b1ea3afc..65a8eedeb13d75db545b22c56e292b6c05c77bb8 100644 (file)
@@ -141,7 +141,11 @@ function macaddr(val)
 end
 
 function hostname(val)
-       if val and (#val < 254) and val.match(val, "^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") then
+       if val and (#val < 254) and (
+          val:match("^[a-zA-Z]+$") or
+          (val:match("^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") and
+           val:match("[^0-9%.]"))
+       ) then
                return true
        end
        return false
@@ -151,6 +155,10 @@ function host(val)
        return hostname(val) or ipaddr(val)
 end
 
+function network(val)
+       return uciname(val) or host(val)
+end
+
 function wpakey(val)
        if #val == 64 then
                return (val:match("^[a-fA-F0-9]+$") ~= nil)
@@ -231,7 +239,7 @@ function neg_network_ip4addr(val)
        if type(v) == "string" then
                v = v:gsub("^%s*!", "")
                return (uciname(v) or ip4addr(v))
-       end     
+       end
 end
 
 function range(val, min, max)
@@ -267,3 +275,25 @@ function max(val, max)
 
        return false
 end
+
+function neg(val, what)
+       if what and type(_M[what]) == "function" then
+               return _M[what](val:gsub("^%s*!%s*", ""))
+       end
+
+       return false
+end
+
+function list(val, what, ...)
+       if type(val) == "string" and what and type(_M[what]) == "function" then
+               for val in val:gmatch("%S+") do
+                       if not _M[what](val, ...) then
+                               return false
+                       end
+               end
+
+               return true
+       end
+
+       return false
+end