From fd911220bedb73d5351d60c1af7fc75d2f85a165 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 18 Nov 2026 22:18:00 +0500 Subject: [PATCH] Merge commit from fork On a profile with `extended-info-parsing` set, an inbound SIP INFO whose content type is `freeswitch/api-request` puts an FSAPI command in the request body and gets its output back in the 200 OK. The branch needs no dialog, and no `mod_sofia` authentication or ACL setting covers an out-of-dialog INFO. That same param also enables the in-dialog `session-event` half, so there is no way to keep one and not the other. The new per-profile `enable-info-api-request` param gates the api-request branch alone and is off unless set, leaving `session-event` reachable under `extended-info-parsing` as before. An INFO selecting the branch on a profile without the new param is answered 403 and logged with the source and the requested command; a permitted one is logged too, since the source address appears in no event the API path fires. Both lines print the body's first line, capped, rather than the whole body. --- conf/testing/sip_profiles/internal.xml | 4 ++ conf/vanilla/sip_profiles/internal.xml | 4 ++ .../endpoints/mod_sofia/conf/sofia.conf.xml | 5 +++ src/mod/endpoints/mod_sofia/mod_sofia.h | 1 + src/mod/endpoints/mod_sofia/sofia.c | 40 ++++++++++++++++++- 5 files changed, 53 insertions(+), 1 deletion(-) diff --git a/conf/testing/sip_profiles/internal.xml b/conf/testing/sip_profiles/internal.xml index 62f52af63f..c4efa30876 100644 --- a/conf/testing/sip_profiles/internal.xml +++ b/conf/testing/sip_profiles/internal.xml @@ -107,6 +107,10 @@ + + + + + + diff --git a/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml b/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml index 4732ae6fbf..c3253ed0b8 100644 --- a/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml +++ b/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml @@ -148,6 +148,11 @@ + + + diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index 81d0517d78..433fff5c9b 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -312,6 +312,7 @@ typedef enum { PFLAG_AUTH_CALLS_ACL_ONLY, PFLAG_USE_PORT_FOR_ACL_CHECK, PFLAG_ENABLE_CHAT_API_PROTO, + PFLAG_ENABLE_INFO_API_REQUEST, /* No new flags below this line */ PFLAG_MAX diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index 93583895c7..1a142761d9 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -5612,6 +5612,12 @@ switch_status_t config_sofia(sofia_config_t reload, char *profile_name) } else { sofia_clear_pflag(profile, PFLAG_EXTENDED_INFO_PARSING); } + } else if (!strcasecmp(var, "enable-info-api-request")) { + if (switch_true(val)) { + sofia_set_pflag(profile, PFLAG_ENABLE_INFO_API_REQUEST); + } else { + sofia_clear_pflag(profile, PFLAG_ENABLE_INFO_API_REQUEST); + } } else if (!strcasecmp(var, "nonce-ttl") && !zstr(val)) { profile->nonce_ttl = atoi(val); } else if (!strcasecmp(var, "max-auth-validity") && !zstr(val)) { @@ -9952,10 +9958,42 @@ void sofia_handle_sip_i_info(nua_t *nua, sofia_profile_t *profile, nua_handle_t return; } else if (!strcasecmp(sip->sip_content_type->c_subtype, "api-request")) { - char *cmd = strdup(sip->sip_payload->pl_data); + char *cmd; char *arg; + char network_ip[80] = ""; + int network_port = 0; + size_t log_len; switch_stream_handle_t stream = { 0 }; + /* leaves the buffer empty when the message carries no address info */ + sofia_glue_get_addr(de->data->e_msg, network_ip, sizeof(network_ip), &network_port); + + /* Both log lines below print the body's first line only, capped: the body comes + from the request, has no length bound, and may hold line breaks. */ + log_len = strcspn(sip->sip_payload->pl_data, "\r\n"); + if (log_len > 256) { + log_len = 256; + } + + if (!sofia_test_pflag(profile, PFLAG_ENABLE_INFO_API_REQUEST)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, + "Profile [%s] rejected an INFO from %s:%d requesting a FreeSWITCH API command [%.*s]. " + "Set enable-info-api-request=true on the profile to permit it.\n", + profile->name, network_ip, network_port, (int) log_len, sip->sip_payload->pl_data); + + nua_respond(nh, SIP_403_FORBIDDEN, NUTAG_WITH_THIS_MSG(de->data->e_msg), + TAG_IF(!zstr(session_id_header), SIPTAG_HEADER_STR(session_id_header)), + TAG_END()); + + return; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Profile [%s] running a FreeSWITCH API command requested by an INFO from %s:%d [%.*s]\n", + profile->name, network_ip, network_port, (int) log_len, sip->sip_payload->pl_data); + + cmd = strdup(sip->sip_payload->pl_data); + switch_assert(cmd); SWITCH_STANDARD_STREAM(stream); switch_assert(stream.data);