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.
5 module("luci.controller.admin.system", package.seeall)
8 local fs = require "nixio.fs"
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
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"))
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"))
24 entry({"admin", "system", "startup"}, view("system/startup"), _("Startup"), 45)
25 entry({"admin", "system", "crontab"}, form("admin_system/crontab"), _("Scheduled Tasks"), 46)
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
33 local nodes, number = fs.glob("/sys/class/leds/*")
35 entry({"admin", "system", "leds"}, view("system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
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"))
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"))
48 entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
49 entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
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)
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
60 luci.sys.call("/etc/init.d/sysfixtime restart")
64 luci.http.prepare_content("application/json")
65 luci.http.write_json({ timestring = os.date("%c") })
68 function action_ntp_restart()
69 if nixio.fs.access("/etc/init.d/sysntpd") then
70 os.execute("/etc/init.d/sysntpd restart")
72 luci.http.prepare_content("text/plain")
76 local function image_supported(image)
77 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
80 local function image_checksum(image)
81 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
84 local function image_sha256_checksum(image)
85 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
88 local function supports_sysupgrade()
89 return nixio.fs.access("/lib/upgrade/platform.sh")
92 local function supports_reset()
93 return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
96 local function storage_size()
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)
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
119 function action_flashops()
123 luci.template.render("admin_system/flashops", {
124 reset_avail = supports_reset(),
125 upgrade_avail = supports_sysupgrade()
129 function action_sysupgrade()
130 local fs = require "nixio.fs"
131 local http = require "luci.http"
132 local image_tmp = "/tmp/firmware.img"
136 function(meta, chunk, eof)
137 if not fp and meta and meta.name == "image" then
138 fp = io.open(image_tmp, "w")
149 if not luci.dispatcher.test_post_security() then
155 -- Cancel firmware flash
157 if http.formvalue("cancel") then
159 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
164 -- Initiate firmware flash
166 local step = tonumber(http.formvalue("step")) or 1
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"))
180 luci.template.render("admin_system/flashops", {
181 reset_avail = supports_reset(),
182 upgrade_avail = supports_sysupgrade(),
188 -- Start sysupgrade flash
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
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)
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") })
206 luci.http.prepare_content("application/x-targz")
207 luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
210 function action_backupmtdblock()
211 local mv = luci.http.formvalue("mtdblockname") or ""
212 local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
215 luci.http.status(400, "Bad Request")
219 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
220 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
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)
226 function action_restore()
227 local fs = require "nixio.fs"
228 local http = require "luci.http"
229 local archive_tmp = "/tmp/restore.tar.gz"
233 function(meta, chunk, eof)
234 if not fp and meta and meta.name == "archive" then
235 fp = io.open(archive_tmp, "w")
246 if not luci.dispatcher.test_post_security() then
247 fs.unlink(archive_tmp)
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)
258 luci.template.render("admin_system/flashops", {
259 reset_avail = supports_reset(),
260 upgrade_avail = supports_sysupgrade(),
261 backup_invalid = true
267 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
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."),
278 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
282 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
285 function action_password()
286 local password = luci.http.formvalue("password")
288 luci.http.status(400, "Bad Request")
292 luci.http.prepare_content("application/json")
293 luci.http.write_json({ code = luci.sys.user.setpasswd("root", password) })
296 function action_sshkeys()
297 local keys = luci.http.formvalue("keys")
299 keys = luci.jsonc.parse(keys)
300 if not keys or type(keys) ~= "table" then
301 luci.http.status(400, "Bad Request")
305 local fd, err = io.open("/etc/dropbear/authorized_keys", "w")
307 luci.http.status(503, err)
312 for _, k in ipairs(keys) do
313 if type(k) == "string" and k:match("^%w+%-") then
322 local fd, err = io.open("/etc/dropbear/authorized_keys", "r")
324 luci.http.status(503, err)
330 local ln = fd:read("*l")
333 elseif ln:match("^[%w%-]+%s+[A-Za-z0-9+/=]+$") or
334 ln:match("^[%w%-]+%s+[A-Za-z0-9+/=]+%s")
342 luci.http.prepare_content("application/json")
343 luci.http.write_json(rv)
346 function action_reboot()