summaryrefslogtreecommitdiff
path: root/shared/nm-libnm-core-intern/nm-libnm-core-utils.c
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-12-05 14:16:48 +0100
committerThomas Haller <thaller@redhat.com>2019-12-10 07:53:25 +0100
commit09e17888f7f032810dbe21bbeab0fa38c7d0d72f (patch)
tree25e1281e90899723026550849e030416bca0fe1a /shared/nm-libnm-core-intern/nm-libnm-core-utils.c
parent5ad095374f04d2ffd34eceec099126ffa41e2173 (diff)
downloadNetworkManager-09e17888f7f032810dbe21bbeab0fa38c7d0d72f.tar.gz
libnm: add mapping functions between string and NMClientPermission enum
Diffstat (limited to 'shared/nm-libnm-core-intern/nm-libnm-core-utils.c')
-rw-r--r--shared/nm-libnm-core-intern/nm-libnm-core-utils.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/shared/nm-libnm-core-intern/nm-libnm-core-utils.c b/shared/nm-libnm-core-intern/nm-libnm-core-utils.c
index 9bf0b1202e..366d812a31 100644
--- a/shared/nm-libnm-core-intern/nm-libnm-core-utils.c
+++ b/shared/nm-libnm-core-intern/nm-libnm-core-utils.c
@@ -4,6 +4,8 @@
#include "nm-libnm-core-utils.h"
+#include "nm-common-macros.h"
+
/*****************************************************************************/
gboolean
@@ -59,3 +61,91 @@ nm_utils_vlan_priority_map_parse_str (NMVlanPriorityMap map_type,
NM_SET_OUT (out_has_wildcard_to, v2 < 0);
return TRUE;
}
+
+/*****************************************************************************/
+
+const char *const nm_auth_permission_names_by_idx[NM_CLIENT_PERMISSION_LAST] = {
+ [NM_CLIENT_PERMISSION_CHECKPOINT_ROLLBACK - 1] = NM_AUTH_PERMISSION_CHECKPOINT_ROLLBACK,
+ [NM_CLIENT_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK,
+ [NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK,
+ [NM_CLIENT_PERMISSION_ENABLE_DISABLE_STATISTICS - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_STATISTICS,
+ [NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI,
+ [NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX,
+ [NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN - 1] = NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN,
+ [NM_CLIENT_PERMISSION_NETWORK_CONTROL - 1] = NM_AUTH_PERMISSION_NETWORK_CONTROL,
+ [NM_CLIENT_PERMISSION_RELOAD - 1] = NM_AUTH_PERMISSION_RELOAD,
+ [NM_CLIENT_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS - 1] = NM_AUTH_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS,
+ [NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME - 1] = NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME,
+ [NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN - 1] = NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN,
+ [NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM - 1] = NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM,
+ [NM_CLIENT_PERMISSION_SLEEP_WAKE - 1] = NM_AUTH_PERMISSION_SLEEP_WAKE,
+ [NM_CLIENT_PERMISSION_WIFI_SCAN - 1] = NM_AUTH_PERMISSION_WIFI_SCAN,
+ [NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN - 1] = NM_AUTH_PERMISSION_WIFI_SHARE_OPEN,
+ [NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED - 1] = NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED,
+};
+
+const NMClientPermission nm_auth_permission_sorted[NM_CLIENT_PERMISSION_LAST] = {
+ NM_CLIENT_PERMISSION_CHECKPOINT_ROLLBACK,
+ NM_CLIENT_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK,
+ NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK,
+ NM_CLIENT_PERMISSION_ENABLE_DISABLE_STATISTICS,
+ NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI,
+ NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX,
+ NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN,
+ NM_CLIENT_PERMISSION_NETWORK_CONTROL,
+ NM_CLIENT_PERMISSION_RELOAD,
+ NM_CLIENT_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS,
+ NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME,
+ NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN,
+ NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM,
+ NM_CLIENT_PERMISSION_SLEEP_WAKE,
+ NM_CLIENT_PERMISSION_WIFI_SCAN,
+ NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN,
+ NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED,
+};
+
+const char *
+nm_auth_permission_to_string (NMClientPermission permission)
+{
+ if (permission < 1)
+ return NULL;
+ if (permission > NM_CLIENT_PERMISSION_LAST)
+ return NULL;
+ return nm_auth_permission_names_by_idx[permission - 1];
+}
+
+#define AUTH_PERMISSION_PREFIX "org.freedesktop.NetworkManager."
+
+static int
+_nm_auth_permission_from_string_cmp (gconstpointer a, gconstpointer b, gpointer user_data)
+{
+ const NMClientPermission *const p = a;
+ const char *const needle = b;
+ const char *ss = nm_auth_permission_names_by_idx[*p - 1];
+
+ nm_assert (NM_STR_HAS_PREFIX (ss, AUTH_PERMISSION_PREFIX));
+ nm_assert (ss[NM_STRLEN (AUTH_PERMISSION_PREFIX)] != '\0');
+
+ return strcmp (&ss[NM_STRLEN (AUTH_PERMISSION_PREFIX)], needle);
+}
+
+NMClientPermission
+nm_auth_permission_from_string (const char *str)
+{
+ gssize idx;
+
+ if (!str)
+ return NM_CLIENT_PERMISSION_NONE;
+
+ if (!NM_STR_HAS_PREFIX (str, AUTH_PERMISSION_PREFIX))
+ return NM_CLIENT_PERMISSION_NONE;
+ idx = nm_utils_array_find_binary_search (nm_auth_permission_sorted,
+ sizeof (nm_auth_permission_sorted[0]),
+ G_N_ELEMENTS (nm_auth_permission_sorted),
+ &str[NM_STRLEN (AUTH_PERMISSION_PREFIX)],
+ _nm_auth_permission_from_string_cmp,
+ NULL);
+ if (idx < 0)
+ return NM_CLIENT_PERMISSION_NONE;
+ return nm_auth_permission_sorted[idx];
+}