summaryrefslogtreecommitdiffhomepage
path: root/modules/rpc/luasrc/controller/rpc.lua
blob: aa77a8f2485d0248a16302b47bdf570f0235beff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
--[[
LuCI - Lua Configuration Interface

Copyright 2008 Steven Barth <steven@midlink.org>
Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

$Id$
]]--

local require = require
local pairs = pairs
local print = print

module "luci.controller.rpc"

function index()
	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.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()
	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