modules/rpc: Check for existence of external libraries
[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         if pcall(require, "luci.model.uci") then
37                 uci = entry({"rpc", "uci"}, call("rpc_uci"))
38                 uci.sysauth = "root"
39                 uci.sysauth_authenticator = authenticator
40         end
41         
42         fs = entry({"rpc", "fs"}, call("rpc_fs"))
43         fs.sysauth = "root"
44         fs.sysauth_authenticator = authenticator
45
46         sys = entry({"rpc", "sys"}, call("rpc_sys"))
47         sys.sysauth = "root"
48         sys.sysauth_authenticator = authenticator
49         
50         if pcall(require, "luci.model.ipkg") then
51                 fs = entry({"rpc", "ipkg"}, call("rpc_ipkg"))
52                 fs.sysauth = "root"
53                 fs.sysauth_authenticator = authenticator
54         end
55         
56         uci = entry({"rpc", "auth"}, call("rpc_auth"))
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         local uci     = require "luci.controller.rpc.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 "luci.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         local ipkg    = require "luci.model.ipkg"
149         local jsonrpc = require "luci.jsonrpc"
150         local http    = require "luci.http"
151         local ltn12   = require "luci.ltn12"
152
153         http.prepare_content("application/json")
154         ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
155 end