libs/core: Reworked some basic libraries to not use package.seeall
[project/luci.git] / modules / rpc / luasrc / controller / rpc.lua
index dd00f63c370de26ac6112b614ee50a8504fd7eb9..aa77a8f2485d0248a16302b47bdf570f0235beff 100644 (file)
@@ -12,33 +12,93 @@ You may obtain a copy of the License at
 
 $Id$
 ]]--
-module("luci.controller.rpc", package.seeall)
+
+local require = require
+local pairs = pairs
+local print = print
+
+module "luci.controller.rpc"
 
 function index()
-       local authenticator = function(validator)
-               require "luci.jsonrpc"
-               require "luci.http"
-               luci.http.setfilehandler()
-               
-               local loginstat
-               
-               local server = {}
-               server.login = function(...)
-                       loginstat = validator(...)
-                       return loginstat
+       local function authenticator(validator, accs)
+               local auth = luci.http.formvalue("auth", true)
+               if auth then
+                       local user = luci.sauth.read(auth)
+                       if user and luci.util.contains(accs, user) then
+                               return user, auth
+                       end
                end
-               
-               luci.http.prepare_content("application/json")
-               luci.http.write(luci.jsonrpc.handle(server, luci.http.content()))
-               
-               return loginstat
+               luci.http.status(403, "Forbidden")
        end
        
        uci = entry({"rpc", "uci"}, call("rpc_uci"))
        uci.sysauth = "root"
        uci.sysauth_authenticator = authenticator
+       
+       fs = entry({"rpc", "fs"}, call("rpc_fs"))
+       fs.sysauth = "root"
+       fs.sysauth_authenticator = authenticator
+
+       fs = entry({"rpc", "sys"}, call("rpc_sys"))
+       fs.sysauth = "root"
+       fs.sysauth_authenticator = authenticator
+       
+       uci = entry({"rpc", "auth"}, call("rpc_auth"))
+end
+
+function rpc_auth()
+       local jsonrpc = require "luci.jsonrpc"
+       local sauth   = require "luci.sauth"
+       local http    = require "luci.http"
+       local sys     = require "luci.sys"
+       
+       http.setfilehandler()
+       
+       local loginstat
+       
+       local server = {}
+       server.login = function(user, pass)
+               local sid
+               
+               if sys.user.checkpasswd(user, pass) then
+                       sid = sys.uniqueid(16)
+                       http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
+                       sauth.write(sid, user)
+               end
+               
+               return sid
+       end
+       
+       http.prepare_content("application/json")
+       http.write(jsonrpc.handle(server, http.content()))
 end
 
 function rpc_uci()
-       luci.http.write("HELLO THAR!")
+       local uci     = require "luci.controller.rpc.uci"
+       local jsonrpc = require "luci.jsonrpc"
+       local http    = require "luci.http"
+       
+       http.setfilehandler()
+       http.prepare_content("application/json")
+       http.write(jsonrpc.handle(uci, http.content()))
+end
+
+function rpc_fs()
+       local fs      = require "luci.fs"
+       local jsonrpc = require "luci.jsonrpc"
+       local http    = require "luci.http"
+       
+       http.setfilehandler()
+       http.prepare_content("application/json")
+       http.write(jsonrpc.handle(fs, http.content()))
+end
+
+function rpc_sys()
+       local sys     = require "luci.sys"
+       local jsonrpc = require "luci.jsonrpc"
+       local http    = require "luci.http"
+       
+       http.setfilehandler()
+       http.prepare_content("application/json")
+       http.write(jsonrpc.handle(sys, http.content()))
 end
\ No newline at end of file