scriptdir = "/opt/freeswitch/scripts"; sounds_dir = "/opt/freeswitch/sounds"; recordings_dir = "/opt/freeswitch/recordings"; dofile (scriptdir.."/includes/functions.lua"); dofile (scriptdir.."/includes/config.lua"); dofile (scriptdir.."/includes/classes/database.class.lua"); dofile (scriptdir.."/includes/classes/geoipcheck.class.lua"); dofile (scriptdir.."/includes/classes/bit.lua"); -- UA Check -- interface = "internal"; source = "directory"; dofile(scriptdir.."/includes/guardian.lua"); -- UA Check -- fs:log("info", "register.lua: Connect to database"); -- Database -- db = Database:create(); db:connect("odbc://core"); fs:log("info", "register.lua: Connected to database"); debug["params"] = true; debug["sql"] = false; debug["xml_request"] = false; debug["xml_string"] = true; sip_profile = "external"; client_ip = params:getHeader("ip") or ""; --freeswitch.consoleLog("notice", params:getHeader("sip_user_agent")); --show param debug info if (debug["params"]) then freeswitch.consoleLog("notice", "[xml_handler] Params:\n" .. params:serialize() .. "\n"); end --get the params and set them as variables local domain_name = params:getHeader("domain"); local action = params:getHeader("action"); local purpose = params:getHeader("purpose"); local profile = params:getHeader("profile"); local key = params:getHeader("key"); local user = params:getHeader("user"); local user_context = params:getHeader("variable_user_context"); local call_context = params:getHeader("Caller-Context"); local destination_number = params:getHeader("Caller-Destination-Number"); local caller_id_number = params:getHeader("Caller-Caller-ID-Number"); method = params:getHeader("sip_auth_method") or ""; --handle the directory if (( XML_REQUEST["section"] == "directory" and key and user and domain_name) or action == "user_call") then --prevent processing for invalid user if(not user:find("^%*")) then sql = "SELECT *,ve.extension as username,AES_DECRYPT(ve.password, UNHEX(SHA2(:key,512))) as password,concat(v.ip,',',ve.ip) as ip FROM extensions as ve,hpbx as v WHERE v.active='Y' AND v.id=ve.hpbx_id and CONCAT(ve.extension,'@',v.domain) = :var1"; sql = db:prepare(sql, {[":key"] = cfg.PASSKEY, [":var1"] = user .. "@"..domain_name}); --freeswitch.consoleLog("info", "### "..sql.." ###"); res = db:query(sql); if(db:num_rows(res)>0) then pbx = db:fetch_object(res); db:close(); password = pbx.secret; extension = pbx.extension; cidr = pbx.ip; if(cidr:sub(1,1) == ",") then cidr = cidr:sub(2,#cidr); end if(cidr:sub(-1) == ",") then cidr = cidr:sub(1,#cidr-1); end if(action == "sip_auth") then gic = GeoIPCheck:new(client_ip, cidr); if( gic:verify() ) then if(cfg.register_debug) then fs:log("warning", "Register: ["..user.."@"..domain_name.."] IP verfied. ( "..client_ip.." )"); end else if(cfg.register_debug) then fs:log("warning", "Register: ["..user.."@"..domain_name.."] Wrong IP, rejected. ( "..client_ip.." )"); end fs:log("warning", "Block ["..client_ip.."] ( IP rejected )"); do return end; end end dial_string = "{presence_id=${dialed_user}@"..domain_name..",transfer_fallback_extension=${dialed_user}}${sofia_contact("..domain_name.."/${dialed_user}@"..domain_name..")}"; --set the xml array and then concatenate the array to a string if (password) then local xml = {} table.insert(xml, [[]]); table.insert(xml, [[]]); table.insert(xml, [[
]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); if(pbx.vm_pin~=nil and pbx.vm_pin ~= "") then table.insert(xml, [[ ]]); else table.insert(xml, [[ ]]); end if(pbx.vm_email=="Y") then table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); end table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); if(pbx.nathack=="Y") then table.insert(xml, [[ ]]); --freeswitch.consoleLog("notice", "Register: applying nathack."); end table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[ ]]); table.insert(xml, [[
]]); table.insert(xml, [[
]]); XML_STRING = table.concat(xml, "\n"); else fs:log("info", "register.lua: Not found ( no password set. )"); local xml = {}; table.insert(xml, [[]]); table.insert(xml, [[ ]]); table.insert(xml, [[
]]); table.insert(xml, [[ ]]); table.insert(xml, [[
]]); table.insert(xml, [[
]]); XML_STRING = table.concat(xml, "\n"); end else db:close(); if(cfg.register_debug) then fs:log("warning", "Register: ["..user.."@"..domain_name.."] No such account. rejected. ( "..client_ip.." )"); end fs:log("warning", "Block ["..client_ip.."] ( Extension not found )"); -- Return not found. fs:log("info", "register.lua: Not found ( user not found. )"); local xml = {}; table.insert(xml, [[]]); table.insert(xml, [[ ]]); table.insert(xml, [[
]]); table.insert(xml, [[ ]]); table.insert(xml, [[
]]); table.insert(xml, [[
]]); XML_STRING = table.concat(xml, "\n"); end else -- Return not found. fs:log("info", "register.lua: Not found ( doesnt contain * )"); local xml = {}; table.insert(xml, [[]]); table.insert(xml, [[ ]]); table.insert(xml, [[
]]); table.insert(xml, [[ ]]); table.insert(xml, [[
]]); table.insert(xml, [[
]]); XML_STRING = table.concat(xml, "\n"); end else -- Return not found. fs:log("info", "register.lua: Not found ( section/action )"); local xml = {}; table.insert(xml, [[]]); table.insert(xml, [[ ]]); table.insert(xml, [[
]]); table.insert(xml, [[ ]]); table.insert(xml, [[
]]); table.insert(xml, [[
]]); XML_STRING = table.concat(xml, "\n"); end -- Debugging -- if (debug["xml_request"]) then freeswitch.consoleLog("notice", "[xml_handler] Section: " .. XML_REQUEST["section"] .. "\n"); freeswitch.consoleLog("notice", "[xml_handler] Tag Name: " .. XML_REQUEST["tag_name"] .. "\n"); freeswitch.consoleLog("notice", "[xml_handler] Key Name: " .. XML_REQUEST["key_name"] .. "\n"); freeswitch.consoleLog("notice", "[xml_handler] Key Value: " .. XML_REQUEST["key_value"] .. "\n"); end --send the xml to a file if (debug["xml_string"]) then local file = assert(io.open("/tmp/directory.xml", "w")); file:write(XML_STRING); file:close(); end --send the xml to the console if (debug["xml_string"]) then freeswitch.consoleLog("notice", "[xml_handler] XML_STRING: \n" .. XML_STRING .. "\n"); end