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