* libs/web: Switched from HTTP-Basic-Auth to Session-Auth
authorSteven Barth <steven@midlink.org>
Sat, 28 Jun 2008 16:03:54 +0000 (16:03 +0000)
committerSteven Barth <steven@midlink.org>
Sat, 28 Jun 2008 16:03:54 +0000 (16:03 +0000)
* Updated Makefiles for better testing environment integration
* Fixed libs/sgi-luci

17 files changed:
Makefile
i18n/english/luasrc/i18n/default.en.lua
i18n/english/luasrc/i18n/sysauth.en.lua [new file with mode: 0644]
i18n/german/luasrc/i18n/default.de.lua
i18n/german/luasrc/i18n/sysauth.de.lua [new file with mode: 0644]
libs/core/luasrc/fs.lua
libs/core/luasrc/sys.lua
libs/sgi-cgi/ipkg/postinst [deleted file]
libs/sgi-cgi/luasrc/sgi/cgi.lua
libs/sgi-luci/root/usr/bin/luci-httpd
libs/web/luasrc/dispatcher.lua
libs/web/luasrc/http.lua
libs/web/luasrc/i18n.lua
libs/web/luasrc/sauth.lua
modules/admin-core/luasrc/view/sysauth.htm [new file with mode: 0644]
themes/fledermaus/luasrc/view/themes/fledermaus/header.htm
themes/openwrt.org/luasrc/view/themes/openwrt.org/header.htm

index 8ce59c268303a503a5d0cb5660872c1435098050..8ac0ac664ad48c63df18044b081e194d068b9b43 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -35,10 +35,14 @@ hostcopy:
        ln -s .$(LUCI_MODULEDIR) host/luci
 
 runboa: host
+       export LUA_PATH="`pwd`/host$(LUCI_MODULEDIR);;"
+       export LUA_CPATH="`pwd`/host$(LUCI_LIBRARYDIR);;"
        libs/sgi-webuci/host/buildconfig.sh `pwd`/host  > host/etc/boa/boa.conf
        ./host/usr/bin/boa -c ./host/etc/boa -d
 
 runluci: luahost
+       export LUA_PATH="`pwd`/host$(LUCI_MODULEDIR);;"
+       export LUA_CPATH="`pwd`/host$(LUCI_LIBRARYDIR);;"
        libs/httpd/host/runluci host$(HTDOCS)
 
 hostclean: clean
index 92ee1625f32033718fc6e9eca52f1597c09b7978..593c11ea3c0d46845b84689a927545e024601258 100644 (file)
@@ -46,6 +46,7 @@ key = "Key"
 language = "Language"
 limit = "Limit"
 load = "Load"
+login = "Login"
 
 macaddress = "MAC-Address"
 manpage = "see '%s' manpage"
diff --git a/i18n/english/luasrc/i18n/sysauth.en.lua b/i18n/english/luasrc/i18n/sysauth.en.lua
new file mode 100644 (file)
index 0000000..a87774a
--- /dev/null
@@ -0,0 +1,3 @@
+sysauth_head = "Authorization Required"
+sysauth_prompt = "Please enter your username and password."
+sysauth_failed = "Invalid username and/or password! Please try again."
index 8fd96d5b2502ff7a4f5e9121a8abd0a85dbac6e8..0eec9cf8333b09e25c4546ca1743c0cfad6a6184 100644 (file)
@@ -40,6 +40,8 @@ ipaddress = "IP-Adresse"
 
 legend = "Legende"
 library = "Bibliothek"
+load = "Last"
+login = "Anmelden"
 
 key = "Schlüssel"
 
