luci-mod-system: remplement dropbear settings as client side view
[oweals/luci.git] / modules / luci-mod-system / 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", "system"}, view("system/system"), _("System"), 1)
11         entry({"admin", "system", "clock_status"}, post_on({ set = true }, "action_clock_status"))
12         entry({"admin", "system", "ntp_restart"}, call("action_ntp_restart"), nil).leaf = true
13
14         entry({"admin", "system", "admin"}, firstchild(), _("Administration"), 2)
15         entry({"admin", "system", "admin", "password"}, view("system/password"), _("Router Password"), 1)
16
17         if fs.access("/etc/config/dropbear") then
18                 entry({"admin", "system", "admin", "dropbear"}, view("system/dropbear"), _("SSH Access"), 2)
19                 entry({"admin", "system", "admin", "sshkeys"}, view("system/sshkeys"), _("SSH-Keys"), 3)
20         end
21
22         entry({"admin", "system", "startup"}, view("system/startup"), _("Startup"), 45)
23         entry({"admin", "system", "crontab"}, view("system/crontab"), _("Scheduled Tasks"), 46)
24
25         if fs.access("/sbin/block") and fs.access("/etc/config/fstab") then
26                 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
27                 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
28                 entry({"admin", "system", "fstab", "swap"},  cbi("admin_system/fstab/swap"),  nil).leaf = true
29         end
30
31         local nodes, number = fs.glob("/sys/class/leds/*")
32         if number > 0 then
33                 entry({"admin", "system", "leds"}, view("system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
34         end
35
36         entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
37         entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
38         entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
39         entry({"admin", "system", "flashops", "backupmtdblock"}, post("action_backupmtdblock"))
40         entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
41
42         -- call() instead of post() due to upload handling!
43         entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
44         entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
45
46         entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
47         entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
48 end
49
50 function action_clock_status()
51         local set = tonumber(luci.http.formvalue("set"))
52         if set ~= nil and set > 0 then
53                 local date = os.date("*t", set)
54                 if date then
55                         luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
56                                 date.year, date.month, date.day, date.hour, date.min, date.sec
57                         })
58                         luci.sys.call("/etc/init.d/sysfixtime restart")
59                 end
60         end
61
62         luci.http.prepare_content("application/json")
63         luci.http.write_json({ timestring = os.date("%c") })
64 end
65
66 function action_ntp_restart()
67         if nixio.fs.access("/etc/init.d/sysntpd") then
68                 os.execute("/etc/init.d/sysntpd restart")
69         end
70         luci.http.prepare_content("text/plain")
71         luci.http.write("0")
72 end
73
74 local function image_supported(image)
75         return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
76 end
77
78 local function image_checksum(image)
79         return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
80 end
81
82 local function image_sha256_checksum(image)
83         return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
84 end
85
86 local function supports_sysupgrade()
87         return nixio.fs.access("/lib/upgrade/platform.sh")
88 end
89
90 local function supports_reset()
91         return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
92 end
93
94 local function storage_size()
95         local size = 0
96         if nixio.fs.access("/proc/mtd") then
97                 for l in io.lines("/proc/mtd") do
98                         local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
99                         if n == "linux" or n == "firmware" then
100                                 size = tonumber(s, 16)
101                                 break
102                         end
103                 end
104         elseif nixio.fs.access("/proc/partitions") then
105                 for l in io.lines("/proc/partitions") do
106                         local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
107                         if b and n and not n:match('[0-9]') then
108                                 size = tonumber(b) * 1024
109                                 break
110                         end
111                 end
112         end
113         return size
114 end
115
116
117 function action_flashops()
118         --
119         -- Overview
120         --
121         luci.template.render("admin_system/flashops", {
122                 reset_avail   = supports_reset(),
123                 upgrade_avail = supports_sysupgrade()
124         })
125 end
126
127 function action_sysupgrade()
128         local fs = require "nixio.fs"
129         local http = require "luci.http"
130         local image_tmp = "/tmp/firmware.img"
131
132         local fp
133         http.setfilehandler(
134                 function(meta, chunk, eof)
135                         if not fp and meta and meta.name == "image" then
136                                 fp = io.open(image_tmp, "w")
137                         end
138                         if fp and chunk then
139                                 fp:write(chunk)
140                         end
141                         if fp and eof then
142                                 fp:close()
143                         end
144                 end
145         )
146
147         if not luci.dispatcher.test_post_security() then
148                 fs.unlink(image_tmp)
149                 return
150         end
151
152         --
153         -- Cancel firmware flash
154         --
155         if http.formvalue("cancel") then
156                 fs.unlink(image_tmp)
157                 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
158                 return
159         end
160
161         --
162         -- Initiate firmware flash
163         --
164         local step = tonumber(http.formvalue("step")) or 1
165         if step == 1 then
166                 local force = http.formvalue("force")
167                 if image_supported(image_tmp) or force then
168                         luci.template.render("admin_system/upgrade", {
169                                 checksum = image_checksum(image_tmp),
170                                 sha256ch = image_sha256_checksum(image_tmp),
171                                 storage  = storage_size(),
172                                 size     = (fs.stat(image_tmp, "size") or 0),
173                                 keep     = (not not http.formvalue("keep")),
174                                 force    = (not not http.formvalue("force"))
175                         })
176                 else
177                         fs.unlink(image_tmp)
178                         luci.template.render("admin_system/flashops", {
179                                 reset_avail   = supports_reset(),
180                                 upgrade_avail = supports_sysupgrade(),
181                                 image_invalid = true
182                         })
183                 end
184
185         --
186         -- Start sysupgrade flash
187         --
188         elseif step == 2 then
189                 local keep = (http.formvalue("keep") == "1") and "" or "-n"
190                 local force = (http.formvalue("force") == "1") and "-F" or ""
191                 luci.template.render("admin_system/applyreboot", {
192                         title = luci.i18n.translate("Flashing..."),
193                         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."),
194                         addr  = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil
195                 })
196                 luci.sys.process.exec({ "/bin/sh", "-c","sleep 1; killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %s %q" %{ keep, force, image_tmp } }, nil, nil, true)
197         end
198 end
199
200 function action_backup()
201         luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"'
202                 %{ luci.sys.hostname(), os.date("%Y-%m-%d") })
203
204         luci.http.prepare_content("application/x-targz")
205         luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
206 end
207
208 function action_backupmtdblock()
209         local mv = luci.http.formvalue("mtdblockname") or ""
210         local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
211
212         if not m and n then
213                 luci.http.status(400, "Bad Request")
214                 return
215         end
216
217         luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
218                 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
219
220         luci.http.prepare_content("application/octet-stream")
221         luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write)
222 end
223
224 function action_restore()
225         local fs = require "nixio.fs"
226         local http = require "luci.http"
227         local archive_tmp = "/tmp/restore.tar.gz"
228
229         local fp
230         http.setfilehandler(
231                 function(meta, chunk, eof)
232                         if not fp and meta and meta.name == "archive" then
233                                 fp = io.open(archive_tmp, "w")
234                         end
235                         if fp and chunk then
236                                 fp:write(chunk)
237                         end
238                         if fp and eof then
239                                 fp:close()
240                         end
241                 end
242         )
243
244         if not luci.dispatcher.test_post_security() then
245                 fs.unlink(archive_tmp)
246                 return
247         end
248
249         local upload = http.formvalue("archive")
250         if upload and #upload > 0 then
251                 if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then
252                         luci.template.render("admin_system/applyreboot")
253                         os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
254                         luci.sys.reboot()
255                 else
256                         luci.template.render("admin_system/flashops", {
257                                 reset_avail   = supports_reset(),
258                                 upgrade_avail = supports_sysupgrade(),
259                                 backup_invalid = true
260                         })
261                 end
262                 return
263         end
264
265         http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
266 end
267
268 function action_reset()
269         if supports_reset() then
270                 luci.template.render("admin_system/applyreboot", {
271                         title = luci.i18n.translate("Erasing..."),
272                         msg   = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
273                         addr  = "192.168.1.1"
274                 })
275
276                 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
277                 return
278         end
279
280         http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
281 end
282
283 function action_reboot()
284         luci.sys.reboot()
285 end