luci-mod-admin-full: protect clock, flash and opkg ops with submit token
[project/luci.git] / modules / luci-mod-admin-full / luasrc / controller / admin / system.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008-2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.admin.system", package.seeall)
6
7 function index()
8         local fs = require "nixio.fs"
9
10         entry({"admin", "system"}, alias("admin", "system", "system"), _("System"), 30).index = true
11         entry({"admin", "system", "system"}, cbi("admin_system/system"), _("System"), 1)
12         entry({"admin", "system", "clock_status"}, post_on({ set = true }, "action_clock_status"))
13
14         entry({"admin", "system", "admin"}, cbi("admin_system/admin"), _("Administration"), 2)
15
16         if fs.access("/bin/opkg") then
17                 entry({"admin", "system", "packages"}, post_on({ exec = "1" }, "action_packages"), _("Software"), 10)
18                 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
19         end
20
21         entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
22         entry({"admin", "system", "crontab"}, form("admin_system/crontab"), _("Scheduled Tasks"), 46)
23
24         if fs.access("/sbin/block") then
25                 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
26                 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
27                 entry({"admin", "system", "fstab", "swap"},  cbi("admin_system/fstab/swap"),  nil).leaf = true
28         end
29
30         if fs.access("/sys/class/leds") then
31                 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
32         end
33
34         entry({"admin", "system", "flashops"}, post_on({ exec = "1" }, "action_flashops"), _("Backup / Flash Firmware"), 70)
35         entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
36
37         entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
38         entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
39 end
40
41 function action_clock_status()
42         local set = tonumber(luci.http.formvalue("set"))
43         if set ~= nil and set > 0 then
44                 local date = os.date("*t", set)
45                 if date then
46                         luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
47                                 date.year, date.month, date.day, date.hour, date.min, date.sec
48                         })
49                 end
50         end
51
52         luci.http.prepare_content("application/json")
53         luci.http.write_json({ timestring = os.date("%c") })
54 end
55
56 function action_packages()
57         local fs = require "nixio.fs"
58         local ipkg = require "luci.model.ipkg"
59         local submit = (luci.http.formvalue("exec") == "1")
60         local update, upgrade
61         local changes = false
62         local install = { }
63         local remove  = { }
64         local stdout  = { "" }
65         local stderr  = { "" }
66         local out, err
67
68         -- Display
69         local display = luci.http.formvalue("display") or "installed"
70
71         -- Letter
72         local letter = string.byte(luci.http.formvalue("letter") or "A", 1)
73         letter = (letter == 35 or (letter >= 65 and letter <= 90)) and letter or 65
74
75         -- Search query
76         local query = luci.http.formvalue("query")
77         query = (query ~= '') and query or nil
78
79
80         -- Modifying actions
81         if submit then
82                 -- Packets to be installed
83                 local ninst = luci.http.formvalue("install")
84                 local uinst = nil
85
86                 -- Install from URL
87                 local url = luci.http.formvalue("url")
88                 if url and url ~= '' then
89                         uinst = url
90                 end
91
92                 -- Do install
93                 if ninst then
94                         install[ninst], out, err = ipkg.install(ninst)
95                         stdout[#stdout+1] = out
96                         stderr[#stderr+1] = err
97                         changes = true
98                 end
99
100                 if uinst then
101                         local pkg
102                         for pkg in luci.util.imatch(uinst) do
103                                 install[uinst], out, err = ipkg.install(pkg)
104                                 stdout[#stdout+1] = out
105                                 stderr[#stderr+1] = err
106                                 changes = true
107                         end
108                 end
109
110                 -- Remove packets
111                 local rem = luci.http.formvalue("remove")
112                 if rem then
113                         remove[rem], out, err = ipkg.remove(rem)
114                         stdout[#stdout+1] = out
115                         stderr[#stderr+1] = err
116                         changes = true
117                 end
118
119
120                 -- Update all packets
121                 update = luci.http.formvalue("update")
122                 if update then
123                         update, out, err = ipkg.update()
124                         stdout[#stdout+1] = out
125                         stderr[#stderr+1] = err
126                 end
127
128
129                 -- Upgrade all packets
130                 upgrade = luci.http.formvalue("upgrade")
131                 if upgrade then
132                         upgrade, out, err = ipkg.upgrade()
133                         stdout[#stdout+1] = out
134                         stderr[#stderr+1] = err
135                 end
136         end
137
138
139         -- List state
140         local no_lists = true
141         local old_lists = false
142         if fs.access("/var/opkg-lists/") then
143                 local list
144                 for list in fs.dir("/var/opkg-lists/") do
145                         no_lists = false
146                         if (fs.stat("/var/opkg-lists/"..list, "mtime") or 0) < (os.time() - (24 * 60 * 60)) then
147                                 old_lists = true
148                                 break
149                         end
150                 end
151         end
152
153
154         luci.template.render("admin_system/packages", {
155                 display   = display,
156                 letter    = letter,
157                 query     = query,
158                 install   = install,
159                 remove    = remove,
160                 update    = update,
161                 upgrade   = upgrade,
162                 no_lists  = no_lists,
163                 old_lists = old_lists,
164                 stdout    = table.concat(stdout, ""),
165                 stderr    = table.concat(stderr, "")
166         })
167
168         -- Remove index cache
169         if changes then
170                 fs.unlink("/tmp/luci-indexcache")
171         end
172 end
173
174 function action_flashops()
175         local http = require "luci.http"
176         local sys  = require "luci.sys"
177         local fs   = require "nixio.fs"
178
179         local submit = (http.formvalue("exec") == "1")
180
181         local upgrade_avail = fs.access("/lib/upgrade/platform.sh")
182         local reset_avail   = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
183
184         local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
185         local backup_cmd  = "sysupgrade --create-backup - 2>/dev/null"
186         local image_tmp   = "/tmp/firmware.img"
187
188         local function image_supported()
189                 return (os.execute("sysupgrade -T %q >/dev/null" % image_tmp) == 0)
190         end
191
192         local function image_checksum()
193                 return (luci.sys.exec("md5sum %q" % image_tmp):match("^([^%s]+)"))
194         end
195
196         local function storage_size()
197                 local size = 0
198                 if fs.access("/proc/mtd") then
199                         for l in io.lines("/proc/mtd") do
200                                 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
201                                 if n == "linux" or n == "firmware" then
202                                         size = tonumber(s, 16)
203                                         break
204                                 end
205                         end
206                 elseif fs.access("/proc/partitions") then
207                         for l in io.lines("/proc/partitions") do
208                                 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
209                                 if b and n and not n:match('[0-9]') then
210                                         size = tonumber(b) * 1024
211                                         break
212                                 end
213                         end
214                 end
215                 return size
216         end
217
218         --
219         -- Handle modifying actions
220         --
221         if submit then
222
223                 local fp
224                 http.setfilehandler(
225                         function(meta, chunk, eof)
226                                 if not fp then
227                                         if meta and meta.name == "image" then
228                                                 fp = io.open(image_tmp, "w")
229                                         else
230                                                 fp = io.popen(restore_cmd, "w")
231                                         end
232                                 end
233                                 if chunk then
234                                         fp:write(chunk)
235                                 end
236                                 if eof then
237                                         fp:close()
238                                 end
239                         end
240                 )
241
242                 if http.formvalue("backup") then
243                         --
244                         -- Assemble file list, generate backup
245                         --
246                         local reader = ltn12_popen(backup_cmd)
247                         http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
248                                 luci.sys.hostname(), os.date("%Y-%m-%d")})
249                         http.prepare_content("application/x-targz")
250                         luci.ltn12.pump.all(reader, http.write)
251                         return
252
253                 elseif http.formvalue("restore") then
254                         --
255                         -- Unpack received .tar.gz
256                         --
257                         local upload = http.formvalue("archive")
258                         if upload and #upload > 0 then
259                                 luci.template.render("admin_system/applyreboot")
260                                 luci.sys.reboot()
261                                 return
262                         end
263
264                 elseif http.formvalue("image") or http.formvalue("step") then
265                         --
266                         -- Initiate firmware flash
267                         --
268                         local step = tonumber(http.formvalue("step") or 1)
269                         if step == 1 then
270                                 if image_supported() then
271                                         luci.template.render("admin_system/upgrade", {
272                                                 checksum = image_checksum(),
273                                                 storage  = storage_size(),
274                                                 size     = (fs.stat(image_tmp, "size") or 0),
275                                                 keep     = (not not http.formvalue("keep"))
276                                         })
277                                 else
278                                         fs.unlink(image_tmp)
279                                         luci.template.render("admin_system/flashops", {
280                                                 reset_avail   = reset_avail,
281                                                 upgrade_avail = upgrade_avail,
282                                                 image_invalid = true
283                                         })
284                                 end
285                                 return
286                         --
287                         -- Start sysupgrade flash
288                         --
289                         elseif step == 2 then
290                                 local keep = (http.formvalue("keep") == "1") and "" or "-n"
291                                 luci.template.render("admin_system/applyreboot", {
292                                         title = luci.i18n.translate("Flashing..."),
293                                         msg   = luci.i18n.translate("The system is flashing now.<br /> DO NOT POWER OFF THE DEVICE!<br /> Wait a few minutes before you try to reconnect. It might be necessary to renew the address of your computer to reach the device again, depending on your settings."),
294                                         addr  = (#keep > 0) and "192.168.1.1" or nil
295                                 })
296                                 fork_exec("killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %q" %{ keep, image_tmp })
297                                 return
298                         end
299                 elseif reset_avail and http.formvalue("reset") then
300                         --
301                         -- Reset system
302                         --
303                         luci.template.render("admin_system/applyreboot", {
304                                 title = luci.i18n.translate("Erasing..."),
305                                 msg   = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
306                                 addr  = "192.168.1.1"
307                         })
308                         fork_exec("killall dropbear uhttpd; sleep 1; mtd -r erase rootfs_data")
309                         return
310                 end
311         end
312
313         --
314         -- Overview
315         --
316         luci.template.render("admin_system/flashops", {
317                 reset_avail   = reset_avail,
318                 upgrade_avail = upgrade_avail
319         })
320 end
321
322 function action_passwd()
323         local p1 = luci.http.formvalue("pwd1")
324         local p2 = luci.http.formvalue("pwd2")
325         local stat = nil
326
327         if p1 or p2 then
328                 if p1 == p2 then
329                         stat = luci.sys.user.setpasswd("root", p1)
330                 else
331                         stat = 10
332                 end
333         end
334
335         luci.template.render("admin_system/passwd", {stat=stat})
336 end
337
338 function action_reboot()
339         luci.sys.reboot()
340 end
341
342 function fork_exec(command)
343         local pid = nixio.fork()
344         if pid > 0 then
345                 return
346         elseif pid == 0 then
347                 -- change to root dir
348                 nixio.chdir("/")
349
350                 -- patch stdin, out, err to /dev/null
351                 local null = nixio.open("/dev/null", "w+")
352                 if null then
353                         nixio.dup(null, nixio.stderr)
354                         nixio.dup(null, nixio.stdout)
355                         nixio.dup(null, nixio.stdin)
356                         if null:fileno() > 2 then
357                                 null:close()
358                         end
359                 end
360
361                 -- replace with target command
362                 nixio.exec("/bin/sh", "-c", command)
363         end
364 end
365
366 function ltn12_popen(command)
367
368         local fdi, fdo = nixio.pipe()
369         local pid = nixio.fork()
370
371         if pid > 0 then
372                 fdo:close()
373                 local close
374                 return function()
375                         local buffer = fdi:read(2048)
376                         local wpid, stat = nixio.waitpid(pid, "nohang")
377                         if not close and wpid and stat == "exited" then
378                                 close = true
379                         end
380
381                         if buffer and #buffer > 0 then
382                                 return buffer
383                         elseif close then
384                                 fdi:close()
385                                 return nil
386                         end
387                 end
388         elseif pid == 0 then
389                 nixio.dup(fdo, nixio.stdout)
390                 fdi:close()
391                 fdo:close()
392                 nixio.exec("/bin/sh", "-c", command)
393         end
394 end