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