treewide: filter shell arguments through shellquote() where applicable
[project/luci.git] / applications / luci-app-cshark / luasrc / controller / cshark.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright (C) 2014, QA Cafe, Inc.
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 ]]--
13
14 module("luci.controller.cshark", package.seeall)
15
16 function index()
17                 page = node("admin", "network", "cloudshark")
18                 page.target = cbi("admin_network/cshark")
19                 page.title = _("CloudShark")
20                 page.order = 70
21
22                 page = entry({"admin", "network", "cshark_iface_dump_start"}, call("cshark_iface_dump_start"), nil)
23                 page.leaf = true
24
25                 page = entry({"admin", "network", "cshark_iface_dump_stop"}, call("cshark_iface_dump_stop"), nil)
26                 page.leaf = true
27
28                 page = entry({"admin", "network", "cshark_check_status"}, call("cshark_check_status"), nil)
29                 page.leaf = true
30
31                 page = entry({"admin", "network", "cshark_link_list_get"}, call("cshark_link_list_get"), nil)
32                 page.leaf = true
33
34                 page = entry({"admin", "network", "cshark_link_list_clear"}, call("cshark_link_list_clear"), nil)
35                 page.leaf = true
36 end
37
38 function cshark_iface_dump_start(ifname, value, flag, filter)
39         if ifname == nil or ifname == '' then
40                 ifname = 'any'
41         end
42         if tonumber(value) == nil
43         then
44                 value = '0'
45         end
46         if filter == nil or filter == '' then
47                 filter = ''
48         end
49
50         if flag == nil or flag == '' then
51                 filter = 'T'
52         end
53
54         luci.http.prepare_content("text/plain")
55
56         local res = os.execute("(/sbin/cshark -i %s -%s %s -p /tmp/cshark-luci.pid %s > /tmp/cshark-luci.out 2>&1) &" %{
57                 luci.util.shellquote(ifname),
58                 luci.util.shellquote(flag),
59                 luci.util.shellquote(value),
60                 luci.util.shellquote(filter)
61         })
62
63         luci.http.write(tostring(res))
64 end
65
66 function cshark_iface_dump_stop()
67         luci.http.prepare_content("text/plain")
68
69         local f = io.open("/tmp/cshark-luci.pid", "rb")
70         local pid = f:read("*all")
71         io.close(f)
72
73         local res = os.execute("kill -TERM " .. pid)
74         luci.http.write(tostring(res))
75 end
76
77 function cshark_check_status()
78
79         local msg = "";
80         local status;
81         local f = io.open("/tmp/cshark-luci.pid","r")
82         if f ~= nil then
83                 status = 1;
84                 io.close(f)
85         else
86                 status = 0;
87         end
88
89         f = io.open("/tmp/cshark-luci.out","r")
90         if f ~= nil then
91                 msg = f:read("*all")
92                 io.close(f)
93                 if msg ~= '' then
94                         os.remove('/tmp/cshark-luci.out')
95                 end
96         end
97
98         luci.http.prepare_content("application/json")
99
100         local res = {}
101         res["status"] = status;
102         res["msg"] = msg;
103
104         luci.http.write_json(res)
105 end
106
107 function cshark_link_list_get()
108         local uci = require("uci").cursor()
109
110         luci.http.prepare_content("application/json")
111
112         luci.http.write("[")
113
114         local t = uci:get("cshark", "cshark", "entry")
115   if (t ~= nil) then
116           for i = #t, 1, -1 do
117                   luci.http.write("[\"" .. t[i] .. "\"],")
118           end
119   end
120
121         luci.http.write("[]]")
122 end
123
124 function cshark_link_list_clear()
125         local uci = require("uci").cursor()
126
127         uci:delete("cshark", "cshark", "entry")
128         uci:commit("cshark");
129
130         luci.http.status(200, "OK")
131 end