Globally reduce copyright headers
[project/luci.git] / modules / luci-mod-rpc / luasrc / controller / rpc.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
3 -- Licensed to the public under the Apache License 2.0.
4
5 local require = require
6 local pairs = pairs
7 local print = print
8 local pcall = pcall
9 local table = table
10
11 module "luci.controller.rpc"
12
13 function index()
14         local function authenticator(validator, accs)
15                 local auth = luci.http.formvalue("auth", true)
16                 if auth then -- if authentication token was given
17                         local sdat = (luci.util.ubus("session", "get", { ubus_rpc_session = auth }) or { }).values
18                         if sdat then -- if given token is valid
19                                 if sdat.user and luci.util.contains(accs, sdat.user) then
20                                         return sdat.user, auth
21                                 end
22                         end
23                 end
24                 luci.http.status(403, "Forbidden")
25         end
26
27         local rpc = node("rpc")
28         rpc.sysauth = "root"
29         rpc.sysauth_authenticator = authenticator
30         rpc.notemplate = true
31
32         entry({"rpc", "uci"}, call("rpc_uci"))
33         entry({"rpc", "fs"}, call("rpc_fs"))
34         entry({"rpc", "sys"}, call("rpc_sys"))
35         entry({"rpc", "ipkg"}, call("rpc_ipkg"))
36         entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
37 end
38
39 function rpc_auth()
40         local jsonrpc = require "luci.jsonrpc"
41         local http    = require "luci.http"
42         local sys     = require "luci.sys"
43         local ltn12   = require "luci.ltn12"
44         local util    = require "luci.util"
45
46         local loginstat
47
48         local server = {}
49         server.challenge = function(user, pass)
50                 local sid, token, secret
51
52                 if sys.user.checkpasswd(user, pass) then
53                         local sdat = util.ubus("session", "create", { timeout = luci.config.sauth.sessiontime })
54                         if sdat then
55                                 sid = sdat.ubus_rpc_session
56                                 token = sys.uniqueid(16)
57                                 secret = sys.uniqueid(16)
58
59                                 http.header("Set-Cookie", "sysauth="..sid.."; path=/")
60                                 util.ubus("session", "set", {
61                                         ubus_rpc_session = sid,
62                                         values = {
63                                                 user = user,
64                                                 token = token,
65                                                 secret = secret
66                                         }
67                                 })
68                         end
69                 end
70
71                 return sid and {sid=sid, token=token, secret=secret}
72         end
73
74         server.login = function(...)
75                 local challenge = server.challenge(...)
76                 return challenge and challenge.sid
77         end
78
79         http.prepare_content("application/json")
80         ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
81 end
82
83 function rpc_uci()
84         if not pcall(require, "luci.model.uci") then
85                 luci.http.status(404, "Not Found")
86                 return nil
87         end
88         local uci     = require "luci.jsonrpcbind.uci"
89         local jsonrpc = require "luci.jsonrpc"
90         local http    = require "luci.http"
91         local ltn12   = require "luci.ltn12"
92
93         http.prepare_content("application/json")
94         ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
95 end
96
97 function rpc_fs()
98         local util    = require "luci.util"
99         local io      = require "io"
100         local fs2     = util.clone(require "nixio.fs")
101         local jsonrpc = require "luci.jsonrpc"
102         local http    = require "luci.http"
103         local ltn12   = require "luci.ltn12"
104
105         function fs2.readfile(filename)
106                 local stat, mime = pcall(require, "mime")
107                 if not stat then
108                         error("Base64 support not available. Please install LuaSocket.")
109                 end
110
111                 local fp = io.open(filename)
112                 if not fp then
113                         return nil
114                 end
115
116                 local output = {}
117                 local sink = ltn12.sink.table(output)
118                 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
119                 return ltn12.pump.all(source, sink) and table.concat(output)
120         end
121
122         function fs2.writefile(filename, data)
123                 local stat, mime = pcall(require, "mime")
124                 if not stat then
125                         error("Base64 support not available. Please install LuaSocket.")
126                 end
127
128                 local  file = io.open(filename, "w")
129                 local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
130                 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
131         end
132
133         http.prepare_content("application/json")
134         ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
135 end
136
137 function rpc_sys()
138         local sys     = require "luci.sys"
139         local jsonrpc = require "luci.jsonrpc"
140         local http    = require "luci.http"
141         local ltn12   = require "luci.ltn12"
142
143         http.prepare_content("application/json")
144         ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
145 end
146
147 function rpc_ipkg()
148         if not pcall(require, "luci.model.ipkg") then
149                 luci.http.status(404, "Not Found")
150                 return nil
151         end
152         local ipkg    = require "luci.model.ipkg"
153         local jsonrpc = require "luci.jsonrpc"
154         local http    = require "luci.http"
155         local ltn12   = require "luci.ltn12"
156
157         http.prepare_content("application/json")
158         ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
159 end