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