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