Fix broken repository link in target/makeccs
[librecmc/librecmc.git] / package / luci / 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"}, cbi("admin_system/system"), _("System"), 1)
11         entry({"admin", "system", "clock_status"}, post_on({ set = true }, "action_clock_status"))
12
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"))
16
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"))
21         end
22
23         entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
24         entry({"admin", "system", "crontab"}, form("admin_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"}, cbi("admin_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 local function image_supported(image)
68         return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
69 end
70
71 local function image_checksum(image)
72         return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
73 end
74
75 local function image_sha256_checksum(image)
76         return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
77 end
78
79 local function supports_sysupgrade()
80         return nixio.fs.access("/lib/upgrade/platform.sh")
81 end
82
83 local function supports_reset()
84         return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
85 end
86
87 local function storage_size()
88         local size = 0
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)
94                                 break
95                         end
96                 end
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
102                                 break
103                         end
104                 end
105         end
106         return size
107 end
108
109
110 function action_flashops()
111         --
112         -- Overview
113         --
114         luci.template.render("admin_system/flashops", {
115                 reset_avail   = supports_reset(),
116                 upgrade_avail = supports_sysupgrade()
117         })
118 end
119
120 function action_sysupgrade()
121         local fs = require "nixio.fs"
122         local http = require "luci.http"
123         local image_tmp = "/tmp/firmware.img"
124
125         local fp
126         http.setfilehandler(
127                 function(meta, chunk, eof)
128                         if not fp and meta and meta.name == "image" then
129                                 fp = io.open(image_tmp, "w")
130                         end
131                         if fp and chunk then
132                                 fp:write(chunk)
133                         end
134                         if fp and eof then
135                                 fp:close()
136                         end
137                 end
138         )
139
140         if not luci.dispatcher.test_post_security() then
141                 fs.unlink(image_tmp)
142                 return
143         end
144
145         --
146         -- Cancel firmware flash
147         --
148         if http.formvalue("cancel") then
149                 fs.unlink(image_tmp)
150                 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
151                 return
152         end
153
154         --
155         -- Initiate firmware flash
156         --
157         local step = tonumber(http.formvalue("step")) or 1
158         if step == 1 then
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"))
168                         })
169                 else
170                         fs.unlink(image_tmp)
171                         luci.template.render("admin_system/flashops", {
172                                 reset_avail   = supports_reset(),
173                                 upgrade_avail = supports_sysupgrade(),
174                                 image_invalid = true
175                         })
176                 end
177
178         --
179         -- Start sysupgrade flash
180         --
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
188                 })
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)
190         end
191 end
192
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") })
196
197         luci.http.prepare_content("application/x-targz")
198         luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
199 end
200
201 function action_backupmtdblock()
202         local mv = luci.http.formvalue("mtdblockname") or ""
203         local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
204
205         if not m and n then
206                 luci.http.status(400, "Bad Request")
207                 return
208         end
209
210         luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
211                 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
212
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)
215 end
216
217 function action_restore()
218         local fs = require "nixio.fs"
219         local http = require "luci.http"
220         local archive_tmp = "/tmp/restore.tar.gz"
221
222         local fp
223         http.setfilehandler(
224                 function(meta, chunk, eof)
225                         if not fp and meta and meta.name == "archive" then
226                                 fp = io.open(archive_tmp, "w")
227                         end
228                         if fp and chunk then
229                                 fp:write(chunk)
230                         end
231                         if fp and eof then
232                                 fp:close()
233                         end
234                 end
235         )
236
237         if not luci.dispatcher.test_post_security() then
238                 fs.unlink(archive_tmp)
239                 return
240         end
241
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)
247                         luci.sys.reboot()
248                 else
249                         luci.template.render("admin_system/flashops", {
250                                 reset_avail   = supports_reset(),
251                                 upgrade_avail = supports_sysupgrade(),
252                                 backup_invalid = true
253                         })
254                 end
255                 return
256         end
257
258         http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
259 end
260
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."),
266                         addr  = "192.168.1.1"
267                 })
268
269                 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
270                 return
271         end
272
273         http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
274 end
275
276 function action_password()
277         local password = luci.http.formvalue("password")
278         if not password then
279                 luci.http.status(400, "Bad Request")
280                 return
281         end
282
283         luci.http.prepare_content("application/json")
284         luci.http.write_json({ code = luci.sys.user.setpasswd("root", password) })
285 end
286
287 function action_sshkeys()
288         local keys = luci.http.formvalue("keys")
289         if keys then
290                 keys = luci.jsonc.parse(keys)
291                 if not keys or type(keys) ~= "table" then
292                         luci.http.status(400, "Bad Request")
293                         return
294                 end
295
296                 local fd, err = io.open("/etc/dropbear/authorized_keys", "w")
297                 if not fd then
298                         luci.http.status(503, err)
299                         return
300                 end
301
302                 local _, k
303                 for _, k in ipairs(keys) do
304                         if type(k) == "string" and k:match("^%w+%-") then
305                                 fd:write(k)
306                                 fd:write("\n")
307                         end
308                 end
309
310                 fd:close()
311         end
312
313         local fd, err = io.open("/etc/dropbear/authorized_keys", "r")
314         if not fd then
315                 luci.http.status(503, err)
316                 return
317         end
318
319         local rv = {}
320         while true do
321                 local ln = fd:read("*l")
322                 if not ln then
323                         break
324                 elseif ln:match("^[%w%-]+%s+[A-Za-z0-9+/=]+$") or
325                        ln:match("^[%w%-]+%s+[A-Za-z0-9+/=]+%s")
326                 then
327                         rv[#rv+1] = ln
328                 end
329         end
330
331         fd:close()
332
333         luci.http.prepare_content("application/json")
334         luci.http.write_json(rv)
335 end
336
337 function action_reboot()
338         luci.sys.reboot()
339 end