* Some more cleanup
[project/luci.git] / applications / luci-splash / root / usr / lib / luci-splash / htdocs / cgi-bin / index.cgi
1 #!/usr/bin/lua
2
3 require("luci.sys")
4 require("luci.model.uci")
5
6 luci.model.uci.set_savedir(luci.model.uci.savedir_state)
7
8 local srv
9 local net
10 local ip = os.getenv("REMOTE_ADDR")
11 luci.model.uci.foreach("network", "interface",
12         function (section)
13                 if section.ipaddr then
14                         local p = luci.sys.net.mask4prefix(section.netmask)
15                         if luci.sys.net.belongs(ip, section.ipaddr, p) then
16                                 net = section[".name"]
17                                 srv = section.ipaddr
18                                 return
19                         end
20                 end
21         end)
22
23 local stat = false
24 luci.model.uci.foreach("luci_splash", "iface",
25         function (section)
26                 if section.network == net then
27                         stat = true
28                 end
29         end)
30
31 if not srv then
32         print("Content-Type: text/plain\n")
33         print("Unable to detect network settings!")
34 elseif not stat then
35         print("Status: 302 Found")
36         print("Location: http://" .. srv)
37 else
38         local action = "splash"
39         
40         local mac = luci.sys.net.ip4mac(ip)
41         if not mac then
42                 action = "unknown"
43         end
44         
45         local status = luci.sys.execl("luci-splash status "..mac)[1]
46         
47         if status == "whitelisted" or status == "lease" then
48                 action = "allowed"
49         end
50         
51         print("Status: 302 Found")
52         print("Location: http://" .. srv .. "/cgi-bin/luci-splash/" .. action)
53 end