From e85adbd947fac6b5385f1b74eba506e53c78f483 Mon Sep 17 00:00:00 2001
From: Alexei Gradinari <alex2grad@gmail.com>
Date: Wed, 10 Aug 2016 15:41:38 -0400
Subject: [PATCH] core: Entity ID is not set or invalid

The Exchanging Device and Mailbox States could not working
if the Entity ID (EID) is not set manually and can't be obtained
from ethernet interface.

This patch replaces debug message to warning
and addes missing description about option 'entityid' to
asterisk.conf.sample.

With this patch the asterisk also:
(1) decline loading the modules which won't work without EID:
    res_corosync and res_pjsip_publish_asterisk.
(2) warn if EID is empty on loading next modules:
    pbx_dundi, res_xmpp

Starting with v197 systemd/udev will automatically assign "predictable"
names for all local Ethernet interfaces.
This patch also addes some new ethernet prefixes "eno" and "ens".

ASTERISK-26164 #close

Change-Id: I72d712f1ad5b6f64571bb179c5cb12461e7c58c6
---
 configs/samples/asterisk.conf.sample |  8 ++++++++
 include/asterisk/utils.h             |  8 ++++++++
 main/asterisk.c                      |  6 +++---
 main/utils.c                         | 12 ++++++++++--
 pbx/pbx_dundi.c                      |  3 +++
 res/res_corosync.c                   |  6 ++++++
 res/res_pjsip_publish_asterisk.c     |  5 +++++
 res/res_xmpp.c                       |  4 ++++
 8 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/configs/samples/asterisk.conf.sample b/configs/samples/asterisk.conf.sample
index 818f5c2fb4..6d6d2f0bb1 100644
--- a/configs/samples/asterisk.conf.sample
+++ b/configs/samples/asterisk.conf.sample
@@ -89,6 +89,14 @@ documentation_language = en_US	; Set the language you want documentation
 				; considered dangerous because they can allow
 				; privilege escalation.
 				; Default no
+;entityid=00:11:22:33:44:55	; Entity ID.
+				; This is in the form of a MAC address.
+				; It should be universally unique.
+				; It must be unique between servers communicating
+				; with a protocol that uses this value.
+				; This is currently is used by DUNDi and
+				; Exchanging Device and Mailbox State
+				; using protocols: XMPP, Corosync and PJSIP.
 
 ; Changing the following lines may compromise your security.
 ;[files]
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index c311e9cd5e..c3df4779ff 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -972,6 +972,14 @@ int ast_str_to_eid(struct ast_eid *eid, const char *s);
  */
 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2);
 
