modules/rpc: remove uvl bindings
[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 sdat = luci.sauth.read(auth)
29                         user = loadstring(sdat)().user
30                         if user and luci.util.contains(accs, user) then
31                                 return user, auth
32                         end
33                 end
34                 luci.http.status(403, "Forbidden")
35         end
36         
37         local rpc = node("rpc")
38         rpc.sysauth = "root"
39         rpc.sysauth_authenticator = authenticator
40         rpc.notemplate = true
41         
42         entry({"rpc", "uci"}, call("rpc_uci"))
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_fs()
103         local util    = require "luci.util"
104         local io      = require "io"
105         local fs2     = util.clone(require "nixio.fs")
106         local jsonrpc = require "luci.jsonrpc"
107         local http    = require "luci.http"
108         local ltn12   = require "luci.ltn12"
109
110         function fs2.readfile(filename)
111                 local stat, mime = pcall(require, "mime")
112                 if not stat then
113                         error("Base64 support not available. Please install LuaSocket.")
114                 end
115
116                 local fp = io.open(filename)
117                 if not fp then
118                         return nil
119                 end
120
121                 local output = {}
122                 local sink = ltn12.sink.table(output)
123                 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
124                 return ltn12.pump.all(source, sink) and table.concat(output)
125         end
126         
127         function fs2.writefile(filename, data)
128                 local stat, mime = pcall(require, "mime")
129                 if not stat then
130                         error("Base64 support not available. Please install LuaSocket.")
131                 end
132
133                 local  file = io.open(filename, "w")
134                 local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
135                 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
136         end
137         
138         http.prepare_content("application/json")
139         ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
140 end
141
142 function rpc_sys()
143         local sys     = require "luci.sys"
144         local jsonrpc = require "luci.jsonrpc"
145         local http    = require "luci.http"
146         local ltn12   = require "luci.ltn12"
147         
148         http.prepare_content("application/json")
149         ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
150 end
151
152 function rpc_ipkg()
153         if not pcall(require, "luci.model.ipkg") then
154                 luci.http.status(404, "Not Found")
155                 return nil
156         end
157         local ipkg    = require "luci.model.ipkg"
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(ipkg, http.source()), http.write)
164 end