diff --git a/i18n/german/luasrc/i18n/sysauth.de.lua b/i18n/german/luasrc/i18n/sysauth.de.lua
new file mode 100644 (file)
index 0000000..c01a4b9
--- /dev/null
@@ -0,0 +1,3 @@
+sysauth_head = "Autorisation benötigt"
+sysauth_prompt = "Bitte Benutzernamen und Passwort eingeben."
+sysauth_failed = "Ungültiger Benutzername und/oder ungültiges Passwort! Bitte nocheinmal versuchen."
index 5c1f2a051b4ad1d2bdd63c0615c0fcc56d25a747..415e8e567cb2fa82d7e5fff23a63c4be29fca80a 100644 (file)
@@ -28,6 +28,9 @@ module("luci.fs", package.seeall)
 
 require("posix")
 
+-- Access
+access = posix.access
+
 -- Glob
 glob = posix.glob
 
index 54c4e061377ed222012c741f9126af36f5bd9f2a..540a636fb864b25330e617934792823db632e07d 100644 (file)
@@ -285,10 +285,18 @@ user = {}
 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))
+function user.checkpasswd(username, password)
+       local account = user.getuser(username)
+       
+       -- FIXME: detect testing environment
+       if luci.fs.isfile("/etc/shadow") and not luci.fs.access("/etc/shadow", "r") then
+               return true
+       elseif account then
+               if account.passwd == "!" then
+                       return true
+               else
+                       return (account.passwd == posix.crypt(account.passwd, password))
+               end
        end
 end
        
diff --git a/libs/sgi-cgi/ipkg/postinst b/libs/sgi-cgi/ipkg/postinst
deleted file mode 100755 (executable)
index d8780c2..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/sh
-PATTERNS='/cgi-bin/luci/admin:root:$p$root'
-
-for i in $PATTERNS
-do
-       grep "$i" ${IPKG_INSTROOT}/etc/httpd.conf >/dev/null 2>/dev/null || echo "$i" >> ${IPKG_INSTROOT}/etc/httpd.conf
-done 
-
-[ -n "${IPKG_INSTROOT}" ] || /etc/init.d/httpd restart
-
index 8ba4c54a3a33abb43d4c7b1522a22c53cfd650df..5555e4a0586c4b76b05d8f14d67c46a8c3c7a8a6 100644 (file)
@@ -47,7 +47,7 @@ function run()
                        print(id)
                        break;
                end
-               
+
                if id == 1 then
                        io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\n")
                elseif id == 2 then
index 7f2ee50fa559e35d2a8055b68203da963518d5b6..091e38b21b854b08e6ebc0ec8b2bd5e07c3a5035 100755 (executable)
@@ -27,7 +27,5 @@ vhost:set_handler("/luci", lucihandler)
 io.stderr:write("Starting LuCI HTTPD on port " .. PORT .. "...\n")
 io.stderr:write("Point your browser to http://localhost:" .. PORT .. "/luci\n")
 
-daemon = luci.httpd.Daemon()
---daemon.debug = true
-daemon:register(serversocket, server:create_daemon_handlers())
-daemon:run()
+luci.httpd.register(serversocket, server:create_daemon_handlers())
+luci.httpd.run()
index 5bf3fc1d6f246ed207807267e73ebe770ebc8898..989eb44021d4e78b9f40a463638e7ad57fd87932 100644 (file)
@@ -43,18 +43,6 @@ function build_url(...)
        return luci.http.getenv("SCRIPT_NAME") .. "/" .. 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 luci.util.copcall(luci.template.render, "error401") then
-               luci.http.prepare_content("text/plain")
-               luci.http.write(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")
@@ -80,6 +68,25 @@ function error500(message)
        return false
 end
 
+-- Renders an authorization form
+function sysauth(default)
+       local user = luci.http.formvalue("username")
+       local pass = luci.http.formvalue("password")
+       
+       if user and luci.sys.user.checkpasswd(user, pass) then
+               local sid = luci.sys.uniqueid(16)
+               luci.http.header("Set-Cookie", "sysauth=" .. sid)
+               luci.sauth.write(sid, user)
+               return true
+       else
+               require("luci.i18n")
+               require("luci.template")
+               context.path = {}
+               luci.template.render("sysauth", {duser=default, fuser=user})
+               return false
+       end
+end
+
 -- Creates a request object for dispatching
 function httpdispatch(request)
        luci.http.context.request = request
@@ -119,34 +126,9 @@ function dispatch(request)
                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)
        end
