* Added native basic authentication support
authorSteven Barth <steven@midlink.org>
Mon, 2 Jun 2008 17:49:27 +0000 (17:49 +0000)
committerSteven Barth <steven@midlink.org>
Mon, 2 Jun 2008 17:49:27 +0000 (17:49 +0000)
* Cleanups

libs/cbi/luasrc/cbi.lua
libs/core/luasrc/sys.lua
libs/sgi-haserl/luasrc/sgi/haserl.lua
libs/sgi-webuci/luasrc/sgi/webuci.lua
libs/sgi-webuci/root/usr/lib/boa/luci.lua
libs/web/luasrc/dispatcher.lua
modules/admin-core/luasrc/controller/admin/index.lua

index 42b58ce..178c46f 100644 (file)
@@ -579,11 +579,6 @@ function AbstractValue.render(self, s, scope)
        if not self.optional or self:cfgvalue(s) or self:formcreated(s) then
                scope = scope or {}
                scope.section = s
-
-               -- fixup size for MultiValue fields
-               if instanceof(self, MultiValue) and self.widget == "select" and not self.size then
-                       self.size = #self.vallist
-               end
                
                Node.render(self, scope)
        end
@@ -741,6 +736,14 @@ function MultiValue.__init__(self, ...)
        self.delimiter = " "
 end
 
+function MultiValue.render(self, ...)
+       if self.widget == "select" and not self.size then
+               self.size = #self.vallist
+       end
+
+       AbstractValue.render(self, ...)
+end
+
 function MultiValue.value(self, key, val)
        val = val or key
        table.insert(self.keylist, tostring(key))
index 6d03f59..80d702b 100644 (file)
@@ -263,6 +263,14 @@ end
 user = {}
 -- returns user information to a given uid
 user.getuser = posix.getpasswd
+
+-- checks whether a string matches the password of a certain system user
+function user.checkpasswd(user, password)
+       local account = user.getuser(user)
+       if posix.crypt and account then
+               return (account.passwd == posix.crypt(account.passwd, password))
+       end
+end
        
 -- Changes the user password of given user
 function user.setpasswd(user, pwd)
index f3994b5..35bc1c9 100644 (file)
@@ -29,6 +29,12 @@ require("luci.fs")
 -- Environment Table
 luci.http.env = ENV
 
+-- Enforces user authentification
+function luci.http.basic_auth(verify_callback, realm)
+       -- Dummy for Haserl
+       return true
+end
+
 -- Returns the main dispatcher URL
 function luci.http.dispatcher()
        return luci.http.env.SCRIPT_NAME or ""
index 2beff69..1ad067c 100644 (file)
@@ -33,6 +33,21 @@ function initenv(env, vars)
        luci.http.vars = vars
 end
 
+-- Enforces user authentification
+function luci.http.basic_auth(verify_callback, realm)
+       local user = luci.http.env.auth_user
+       local pass = luci.http.env.auth_password
+       realm = realm or ""
+       
+       if not user or not verify_callback(user, pass) then
+               luci.http.status("401", "Unauthorized")
+               luci.http.header("WWW-Authenticate", string.format('Basic realm="%s"', realm))
+               return false    
+       else
+               return true
+       end
+end
+
 -- Returns the main dispatcher URL
 function luci.http.dispatcher()
        return luci.http.env.SCRIPT_NAME or ""
index 2ea6ba4..e34bd5e 100644 (file)
@@ -38,6 +38,8 @@ function init(path)
                luci.sys.net.routes                     = function() return {} end
                luci.sys.wifi.getiwconfig       = function() return {} end
                luci.sys.wifi.iwscan            = function() return {} end
+               
+               luci.sys.user.checkpasswd   = function() return true end
        end
 end
 
index feda28d..cd48162 100644 (file)
@@ -58,6 +58,18 @@ function build_url(...)
        return luci.http.dispatcher() .. "/" .. table.concat(arg, "/")
 end
 
+-- Prints an error message or renders the "error401" template if available
+function error401(message)
+       message = message or "Unauthorized"
+
+       require("luci.template")
+       if not pcall(luci.template.render, "error401") then
+               luci.http.prepare_content("text/plain")
+               print(message)
+       end
+       return false
+end
+
 -- Sends a 404 error code and renders the "error404" template if available
 function error404(message)
        luci.http.status(404, "Not Found")
@@ -115,6 +127,20 @@ function dispatch()
                end
        end
 
+       if track.sysauth then
+               local accs = track.sysauth
+               accs = (type(accs) == "string") and {accs} or accs
+               
+               local function sysauth(user, password)
+                       return (luci.util.contains(accs, user)
+                               and luci.sys.user.checkpasswd(user, password)) 
+               end
+               
+               if not luci.http.basic_auth(sysauth) then
+                       error401()
+                       return
+               end
+       end
 
        if track.i18n then
                require("luci.i18n").loadc(track.i18n)
index 51f60bd..a6c57c6 100644 (file)
@@ -11,11 +11,12 @@ function index()
        
        entry({"about"}, template("about")).i18n = "admin-core"
        
-       local page  = node("admin")
-       page.target = alias("admin", "index")
-       page.title  = i18n("administration", "Administration")
-       page.order  = 10
-       page.i18n   = "admin-core"
+       local page   = node("admin")
+       page.target  = alias("admin", "index")
+       page.title   = i18n("administration", "Administration")
+       page.order   = 10
+       page.i18n    = "admin-core"
+       page.sysauth = "root"
        
        local page  = node("admin", "index")
        page.target = template("admin_index/index")