Fixed login form
[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
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 module("luci.controller.admin.system", package.seeall)
15
16 function index()
17 luci.i18n.loadc("admin-core")
18 local i18n = luci.i18n.translate
19
20 entry({"admin", "system"}, template("admin_system/index"), i18n("system"), 30)
21 entry({"admin", "system", "packages"}, call("action_packages"), i18n("a_s_packages"), 10)
22 entry({"admin", "system", "packages", "ipkg"}, call("action_ipkg"), i18n("a_s_p_ipkg"))
23 entry({"admin", "system", "passwd"}, call("action_passwd"), i18n("a_s_changepw"), 20)
24 entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("a_s_sshkeys"), 30)
25 entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("system"), 40)
26 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("a_s_fstab"), 50)
27 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("leds", "LEDs"), 60)
28 entry({"admin", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 70)
29 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("a_s_flash"), 80)
30 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 90)
31 end
32
33 function action_editor()
34 local file = luci.http.formvalue("file", "")
35 local data = luci.http.formvalue("data")
36 local err = nil
37 local msg = nil
38 local stat = true
39
40 if file and data then
41 stat, err = luci.fs.writefile(file, data)
42 end
43
44 if not stat then
45 err = luci.util.split(err, " ")
46 table.remove(err, 1)
47 msg = table.concat(err, " ")
48 end
49
50 local cnt, err = luci.fs.readfile(file)
51 if cnt then
52 cnt = luci.util.pcdata(cnt)
53 end
54 luci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg})
55 end
56
57 function action_ipkg()
58 local file = "/etc/ipkg.conf"
59 local data = luci.http.formvalue("data")
60 local stat = nil
61 local err = nil
62
63 if data then
64 stat, err = luci.fs.writefile(file, data)
65 end
66
67 local cnt = luci.fs.readfile(file)
68 if cnt then
69 cnt = luci.util.pcdata(cnt)
70 end
71
72 luci.template.render("admin_system/ipkg", {cnt=cnt, msg=err})
73 end
74
75 function action_packages()
76 local ipkg = require("luci.model.ipkg")
77 local void = nil
78 local submit = luci.http.formvalue("submit")
79
80
81 -- Search query
82 local query = luci.http.formvalue("query")
83 query = (query ~= '') and query or nil
84
85
86 -- Packets to be installed
87 local install = submit and luci.http.formvaluetable("install")
88
89 -- Install from URL
90 local url = luci.http.formvalue("url")
91 if url and url ~= '' and submit then
92 if not install then
93 install = {}
94 end
95 install[url] = 1
96 end
97
98 -- Do install
99 if install then
100 for k, v in pairs(install) do
101 void, install[k] = ipkg.install(k)
102 end
103 end
104
105
106 -- Remove packets
107 local remove = submit and luci.http.formvaluetable("remove")
108 if remove then
109 for k, v in pairs(remove) do
110 void, remove[k] = ipkg.remove(k)
111 end
112 end
113
114
115 -- Update all packets
116 local update = luci.http.formvalue("update")
117 if update then
118 void, update = ipkg.update()
119 end
120
121
122 -- Upgrade all packets
123 local upgrade = luci.http.formvalue("upgrade")
124 if upgrade then
125 void, upgrade = ipkg.upgrade()
126 end
127
128
129 -- Package info
130 local info = luci.model.ipkg.info(query)
131 info = info or {}
132 local pkgs = {}
133
134 -- Sort after status and name
135 for k, v in pairs(info) do
136 local x = 0
137 for i, j in pairs(pkgs) do
138 local vins = (v.Status and v.Status.installed)
139 local jins = (j.Status and j.Status.installed)
140 if vins ~= jins then
141 if vins then
142 break
143 end
144 else
145 if j.Package > v.Package then
146 break
147 end
148 end
149 x = i
150 end
151 table.insert(pkgs, x+1, v)
152 end
153
154 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
155 install=install, remove=remove, update=update, upgrade=upgrade})
156 end
157
158 function action_backup()
159 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
160 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
161 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
162
163 local restore_fpi
164 luci.http.setfilehandler(
165 function(meta, chunk, eof)
166 if not restore_fpi then
167 restore_fpi = io.popen(restore_cmd, "w")
168 end
169 if chunk then
170 restore_fpi:write(chunk)
171 end
172 if eof then
173 restore_fpi:close()
174 end
175 end
176 )
177
178 local upload = luci.http.formvalue("archive")
179 local backup = luci.http.formvalue("backup")
180 local reset = reset_avail and luci.http.formvalue("reset")
181
182 if upload and #upload > 0 then
183 luci.template.render("admin_system/applyreboot")
184 luci.sys.reboot()
185 elseif backup then
186 luci.util.perror(backup_cmd:format(_keep_pattern()))
187 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
188 luci.http.header('Content-Disposition', 'attachment; filename="backup.tar.gz"')
189 luci.http.prepare_content("application/x-targz")
190 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
191 elseif reset then
192 luci.template.render("admin_system/applyreboot")
193 luci.util.exec("mtd -r erase rootfs_data")
194 else
195 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
196 end
197 end
198
199 function action_passwd()
200 local p1 = luci.http.formvalue("pwd1")
201 local p2 = luci.http.formvalue("pwd2")
202 local stat = nil
203
204 if p1 or p2 then
205 if p1 == p2 then
206 stat = luci.sys.user.setpasswd("root", p1)
207 else
208 stat = 10
209 end
210 end
211
212 luci.template.render("admin_system/passwd", {stat=stat})
213 end
214
215 function action_reboot()
216 local reboot = luci.http.formvalue("reboot")
217 luci.template.render("admin_system/reboot", {reboot=reboot})
218 if reboot then
219 luci.sys.reboot()
220 end
221 end
222
223 function action_upgrade()
224 require("luci.model.uci")
225
226 local ret = nil
227 local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
228 local tmpfile = "/tmp/firmware.img"
229
230 local keep_avail = true
231
232 local file
233 luci.http.setfilehandler(
234 function(meta, chunk, eof)
235 if not file then
236 file = io.open(tmpfile, "w")
237 end
238 if chunk then
239 file:write(chunk)
240 end
241 if eof then
242 file:close()
243 end
244 end
245 )
246
247 local fname = luci.http.formvalue("image")
248 local keepcfg = keep_avail and luci.http.formvalue("keepcfg")
249
250 if plat and fname then
251 ret = luci.sys.flash(tmpfile, keepcfg and _keep_pattern())
252 end
253
254 luci.template.render("admin_system/upgrade", {sysupgrade=plat, ret=ret, keep_avail=keep_avail})
255 end
256
257 function _keep_pattern()
258 local kpattern = ""
259 local files = luci.model.uci.get_all("luci", "flash_keep")
260 if files then
261 kpattern = ""
262 for k,v in pairs(files) do
263 kpattern = kpattern .. " " .. v
264 end
265 end
266 return kpattern
267 end