device: support match.interface-name

Add support for matching a connection with the new
match.interface-name property.
This commit is contained in:
Beniamino Galvani
2018-08-09 09:29:51 +02:00
parent d47e0beb7d
commit 81978e36ba
3 changed files with 47 additions and 4 deletions

View File

@@ -5511,6 +5511,9 @@ check_connection_compatible (NMDevice *self, NMConnection *connection, GError **
gs_free_error GError *local = NULL;
gs_free char *conn_iface = NULL;
NMDeviceClass *klass;
const char *const *patterns;
NMSettingMatch *s_match;
guint num_patterns;
klass = NM_DEVICE_GET_CLASS (self);
if (klass->connection_type_check_compatible) {
@@ -5534,15 +5537,23 @@ check_connection_compatible (NMDevice *self, NMConnection *connection, GError **
"cannot get interface name due to %s", local->message);
return FALSE;
}
return TRUE;
}
if (!nm_streq0 (conn_iface, device_iface)) {
} else if (!nm_streq0 (conn_iface, device_iface)) {
nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY,
"mismatching interface name");
return FALSE;
}
s_match = (NMSettingMatch *) nm_connection_get_setting (connection,
NM_TYPE_SETTING_MATCH);
if (s_match) {
patterns = nm_setting_match_get_interface_names (s_match, &num_patterns);
if (!nm_wildcard_match_check (device_iface, patterns, num_patterns)) {
nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY,
"device does not satisfy match.interface-name property");
return FALSE;
}
}
return TRUE;
}

View File

@@ -25,6 +25,7 @@
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
@@ -1730,6 +1731,33 @@ nm_match_spec_join (GSList *specs)
return g_string_free (str, FALSE);
}
gboolean
nm_wildcard_match_check (const char *str,
const char *const *patterns,
guint num_patterns)
{
guint i, neg = 0;
for (i = 0; i < num_patterns; i++) {
if (patterns[i][0] == '!') {
neg++;
if (!fnmatch (patterns[i] + 1, str, 0))
return FALSE;
}
}
if (neg == num_patterns)
return TRUE;
for (i = 0; i < num_patterns; i++) {
if ( patterns[i][0] != '!'
&& !fnmatch (patterns[i], str, 0))
return TRUE;
}
return FALSE;
}
/*****************************************************************************/
char *

View File

@@ -215,6 +215,10 @@ NMMatchSpecMatchType nm_match_spec_config (const GSList *specs,
GSList *nm_match_spec_split (const char *value);
char *nm_match_spec_join (GSList *specs);
gboolean nm_wildcard_match_check (const char *str,
const char *const *patterns,
guint num_patterns);
/*****************************************************************************/
const char *nm_utils_get_ip_config_method (NMConnection *connection,