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