treewide: filter shell arguments through shellquote() where applicable
[project/luci.git] / applications / luci-app-wol / luasrc / model / cbi / wol.lua
1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local utl = require "luci.util"
5 local sys = require "luci.sys"
6 local ipc = require "luci.ip"
7 local fs  = require "nixio.fs"
8
9 m = SimpleForm("wol", translate("Wake on LAN"),
10         translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
11
12 m.submit = translate("Wake up host")
13 m.reset  = false
14
15
16 local has_ewk = fs.access("/usr/bin/etherwake")
17 local has_wol = fs.access("/usr/bin/wol")
18
19
20 s = m:section(SimpleSection)
21
22 if has_ewk and has_wol then
23         bin = s:option(ListValue, "binary", translate("WoL program"),
24                 translate("Sometimes only one of the two tools works. If one fails, try the other one"))
25
26         bin:value("/usr/bin/etherwake", "Etherwake")
27         bin:value("/usr/bin/wol", "WoL")
28 end
29
30 if has_ewk then
31         iface = s:option(ListValue, "iface", translate("Network interface to use"),
32                 translate("Specifies the interface the WoL packet is sent on"))
33
34         if has_wol then
35                 iface:depends("binary", "/usr/bin/etherwake")
36         end
37
38         iface:value("", translate("Broadcast on all interfaces"))
39
40         for _, e in ipairs(sys.net.devices()) do
41                 if e ~= "lo" then iface:value(e) end
42         end
43 end
44
45
46 host = s:option(Value, "mac", translate("Host to wake up"),
47         translate("Choose the host to wake up or enter a custom MAC address to use"))
48
49 sys.net.mac_hints(function(mac, name)
50         host:value(mac, "%s (%s)" %{ mac, name })
51 end)
52
53 if has_ewk then
54         broadcast = s:option(Flag, "broadcast",
55                 translate("Send to broadcast address"))
56         if has_wol then
57                 broadcast:depends("binary", "/usr/bin/etherwake")
58         end
59 end
60
61 function host.write(self, s, val)
62         local host = luci.http.formvalue("cbid.wol.1.mac")
63         local mac = ipc.checkmac(host)
64         if mac then
65                 local cmd
66                 local util = luci.http.formvalue("cbid.wol.1.binary") or (
67                         has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
68                 )
69
70                 if util == "/usr/bin/etherwake" then
71                         local iface = luci.http.formvalue("cbid.wol.1.iface")
72                         local broadcast = luci.http.formvalue("cbid.wol.1.broadcast")
73                         cmd = "%s -D%s %s %q 2>&1" %{
74                                 util, (iface ~= "" and " -i %s" % utl.shellquote(iface) or ""),
75                                 (broadcast == "1" and " -b" or ""), mac
76                         }
77                 else
78                         cmd = "%s -v %q" %{ util, mac }
79                 end
80
81                 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
82                         translate("Starting WoL utility:"), utl.pcdata(cmd)
83                 }
84
85                 local p = io.popen(cmd .. " 2>&1")
86                 if p then
87                         while true do
88                                 local l = p:read("*l")
89                                 if l then
90                                         if #l > 100 then l = l:sub(1, 100) .. "..." end
91                                         msg = msg .. l .. "<br />"
92                                 else
93                                         break
94                                 end
95                         end
96                         p:close()
97                 end
98
99                 msg = msg .. "</code></p>"
100
101                 m.message = msg
102         end
103 end
104
105
106 return m