libs/web: Prevent luci.http to prematurely parse the POST data
[project/luci.git] / modules / admin-full / luasrc / controller / admin / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 module("luci.controller.admin.system", package.seeall)
15
16 function index()
17 luci.i18n.loadc("admin-core")
18 local i18n = luci.i18n.translate
19
20 entry({"admin", "system"}, template("admin_system/index"), i18n("system", "System"), 30)
21 entry({"admin", "system", "packages"}, call("action_packages"), i18n("a_s_packages", "Paketverwaltung"), 10)
22 entry({"admin", "system", "packages", "ipkg"}, call("action_ipkg"), i18n("a_s_p_ipkg", "IPKG-Konfiguration"))
23 entry({"admin", "system", "passwd"}, call("action_passwd"), i18n("a_s_changepw", "Passwort ändern"), 20)
24 entry({"admin", "system", "sshkeys"}, call("action_sshkeys"), i18n("a_s_sshkeys", "SSH-Schlüssel"), 30)
25 entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("system", "System"), 40)
26 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("a_s_fstab", "Einhängepunkte"), 50)
27 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("a_s_flash", "Firmwareupgrade"), 60)
28 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot", "Neu starten"), 70)
29 end
30
31 function action_editor()
32 local file = luci.http.formvalue("file", "")
33 local data = luci.http.formvalue("data")
34 local err = nil
35 local msg = nil
36 local stat = true
37
38 if file and data then
39 stat, err = luci.fs.writefile(file, data)
40 end
41
42 if not stat then
43 err = luci.util.split(err, " ")
44 table.remove(err, 1)
45 msg = table.concat(err, " ")
46 end
47
48 local cnt, err = luci.fs.readfile(file)
49 if cnt then
50 cnt = luci.util.pcdata(cnt)
51 end
52 luci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg})
53 end
54
55 function action_ipkg()
56 local file = "/etc/ipkg.conf"
57 local data = luci.http.formvalue("data")
58 local stat = nil
59 local err = nil
60
61 if data then
62 stat, err = luci.fs.writefile(file, data)
63 end
64
65 local cnt = luci.fs.readfile(file)
66 if cnt then
67 cnt = luci.util.pcdata(cnt)
68 end
69
70 luci.template.render("admin_system/ipkg", {cnt=cnt, msg=err})
71 end
72
73 function action_packages()
74 local ipkg = require("luci.model.ipkg")
75 local void = nil
76 local submit = luci.http.formvalue("submit")
77
78
79 -- Search query
80 local query = luci.http.formvalue("query")
81 query = (query ~= '') and query or nil
82
83
84 -- Packets to be installed
85 local install = submit and luci.http.formvaluetable("install")
86
87 -- Install from URL
88 local url = luci.http.formvalue("url")
89 if url and url ~= '' and submit then
90 if not install then
91 install = {}
92 end
93 install[url] = 1
94 end
95
96 -- Do install
97 if install then
98 for k, v in pairs(install) do
99 void, install[k] = ipkg.install(k)
100 end
101 end
102
103
104 -- Remove packets
105 local remove = submit and luci.http.formvaluetable("remove")
106 if remove then
107 for k, v in pairs(remove) do
108 void, remove[k] = ipkg.remove(k)
109 end
110 end
111
112
113 -- Update all packets
114 local update = luci.http.formvalue("update")
115 if update then
116 void, update = ipkg.update()
117 end
118
119
120 -- Upgrade all packets
121 local upgrade = luci.http.formvalue("upgrade")
122 if upgrade then
123 void, upgrade = ipkg.upgrade()
124 end
125
126
127 -- Package info
128 local info = luci.model.ipkg.info(query)
129 info = info or {}
130 local pkgs = {}
131
132 -- Sort after status and name
133 for k, v in pairs(info) do
134 local x = 0
135 for i, j in pairs(pkgs) do
136 local vins = (v.Status and v.Status.installed)
137 local jins = (j.Status and j.Status.installed)
138 if vins ~= jins then
139 if vins then
140 break
141 end
142 else
143 if j.Package > v.Package then
144 break
145 end
146 end
147 x = i
148 end
149 table.insert(pkgs, x+1, v)
150 end
151
152 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
153 install=install, remove=remove, update=update, upgrade=upgrade})
154 end
155
156 function action_passwd()
157 local p1 = luci.http.formvalue("pwd1")
158 local p2 = luci.http.formvalue("pwd2")
159 local stat = nil
160
161 if p1 or p2 then
162 if p1 == p2 then
163 stat = luci.sys.user.setpasswd("root", p1)
164 else
165 stat = 10
166 end
167 end
168
169 luci.template.render("admin_system/passwd", {stat=stat})
170 end
171
172 function action_reboot()
173 local reboot = luci.http.formvalue("reboot")
174 luci.template.render("admin_system/reboot", {reboot=reboot})
175 if reboot then
176 luci.sys.reboot()
177 end
178 end
179
180 function action_sshkeys()
181 local file = "/etc/dropbear/authorized_keys"
182 local data = luci.http.formvalue("data")
183 local stat = nil
184 local err = nil
185
186 if data then
187 stat, err = luci.fs.writefile(file, data)
188 end
189
190 local cnt = luci.fs.readfile(file)
191 if cnt then
192 cnt = luci.util.pcdata(cnt)
193 end
194
195 luci.template.render("admin_system/sshkeys", {cnt=cnt, msg=err})
196 end
197
198 function action_upgrade()
199 require("luci.model.uci")
200
201 local ret = nil
202 local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
203 local tmpfile = "/tmp/firmware.img"
204
205 local file
206 luci.http.setfilehandler(
207 function(meta, chunk, eof)
208 if not file then
209 file = io.open(tmpfile, "w")
210 end
211 if chunk then
212 file:write(chunk)
213 end
214 if eof then
215 file:close()
216 end
217 end
218 )
219
220 local fname = luci.http.formvalue("image")
221 local keepcfg = luci.http.formvalue("keepcfg")
222
223 if plat and fname then
224 local kpattern = nil
225 if keepcfg then
226 local files = luci.model.uci.get_all("luci", "flash_keep")
227 if files.luci and files.luci.flash_keep then
228 kpattern = ""
229 for k,v in pairs(files.luci.flash_keep) do
230 kpattern = kpattern .. " " .. v
231 end
232 end
233 end
234 ret = luci.sys.flash(tmpfile, kpattern)
235 end
236
237 luci.template.render("admin_system/upgrade", {sysupgrade=plat, ret=ret})
238 end