2 Copyright (C) 2018 Jianhui Zhao <jianhuizhao329@gmail.com>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 local uci = require "uci"
21 local ubus = require "ubus"
22 local http = require "socket.http"
23 local auth = require "wifidog-ng.auth"
24 local config = require "wifidog-ng.config"
32 ["INVALID_COMMAND"] = 1,
33 ["INVALID_ARGUMENT"] = 2,
34 ["METHOD_NOT_FOUND"] = 3,
37 ["PERMISSION_DENIED"] = 6,
39 ["NOT_SUPPORTED"] = 8,
40 ["UNKNOWN_ERROR"] = 9,
41 ["CONNECTION_FAILED"] = 10
44 local function reload_validated_domain()
45 local c = uci.cursor()
47 local file = io.open("/tmp/dnsmasq.d/wifidog-ng", "w")
49 c:foreach("wifidog-ng", "validated_domain", function(s)
50 file:write("ipset=/" .. s.domain .. "/wifidog-ng-ip\n")
54 os.execute("/etc/init.d/dnsmasq restart &")
61 local cfg = config.get()
63 if not msg.ip or not msg.mac then
64 return ubus_codes["INVALID_ARGUMENT"]
67 local url = string.format("%s&stage=roam&ip=%s&mac=%s", cfg.auth_url, msg.ip, msg.mac)
68 local r = http.request(url) or ""
69 local token = r:match("token=(%w+)")
71 auth.new_term(msg.ip, msg.mac, token)
73 end, {ip = ubus.STRING, mac = ubus.STRING }
77 if msg.action == "show" then
78 conn:reply(req, {terms = auth.get_terms()});
82 if not msg.action or not msg.mac then
83 return ubus_codes["INVALID_ARGUMENT"]
86 if msg.action == "add" then
88 elseif msg.action == "del" then
91 end, {action = ubus.STRING, mac = ubus.STRING }
95 if not msg.action or not msg.type or not msg.value then
96 return ubus_codes["INVALID_ARGUMENT"]
99 if msg.action == "add" then
100 config.add_whitelist(msg.type, msg.value)
101 if msg.type == "mac" then
102 auth.allow_user(msg.value)
104 elseif msg.action == "del" then
105 config.del_whitelist(msg.type, msg.value)
106 if msg.type == "mac" then
107 auth.deny_user(msg.value)
111 if msg.type == "domain" then
112 reload_validated_domain()
114 end, {action = ubus.STRING, type = ubus.STRING, value = ubus.STRING }
120 conn = ubus.connect()
122 error("Failed to connect to ubus")