modules/admin-full: utilize luci.model.network, add uptime in iface_status action
[project/luci.git] / modules / admin-full / luasrc / controller / admin / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 module("luci.controller.admin.system", package.seeall)
17
18 function index()
19         luci.i18n.loadc("base")
20         local i18n = luci.i18n.translate
21
22         entry({"admin", "system"}, alias("admin", "system", "system"), i18n("System"), 30).index = true
23         entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("System"), 1)
24         entry({"admin", "system", "packages"}, call("action_packages"), i18n("Software"), 10)
25         entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
26         entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("Admin Password"), 20)
27         entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("<abbr title=\"Secure Shell\">SSH</abbr>-Keys"), 30)
28         entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("Processes"), 45)
29
30         if nixio.fs.access("/etc/config/fstab") then
31                 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("Mount Points"), 50)
32                 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
33                 entry({"admin", "system", "fstab", "swap"},  cbi("admin_system/fstab/swap"),  nil).leaf = true
34         end
35
36         if nixio.fs.access("/sys/class/leds") then
37                 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
38         end
39
40         entry({"admin", "system", "backup"}, call("action_backup"), i18n("Backup / Restore"), 70)
41         entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("Flash Firmware"), 80)
42         entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("Reboot"), 90)
43 end
44
45 function action_packages()
46         local ipkg = require("luci.model.ipkg")
47         local submit = luci.http.formvalue("submit")
48         local changes = false
49         local install = { }
50         local remove  = { }
51
52         -- Search query
53         local query = luci.http.formvalue("query")
54         query = (query ~= '') and query or nil
55
56
57         -- Packets to be installed
58         local ninst = submit and luci.http.formvalue("install")
59         local uinst = nil
60
61         -- Install from URL
62         local url = luci.http.formvalue("url")
63         if url and url ~= '' and submit then
64                 uinst = url
65         end
66
67         -- Do install
68         if ninst then
69                 _, install[ninst] = ipkg.install(ninst)
70                 changes = true
71         end
72
73         if uinst then
74                 _, install[uinst] = ipkg.install(uinst)
75                 changes = true
76         end
77
78         -- Remove packets
79         local rem = submit and luci.http.formvalue("remove")
80         if rem then
81                 _, remove[rem] = ipkg.remove(rem)
82                 changes = true
83         end
84
85
86         -- Update all packets
87         local update = luci.http.formvalue("update")
88         if update then
89                 _, update = ipkg.update()
90         end
91
92
93         -- Upgrade all packets
94         local upgrade = luci.http.formvalue("upgrade")
95         if upgrade then
96                 _, upgrade = ipkg.upgrade()
97         end
98
99
100         luci.template.render("admin_system/packages", {
101                 query=query, install=install, remove=remove, update=update, upgrade=upgrade
102         })
103
104         -- Remove index cache
105         if changes then
106                 nixio.fs.unlink("/tmp/luci-indexcache")
107         end
108 end
109
110 function action_backup()
111         local sys = require "luci.sys"
112         local fs  = require "luci.fs"
113
114         local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
115         local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
116         local backup_cmd  = "tar -czT %s 2>/dev/null"
117
118         local restore_fpi
119         luci.http.setfilehandler(
120                 function(meta, chunk, eof)
121                         if not restore_fpi then
122                                 restore_fpi = io.popen(restore_cmd, "w")
123                         end
124                         if chunk then
125                                 restore_fpi:write(chunk)
126                         end
127                         if eof then
128                                 restore_fpi:close()
129                         end
130                 end
131         )
132
133         local upload = luci.http.formvalue("archive")
134         local backup = luci.http.formvalue("backup")
135         local reset  = reset_avail and luci.http.formvalue("reset")
136
137         if upload and #upload > 0 then
138                 luci.template.render("admin_system/applyreboot")
139                 luci.sys.reboot()
140         elseif backup then
141                 local filelist = "/tmp/luci-backup-list.%d" % os.time()
142
143                 sys.call(
144                         "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..
145                         "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..
146                         "opkg list-changed-conffiles ) | sort -u > %s" % filelist
147                 )
148
149                 if fs.access(filelist) then
150                         local reader = ltn12_popen(backup_cmd:format(filelist))
151                         luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
152                                 luci.sys.hostname(), os.date("%Y-%m-%d")})
153                         luci.http.prepare_content("application/x-targz")
154                         luci.ltn12.pump.all(reader, luci.http.write)
155                         fs.unlink(filelist)
156                 end
157         elseif reset then
158                 luci.template.render("admin_system/applyreboot")
159                 luci.util.exec("mtd -r erase rootfs_data")
160         else
161                 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
162         end
163 end
164
165 function action_passwd()
166         local p1 = luci.http.formvalue("pwd1")
167         local p2 = luci.http.formvalue("pwd2")
168         local stat = nil
169
170         if p1 or p2 then
171                 if p1 == p2 then
172                         stat = luci.sys.user.setpasswd("root", p1)
173                 else
174                         stat = 10
175                 end
176         end
177
178         luci.template.render("admin_system/passwd", {stat=stat})
179 end
180
181 function action_reboot()
182         local reboot = luci.http.formvalue("reboot")
183         luci.template.render("admin_system/reboot", {reboot=reboot})
184         if reboot then
185                 luci.sys.reboot()
186         end
187 end
188
189 function action_upgrade()
190         require("luci.model.uci")
191
192         local tmpfile = "/tmp/firmware.img"
193
194         local function image_supported()
195                 -- XXX: yay...
196                 return ( 0 == os.execute(
197                         ". /etc/functions.sh; " ..
198                         "include /lib/upgrade; " ..
199                         "platform_check_image %q >/dev/null"
200                                 % tmpfile
201                 ) )
202         end
203
204         local function image_checksum()
205                 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
206         end
207
208         local function storage_size()
209                 local size = 0
210                 if nixio.fs.access("/proc/mtd") then
211                         for l in io.lines("/proc/mtd") do
212                                 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
213                                 if n == "linux" then
214                                         size = tonumber(s, 16)
215                                         break
216                                 end
217                         end
218                 elseif nixio.fs.access("/proc/partitions") then
219                         for l in io.lines("/proc/partitions") do
220                                 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
221                                 if b and n and not n:match('[0-9]') then
222                                         size = tonumber(b) * 1024
223                                         break
224                                 end
225                         end
226                 end
227                 return size
228         end
229
230
231         -- Install upload handler
232         local file
233         luci.http.setfilehandler(
234                 function(meta, chunk, eof)
235                         if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
236                                 file = io.open(tmpfile, "w")
237                         end
238                         if file and chunk then
239                                 file:write(chunk)
240                         end
241                         if file and eof then
242                                 file:close()
243                         end
244                 end
245         )
246
247
248         -- Determine state
249         local keep_avail   = true
250         local step         = tonumber(luci.http.formvalue("step") or 1)
251         local has_image    = nixio.fs.access(tmpfile)
252         local has_support  = image_supported()
253         local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
254         local has_upload   = luci.http.formvalue("image")
255
256         -- This does the actual flashing which is invoked inside an iframe
257         -- so don't produce meaningful errors here because the the
258         -- previous pages should arrange the stuff as required.
259         if step == 4 then
260                 if has_platform and has_image and has_support then
261                         -- Mimetype text/plain
262                         luci.http.prepare_content("text/plain")
263                         luci.http.write("Starting sysupgrade...\n")
264
265                         io.flush()
266
267                         -- Now invoke sysupgrade
268                         local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
269                         local flash = ltn12_popen("/sbin/sysupgrade %s %q" %{
270                                 keepcfg and "" or "-n", tmpfile
271                         })
272
273                         luci.ltn12.pump.all(flash, luci.http.write)
274                 end
275
276
277         --
278         -- This is step 1-3, which does the user interaction and
279         -- image upload.
280         --
281
282         -- Step 1: file upload, error on unsupported image format
283         elseif not has_image or not has_support or step == 1 then
284                 -- If there is an image but user has requested step 1
285                 -- or type is not supported, then remove it.
286                 if has_image then
287                         nixio.fs.unlink(tmpfile)
288                 end
289
290                 luci.template.render("admin_system/upgrade", {
291                         step=1,
292                         bad_image=(has_image and not has_support or false),
293                         keepavail=keep_avail,
294                         supported=has_platform
295                 } )
296
297         -- Step 2: present uploaded file, show checksum, confirmation
298         elseif step == 2 then
299                 luci.template.render("admin_system/upgrade", {
300                         step=2,
301                         checksum=image_checksum(),
302                         filesize=nixio.fs.stat(tmpfile).size,
303                         flashsize=storage_size(),
304                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
305                 } )
306
307         -- Step 3: load iframe which calls the actual flash procedure
308         elseif step == 3 then
309                 luci.template.render("admin_system/upgrade", {
310                         step=3,
311                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
312                 } )
313         end
314 end
315
316 function ltn12_popen(command)
317
318         local fdi, fdo = nixio.pipe()
319         local pid = nixio.fork()
320
321         if pid > 0 then
322                 fdo:close()
323                 local close
324                 return function()
325                         local buffer = fdi:read(2048)
326                         local wpid, stat = nixio.waitpid(pid, "nohang")
327                         if not close and wpid and stat == "exited" then
328                                 close = true
329                         end
330
331                         if buffer and #buffer > 0 then
332                                 return buffer
333                         elseif close then
334                                 fdi:close()
335                                 return nil
336                         end
337                 end
338         elseif pid == 0 then
339                 nixio.dup(fdo, nixio.stdout)
340                 fdi:close()
341                 fdo:close()
342                 nixio.exec("/bin/sh", "-c", command)
343         end
344 end