+/*!
+ * \brief Check if EID is empty
+ *
+ * \return 1 if the EID is empty, zero otherwise
+ * \since 13.12.0
+ */
+int ast_eid_is_empty(const struct ast_eid *eid);
+
 /*!
  * \brief Get current thread ID
  * \return the ID if platform is supported, else -1
diff --git a/main/asterisk.c b/main/asterisk.c
index 50d8af24ae..3c2ca259ee 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -3661,10 +3661,10 @@ static void ast_readconfig(void)
 		} else if (!strcasecmp(v->name, "entityid")) {
 			struct ast_eid tmp_eid;
 			if (!ast_str_to_eid(&tmp_eid, v->value)) {
-				ast_verbose("Successfully set global EID to '%s'\n", v->value);
 				ast_eid_default = tmp_eid;
-			} else
-				ast_verbose("Invalid Entity ID '%s' provided\n", v->value);
+			} else {
+				ast_log(LOG_WARNING, "Invalid Entity ID '%s' provided\n", v->value);
+			}
 		} else if (!strcasecmp(v->name, "lightbackground")) {
 			ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_LIGHT_BACKGROUND);
 		} else if (!strcasecmp(v->name, "forceblackbackground")) {
diff --git a/main/utils.c b/main/utils.c
index 46edf43ea2..1d2aa0b272 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -2488,7 +2488,7 @@ void ast_set_default_eid(struct ast_eid *eid)
 		return;
 	}
 	for (x = 0; x < MAXIF; x++) {
-		static const char *prefixes[] = { "eth", "em" };
+		static const char *prefixes[] = { "eth", "em", "eno", "ens" };
 		unsigned int i;
 
 		for (i = 0; i < ARRAY_LEN(prefixes); i++) {
@@ -2539,7 +2539,7 @@ void ast_set_default_eid(struct ast_eid *eid)
 	}
 #endif
 #endif
-	ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
+	ast_log(LOG_WARNING, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
 }
 
 int ast_str_to_eid(struct ast_eid *eid, const char *s)
@@ -2564,6 +2564,14 @@ int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
 	return memcmp(eid1, eid2, sizeof(*eid1));
 }
 
+int ast_eid_is_empty(const struct ast_eid *eid)
+{
+	struct ast_eid empty_eid;
+
+	memset(&empty_eid, 0, sizeof(empty_eid));
+	return memcmp(eid, &empty_eid, sizeof(empty_eid)) ? 0 : 1;
+}
+
 int ast_file_is_readable(const char *filename)
 {
 #if defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS)
diff --git a/pbx/pbx_dundi.c b/pbx/pbx_dundi.c
index 94b71a002e..af5cb32ae3 100644
--- a/pbx/pbx_dundi.c
+++ b/pbx/pbx_dundi.c
@@ -4847,6 +4847,9 @@ static int set_config(char *config_file, struct sockaddr_in* sin, int reload)
 		ast_log(LOG_WARNING, "Unable to get host name!\n");
 	AST_LIST_LOCK(&peers);
 
+	if (ast_eid_is_empty(&ast_eid_default)) {
+		ast_log(LOG_WARNING, "Entity ID is not set.\n");
+	}
 	memcpy(&global_eid, &ast_eid_default, sizeof(global_eid));
 
 	global_storehistory = 0;
diff --git a/res/res_corosync.c b/res/res_corosync.c
index 675bb33071..dbb8846974 100644
--- a/res/res_corosync.c
+++ b/res/res_corosync.c
@@ -1103,6 +1103,11 @@ static int load_module(void)
 	cs_error_t cs_err;
 	struct cpg_name name;
 
+	if (ast_eid_is_empty(&ast_eid_default)) {
+		ast_log(LOG_ERROR, "Entity ID is not set.\n");
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
 	nodes = ao2_container_alloc(23, corosync_node_hash_fn, corosync_node_cmp_fn);
 	if (!nodes) {
 		goto failed;
@@ -1162,6 +1167,7 @@ static int load_module(void)
 
 	ast_cli_register_multiple(corosync_cli, ARRAY_LEN(corosync_cli));
 
+
 	return AST_MODULE_LOAD_SUCCESS;
 
 failed:
diff --git a/res/res_pjsip_publish_asterisk.c b/res/res_pjsip_publish_asterisk.c
index 002d976d42..b32408aa3f 100644
--- a/res/res_pjsip_publish_asterisk.c
+++ b/res/res_pjsip_publish_asterisk.c
@@ -858,6 +858,11 @@ static int load_module(void)
 {
 	CHECK_PJSIP_PUBSUB_MODULE_LOADED();
 
+	if (ast_eid_is_empty(&ast_eid_default)) {
+		ast_log(LOG_ERROR, "Entity ID is not set.\n");
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
 	ast_sorcery_apply_config(ast_sip_get_sorcery(), "asterisk-publication");
 	ast_sorcery_apply_default(ast_sip_get_sorcery(), "asterisk-publication", "config", "pjsip.conf,criteria=type=asterisk-publication");
 
diff --git a/res/res_xmpp.c b/res/res_xmpp.c
index 67e479d862..c2b7796285 100644
--- a/res/res_xmpp.c
+++ b/res/res_xmpp.c
@@ -4652,6 +4652,10 @@ static int load_module(void)
 	ast_mutex_init(&messagelock);
 	ast_cond_init(&message_received_condition, NULL);
 
+	if (ast_eid_is_empty(&ast_eid_default)) {
+		ast_log(LOG_WARNING, "Entity ID is not set. The distributing device state or MWI will not work.\n");
+	}
+
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
-- 
GitLab