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