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