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