From 3a888102ab8da5fd0c10811f10862ebd83ad33f7 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Tue, 23 Jan 2024 16:25:50 +0100 Subject: [PATCH] utils: Introduce XDP_APP_INFO_KIND_CONTAINERS1 This new app kind is authenticated by D-Bus and x-d-p gets all the information about the app from D-Bus which itself get the information from the sandbox engine which set up the sandbox of the app. Currently only flatpak and dbus (dbus-daemon) supports this mechanism but it can be supported by any other sandbox engine, such as snap and firejail. This also means that an app with XDP_APP_INFO_KIND_CONTAINERS1 can still be a flatpak or a snap app. The goal is to make as many paths in x-d-p agnostic to the actual sandbox engine and handle all XDP_APP_INFO_KIND_CONTAINERS1 apps the same. Eventually we can then remove any other XDP_APP_INFO_KIND variants. This commit sets up a XdpAppInfo object with all the metadata x-d-p will need. The follow up commit implements various XdpAppInfo functionalities. --- src/xdp-utils.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- src/xdp-utils.h | 7 ++++--- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/xdp-utils.c b/src/xdp-utils.c index 89e52080c..9ad33821c 100644 --- a/src/xdp-utils.c +++ b/src/xdp-utils.c @@ -144,6 +144,14 @@ struct _XdpAppInfo { { GKeyFile *keyfile; } snap; + struct + { + char *container_type; + char *instance_id; + int pidfd; + char *desktop_file; + gboolean has_network; + } containers1; } u; }; @@ -266,6 +274,13 @@ xdp_app_info_free (XdpAppInfo *app_info) g_clear_pointer (&app_info->u.snap.keyfile, g_key_file_free); break; + case XDP_APP_INFO_KIND_CONTAINERS1: + g_clear_pointer (&app_info->u.containers1.container_type, g_free); + g_clear_pointer (&app_info->u.containers1.instance_id, g_free); + g_clear_pointer (&app_info->u.containers1.desktop_file, g_free); + xdp_close_fd (&app_info->u.containers1.pidfd); + break; + default: break; } @@ -844,7 +859,45 @@ xdp_app_info_from_containers1 (GVariant *containers1_data, int pidfd, GError **error) { - return NULL; + XdpAppInfo *app_info = NULL; + const char *container_type; + const char *app_id; + const char *instance_id; + GVariant *metadata; + const char *desktop_file; + gboolean network_access; + + if (!containers1_data || pidfd < 0) + return NULL; + + g_variant_get (containers1_data, "(o@a{sv}sss@a{sv})", + NULL, + NULL, + &container_type, + &app_id, + &instance_id, + &metadata); + + if (!app_id || !instance_id || !container_type) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Bad Containers1 metadata"); + return NULL; + } + + app_info = xdp_app_info_new (XDP_APP_INFO_KIND_CONTAINERS1); + app_info->id = g_strdup (app_id); + app_info->u.containers1.container_type = g_strdup (container_type); + app_info->u.containers1.instance_id = g_strdup (instance_id); + app_info->u.containers1.pidfd = pidfd; + + if (g_variant_lookup (metadata, "DesktopFile", "&s", &desktop_file)) + app_info->u.containers1.desktop_file = g_strdup (desktop_file); + + app_info->u.containers1.has_network = TRUE; + if (g_variant_lookup (metadata, "NetworkAccess", "b", &network_access)) + app_info->u.containers1.has_network = network_access; + + return app_info; } static gboolean diff --git a/src/xdp-utils.h b/src/xdp-utils.h index 2f4f2c8ab..8bfb77ce8 100644 --- a/src/xdp-utils.h +++ b/src/xdp-utils.h @@ -48,9 +48,10 @@ typedef enum { - XDP_APP_INFO_KIND_HOST = 0, - XDP_APP_INFO_KIND_FLATPAK = 1, - XDP_APP_INFO_KIND_SNAP = 2, + XDP_APP_INFO_KIND_HOST = 0, + XDP_APP_INFO_KIND_FLATPAK = 1, + XDP_APP_INFO_KIND_SNAP = 2, + XDP_APP_INFO_KIND_CONTAINERS1 = 3, } XdpAppInfoKind; gint xdp_mkstempat (int dir_fd,