Merge pull request #953 from musashino205/mount-fix-disp
[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") and fs.access("/etc/config/fstab") 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"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
35         entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
36         entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
37         entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
38
39         -- call() instead of post() due to upload handling!
40         entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
41         entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
42
43         entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
44         entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
45 end
46
47 function action_clock_status()
48         local set = tonumber(luci.http.formvalue("set"))
49         if set ~= nil and set > 0 then
50                 local date = os.date("*t", set)
51                 if date then
52                         luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
53                                 date.year, date.month, date.day, date.hour, date.min, date.sec
54                         })
55                         luci.sys.call("/etc/init.d/sysfixtime restart")
56                 end
57         end
58
59         luci.http.prepare_content("application/json")
60         luci.http.write_json({ timestring = os.date("%c") })
61 end
62
63 function action_packages()
64         local fs = require "nixio.fs"
65         local ipkg = require "luci.model.ipkg"
66         local submit = (luci.http.formvalue("exec") == "1")
67         local update, upgrade
68         local changes = false
69         local install = { }
70         local remove  = { }
71         local stdout  = { "" }
72         local stderr  = { "" }
73         local out, err
74
75         -- Display
76         local display = luci.http.formvalue("display") or "installed"
77
78         -- Letter
79         local letter = string.byte(luci.http.formvalue("letter") or "A", 1)
80         letter = (letter == 35 or (letter >= 65 and letter <= 90)) and letter or 65
81
82         -- Search query
83         local query = luci.http.formvalue("query")
84         query = (query ~= '') and query or nil
85
86
87         -- Modifying actions
88         if submit then
89                 -- Packets to be installed
90                 local ninst = luci.http.formvalue("install")
91                 local uinst = nil
92
93                 -- Install from URL
94                 local url = luci.http.formvalue("url")
95                 if url and url ~= '' then
96                         uinst = url
97                 end
98
99                 -- Do install
100                 if ninst then
101                         install[ninst], out, err = ipkg.install(ninst)
102                         stdout[#stdout+1] = out
103                         stderr[#stderr+1] = err
104                         changes = true
105                 end
106
107                 if uinst then
108                         local pkg
109                         for pkg in luci.util.imatch(uinst) do
110                                 install[uinst], out, err = ipkg.install(pkg)
111                                 stdout[#stdout+1] = out
112                                 stderr[#stderr+1] = err
113                                 changes = true
114                         end
115                 end
116
117                 -- Remove packets
118                 local rem = luci.http.formvalue("remove")
119                 if rem then
120                         remove[rem], out, err = ipkg.remove(rem)
121                         stdout[#stdout+1] = out
122                         stderr[#stderr+1] = err
123                         changes = true
124                 end
125
126
127                 -- Update all packets
128                 update = luci.http.formvalue("update")
129                 if update then
130                         update, out, err = ipkg.update()
131                         stdout[#stdout+1] = out
132                         stderr[#stderr+1] = err
133                 end
134
135
136                 -- Upgrade all packets
137                 upgrade = luci.http.formvalue("upgrade")
138                 if upgrade then
139                         upgrade, out, err = ipkg.upgrade()
140                         stdout[#stdout+1] = out
141                         stderr[#stderr+1] = err
142                 end
143         end
144
145
146         -- List state
147         local no_lists = true
148         local old_lists = false
149         if fs.access("/var/opkg-lists/") then
150                 local list
151                 for list in fs.dir("/var/opkg-lists/") do
152                         no_lists = false
153                         if (fs.stat("/var/opkg-lists/"..list, "mtime") or 0) < (os.time() - (24 * 60 * 60)) then
154                                 old_lists = true
155                                 break
156                         end
157                 end
158         end
159
160
161         luci.template.render("admin_system/packages", {
162                 display   = display,
163                 letter    = letter,
164                 query     = query,
165                 install   = install,
166                 remove    = remove,
167                 update    = update,
168                 upgrade   = upgrade,
169                 no_lists  = no_lists,
170                 old_lists = old_lists,
171                 stdout    = table.concat(stdout, ""),
172                 stderr    = table.concat(stderr, "")
173         })
174
175         -- Remove index cache
176         if changes then
177                 fs.unlink("/tmp/luci-indexcache")
178         end
179 end
180
181 local function image_supported(image)
182         return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
183 end
184
185 local function image_checksum(image)
186         return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
187 end
188
189 local function image_sha256_checksum(image)
190         return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
191 end
192
193 local function supports_sysupgrade()
194         return nixio.fs.access("/lib/upgrade/platform.sh")
195 end
196
197 local function supports_reset()
198         return (os.execute([[grep -sqE '"rootfs_data"|"ubi"' /proc/mtd]]) == 0)
199 end
200
201 local function storage_size()
202         local size = 0
203         if nixio.fs.access("/proc/mtd") then
204                 for l in io.lines("/proc/mtd") do
205                         local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
206                         if n == "linux" or n == "firmware" then
207                                 size = tonumber(s, 16)
208                                 break
209                         end
210                 end
211         elseif nixio.fs.access("/proc/partitions") then
212                 for l in io.lines("/proc/partitions") do
213                         local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
214                         if b and n and not n:match('[0-9]') then
215                                 size = tonumber(b) * 1024
216                                 break
217                         end
218                 end
219         end
220         return size
221 end
222
223
224 function action_flashops()
225         --
226         -- Overview
227         --
228         luci.template.render("admin_system/flashops", {
229                 reset_avail   = supports_reset(),
230                 upgrade_avail = supports_sysupgrade()
231         })
232 end
233
234 function action_sysupgrade()
235         local fs = require "nixio.fs"
236         local http = require "luci.http"
237         local image_tmp = "/tmp/firmware.img"
238
239         local fp
240         http.setfilehandler(
241                 function(meta, chunk, eof)
242                         if not fp and meta and meta.name == "image" then
243                                 fp = io.open(image_tmp, "w")
244                         end
245                         if fp and chunk then
246                                 fp:write(chunk)
247                         end
248                         if fp and eof then
249                                 fp:close()
250                         end
251                 end
252         )
253
254         if not luci.dispatcher.test_post_security() then
255                 fs.unlink(image_tmp)
256                 return
257         end
258
259         --
260         -- Cancel firmware flash
261         --
262         if http.formvalue("cancel") then
263                 fs.unlink(image_tmp)
264                 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
265                 return
266         end
267
268         --
269         -- Initiate firmware flash
270         --
271         local step = tonumber(http.formvalue("step") or 1)
272         if step == 1 then
273                 if image_supported(image_tmp) then
274                         luci.template.render("admin_system/upgrade", {
275                                 checksum = image_checksum(image_tmp),
276                                 sha256ch = image_sha256_checksum(image_tmp),
277                                 storage  = storage_size(),
278                                 size     = (fs.stat(image_tmp, "size") or 0),
279                                 keep     = (not not http.formvalue("keep"))
280                         })
281                 else
282                         fs.unlink(image_tmp)
283                         luci.template.render("admin_system/flashops", {
284                                 reset_avail   = supports_reset(),
285                                 upgrade_avail = supports_sysupgrade(),
286                                 image_invalid = true
287                         })
288                 end
289         --
290         -- Start sysupgrade flash
291         --
292         elseif step == 2 then
293                 local keep = (http.formvalue("keep") == "1") and "" or "-n"
294                 luci.template.render("admin_system/applyreboot", {
295                         title = luci.i18n.translate("Flashing..."),
296                         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."),
297                         addr  = (#keep > 0) and "192.168.1.1" or nil
298                 })
299                 fork_exec("sleep 1; killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %q" %{ keep, image_tmp })
300         end
301 end
302
303 function action_backup()
304         local reader = ltn12_popen("sysupgrade --create-backup - 2>/dev/null")
305
306         luci.http.header(
307                 'Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' %{
308                         luci.sys.hostname(),
309                         os.date("%Y-%m-%d")
310                 })
311
312         luci.http.prepare_content("application/x-targz")
313         luci.ltn12.pump.all(reader, luci.http.write)
314 end
315
316 function action_restore()
317         local fs = require "nixio.fs"
318         local http = require "luci.http"
319         local archive_tmp = "/tmp/restore.tar.gz"
320
321         local fp
322         http.setfilehandler(
323                 function(meta, chunk, eof)
324                         if not fp and meta and meta.name == "archive" then
325                                 fp = io.open(archive_tmp, "w")
326                         end
327                         if fp and chunk then
328                                 fp:write(chunk)
329                         end
330                         if fp and eof then
331                                 fp:close()
332                         end
333                 end
334         )
335
336         if not luci.dispatcher.test_post_security() then
337                 fs.unlink(archive_tmp)
338                 return
339         end
340
341         local upload = http.formvalue("archive")
342         if upload and #upload > 0 then
343                 luci.template.render("admin_system/applyreboot")
344                 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
345                 luci.sys.reboot()
346                 return
347         end
348
349         http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
350 end
351
352 function action_reset()
353         if supports_reset() then
354                 luci.template.render("admin_system/applyreboot", {
355                         title = luci.i18n.translate("Erasing..."),
356                         msg   = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
357                         addr  = "192.168.1.1"
358                 })
359
360                 fork_exec("sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot")
361                 return
362         end
363
364         http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
365 end
366
367 function action_passwd()
368         local p1 = luci.http.formvalue("pwd1")
369         local p2 = luci.http.formvalue("pwd2")
370         local stat = nil
371
372         if p1 or p2 then
373                 if p1 == p2 then
374                         stat = luci.sys.user.setpasswd("root", p1)
375                 else
376                         stat = 10
377                 end
378         end
379
380         luci.template.render("admin_system/passwd", {stat=stat})
381 end
382
383 function action_reboot()
384         luci.sys.reboot()
385 end
386
387 function fork_exec(command)
388         local pid = nixio.fork()
389         if pid > 0 then
390                 return
391         elseif pid == 0 then
392                 -- change to root dir
393                 nixio.chdir("/")
394
395                 -- patch stdin, out, err to /dev/null
396                 local null = nixio.open("/dev/null", "w+")
397                 if null then
398                         nixio.dup(null, nixio.stderr)
399                         nixio.dup(null, nixio.stdout)
400                         nixio.dup(null, nixio.stdin)
401                         if null:fileno() > 2 then
402                                 null:close()
403                         end
404                 end
405
406                 -- replace with target command
407                 nixio.exec("/bin/sh", "-c", command)
408         end
409 end
410
411 function ltn12_popen(command)
412
413         local fdi, fdo = nixio.pipe()
414         local pid = nixio.fork()
415
416         if pid > 0 then
417                 fdo:close()
418                 local close
419                 return function()
420                         local buffer = fdi:read(2048)
421                         local wpid, stat = nixio.waitpid(pid, "nohang")
422                         if not close and wpid and stat == "exited" then
423                                 close = true
424                         end
425
426                         if buffer and #buffer > 0 then
427                                 return buffer
428                         elseif close then
429                                 fdi:close()
430                                 return nil
431                         end
432                 end
433         elseif pid == 0 then
434                 nixio.dup(fdo, nixio.stdout)
435                 fdi:close()
436                 fdo:close()
437                 nixio.exec("/bin/sh", "-c", command)
438         end
439 end