1aeea69b418631d0c7d452a5cc4cb0a5f3d4f176
[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         uci = entry({"rpc", "uci"}, call("rpc_uci"))
37         uci.sysauth = "root"
38         uci.sysauth_authenticator = authenticator
39         uci.notemplate = true
40         
41         fs = entry({"rpc", "fs"}, call("rpc_fs"))
42         fs.sysauth = "root"
43         fs.sysauth_authenticator = authenticator
44         fs.notemplate = true
45
46         sys = entry({"rpc", "sys"}, call("rpc_sys"))
47         sys.sysauth = "root"
48         sys.sysauth_authenticator = authenticator
49         sys.notemplate = true
50
51         ipkg = entry({"rpc", "ipkg"}, call("rpc_ipkg"))
52         ipkg.sysauth = "root"
53         ipkg.sysauth_authenticator = authenticator
54         ipkg.notemplate = true
55         
56         entry({"rpc", "auth"}, call("rpc_auth")).notemplate = true
57 end
58
59 function rpc_auth()
60         local jsonrpc = require "luci.jsonrpc"
61         local sauth   = require "luci.sauth"
62         local http    = require "luci.http"
63         local sys     = require "luci.sys"
64         local ltn12   = require "luci.ltn12"
65         
66         http.setfilehandler()
67         
68         local loginstat
69         
70         local server = {}
71         server.login = function(user, pass)
72                 local sid
73                 
74                 if sys.user.checkpasswd(user, pass) then
75                         sid = sys.uniqueid(16)
76                         http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
77                         sauth.write(sid, user)
78                 end
79                 
80                 return sid
81         end
82         
83         http.prepare_content("application/json")
84         ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
85 end
86
87 function rpc_uci()
88         if not pcall(require, "luci.model.uci") then
89                 luci.http.status(404, "Not Found")
90                 return nil
91         end
92         local uci     = require "luci.controller.rpc.uci"
93         local jsonrpc = require "luci.jsonrpc"
94         local http    = require "luci.http"
95         local ltn12   = require "luci.ltn12"
96         
97         http.prepare_content("application/json")
98         ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
99 end
100
101 function rpc_fs()
102         local util    = require "luci.util"
103         local io      = require "io"
104         local fs2     = util.clone(require "luci.fs")
105         local jsonrpc = require "luci.jsonrpc"
106         local http    = require "luci.http"
107         local ltn12   = require "luci.ltn12"
108
109         function fs2.readfile(filename)
110                 local stat, mime = pcall(require, "mime")
111                 if not stat then
112                         error("Base64 support not available. Please install LuaSocket.")
113                 end
114
115                 local fp = io.open(filename)
116                 if not fp then
117                         return nil
118                 end
119
120                 local output = {}
121                 local sink = ltn12.sink.table(output)
122                 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
123                 return ltn12.pump.all(source, sink) and table.concat(output)
124         end
125         
126         function fs2.writefile(filename, data)
127                 local stat, mime = pcall(require, "mime")
128                 if not stat then
129                         error("Base64 support not available. Please install LuaSocket.")
130                 end
131
132                 local  file = io.open(filename, "w")
133                 local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
134                 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
135         end
136         
137         http.prepare_content("application/json")
138         ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
139 end
140
141 function rpc_sys()
142         local sys     = require "luci.sys"
143         local jsonrpc = require "luci.jsonrpc"
144         local http    = require "luci.http"
145         local ltn12   = require "luci.ltn12"
146         
147         http.prepare_content("application/json")
148         ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
149 end
150
151 function rpc_ipkg()
152         if not pcall(require, "luci.model.ipkg") then
153                 luci.http.status(404, "Not Found")
154                 return nil
155         end
156         local ipkg    = require "luci.model.ipkg"
157         local jsonrpc = require "luci.jsonrpc"
158         local http    = require "luci.http"
159         local ltn12   = require "luci.ltn12"
160
161         http.prepare_content("application/json")
162         ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
163 end