[Asterisk-code-review] res pjsip: Add ignore uri user options option. (asterisk[certified/13.8])

Richard Mudgett asteriskteam at digium.com
Thu Sep 8 19:25:42 CDT 2016


Richard Mudgett has uploaded a new change for review.

  https://gerrit.asterisk.org/3860

Change subject: res_pjsip: Add ignore_uri_user_options option.
......................................................................

res_pjsip: Add ignore_uri_user_options option.

This implements the chan_sip legacy_useroption_parsing option but with a
better name.

ASTERISK-26316 #close
Reported by: Kevin Harwell

Change-Id: Ib42b0e940dd34d84c7b14bc2e90d1ba392624f62
---
M CHANGES
M configs/samples/pjsip.conf.sample
A contrib/ast-db-manage/config/versions/a6ef36f1309_ps_globals_add_ignore_uri_user_options.py
M include/asterisk/res_pjsip.h
M res/res_pjsip.c
M res/res_pjsip/config_global.c
M res/res_pjsip/pjsip_options.c
M res/res_pjsip_endpoint_identifier_user.c
M res/res_pjsip_messaging.c
M res/res_pjsip_path.c
M res/res_pjsip_pubsub.c
M res/res_pjsip_refer.c
M res/res_pjsip_registrar.c
M res/res_pjsip_session.c
14 files changed, 235 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/60/3860/1

diff --git a/CHANGES b/CHANGES
index fed0d40..756e224 100644
--- a/CHANGES
+++ b/CHANGES
@@ -9,6 +9,20 @@
 ==============================================================================
 
 ------------------------------------------------------------------------------
+--- Functionality changes from Asterisk 13.8-cert3 to Asterisk 13.8-cert4 ----
+------------------------------------------------------------------------------
+
+res_pjsip
+------------------
+ * Added "ignore_uri_user_options" global configuration option for
+   compatibility with an ITSP that sends URI user field options.  When enabled
+   the user field is truncated at the first semicolon.
+   Example:
+   URI: "sip:1235557890;phone-context=national at x.x.x.x;user=phone"
+   The user field is "1235557890;phone-context=national"
+   Which is truncated to this: "1235557890"
+
+------------------------------------------------------------------------------
 --- Functionality changes from Asterisk 13.8-cert2 to Asterisk 13.8-cert3 ----
 ------------------------------------------------------------------------------
 
diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample
index 0fa1a9d..6f83f86 100644
--- a/configs/samples/pjsip.conf.sample
+++ b/configs/samples/pjsip.conf.sample
@@ -910,6 +910,18 @@
 ; with us. The extension added is the name of the endpoint.
 ;regcontext=sipregistrations
 
+;ignore_uri_user_options=no ; Enable/Disable ignoring SIP URI user field options.
+                    ; If you have this option enabled and there are semicolons
+                    ; in the user field of a SIP URI then the field is truncated
+                    ; at the first semicolon.  This effectively makes the semicolon
+                    ; a non-usable character for PJSIP endpoint names, extensions,
+                    ; and AORs.  This can be useful for improving compatability with
+                    ; an ITSP that likes to use user options for whatever reason.
+                    ; Example:
+                    ; URI: "sip:1235557890;phone-context=national at x.x.x.x;user=phone"
+                    ; The user field is "1235557890;phone-context=national"
+                    ; Which becomes this: "1235557890"
+
 ; MODULE PROVIDING BELOW SECTION(S): res_pjsip_acl
 ;==========================ACL SECTION OPTIONS=========================
 ;[acl]