-
-       if track.setgroup then
-               luci.sys.process.setgroup(track.setgroup)
-       end
-
-       if track.setuser then
-               luci.sys.process.setuser(track.setuser)
-       end
        
        -- Init template engine
        local tpl = require("luci.template")
@@ -159,6 +141,27 @@ function dispatch(request)
        viewns.resource    = luci.config.main.resourcebase
        viewns.REQUEST_URI = luci.http.getenv("SCRIPT_NAME") .. (luci.http.getenv("PATH_INFO") or "")
        
+       if track.sysauth then
+               require("luci.sauth")
+               local def  = (type(track.sysauth) == "string") and track.sysauth
+               local accs = def and {track.sysauth} or track.sysauth
+               local user = luci.sauth.read(luci.http.getcookie("sysauth"))
+               
+               
+               if not luci.util.contains(accs, user) then
+                       if not sysauth(def) then
+                               return
+                       end
+               end
+       end
+
+       if track.setgroup then
+               luci.sys.process.setgroup(track.setgroup)
+       end
+
+       if track.setuser then
+               luci.sys.process.setuser(track.setuser)
+       end
 
        if c and type(c.target) == "function" then
                context.dispatched = c
index 37050e47853eb87b2ab944da108954e4420c77af..f37d67343ea59b561ff74fcd52e6055ed008454e 100644 (file)
@@ -51,13 +51,13 @@ function Request.__init__(self, env, sourcein, sinkerr)
        self.parsed_input = false
 end
 
-function Request.formvalue(self, name, default)
+function Request.formvalue(self, name)
        if not self.parsed_input then
                self:_parse_input()
        end
        
        if name then
-               return self.message.params[name] and tostring(self.message.params[name]) or default
+               return self.message.params[name]
        else
                return self.message.params
        end
@@ -84,7 +84,7 @@ end
 function Request.getcookie(self, name)
   local c = string.gsub(";" .. (self:getenv("HTTP_COOKIE") or "") .. ";", "%s*;%s*", ";")
   local p = ";" .. name .. "=(.-);"
-  local i, j, value = cookies:find(p)
+  local i, j, value = c:find(p)
   return value and urldecode(value)
 end
 
@@ -130,6 +130,10 @@ function formvaluetable(...)
        return context.request:formvaluetable(...)
 end
 
+function getcookie(...)
+       return context.request:getcookie(...)
+end
+
 function getvalue(...)
        return context.request:getvalue(...)
 end
@@ -147,9 +151,6 @@ function setfilehandler(...)
 end
 
 function header(key, value)
-       if not context.status then
-               status()
-       end
        if not context.headers then
                context.headers = {}
        end
@@ -187,7 +188,7 @@ function write(content)
 end
 
 function redirect(url)
-       header("Status", "302 Found")
+       status(302, "Found")
        header("Location", url)
        close()
 end
index 35ad0965d0715ed01a8c8e914f70054b45733544..2c2bb2e1a20df45d6320c5650d22f01e069bd467 100644 (file)
@@ -71,10 +71,10 @@ function setlanguage(lang)
 end
 
 -- Returns the i18n-value defined by "key" or if there is no such: "default"
-function translate(key, default)
+function translate(key, def)
        return (table[context.lang] and table[context.lang][key])
                or (table[default] and table[default][key])
-               or default
+               or def
 end
 
 -- Translate shourtcut with sprintf/string.format inclusion
