libs/cbi: add wep and wpa key datatype validators
This commit is contained in:
parent
e75cb4f5ba
commit
f1ebca9388
1 changed files with 38 additions and 18 deletions
|
@ -23,7 +23,7 @@ local tonumber = tonumber
|
||||||
module "luci.cbi.datatypes"
|
module "luci.cbi.datatypes"
|
||||||
|
|
||||||
|
|
||||||
function bool( val )
|
function bool(val)
|
||||||
if val == "1" or val == "yes" or val == "on" or val == "true" then
|
if val == "1" or val == "yes" or val == "on" or val == "true" then
|
||||||
return true
|
return true
|
||||||
elseif val == "0" or val == "no" or val == "off" or val == "false" then
|
elseif val == "0" or val == "no" or val == "off" or val == "false" then
|
||||||
|
@ -35,7 +35,7 @@ function bool( val )
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function uint( val )
|
function uint(val)
|
||||||
local n = tonumber(val)
|
local n = tonumber(val)
|
||||||
if n ~= nil and math.floor(n) == n and n >= 0 then
|
if n ~= nil and math.floor(n) == n and n >= 0 then
|
||||||
return true
|
return true
|
||||||
|
@ -44,7 +44,7 @@ function uint( val )
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function int( val )
|
function int(val)
|
||||||
local n = tonumber(val)
|
local n = tonumber(val)
|
||||||
if n ~= nil and math.floor(n) == n then
|
if n ~= nil and math.floor(n) == n then
|
||||||
return true
|
return true
|
||||||
|
@ -53,15 +53,15 @@ function int( val )
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function float( val )
|
function float(val)
|
||||||
return ( tonumber(val) ~= nil )
|
return ( tonumber(val) ~= nil )
|
||||||
end
|
end
|
||||||
|
|
||||||
function ipaddr( val )
|
function ipaddr(val)
|
||||||
return ip4addr(val) or ip6addr(val)
|
return ip4addr(val) or ip6addr(val)
|
||||||
end
|
end
|
||||||
|
|
||||||
function ip4addr( val )
|
function ip4addr(val)
|
||||||
if val then
|
if val then
|
||||||
return ip.IPv4(val) and true or false
|
return ip.IPv4(val) and true or false
|
||||||
end
|
end
|
||||||
|
@ -69,12 +69,12 @@ function ip4addr( val )
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function ip4prefix( val )
|
function ip4prefix(val)
|
||||||
val = tonumber(val)
|
val = tonumber(val)
|
||||||
return ( val and val >= 0 and val <= 32 )
|
return ( val and val >= 0 and val <= 32 )
|
||||||
end
|
end
|
||||||
|
|
||||||
function ip6addr( val )
|
function ip6addr(val)
|
||||||
if val then
|
if val then
|
||||||
return ip.IPv6(val) and true or false
|
return ip.IPv6(val) and true or false
|
||||||
end
|
end
|
||||||
|
@ -82,17 +82,17 @@ function ip6addr( val )
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function ip6prefix( val )
|
function ip6prefix(val)
|
||||||
val = tonumber(val)
|
val = tonumber(val)
|
||||||
return ( val and val >= 0 and val <= 128 )
|
return ( val and val >= 0 and val <= 128 )
|
||||||
end
|
end
|
||||||
|
|
||||||
function port( val )
|
function port(val)
|
||||||
val = tonumber(val)
|
val = tonumber(val)
|
||||||
return ( val and val >= 1 and val <= 65535 )
|
return ( val and val >= 1 and val <= 65535 )
|
||||||
end
|
end
|
||||||
|
|
||||||
function portrange( val )
|
function portrange(val)
|
||||||
local p1, p2 = val:match("^(%d+)%-(%d+)$")
|
local p1, p2 = val:match("^(%d+)%-(%d+)$")
|
||||||
if p1 and p2 and port(p1) and port(p2) then
|
if p1 and p2 and port(p1) and port(p2) then
|
||||||
return true
|
return true
|
||||||
|
@ -101,7 +101,7 @@ function portrange( val )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function macaddr( val )
|
function macaddr(val)
|
||||||
if val and val:match(
|
if val and val:match(
|
||||||
"^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
|
"^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
|
||||||
"[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
|
"[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
|
||||||
|
@ -121,7 +121,7 @@ function macaddr( val )
|
||||||
return false
|
return false
|
||||||
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:match("[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*") then
|
||||||
return true -- XXX: ToDo: need better solution
|
return true -- XXX: ToDo: need better solution
|
||||||
end
|
end
|
||||||
|
@ -129,16 +129,36 @@ function hostname( val )
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function host( val )
|
function host(val)
|
||||||
return hostname(val) or ipaddr(val)
|
return hostname(val) or ipaddr(val)
|
||||||
end
|
end
|
||||||
|
|
||||||
function string( val )
|
function wpakey(val)
|
||||||
|
if #val == 64 then
|
||||||
|
return (val:match("^[a-fA-F0-9]+$") ~= nil)
|
||||||
|
else
|
||||||
|
return (#val >= 8) and (#val <= 63)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function wepkey(val)
|
||||||
|
if val:sub(1, 2) == "s:" then
|
||||||
|
val = val:sub(3)
|
||||||
|
end
|
||||||
|
|
||||||
|
if (#val == 10) or (#val == 26) then
|
||||||
|
return (val:match("^[a-fA-F0-9]+$") ~= nil)
|
||||||
|
else
|
||||||
|
return (#v == 5) or (#v == 13)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function string(val)
|
||||||
return true -- Everything qualifies as valid string
|
return true -- Everything qualifies as valid string
|
||||||
end
|
end
|
||||||
|
|
||||||
function directory( val, seen )
|
function directory( val, seen )
|
||||||
local s = fs.stat( val )
|
local s = fs.stat(val)
|
||||||
seen = seen or { }
|
seen = seen or { }
|
||||||
|
|
||||||
if s and not seen[s.ino] then
|
if s and not seen[s.ino] then
|
||||||
|
@ -154,7 +174,7 @@ function directory( val, seen )
|
||||||
end
|
end
|
||||||
|
|
||||||
function file( val, seen )
|
function file( val, seen )
|
||||||
local s = fs.stat( val )
|
local s = fs.stat(val)
|
||||||
seen = seen or { }
|
seen = seen or { }
|
||||||
|
|
||||||
if s and not seen[s.ino] then
|
if s and not seen[s.ino] then
|
||||||
|
@ -170,7 +190,7 @@ function file( val, seen )
|
||||||
end
|
end
|
||||||
|
|
||||||
function device( val, seen )
|
function device( val, seen )
|
||||||
local s = fs.stat( val )
|
local s = fs.stat(val)
|
||||||
seen = seen or { }
|
seen = seen or { }
|
||||||
|
|
||||||
if s and not seen[s.ino] then
|
if s and not seen[s.ino] then
|
||||||
|
|
Loading…
Reference in a new issue