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