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"}, cbi("admin_system/system"), _("System"), 1)
11 entry({"admin", "system", "clock_status"}, post_on({ set = true }, "action_clock_status"))
13 entry({"admin", "system", "admin"}, firstchild(), _("Administration"), 2)
14 entry({"admin", "system", "admin", "password"}, template("admin_system/password"), _("Router Password"), 1)
15 entry({"admin", "system", "admin", "password", "json"}, post("action_password"))
17 if fs.access("/etc/config/dropbear") then
18 entry({"admin", "system", "admin", "dropbear"}, cbi("admin_system/dropbear"), _("SSH Access"), 2)
19 entry({"admin", "system", "admin", "sshkeys"}, template("admin_system/sshkeys"), _("SSH-Keys"), 3)
20 entry({"admin", "system", "admin", "sshkeys", "json"}, post_on({ keys = true }, "action_sshkeys"))
23 entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
24 entry({"admin", "system", "crontab"}, form("admin_system/crontab"), _("Scheduled Tasks"), 46)
26 if fs.access("/sbin/block") and fs.access("/etc/config/fstab") then
27 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
28 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
29 entry({"admin", "system", "fstab", "swap"}, cbi("admin_system/fstab/swap"), nil).leaf = true
32 local nodes, number = fs.glob("/sys/class/leds/*")
34 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
37 entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
38 entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
39 entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
40 entry({"admin", "system", "flashops", "backupmtdblock"}, post("action_backupmtdblock"))
41 entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
43 -- call() instead of post() due to upload handling!
44 entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
45 entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
47 entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
48 entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
51 function action_clock_status()
52 local set = tonumber(luci.http.formvalue("set"))
53 if set ~= nil and set > 0 then
54 local date = os.date("*t", set)
56 luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
57 date.year, date.month, date.day, date.hour, date.min, date.sec
59 luci.sys.call("/etc/init.d/sysfixtime restart")
63 luci.http.prepare_content("application/json")
64 luci.http.write_json({ timestring = os.date("%c") })
67 local function image_supported(image)
68 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
71 local function image_checksum(image)
72 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
75 local function image_sha256_checksum(image)
76 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
79 local function supports_sysupgrade()
80 return nixio.fs.access("/lib/upgrade/platform.sh")
83 local function supports_reset()
84 return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
87 local function storage_size()
89 if nixio.fs.access("/proc/mtd") then
90 for l in io.lines("/proc/mtd") do
91 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
92 if n == "linux" or n == "firmware" then
93 size = tonumber(s, 16)
97 elseif nixio.fs.access("/proc/partitions") then
98 for l in io.lines("/proc/partitions") do
99 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
100 if b and n and not n:match('[0-9]') then
101 size = tonumber(b) * 1024
110 function action_flashops()
114 luci.template.render("admin_system/flashops", {
115 reset_avail = supports_reset(),
116 upgrade_avail = supports_sysupgrade()
120 function action_sysupgrade()
121 local fs = require "nixio.fs"
122 local http = require "luci.http"
123 local image_tmp = "/tmp/firmware.img"
127 function(meta, chunk, eof)
128 if not fp and meta and meta.name == "image" then
129 fp = io.open(image_tmp, "w")
140 if not luci.dispatcher.test_post_security() then
146 -- Cancel firmware flash
148 if http.formvalue("cancel") then
150 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
155 -- Initiate firmware flash
157 local step = tonumber(http.formvalue("step")) or 1
159 local force = http.formvalue("force")
160 if image_supported(image_tmp) or force then
161 luci.template.render("admin_system/upgrade", {
162 checksum = image_checksum(image_tmp),
163 sha256ch = image_sha256_checksum(image_tmp),
164 storage = storage_size(),
165 size = (fs.stat(image_tmp, "size") or 0),
166 keep = (not not http.formvalue("keep")),
167 force = (not not http.formvalue("force"))
171 luci.template.render("admin_system/flashops", {
172 reset_avail = supports_reset(),
173 upgrade_avail = supports_sysupgrade(),
179 -- Start sysupgrade flash
181 elseif step == 2 then
182 local keep = (http.formvalue("keep") == "1") and "" or "-n"
183 local force = (http.formvalue("force") == "1") and "-F" or ""
184 luci.template.render("admin_system/applyreboot", {
185 title = luci.i18n.translate("Flashing..."),
186 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."),
187 addr = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil
189 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)
193 function action_backup()
194 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"'
195 %{ luci.sys.hostname(), os.date("%Y-%m-%d") })
197 luci.http.prepare_content("application/x-targz")
198 luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
201 function action_backupmtdblock()
202 local mv = luci.http.formvalue("mtdblockname") or ""
203 local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
206 luci.http.status(400, "Bad Request")
210 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
211 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
213 luci.http.prepare_content("application/octet-stream")
214 luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write)
217 function action_restore()
218 local fs = require "nixio.fs"
219 local http = require "luci.http"
220 local archive_tmp = "/tmp/restore.tar.gz"
224 function(meta, chunk, eof)
225 if not fp and meta and meta.name == "archive" then
226 fp = io.open(archive_tmp, "w")
237 if not luci.dispatcher.test_post_security() then
238 fs.unlink(archive_tmp)
242 local upload = http.formvalue("archive")
243 if upload and #upload > 0 then
244 if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then
245 luci.template.render("admin_system/applyreboot")
246 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
249 luci.template.render("admin_system/flashops", {
250 reset_avail = supports_reset(),
251 upgrade_avail = supports_sysupgrade(),
252 backup_invalid = true
258 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
261 function action_reset()
262 if supports_reset() then
263 luci.template.render("admin_system/applyreboot", {
264 title = luci.i18n.translate("Erasing..."),
265 msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
269 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
273 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
276 function action_password()
277 local password = luci.http.formvalue("password")
279 luci.http.status(400, "Bad Request")
283 luci.http.prepare_content("application/json")
284 luci.http.write_json({ code = luci.sys.user.setpasswd("root", password) })
287 function action_sshkeys()
288 local keys = luci.http.formvalue("keys")
290 keys = luci.jsonc.parse(keys)
291 if not keys or type(keys) ~= "table" then
292 luci.http.status(400, "Bad Request")
296 local fd, err = io.open("/etc/dropbear/authorized_keys", "w")
298 luci.http.status(503, err)
303 for _, k in ipairs(keys) do
304 if type(k) == "string" and k:match("^%w+%-") then
313 local fd, err = io.open("/etc/dropbear/authorized_keys", "r")
315 luci.http.status(503, err)
321 local ln = fd:read("*l")
324 elseif ln:match("^[%w%-]+%s+[A-Za-z0-9+/=]+$") or
325 ln:match("^[%w%-]+%s+[A-Za-z0-9+/=]+%s")
333 luci.http.prepare_content("application/json")
334 luci.http.write_json(rv)
337 function action_reboot()