index 724e22d201a40dddceb6401482f5bcb5c5c0578b..d838f84f693a07e1455168c763c60d0b24ea0ac8 100644 (file)
@@ -19,7 +19,7 @@ require("luci.config")
 
 luci.config.sauth = luci.config.sauth or {}
 sessionpath = luci.config.sauth.sessionpath
-sessiontime = luci.config.sauth.sessiontime
+sessiontime = tonumber(luci.config.sauth.sessiontime)
 
 
 function clean()
@@ -30,7 +30,7 @@ function clean()
                return nil
        end
        
-       for i, file in files do
+       for i, file in pairs(files) do
                local fname = sessionpath .. "/" .. file
                local stat = luci.fs.stat(fname)
                if stat and stat.type == "regular" and stat.atime + sessiontime < now then
@@ -41,11 +41,14 @@ end
 
 function prepare()
        luci.fs.mkdir(sessionpath)
-       luci.fs.chmod(sessionpath, "a-rwx,u+rw")
+       luci.fs.chmod(sessionpath, "a-rwx,u+rwx")
 end
 
 function read(id)
-       cleansessions()
+       if not id then
+               return
+       end
+       clean()
        return luci.fs.readfile(sessionpath .. "/" .. id)
 end
 
diff --git a/modules/admin-core/luasrc/view/sysauth.htm b/modules/admin-core/luasrc/view/sysauth.htm
new file mode 100644 (file)
index 0000000..4e5367a
--- /dev/null
@@ -0,0 +1,26 @@
+<%+header%>
+<% luci.i18n.loadc("sysauth") %>
+<h1><%:sysauth_head%></h1>
+<p><%:sysauth_prompt%></p>
+<% if fuser then %>
+<div class="error"><%:sysauth_failed%></div>
+<br />
+<% end %>
+<form method="post" action="<%=REQUEST_URI%>">
+               <div class="cbi-section-node">
+                       <div class="cbi-value">
+                               <div class="cbi-value-title"><%:username%></div>
+                               <div class="cbi-value-field"><input type="text" name="username" value="<%=duser%>" /></div>
+                       </div>
+                       <div class="cbi-value">
+                               <div class="cbi-value-title"><%:password%></div>
+                               <div class="cbi-value-field"><input type="password" name="password" /></div>
+                       </div>
+                       <br />
+                       <div>
+                               <input type="submit" value="<%:login%>" />
+                               <input type="reset" value="<%:reset%>" />
+                       </div>
+               </div>
+</form>
+<%+footer%>
\ No newline at end of file
index 9f27f98b8b8005384c6aa39a807309343b4dc32e..f1343a0e1ba20378e7230042fb23f7fec8e0c5ac 100644 (file)
@@ -26,7 +26,7 @@ require("luci.http").prepare_content("text/html")
        <% if node and node.css then %><link rel="stylesheet" type="text/css" href="<%=resource%>/<%=node.css%>" /><% end %>
        <meta http-equiv="content-type" content="text/xhtml+xml; charset=utf-8" />
        <meta http-equiv="content-script-type" content="text/javascript" />
-       <title>LuCI - Lua Configuration Interface - <%=node.title%></title>
+       <title>LuCI - Lua Configuration Interface - <%=(node and node.title)%></title>
 </head>
 <body>
 <div id="header">
index 6852b386e01333d1def5a4676a30c6583b22c4e9..2501051a778e4f5fc0e6ed90156110c5c75c08c4 100644 (file)
@@ -26,7 +26,7 @@ require("luci.http").prepare_content("text/html")
        <% if node and node.css then %><link rel="stylesheet" type="text/css" href="<%=resource%>/<%=node.css%>" /><% end %>
        <meta http-equiv="content-type" content="text/xhtml+xml; charset=utf-8" />
        <meta http-equiv="content-script-type" content="text/javascript" />
-       <title>LuCI - Lua Configuration Interface - <%=node.title%></title>
+       <title>LuCI - Lua Configuration Interface - <%=(node and node.title)%></title>
 </head>
 <body>
 <div id="header">