diff --git a/contrib/ast-db-manage/config/versions/a6ef36f1309_ps_globals_add_ignore_uri_user_options.py b/contrib/ast-db-manage/config/versions/a6ef36f1309_ps_globals_add_ignore_uri_user_options.py
new file mode 100644
index 0000000..2ce40a0
--- /dev/null
+++ b/contrib/ast-db-manage/config/versions/a6ef36f1309_ps_globals_add_ignore_uri_user_options.py
@@ -0,0 +1,32 @@
+"""ps_globals add ignore_uri_user_options
+
+Revision ID: a6ef36f1309
+Revises: 4e2493ef32e6
+Create Date: 2016-08-31 12:24:22.368956
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = 'a6ef36f1309'
+down_revision = '4e2493ef32e6'
+
+from alembic import op
+import sqlalchemy as sa
+from sqlalchemy.dialects.postgresql import ENUM
+
+YESNO_NAME = 'yesno_values'
+YESNO_VALUES = ['yes', 'no']
+
+def upgrade():
+    ############################# Enums ##############################
+
+    # yesno_values have already been created, so use postgres enum object
+    # type to get around "already created" issue - works okay with mysql
+    yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=False)
+
+    op.add_column('ps_globals', sa.Column('ignore_uri_user_options', yesno_values))
+
+
+def downgrade():
+    op.drop_column('ps_globals', 'ignore_uri_user_options')
+
diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index 4475f8d..68f9544 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -2193,6 +2193,38 @@
 void ast_sip_unregister_supplement(struct ast_sip_supplement *supplement);
 
 /*!
+ * \brief Retrieve the global setting 'ignore_uri_user_options'.
+ * \since 13.12.0
+ *
+ * \retval non zero if ignore the user field options.
+ */
+unsigned int ast_sip_get_ignore_uri_user_options(void);
+
+/*!
+ * \brief Truncate the URI user field options string if enabled.
+ * \since 13.12.0
+ *
+ * \param str URI user field string to truncate if enabled
+ *
+ * \details
+ * We need to be able to handle URI's looking like
+ * "sip:1235557890;phone-context=national at x.x.x.x;user=phone"
+ *
+ * Where the URI user field is:
+ * "1235557890;phone-context=national"
+ *
+ * When truncated the string will become:
+ * "1235557890"
+ */
+#define AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(str)				\
+	do {														\
+		char *__semi = strchr((str), ';');						\
+		if (__semi && ast_sip_get_ignore_uri_user_options()) {	\
+			*__semi = '\0';										\
+		}														\
+	} while (0)
+
+/*!
  * \brief Retrieve the system debug setting (yes|no|host).
  *
  * \note returned string needs to be de-allocated by caller.
diff --git a/res/res_pjsip.c b/res/res_pjsip.c
index 5fda931..ac403a6 100644
--- a/res/res_pjsip.c
+++ b/res/res_pjsip.c
@@ -1330,6 +1330,27 @@
                                         set to this value if there is no better option (such as CallerID) to be
                                         used.</synopsis>
 				</configOption>
+				<configOption name="ignore_uri_user_options">
+					<synopsis>Enable/Disable ignoring SIP URI user field options.</synopsis>
+					<description>
+						<para>If you have this option enabled and there are semicolons
+						in the user field of a SIP URI then the field is truncated
+						at the first semicolon.  This effectively makes the semicolon
+						a non-usable character for PJSIP endpoint names, extensions,
+						and AORs.  This can be useful for improving compatability with
+						an ITSP that likes to use user options for whatever reason.
+						</para>
+						<example title="Sample SIP URI">
+							sip:1235557890;phone-context=national at x.x.x.x;user=phone
+						</example>
+						<example title="Sample SIP URI user field">
+							1235557890;phone-context=national
+						</example>
+						<example title="Sample SIP URI user field truncated">
+							1235557890
+						</example>
+					</description>
+				</configOption>
 			</configObject>
 		</configFile>
 	</configInfo>
diff --git a/res/res_pjsip/config_global.c b/res/res_pjsip/config_global.c
index ca608dc..7a34876 100644
--- a/res/res_pjsip/config_global.c
+++ b/res/res_pjsip/config_global.c
@@ -37,6 +37,7 @@
 #define DEFAULT_FROM_USER "asterisk"
 #define DEFAULT_REGCONTEXT ""
 #define DEFAULT_DISABLE_MULTI_DOMAIN 0
+#define DEFAULT_IGNORE_URI_USER_OPTIONS 0
 
 static char default_useragent[256];
 
@@ -61,6 +62,8 @@
 	unsigned int max_initial_qualify_time;
 	/*! Nonzero to disable multi domain support */
 	unsigned int disable_multi_domain;
+	/*! Nonzero if URI user field options are ignored. */
+	unsigned int ignore_uri_user_options;
 };
 
 static void global_destructor(void *obj)
@@ -232,6 +235,21 @@
 	}
 }
 
+unsigned int ast_sip_get_ignore_uri_user_options(void)
+{
+	unsigned int ignore_uri_user_options;
+	struct global_config *cfg;
+
+	cfg = get_global_cfg();
+	if (!cfg) {
+		return DEFAULT_IGNORE_URI_USER_OPTIONS;
+	}
+
+	ignore_uri_user_options = cfg->ignore_uri_user_options;
+	ao2_ref(cfg, -1);
+	return ignore_uri_user_options;
+}
+
 /*!
  * \internal
  * \brief Observer to set default global object if none exist.
@@ -351,6 +369,9 @@
 		OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, regcontext));
 	ast_sorcery_object_field_register(sorcery, "global", "disable_multi_domain", "no",
 		OPT_BOOL_T, 1, FLDSET(struct global_config, disable_multi_domain));
+	ast_sorcery_object_field_register(sorcery, "global", "ignore_uri_user_options",
+		DEFAULT_IGNORE_URI_USER_OPTIONS ? "yes" : "no",
+		OPT_BOOL_T, 1, FLDSET(struct global_config, ignore_uri_user_options));
 
 	if (ast_sorcery_instance_observer_add(sorcery, &observer_callbacks_global)) {
 		return -1;
diff --git a/res/res_pjsip/pjsip_options.c b/res/res_pjsip/pjsip_options.c
index 80fa1c2..15158bc 100644
--- a/res/res_pjsip/pjsip_options.c
+++ b/res/res_pjsip/pjsip_options.c
@@ -701,8 +701,7 @@
 	pjsip_sip_uri *sip_ruri;
 	char exten[AST_MAX_EXTENSION];
 
-	if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
-			     &pjsip_options_method)) {
+	if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_options_method)) {
 		return PJ_FALSE;
 	}
 
@@ -719,13 +718,20 @@
 	sip_ruri = pjsip_uri_get_uri(ruri);
 	ast_copy_pj_str(exten, &sip_ruri->user, sizeof(exten));
 
+	/*
+	 * We may want to match in the dialplan without any user
+	 * options getting in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
+
 	if (ast_shutting_down()) {
 		/*
 		 * Not taking any new calls at this time.
 		 * Likely a server availability OPTIONS poll.
 		 */
 		send_options_response(rdata, 503);
-	} else if (!ast_strlen_zero(exten) && !ast_exists_extension(NULL, endpoint->context, exten, 1, NULL)) {
+	} else if (!ast_strlen_zero(exten)
+		&& !ast_exists_extension(NULL, endpoint->context, exten, 1, NULL)) {
 		send_options_response(rdata, 404);
 	} else {
 		send_options_response(rdata, 200);
diff --git a/res/res_pjsip_endpoint_identifier_user.c b/res/res_pjsip_endpoint_identifier_user.c
index 10b08af..4ad10bd 100644
--- a/res/res_pjsip_endpoint_identifier_user.c
+++ b/res/res_pjsip_endpoint_identifier_user.c
@@ -33,6 +33,7 @@
 {
 	pjsip_uri *from = rdata->msg_info.from->uri;
 	pjsip_sip_uri *sip_from;
+
 	if (!PJSIP_URI_SCHEME_IS_SIP(from) && !PJSIP_URI_SCHEME_IS_SIPS(from)) {
 		return -1;
 	}
@@ -69,6 +70,12 @@
 		return NULL;
 	}
 
+	/*
+	 * We may want to be matched without any user options getting
+	 * in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(endpoint_name);
+
 	if (!ast_sip_get_disable_multi_domain()) {
 		/* Attempt to find the endpoint given the name and domain provided */
 		snprintf(id, sizeof(id), "%s@%s", endpoint_name, domain_name);
diff --git a/res/res_pjsip_messaging.c b/res/res_pjsip_messaging.c
index 594c0fd..edc66bf 100644
--- a/res/res_pjsip_messaging.c
+++ b/res/res_pjsip_messaging.c
@@ -133,6 +133,12 @@
 	} else if ((aor_uri = strchr(name, '@'))) {
 		/* format was 'endpoint@' - don't use the rest */
 		*aor_uri = '\0';
+
+		/*
+		 * We may want to match without any user options getting
+		 * in the way.
+		 */
+		AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(name);
 	}
 
 	/* at this point, if name is not empty then it
@@ -448,6 +454,12 @@
 	sip_ruri = pjsip_uri_get_uri(ruri);
 	ast_copy_pj_str(exten, &sip_ruri->user, AST_MAX_EXTENSION);
 
+	/*
+	 * We may want to match in the dialplan without any user
+	 * options getting in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
+
 	endpt = ast_pjsip_rdata_get_endpoint(rdata);
 	ast_assert(endpt != NULL);
 
@@ -528,7 +540,7 @@
 
 static struct msg_data* msg_data_create(const struct ast_msg *msg, const char *to, const char *from)
 {
-	char *tag;
+	char *uri_params;
 	struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy);
 
 	if (!mdata) {
@@ -553,9 +565,14 @@
 		return NULL;
 	}
 
-	/* sometimes from can still contain the tag at this point, so remove it */
-	if ((tag = strchr(mdata->from, ';'))) {
-		*tag = '\0';
+	/*
+	 * Sometimes from URI can contain URI parameters, so remove them.
+	 *
+	 * sip:user;user-options at domain;uri-parameters
+	 */
+	uri_params = strchr(mdata->from, '@');
+	if (uri_params && (uri_params = strchr(mdata->from, ';'))) {
+		*uri_params = '\0';
 	}
 	return mdata;
 }
diff --git a/res/res_pjsip_path.c b/res/res_pjsip_path.c
index 47d6a79..20c9d43 100644
--- a/res/res_pjsip_path.c
+++ b/res/res_pjsip_path.c
@@ -40,7 +40,8 @@
 	char *configured_aors, *aor_name;
 	pjsip_sip_uri *sip_uri;
 	char *domain_name;
-	RAII_VAR(struct ast_str *, id, NULL, ast_free);
+	char *username;
+	struct ast_str *id = NULL;
 
 	if (ast_strlen_zero(endpoint->aors)) {
 		return NULL;
@@ -49,6 +50,14 @@
 	sip_uri = pjsip_uri_get_uri(uri);
 	domain_name = ast_alloca(sip_uri->host.slen + 1);
 	ast_copy_pj_str(domain_name, &sip_uri->host, sip_uri->host.slen + 1);
+	username = ast_alloca(sip_uri->user.slen + 1);
+	ast_copy_pj_str(username, &sip_uri->user, sip_uri->user.slen + 1);
+
+	/*
+	 * We may want to match without any user options getting
+	 * in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(username);
 
 	configured_aors = ast_strdupa(endpoint->aors);
 
@@ -60,15 +69,16 @@
 			continue;
 		}
 
-		if (!pj_strcmp2(&sip_uri->user, aor_name)) {
+		if (!strcmp(username, aor_name)) {
 			break;
 		}
 
-		if (!id && !(id = ast_str_create(sip_uri->user.slen + sip_uri->host.slen + 2))) {
-			return NULL;
+		if (!id && !(id = ast_str_create(strlen(username) + sip_uri->host.slen + 2))) {
+			aor_name = NULL;
+			break;
 		}
 
-		ast_str_set(&id, 0, "%.*s@", (int)sip_uri->user.slen, sip_uri->user.ptr);
+		ast_str_set(&id, 0, "%s@", username);
 		if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
 			ast_str_append(&id, 0, "%s", alias->domain);
 			ao2_cleanup(alias);
@@ -77,10 +87,10 @@
 		}
 
 		if (!strcmp(aor_name, ast_str_buffer(id))) {
-			ast_free(id);
 			break;
 		}
 	}
+	ast_free(id);
 
 	if (ast_strlen_zero(aor_name)) {
 		return NULL;
diff --git a/res/res_pjsip_pubsub.c b/res/res_pjsip_pubsub.c
index ae75ce7..c24ac01 100644
--- a/res/res_pjsip_pubsub.c
+++ b/res/res_pjsip_pubsub.c
@@ -1400,6 +1400,12 @@
 	resource = ast_alloca(resource_size);
 	ast_copy_pj_str(resource, &request_uri->user, resource_size);
 
+	/*
+	 * We may want to match without any user options getting
+	 * in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(resource);
+
 	handler = subscription_get_handler_from_rdata(rdata);
 	if (!handler || !handler->notifier) {
 		ast_log(LOG_WARNING, "Failed recreating '%s' subscription: Could not get subscription handler.\n",
@@ -2796,6 +2802,12 @@
 	resource = ast_alloca(resource_size);
 	ast_copy_pj_str(resource, &request_uri_sip->user, resource_size);
 
+	/*
+	 * We may want to match without any user options getting
+	 * in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(resource);
+
 	expires_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, rdata->msg_info.msg->hdr.next);
 
 	if (expires_header) {
@@ -3009,6 +3021,12 @@
 	resource_name = ast_alloca(resource_size);
 	ast_copy_pj_str(resource_name, &request_uri_sip->user, resource_size);
 
+	/*
+	 * We may want to match without any user options getting
+	 * in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(resource_name);
+
 	resource = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "inbound-publication", resource_name);
 	if (!resource) {
 		pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL);
diff --git a/res/res_pjsip_refer.c b/res/res_pjsip_refer.c
index f3af65c..f586718 100644
--- a/res/res_pjsip_refer.c
+++ b/res/res_pjsip_refer.c
@@ -809,6 +809,13 @@
 
 	/* Using the user portion of the target URI see if it exists as a valid extension in their context */
 	ast_copy_pj_str(exten, &target->user, sizeof(exten));
+
+	/*
+	 * We may want to match in the dialplan without any user
+	 * options getting in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
+
 	if (!ast_exists_extension(NULL, context, exten, 1, NULL)) {
 		ast_log(LOG_ERROR, "Channel '%s' from endpoint '%s' attempted blind transfer to '%s@%s' but target does not exist\n",
 			ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), exten, context);
diff --git a/res/res_pjsip_registrar.c b/res/res_pjsip_registrar.c
index 29ee077..099ccc0 100644
--- a/res/res_pjsip_registrar.c
+++ b/res/res_pjsip_registrar.c
@@ -519,6 +519,7 @@
 	struct ast_sip_aor *aor = NULL;
 	pjsip_sip_uri *uri;
 	char *domain_name;
+	char *username;
 	char *configured_aors;
 	char *aor_name;
 	struct ast_str *id = NULL;
@@ -526,6 +527,14 @@
 	uri = pjsip_uri_get_uri(rdata->msg_info.to->uri);
 	domain_name = ast_alloca(uri->host.slen + 1);
 	ast_copy_pj_str(domain_name, &uri->host, uri->host.slen + 1);
+	username = ast_alloca(uri->user.slen + 1);
+	ast_copy_pj_str(username, &uri->user, uri->user.slen + 1);
+
+	/*
+	 * We may want to match without any user options getting
+	 * in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(username);
 
 	configured_aors = ast_strdupa(endpoint->aors);
 
@@ -537,16 +546,16 @@
 			continue;
 		}
 
-		if (!pj_strcmp2(&uri->user, aor_name)) {
+		if (!strcmp(username, aor_name)) {
 			break;
 		}
 
-		if (!id && !(id = ast_str_create(uri->user.slen + uri->host.slen + 2))) {
+		if (!id && !(id = ast_str_create(strlen(username) + uri->host.slen + 2))) {
 			aor_name = NULL;
 			break;
 		}
 
-		ast_str_set(&id, 0, "%.*s@", (int)uri->user.slen, uri->user.ptr);
+		ast_str_set(&id, 0, "%s@", username);
 		if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
 			ast_str_append(&id, 0, "%s", alias->domain);
 			ao2_cleanup(alias);
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index a4108d5..c04d019 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -1952,6 +1952,12 @@
 	sip_ruri = pjsip_uri_get_uri(ruri);
 	ast_copy_pj_str(session->exten, &sip_ruri->user, sizeof(session->exten));
 
+	/*
+	 * We may want to match in the dialplan without any user
+	 * options getting in the way.
+	 */
+	AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(session->exten);
+
 	pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
 	if (!pickup_cfg) {
 		ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
@@ -2903,6 +2909,13 @@
 		char exten[AST_MAX_EXTENSION];
 
 		ast_copy_pj_str(exten, &uri->user, sizeof(exten));
+
+		/*
+		 * We may want to match in the dialplan without any user
+		 * options getting in the way.
+		 */
+		AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
+
 		ast_channel_call_forward_set(session->channel, exten);
 	} else if (session->endpoint->redirect_method == AST_SIP_REDIRECT_URI_CORE) {
 		char target_uri[PJSIP_MAX_URL_SIZE];

-- 
To view, visit https://gerrit.asterisk.org/3860
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib42b0e940dd34d84c7b14bc2e90d1ba392624f62
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: certified/13.8
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>



More information about the asterisk-code-review mailing list