From 89de6ddcbf2ce6c3a5d417f0a53aaa317a624ee5 Mon Sep 17 00:00:00 2001 From: Anton Farygin Date: Wed, 25 Nov 2026 14:29:12 +0500 Subject: [PATCH 1/4] fix(mod_xml_rpc): mark authenticated portal POST requests The portal needs to distinguish a deliberate authenticated form submission from inputs supplied as ordinary XML-RPC event parameters. Derive the write marker from the HTTP request itself, and prevent caches from retaining the response, which can contain the new SIP password. --- src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c b/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c index 9bc2c1514d..ee38e9b7b3 100644 --- a/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c +++ b/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c @@ -1007,6 +1007,16 @@ abyss_bool handler_hook(TSession * r) free(qd); } + + /* Only the actual HTTP header, not a query/body parameter, can mark a portal write. */ + switch_event_del_header(evnt, "HTTP-Portal-Write"); + if (info->method == m_post && !zstr(info->user)) { + const char *portal_write = RequestHeaderValue(r, "x-freeswitch-portal-write"); + if (portal_write && !strcmp(portal_write, "1")) { + ResponseAddField(r, "Cache-Control", "no-store"); + switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-Portal-Write", "1"); + } + } } switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-URI", uri); -- 2.50.1 From 9e0536897382cb143fd0672c757dee181951b9ac Mon Sep 17 00:00:00 2001 From: Anton Farygin Date: Wed, 25 Nov 2026 14:29:14 +0500 Subject: [PATCH 2/4] fix(portal): write SIP users to an opt-in directory The old script copied a sample user from a hard-coded configuration path and appended to a file that might already exist. This fails on packaged installations with read-only configuration and can damage an existing entry. Use an explicitly configured directory, publish each user without replacing a file, and report success only after the user is visible in the reloaded directory. --- scripts/lua/portal/create_user.lua | 91 +++++++++++++++++++++++------- tests/portal_create_user.lua | 62 ++++++++++++++++++++ 2 files changed, 132 insertions(+), 21 deletions(-) create mode 100644 tests/portal_create_user.lua diff --git a/scripts/lua/portal/create_user.lua b/scripts/lua/portal/create_user.lua index b1abb20c4d..2bcf7f2b34 100644 --- a/scripts/lua/portal/create_user.lua +++ b/scripts/lua/portal/create_user.lua @@ -1,21 +1,70 @@ -user_id = argv[1]; -if (user_id == nil or user_id == "") then os.exit() end - -conf = "/usr/local/freeswitch/conf" -dir = conf .. "/directory/default" -template = dir .. "/1001.xml" -dest = dir .. "/" .. user_id .. ".xml" - -template_file = io.open(template, "r") -dest_file = io.open(dest, "a+") -print(dest_file) -while true do - line = template_file:read("*line") - if line == nil then break end - line = line:gsub("1001", user_id) - print(line) - dest_file:write(line .. "\n") -end - -api = freeswitch.API() -api:execute("reloadxml") +local function failure(message) + stream:write('-ERR ' .. message .. '\n') +end + +if not env or env:getHeader('HTTP-Portal-Write') ~= '1' then + return failure('Create users from the FreeSWITCH Portal form.') +end + +local dir = freeswitch.getGlobalVariable('portal_users_dir') +local domain = freeswitch.getGlobalVariable('domain') +if not dir or not dir:match('^/[%w_./-]+$') or not domain or not domain:match('^[%w_.:-]+$') then + return failure('Portal user creation is not configured. See portal-users.xml.') +end + +local id = argv[1] +if not id or #id > 16 or not id:match('^[0-9]+$') or argv[2] then + return failure('Enter a user ID of 1 to 16 digits.') +end + +local api = freeswitch.API() +local lookup = 'id ' .. id .. ' ' .. domain +local exists = api:execute('user_exists', lookup) +if exists == 'true' then + return failure('This user already exists.') +elseif exists ~= 'false' then + return failure('Could not look up users in the FreeSWITCH directory.') +end + +local random = io.open('/dev/urandom', 'rb') +if not random then return failure('Could not generate a SIP password.') end +local bytes = random:read(16) +random:close() +if not bytes or #bytes ~= 16 then return failure('Could not generate a SIP password.') end +local password = bytes:gsub('.', function(byte) return ('%02x'):format(byte:byte()) end) + +local xml = ([[ + + + + + +]]):format(id, password) + +-- Publish a complete file without replacing another request's file. +local pipe = io.popen('mktemp -- ' .. dir .. '/.user-XXXXXXXXXX') +if not pipe then return failure('Could not create a temporary user file.') end +local tmp = pipe:read('*l') +if not pipe:close() or not tmp then return failure('Could not create a temporary user file.') end +local file = io.open(tmp, 'wb') +local written, closed +if file then + written = file:write(xml) + closed = file:close() +end +local dest = dir .. '/' .. id .. '.xml' +local published = written and closed and os.execute('ln -T -- ' .. tmp .. ' ' .. dest .. ' 2>/dev/null') +os.remove(tmp) +if published ~= true and published ~= 0 then + return failure('Could not save the user. The file may already exist.') +end + +local result = api:execute('reloadxml', '') +if not result or not result:match('^%+OK %[Success%]%s*$') or api:execute('user_exists', lookup) ~= 'true' then + if not os.remove(dest) then + return failure('Could not load the user or remove the new file. Contact the administrator.') + end + api:execute('reloadxml', '') + return failure('Could not load the user. Check the portal directory include and the FreeSWITCH log.') +end +stream:write('+OK ' .. password .. '\n') diff --git a/tests/portal_create_user.lua b/tests/portal_create_user.lua new file mode 100644 index 0000000000..dc59bc0a61 --- /dev/null +++ b/tests/portal_create_user.lua @@ -0,0 +1,62 @@ +-- Run with: lua tests/portal_create_user.lua +local dir = assert(io.popen('mktemp -d')) +local path = assert(dir:read('*l')) +assert(dir:close()) + +local enabled, header, duplicate, loaded, reload_ok = true, '1', false, false, true +local response +freeswitch = { + getGlobalVariable = function(name) + return ({portal_users_dir = enabled and path or nil, domain = 'example.org'})[name] + end, + API = function() + return {execute = function(_, command) + if command == 'user_exists' then return (duplicate or loaded) and 'true' or 'false' end + assert(command == 'reloadxml') + loaded = reload_ok + return reload_ok and '+OK [Success]\n' or '+OK [Parse error]\n' + end} + end +} +env = {getHeader = function(_, name) + assert(name == 'HTTP-Portal-Write') + return header +end} +stream = {write = function(_, message) response = message end} + +local function create(id, expected) + argv = {id} + response = nil + dofile('scripts/lua/portal/create_user.lua') + assert(response and response:match(expected), tostring(response)) +end + +local ok, err = pcall(function() + create('../1020', '^%-ERR') + header = nil + create('1020', '^%-ERR') + header, enabled = '1', false + create('1020', '^%-ERR') + enabled, duplicate = true, true + create('1020', '^%-ERR') + duplicate, reload_ok = false, false + create('1020', '^%-ERR') + assert(not io.open(path .. '/1020.xml', 'r')) + reload_ok = true + create('1020', '^%+OK [a-f0-9]+\n$') + local password = response:match('^%+OK ([a-f0-9]+)\n$') + assert(#password == 32) + local file = assert(io.open(path .. '/1020.xml', 'r')) + local xml = file:read('*a') + file:close() + assert(xml:find('', 1, true)) + assert(xml:find('name="password" value="' .. password .. '"', 1, true)) + loaded = false -- Even if the file is not in the live directory, do not replace it. + create('1020', '^%-ERR') + file = assert(io.open(path .. '/1020.xml', 'r')) + assert(file:read('*a') == xml) + file:close() +end) +os.remove(path .. '/1020.xml') +assert(os.remove(path)) +assert(ok, err) -- 2.50.1 From f5c0dc6978f21cc657599b152ead3f5ce756d713 Mon Sep 17 00:00:00 2001 From: Anton Farygin Date: Wed, 25 Nov 2026 14:29:14 +0500 Subject: [PATCH 3/4] fix(portal): remove unsupported gateway creation form The gateway form hides itself without reporting whether the operation worked. Its Lua backend assumes a hard-coded configuration path and cannot reliably add a gateway on packaged installations. Leave the gateway list intact and point administrators to the Sofia profile instead. --- htdocs/portal/assets/js/fsportal.js | 40 ----------------------------- htdocs/portal/index.html | 39 +--------------------------- 2 files changed, 1 insertion(+), 78 deletions(-) diff --git a/htdocs/portal/assets/js/fsportal.js b/htdocs/portal/assets/js/fsportal.js index 0ac0d4ec8f..714b592667 100644 --- a/htdocs/portal/assets/js/fsportal.js +++ b/htdocs/portal/assets/js/fsportal.js @@ -248,7 +248,6 @@ App.Router.map(function(){ this.route("users"); this.route("newUser"); this.route("sofiaStatus"); - this.route("addGateway"); this.route("about", { path: "/about" }); }); @@ -842,45 +841,6 @@ App.sofiaStatusController = Ember.ArrayController.create({ } }); -App.SofiaStatusController = Ember.ObjectController.extend({ - addGatewayButtons: [ - Ember.Object.create({title: 'Add', clicked:"submit", type:"primary"}), - Ember.Object.create({title: 'Cancel', clicked: "cancel", dismiss: 'modal'}) - ], - - actions: { - //Submit the modal - submit: function() { - // alert("Not implemented"); - // return false; - url = "/txtapi/lua?portal/create_gateway.lua%20" + - $("#gateway_name").val() + "%20" + - $("#gateway_realm").val() + "%20" + - $("#gateway_username").val() + "%20" + - $("#gateway_password").val() + "%20" + - $("#gateway_register").is(":checked"); - $.post(url, { - success: function() { }, - error: function(e) { } - }); - - // Bootstrap.NM.push('Successfully submitted modal', 'success'); - return Bootstrap.ModalManager.hide('newUserForm'); - }, - - //Cancel the modal, we don't need to hide the model manually because we set {..., dismiss: 'modal'} on the button meta data - cancel: function() { - Bootstrap.ModalManager.hide('newUserForm'); - return Bootstrap.NM.push('Modal was cancelled', 'info'); - }, - - //Show the modal - addGateway: function() { - return Bootstrap.ModalManager.show('newUserForm'); - } - } -}); - // App.initialize(); var global_debug_event = false; var global_background_job = false; diff --git a/htdocs/portal/index.html b/htdocs/portal/index.html index 77db574741..8c2b4281b2 100644 --- a/htdocs/portal/index.html +++ b/htdocs/portal/index.html @@ -636,46 +636,9 @@ +

To add a gateway, edit the Sofia profile configuration.

-

To add a gateway, edit the Sofia profile configuration.