* Temporarily disabled fastindex-support due to a bug where controllers containing...
[project/luci.git] / modules / admin-core / luasrc / controller / admin / system.lua
1 module("luci.controller.admin.system", package.seeall)
2
3 require("luci.sys")
4 require("luci.http")
5 require("luci.util")
6 require("luci.fs")
7 require("luci.model.ipkg")
8 require("luci.model.uci")
9
10 function index()
11 entry({"admin", "system"}, template("admin_system/index"), "System", 30)
12 entry({"admin", "system", "packages"}, call("action_packages"), "Paketverwaltung", 10)
13 entry({"admin", "system", "packages", "ipkg"}, call("action_ipkg"), "IPKG-Konfiguration")
14 entry({"admin", "system", "passwd"}, call("action_passwd"), "Passwort ändern", 20)
15 entry({"admin", "system", "sshkeys"}, call("action_sshkeys"), "SSH-Schlüssel", 30)
16 entry({"admin", "system", "hostname"}, cbi("admin_system/hostname"), "Hostname", 40)
17 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), "Einhängepunkte", 50)
18 entry({"admin", "system", "upgrade"}, call("action_upgrade"), "Firmwareupgrade", 60)
19 entry({"admin", "system", "reboot"}, call("action_reboot"), "Neu starten", 70)
20 end
21
22 function action_editor()
23 local file = luci.http.formvalue("file", "")
24 local data = luci.http.formvalue("data")
25 local err = nil
26 local msg = nil
27 local stat = true
28
29 if file and data then
30 stat, err = luci.fs.writefile(file, data)
31 end
32
33 if not stat then
34 err = luci.util.split(err, " ")
35 table.remove(err, 1)
36 msg = table.concat(err, " ")
37 end
38
39 local cnt, err = luci.fs.readfile(file)
40 if cnt then
41 cnt = luci.util.pcdata(cnt)
42 end
43 luci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg})
44 end
45
46 function action_ipkg()
47 local file = "/etc/ipkg.conf"
48 local data = luci.http.formvalue("data")
49 local stat = nil
50 local err = nil
51
52 if data then
53 stat, err = luci.fs.writefile(file, data)
54 end
55
56 local cnt = luci.fs.readfile(file)
57 if cnt then
58 cnt = luci.util.pcdata(cnt)
59 end
60
61 luci.template.render("admin_system/ipkg", {cnt=cnt, msg=err})
62 end
63
64 function action_packages()
65 local ipkg = luci.model.ipkg
66 local void = nil
67 local submit = luci.http.formvalue("submit")
68
69
70 -- Search query
71 local query = luci.http.formvalue("query")
72 query = (query ~= '') and query or nil
73
74
75 -- Packets to be installed
76 local install = submit and luci.http.formvaluetable("install")
77
78 -- Install from URL
79 local url = luci.http.formvalue("url")
80 if url and url ~= '' and submit then
81 if not install then
82 install = {}
83 end
84 install[url] = 1
85 end
86
87 -- Do install
88 if install then
89 for k, v in pairs(install) do
90 void, install[k] = ipkg.install(k)
91 end
92 end
93
94
95 -- Remove packets
96 local remove = submit and luci.http.formvaluetable("remove")
97 if remove then
98 for k, v in pairs(remove) do
99 void, remove[k] = ipkg.remove(k)
100 end
101 end
102
103
104 -- Update all packets
105 local update = luci.http.formvalue("update")
106 if update then
107 void, update = ipkg.update()
108 end
109
110
111 -- Upgrade all packets
112 local upgrade = luci.http.formvalue("upgrade")
113 if upgrade then
114 void, upgrade = ipkg.upgrade()
115 end
116
117
118 -- Package info
119 local info = luci.model.ipkg.info(query)
120 info = info or {}
121 local pkgs = {}
122
123 -- Sort after status and name
124 for k, v in pairs(info) do
125 local x = 0
126 for i, j in pairs(pkgs) do
127 local vins = (v.Status and v.Status.installed)
128 local jins = (j.Status and j.Status.installed)
129 if vins ~= jins then
130 if vins then
131 break
132 end
133 else
134 if j.Package > v.Package then
135 break
136 end
137 end
138 x = i
139 end
140 table.insert(pkgs, x+1, v)
141 end
142
143 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
144 install=install, remove=remove, update=update, upgrade=upgrade})
145 end
146
147 function action_passwd()
148 local p1 = luci.http.formvalue("pwd1")
149 local p2 = luci.http.formvalue("pwd2")
150 local stat = nil
151
152 if p1 or p2 then
153 if p1 == p2 then
154 stat = luci.sys.user.setpasswd("root", p1)
155 else
156 stat = 10
157 end
158 end
159
160 luci.template.render("admin_system/passwd", {stat=stat})
161 end
162
163 function action_reboot()
164 local reboot = luci.http.formvalue("reboot")
165 luci.template.render("admin_system/reboot", {reboot=reboot})
166 if reboot then
167 luci.sys.reboot()
168 end
169 end
170
171 function action_sshkeys()
172 local file = "/etc/dropbear/authorized_keys"
173 local data = luci.http.formvalue("data")
174 local stat = nil
175 local err = nil
176
177 if data then
178 stat, err = luci.fs.writefile(file, data)
179 end
180
181 local cnt = luci.fs.readfile(file)
182 if cnt then
183 cnt = luci.util.pcdata(cnt)
184 end
185
186 luci.template.render("admin_system/sshkeys", {cnt=cnt, msg=err})
187 end
188
189 function action_upgrade()
190 local ret = nil
191 local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
192
193 local image = luci.http.upload("image")
194 local keepcfg = luci.http.formvalue("keepcfg")
195
196 if plat and image then
197 local kpattern = nil
198 if keepcfg then
199 local files = luci.model.uci.sections("luci").flash_keep
200 if files.luci and files.luci.flash_keep then
201 kpattern = ""
202 for k,v in pairs(files.luci.flash_keep) do
203 kpattern = kpattern .. " " .. v
204 end
205 end
206 end
207 ret = luci.sys.flash(image, kpattern)
208 end
209
210 luci.template.render("admin_system/upgrade", {sysupgrade=plat, ret=ret})
211 end