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