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