Merge pull request #2770 from geekinaboxx/master
[oweals/luci.git] / applications / luci-app-opkg / luasrc / controller / opkg.lua
1 -- Copyright 2018 Jo-Philipp Wich <jo@mein.io>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.opkg", package.seeall)
5
6 function index()
7         entry({"admin", "system", "opkg"}, template("opkg"), _("Software"), 30)
8         entry({"admin", "system", "opkg", "list"}, call("action_list")).leaf = true
9         entry({"admin", "system", "opkg", "exec"}, post("action_exec")).leaf = true
10         entry({"admin", "system", "opkg", "statvfs"}, call("action_statvfs")).leaf = true
11         entry({"admin", "system", "opkg", "config"}, post_on({ data = true }, "action_config")).leaf = true
12 end
13
14 function action_list(mode)
15         local cmd
16
17         if mode == "installed" then
18                 cmd = { "/bin/cat", "/usr/lib/opkg/status" }
19         else
20                 cmd = { "/bin/sh", "-c", [[find /tmp/opkg-lists/ -type f '!' -name '*.sig' | xargs -r gzip -cd]] }
21         end
22
23         luci.http.prepare_content("text/plain; charset=utf-8")
24         luci.sys.process.exec(cmd, luci.http.write)
25 end
26
27 function action_exec(command, package)
28         local sys = require "luci.sys"
29         local cmd = { "/bin/opkg", "--force-removal-of-dependent-packages" }
30         local pkg = luci.http.formvalue("package")
31
32         if luci.http.formvalue("autoremove") == "true" then
33                 cmd[#cmd + 1] = "--autoremove"
34         end
35
36         if luci.http.formvalue("overwrite") == "true" then
37                 cmd[#cmd + 1] = "--force-overwrite"
38         end
39
40         cmd[#cmd + 1] = command
41
42         if pkg then
43                 cmd[#cmd + 1] = pkg
44         end
45
46         luci.http.prepare_content("application/json")
47         luci.http.write_json(sys.process.exec(cmd, true, true))
48 end
49
50 function action_statvfs()
51         local fs = require "nixio.fs"
52
53         luci.http.prepare_content("application/json")
54         luci.http.write_json(fs.statvfs("/") or {})
55 end
56
57 function action_config()
58         local fs = require "nixio.fs"
59         local js = require "luci.jsonc"
60         local data = luci.http.formvalue("data")
61
62         if data then
63                 data = js.parse(data)
64
65                 if not data then
66                         luci.http.status(400, "Bad Request")
67                         return
68                 end
69
70                 local file, content
71                 for file, content in pairs(data) do
72                         if type(content) ~= "string" or
73                            (file ~= "opkg.conf" and not file:match("^opkg/[^/]+%.conf$"))
74                         then
75                                 luci.http.status(400, "Bad Request")
76                                 return
77                         end
78
79                         local path = "/etc/%s" % file
80                         if not fs.access(path, "w") then
81                                 luci.http.status(403, "Permission denied")
82                                 return
83                         end
84
85                         fs.writefile(path, content:gsub("\r\n", "\n"))
86                 end
87
88                 luci.http.status(204, "Saved")
89         else
90                 local rv = { ["opkg.conf"] = fs.readfile("/etc/opkg.conf") }
91                 local entries = fs.dir("/etc/opkg")
92                 if entries then
93                         local entry
94                         for entry in entries do
95                                 if entry:match("%.conf$") then
96                                         rv["opkg/%s" % entry] = fs.readfile("/etc/opkg/%s" % entry)
97                                 end
98                         end
99                 end
100
101                 luci.http.prepare_content("application/json")
102                 luci.http.write_json(rv)
103         end
104 end