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