From c24528d3697c62cad8ff746a56992a59f31d333d Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Fri, 6 May 2016 18:03:47 +0200 Subject: Rename source files to flatpak --- session-helper/Makefile.am.inc | 6 +- session-helper/flatpak-permission-store.c | 465 ++++++++++++++++++++++++++++++ session-helper/flatpak-permission-store.h | 28 ++ session-helper/flatpak-session-helper.c | 162 +++++++++++ session-helper/xdg-app-permission-store.c | 465 ------------------------------ session-helper/xdg-app-permission-store.h | 28 -- session-helper/xdg-app-session-helper.c | 162 ----------- 7 files changed, 658 insertions(+), 658 deletions(-) create mode 100644 session-helper/flatpak-permission-store.c create mode 100644 session-helper/flatpak-permission-store.h create mode 100644 session-helper/flatpak-session-helper.c delete mode 100644 session-helper/xdg-app-permission-store.c delete mode 100644 session-helper/xdg-app-permission-store.h delete mode 100644 session-helper/xdg-app-session-helper.c (limited to 'session-helper') diff --git a/session-helper/Makefile.am.inc b/session-helper/Makefile.am.inc index 5782fe3..55f5b02 100644 --- a/session-helper/Makefile.am.inc +++ b/session-helper/Makefile.am.inc @@ -9,9 +9,9 @@ service_in_files += session-helper/org.freedesktop.XdgApp.service.in dbus_service_DATA += session-helper/org.freedesktop.XdgApp.service xdg_app_session_helper_SOURCES = \ - session-helper/xdg-app-session-helper.c \ - session-helper/xdg-app-permission-store.c \ - session-helper/xdg-app-permission-store.h \ + session-helper/flatpak-session-helper.c \ + session-helper/flatpak-permission-store.c \ + session-helper/flatpak-permission-store.h \ $(NULL) xdg_app_session_helper_LDADD = $(BASE_LIBS) libxdgapp-common.la diff --git a/session-helper/flatpak-permission-store.c b/session-helper/flatpak-permission-store.c new file mode 100644 index 0000000..7cab93f --- /dev/null +++ b/session-helper/flatpak-permission-store.c @@ -0,0 +1,465 @@ +/* + * Copyright © 2015 Red Hat, Inc + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + * Authors: + * Alexander Larsson + */ + +#include "config.h" + +#include +#include +#include +#include +#include "flatpak-permission-store.h" +#include "flatpak-db.h" +#include "flatpak-portal-error.h" + +GHashTable *tables = NULL; + +typedef struct +{ + char *name; + FlatpakDb *db; + GList *outstanding_writes; + GList *current_writes; + gboolean writing; +} Table; + +static void start_writeout (Table *table); + +static void +table_free (Table *table) +{ + g_free (table->name); + g_object_unref (table->db); + g_free (table); +} + +static Table * +lookup_table (const char *name, + GDBusMethodInvocation *invocation) +{ + Table *table; + FlatpakDb *db; + g_autofree char *dir = NULL; + g_autofree char *path = NULL; + + g_autoptr(GError) error = NULL; + + table = g_hash_table_lookup (tables, name); + if (table != NULL) + return table; + + dir = g_build_filename (g_get_user_data_dir (), "xdg-app/db", NULL); + g_mkdir_with_parents (dir, 0755); + + path = g_build_filename (dir, name, NULL); + db = flatpak_db_new (path, FALSE, &error); + if (db == NULL) + { + g_dbus_method_invocation_return_error (invocation, + FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_FAILED, + "Unable to load db file: %s", error->message); + return NULL; + } + + table = g_new0 (Table, 1); + table->name = g_strdup (name); + table->db = db; + + g_hash_table_insert (tables, table->name, table); + + return table; +} + +static void +writeout_done (GObject *source_object, + GAsyncResult *res, + gpointer user_data) +{ + Table *table = user_data; + GList *l; + + g_autoptr(GError) error = NULL; + gboolean ok; + + ok = flatpak_db_save_content_finish (table->db, res, &error); + + for (l = table->current_writes; l != NULL; l = l->next) + { + GDBusMethodInvocation *invocation = l->data; + + if (ok) + g_dbus_method_invocation_return_value (invocation, + g_variant_new ("()")); + else + g_dbus_method_invocation_return_error (invocation, + FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_FAILED, + "Unable to write db: %s", error->message); + } + + g_list_free (table->current_writes); + table->current_writes = NULL; + table->writing = FALSE; + + if (table->outstanding_writes != NULL) + start_writeout (table); +} + +static void +start_writeout (Table *table) +{ + g_assert (table->current_writes == NULL); + table->current_writes = table->outstanding_writes; + table->outstanding_writes = NULL; + table->writing = TRUE; + + flatpak_db_update (table->db); + + flatpak_db_save_content_async (table->db, NULL, writeout_done, table); +} + +static void +ensure_writeout (Table *table, + GDBusMethodInvocation *invocation) +{ + table->outstanding_writes = g_list_prepend (table->outstanding_writes, invocation); + + if (!table->writing) + start_writeout (table); +} + +static gboolean +handle_list (XdgAppPermissionStore *object, + GDBusMethodInvocation *invocation, + const gchar *table_name) +{ + Table *table; + + g_auto(GStrv) ids = NULL; + + table = lookup_table (table_name, invocation); + if (table == NULL) + return TRUE; + + ids = flatpak_db_list_ids (table->db); + + xdg_app_permission_store_complete_list (object, invocation, (const char * const *) ids); + + return TRUE; +} + +static GVariant * +get_app_permissions (FlatpakDbEntry *entry) +{ + g_autofree const char **apps = NULL; + GVariantBuilder builder; + int i; + + apps = flatpak_db_entry_list_apps (entry); + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sas}")); + + for (i = 0; apps[i] != NULL; i++) + { + g_autofree const char **permissions = flatpak_db_entry_list_permissions (entry, apps[i]); + g_variant_builder_add_value (&builder, + g_variant_new ("{s@as}", + apps[i], + g_variant_new_strv (permissions, -1))); + } + + return g_variant_ref_sink (g_variant_builder_end (&builder)); +} + +static gboolean +handle_lookup (XdgAppPermissionStore *object, + GDBusMethodInvocation *invocation, + const gchar *table_name, + const gchar *id) +{ + Table *table; + + g_autoptr(GVariant) data = NULL; + g_autoptr(GVariant) permissions = NULL; + g_autoptr(FlatpakDbEntry) entry = NULL; + + table = lookup_table (table_name, invocation); + if (table == NULL) + return TRUE; + + entry = flatpak_db_lookup (table->db, id); + if (entry == NULL) + { + g_dbus_method_invocation_return_error (invocation, + FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, + "No entry for %s", id); + return TRUE; + } + + data = flatpak_db_entry_get_data (entry); + permissions = get_app_permissions (entry); + + xdg_app_permission_store_complete_lookup (object, invocation, + permissions, + g_variant_new_variant (data)); + + return TRUE; +} + +static void +emit_deleted (XdgAppPermissionStore *object, + const gchar *table_name, + const gchar *id, + FlatpakDbEntry *entry) +{ + g_autoptr(GVariant) data = NULL; + g_autoptr(GVariant) permissions = NULL; + + data = flatpak_db_entry_get_data (entry); + permissions = g_variant_ref_sink (g_variant_new_array (G_VARIANT_TYPE ("{sas}"), NULL, 0)); + + xdg_app_permission_store_emit_changed (object, + table_name, id, + TRUE, + g_variant_new_variant (data), + permissions); +} + + +static void +emit_changed (XdgAppPermissionStore *object, + const gchar *table_name, + const gchar *id, + FlatpakDbEntry *entry) +{ + g_autoptr(GVariant) data = NULL; + g_autoptr(GVariant) permissions = NULL; + + data = flatpak_db_entry_get_data (entry); + permissions = get_app_permissions (entry); + + xdg_app_permission_store_emit_changed (object, + table_name, id, + FALSE, + g_variant_new_variant (data), + permissions); +} + +static gboolean +handle_delete (XdgAppPermissionStore *object, + GDBusMethodInvocation *invocation, + const gchar *table_name, + const gchar *id) +{ + Table *table; + + g_autoptr(FlatpakDbEntry) entry = NULL; + + table = lookup_table (table_name, invocation); + if (table == NULL) + return TRUE; + + entry = flatpak_db_lookup (table->db, id); + if (entry == NULL) + { + g_dbus_method_invocation_return_error (invocation, + FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, + "No entry for %s", id); + return TRUE; + } + + flatpak_db_set_entry (table->db, id, NULL); + emit_deleted (object, table_name, id, entry); + + ensure_writeout (table, invocation); + + return TRUE; +} + +static gboolean +handle_set (XdgAppPermissionStore *object, + GDBusMethodInvocation *invocation, + const gchar *table_name, + gboolean create, + const gchar *id, + GVariant *app_permissions, + GVariant *data) +{ + Table *table; + GVariantIter iter; + GVariant *child; + + g_autoptr(GVariant) data_child = NULL; + g_autoptr(FlatpakDbEntry) old_entry = NULL; + g_autoptr(FlatpakDbEntry) new_entry = NULL; + + table = lookup_table (table_name, invocation); + if (table == NULL) + return TRUE; + + old_entry = flatpak_db_lookup (table->db, id); + if (old_entry == NULL && !create) + { + g_dbus_method_invocation_return_error (invocation, + FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, + "Id %s not found", id); + return TRUE; + } + + data_child = g_variant_get_child_value (data, 0); + new_entry = flatpak_db_entry_new (data_child); + + /* Add all the given app permissions */ + + g_variant_iter_init (&iter, app_permissions); + while ((child = g_variant_iter_next_value (&iter))) + { + g_autoptr(FlatpakDbEntry) old_entry; + const char *child_app_id; + g_autofree const char **permissions; + + g_variant_get (child, "{&s^a&s}", &child_app_id, &permissions); + + old_entry = new_entry; + new_entry = flatpak_db_entry_set_app_permissions (new_entry, child_app_id, (const char **) permissions); + + g_variant_unref (child); + } + + flatpak_db_set_entry (table->db, id, new_entry); + emit_changed (object, table_name, id, new_entry); + + ensure_writeout (table, invocation); + + return TRUE; +} + +static gboolean +handle_set_permission (XdgAppPermissionStore *object, + GDBusMethodInvocation *invocation, + const gchar *table_name, + gboolean create, + const gchar *id, + const gchar *app, + const gchar *const *permissions) +{ + Table *table; + + g_autoptr(FlatpakDbEntry) entry = NULL; + g_autoptr(FlatpakDbEntry) new_entry = NULL; + + table = lookup_table (table_name, invocation); + if (table == NULL) + return TRUE; + + entry = flatpak_db_lookup (table->db, id); + if (entry == NULL) + { + if (create) + { + entry = flatpak_db_entry_new (NULL); + } + else + { + g_dbus_method_invocation_return_error (invocation, + FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, + "Id %s not found", id); + return TRUE; + } + } + + new_entry = flatpak_db_entry_set_app_permissions (entry, app, (const char **) permissions); + flatpak_db_set_entry (table->db, id, new_entry); + emit_changed (object, table_name, id, new_entry); + + ensure_writeout (table, invocation); + + return TRUE; +} + +static gboolean +handle_set_value (XdgAppPermissionStore *object, + GDBusMethodInvocation *invocation, + const gchar *table_name, + gboolean create, + const gchar *id, + GVariant *data) +{ + Table *table; + + g_autoptr(FlatpakDbEntry) entry = NULL; + g_autoptr(FlatpakDbEntry) new_entry = NULL; + + table = lookup_table (table_name, invocation); + if (table == NULL) + return TRUE; + + entry = flatpak_db_lookup (table->db, id); + if (entry == NULL) + { + if (create) + { + new_entry = flatpak_db_entry_new (data); + } + else + { + g_dbus_method_invocation_return_error (invocation, + FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, + "Id %s not found", id); + return TRUE; + } + } + else + { + new_entry = flatpak_db_entry_modify_data (entry, data); + } + + flatpak_db_set_entry (table->db, id, new_entry); + emit_changed (object, table_name, id, new_entry); + + ensure_writeout (table, invocation); + + return TRUE; +} + +void +flatpak_permission_store_start (GDBusConnection *connection) +{ + XdgAppPermissionStore *store; + GError *error = NULL; + + tables = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, (GDestroyNotify) table_free); + + store = xdg_app_permission_store_skeleton_new (); + + g_signal_connect (store, "handle-list", G_CALLBACK (handle_list), NULL); + g_signal_connect (store, "handle-lookup", G_CALLBACK (handle_lookup), NULL); + g_signal_connect (store, "handle-set", G_CALLBACK (handle_set), NULL); + g_signal_connect (store, "handle-set-permission", G_CALLBACK (handle_set_permission), NULL); + g_signal_connect (store, "handle-set-value", G_CALLBACK (handle_set_value), NULL); + g_signal_connect (store, "handle-delete", G_CALLBACK (handle_delete), NULL); + + if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (store), + connection, + "/org/freedesktop/Flatpak/PermissionStore", + &error)) + { + g_warning ("error: %s\n", error->message); + g_error_free (error); + } +} diff --git a/session-helper/flatpak-permission-store.h b/session-helper/flatpak-permission-store.h new file mode 100644 index 0000000..57de5da --- /dev/null +++ b/session-helper/flatpak-permission-store.h @@ -0,0 +1,28 @@ +/* + * Copyright © 2015 Red Hat, Inc + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + * Authors: + * Alexander Larsson + */ + +#ifndef __FLATPAK_PERMISSION_STORE_H__ +#define __FLATPAK_PERMISSION_STORE_H__ + +#include "flatpak-dbus.h" + +void flatpak_permission_store_start (GDBusConnection *connection); + +#endif /* __FLATPAK_PERMISSION_STORE_H__ */ diff --git a/session-helper/flatpak-session-helper.c b/session-helper/flatpak-session-helper.c new file mode 100644 index 0000000..d9ba8b3 --- /dev/null +++ b/session-helper/flatpak-session-helper.c @@ -0,0 +1,162 @@ +/* + * Copyright © 2014 Red Hat, Inc + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + * Authors: + * Alexander Larsson + */ + +#include "config.h" + +#include +#include +#include +#include +#include "flatpak-dbus.h" +#include "flatpak-permission-store.h" + +static char *monitor_dir; + +static gboolean +handle_request_monitor (XdgAppSessionHelper *object, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + xdg_app_session_helper_complete_request_monitor (object, invocation, + monitor_dir); + + return TRUE; +} + +static void +on_bus_acquired (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + XdgAppSessionHelper *helper; + GError *error = NULL; + + flatpak_permission_store_start (connection); + + helper = xdg_app_session_helper_skeleton_new (); + + g_signal_connect (helper, "handle-request-monitor", G_CALLBACK (handle_request_monitor), NULL); + + if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (helper), + connection, + "/org/freedesktop/Flatpak/SessionHelper", + &error)) + { + g_warning ("error: %s\n", error->message); + g_error_free (error); + } +} + +static void +on_name_acquired (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ +} + +static void +on_name_lost (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + exit (1); +} + +static void +copy_file (const char *source, + const char *target_dir) +{ + char *basename = g_path_get_basename (source); + char *dest = g_build_filename (target_dir, basename, NULL); + gchar *contents = NULL; + gsize len; + + if (g_file_get_contents (source, &contents, &len, NULL)) + g_file_set_contents (dest, contents, len, NULL); + + g_free (basename); + g_free (dest); + g_free (contents); +} + +static void +file_changed (GFileMonitor *monitor, + GFile *file, + GFile *other_file, + GFileMonitorEvent event_type, + char *source) +{ + if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT || + event_type == G_FILE_MONITOR_EVENT_CREATED) + copy_file (source, monitor_dir); +} + +static void +setup_file_monitor (const char *source) +{ + GFile *s = g_file_new_for_path (source); + GFileMonitor *monitor; + + copy_file (source, monitor_dir); + + monitor = g_file_monitor_file (s, G_FILE_MONITOR_NONE, NULL, NULL); + if (monitor) + g_signal_connect (monitor, "changed", G_CALLBACK (file_changed), (char *) source); +} + +int +main (int argc, + char **argv) +{ + guint owner_id; + GMainLoop *loop; + + setlocale (LC_ALL, ""); + + g_setenv ("GIO_USE_VFS", "local", TRUE); + + g_set_prgname (argv[0]); + + monitor_dir = g_build_filename (g_get_user_runtime_dir (), "xdg-app-monitor", NULL); + if (g_mkdir_with_parents (monitor_dir, 0755) != 0) + { + g_print ("Can't create %s\n", monitor_dir); + exit (1); + } + + setup_file_monitor ("/etc/resolv.conf"); + setup_file_monitor ("/etc/localtime"); + + owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, + "org.freedesktop.Flatpak", + G_BUS_NAME_OWNER_FLAGS_NONE, + on_bus_acquired, + on_name_acquired, + on_name_lost, + NULL, + NULL); + + loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (loop); + + g_bus_unown_name (owner_id); + + return 0; +} diff --git a/session-helper/xdg-app-permission-store.c b/session-helper/xdg-app-permission-store.c deleted file mode 100644 index 25b3edc..0000000 --- a/session-helper/xdg-app-permission-store.c +++ /dev/null @@ -1,465 +0,0 @@ -/* - * Copyright © 2015 Red Hat, Inc - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - * - * Authors: - * Alexander Larsson - */ - -#include "config.h" - -#include -#include -#include -#include -#include "xdg-app-permission-store.h" -#include "xdg-app-db.h" -#include "xdg-app-portal-error.h" - -GHashTable *tables = NULL; - -typedef struct -{ - char *name; - FlatpakDb *db; - GList *outstanding_writes; - GList *current_writes; - gboolean writing; -} Table; - -static void start_writeout (Table *table); - -static void -table_free (Table *table) -{ - g_free (table->name); - g_object_unref (table->db); - g_free (table); -} - -static Table * -lookup_table (const char *name, - GDBusMethodInvocation *invocation) -{ - Table *table; - FlatpakDb *db; - g_autofree char *dir = NULL; - g_autofree char *path = NULL; - - g_autoptr(GError) error = NULL; - - table = g_hash_table_lookup (tables, name); - if (table != NULL) - return table; - - dir = g_build_filename (g_get_user_data_dir (), "xdg-app/db", NULL); - g_mkdir_with_parents (dir, 0755); - - path = g_build_filename (dir, name, NULL); - db = flatpak_db_new (path, FALSE, &error); - if (db == NULL) - { - g_dbus_method_invocation_return_error (invocation, - FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_FAILED, - "Unable to load db file: %s", error->message); - return NULL; - } - - table = g_new0 (Table, 1); - table->name = g_strdup (name); - table->db = db; - - g_hash_table_insert (tables, table->name, table); - - return table; -} - -static void -writeout_done (GObject *source_object, - GAsyncResult *res, - gpointer user_data) -{ - Table *table = user_data; - GList *l; - - g_autoptr(GError) error = NULL; - gboolean ok; - - ok = flatpak_db_save_content_finish (table->db, res, &error); - - for (l = table->current_writes; l != NULL; l = l->next) - { - GDBusMethodInvocation *invocation = l->data; - - if (ok) - g_dbus_method_invocation_return_value (invocation, - g_variant_new ("()")); - else - g_dbus_method_invocation_return_error (invocation, - FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_FAILED, - "Unable to write db: %s", error->message); - } - - g_list_free (table->current_writes); - table->current_writes = NULL; - table->writing = FALSE; - - if (table->outstanding_writes != NULL) - start_writeout (table); -} - -static void -start_writeout (Table *table) -{ - g_assert (table->current_writes == NULL); - table->current_writes = table->outstanding_writes; - table->outstanding_writes = NULL; - table->writing = TRUE; - - flatpak_db_update (table->db); - - flatpak_db_save_content_async (table->db, NULL, writeout_done, table); -} - -static void -ensure_writeout (Table *table, - GDBusMethodInvocation *invocation) -{ - table->outstanding_writes = g_list_prepend (table->outstanding_writes, invocation); - - if (!table->writing) - start_writeout (table); -} - -static gboolean -handle_list (XdgAppPermissionStore *object, - GDBusMethodInvocation *invocation, - const gchar *table_name) -{ - Table *table; - - g_auto(GStrv) ids = NULL; - - table = lookup_table (table_name, invocation); - if (table == NULL) - return TRUE; - - ids = flatpak_db_list_ids (table->db); - - xdg_app_permission_store_complete_list (object, invocation, (const char * const *) ids); - - return TRUE; -} - -static GVariant * -get_app_permissions (FlatpakDbEntry *entry) -{ - g_autofree const char **apps = NULL; - GVariantBuilder builder; - int i; - - apps = flatpak_db_entry_list_apps (entry); - g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sas}")); - - for (i = 0; apps[i] != NULL; i++) - { - g_autofree const char **permissions = flatpak_db_entry_list_permissions (entry, apps[i]); - g_variant_builder_add_value (&builder, - g_variant_new ("{s@as}", - apps[i], - g_variant_new_strv (permissions, -1))); - } - - return g_variant_ref_sink (g_variant_builder_end (&builder)); -} - -static gboolean -handle_lookup (XdgAppPermissionStore *object, - GDBusMethodInvocation *invocation, - const gchar *table_name, - const gchar *id) -{ - Table *table; - - g_autoptr(GVariant) data = NULL; - g_autoptr(GVariant) permissions = NULL; - g_autoptr(FlatpakDbEntry) entry = NULL; - - table = lookup_table (table_name, invocation); - if (table == NULL) - return TRUE; - - entry = flatpak_db_lookup (table->db, id); - if (entry == NULL) - { - g_dbus_method_invocation_return_error (invocation, - FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, - "No entry for %s", id); - return TRUE; - } - - data = flatpak_db_entry_get_data (entry); - permissions = get_app_permissions (entry); - - xdg_app_permission_store_complete_lookup (object, invocation, - permissions, - g_variant_new_variant (data)); - - return TRUE; -} - -static void -emit_deleted (XdgAppPermissionStore *object, - const gchar *table_name, - const gchar *id, - FlatpakDbEntry *entry) -{ - g_autoptr(GVariant) data = NULL; - g_autoptr(GVariant) permissions = NULL; - - data = flatpak_db_entry_get_data (entry); - permissions = g_variant_ref_sink (g_variant_new_array (G_VARIANT_TYPE ("{sas}"), NULL, 0)); - - xdg_app_permission_store_emit_changed (object, - table_name, id, - TRUE, - g_variant_new_variant (data), - permissions); -} - - -static void -emit_changed (XdgAppPermissionStore *object, - const gchar *table_name, - const gchar *id, - FlatpakDbEntry *entry) -{ - g_autoptr(GVariant) data = NULL; - g_autoptr(GVariant) permissions = NULL; - - data = flatpak_db_entry_get_data (entry); - permissions = get_app_permissions (entry); - - xdg_app_permission_store_emit_changed (object, - table_name, id, - FALSE, - g_variant_new_variant (data), - permissions); -} - -static gboolean -handle_delete (XdgAppPermissionStore *object, - GDBusMethodInvocation *invocation, - const gchar *table_name, - const gchar *id) -{ - Table *table; - - g_autoptr(FlatpakDbEntry) entry = NULL; - - table = lookup_table (table_name, invocation); - if (table == NULL) - return TRUE; - - entry = flatpak_db_lookup (table->db, id); - if (entry == NULL) - { - g_dbus_method_invocation_return_error (invocation, - FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, - "No entry for %s", id); - return TRUE; - } - - flatpak_db_set_entry (table->db, id, NULL); - emit_deleted (object, table_name, id, entry); - - ensure_writeout (table, invocation); - - return TRUE; -} - -static gboolean -handle_set (XdgAppPermissionStore *object, - GDBusMethodInvocation *invocation, - const gchar *table_name, - gboolean create, - const gchar *id, - GVariant *app_permissions, - GVariant *data) -{ - Table *table; - GVariantIter iter; - GVariant *child; - - g_autoptr(GVariant) data_child = NULL; - g_autoptr(FlatpakDbEntry) old_entry = NULL; - g_autoptr(FlatpakDbEntry) new_entry = NULL; - - table = lookup_table (table_name, invocation); - if (table == NULL) - return TRUE; - - old_entry = flatpak_db_lookup (table->db, id); - if (old_entry == NULL && !create) - { - g_dbus_method_invocation_return_error (invocation, - FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, - "Id %s not found", id); - return TRUE; - } - - data_child = g_variant_get_child_value (data, 0); - new_entry = flatpak_db_entry_new (data_child); - - /* Add all the given app permissions */ - - g_variant_iter_init (&iter, app_permissions); - while ((child = g_variant_iter_next_value (&iter))) - { - g_autoptr(FlatpakDbEntry) old_entry; - const char *child_app_id; - g_autofree const char **permissions; - - g_variant_get (child, "{&s^a&s}", &child_app_id, &permissions); - - old_entry = new_entry; - new_entry = flatpak_db_entry_set_app_permissions (new_entry, child_app_id, (const char **) permissions); - - g_variant_unref (child); - } - - flatpak_db_set_entry (table->db, id, new_entry); - emit_changed (object, table_name, id, new_entry); - - ensure_writeout (table, invocation); - - return TRUE; -} - -static gboolean -handle_set_permission (XdgAppPermissionStore *object, - GDBusMethodInvocation *invocation, - const gchar *table_name, - gboolean create, - const gchar *id, - const gchar *app, - const gchar *const *permissions) -{ - Table *table; - - g_autoptr(FlatpakDbEntry) entry = NULL; - g_autoptr(FlatpakDbEntry) new_entry = NULL; - - table = lookup_table (table_name, invocation); - if (table == NULL) - return TRUE; - - entry = flatpak_db_lookup (table->db, id); - if (entry == NULL) - { - if (create) - { - entry = flatpak_db_entry_new (NULL); - } - else - { - g_dbus_method_invocation_return_error (invocation, - FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, - "Id %s not found", id); - return TRUE; - } - } - - new_entry = flatpak_db_entry_set_app_permissions (entry, app, (const char **) permissions); - flatpak_db_set_entry (table->db, id, new_entry); - emit_changed (object, table_name, id, new_entry); - - ensure_writeout (table, invocation); - - return TRUE; -} - -static gboolean -handle_set_value (XdgAppPermissionStore *object, - GDBusMethodInvocation *invocation, - const gchar *table_name, - gboolean create, - const gchar *id, - GVariant *data) -{ - Table *table; - - g_autoptr(FlatpakDbEntry) entry = NULL; - g_autoptr(FlatpakDbEntry) new_entry = NULL; - - table = lookup_table (table_name, invocation); - if (table == NULL) - return TRUE; - - entry = flatpak_db_lookup (table->db, id); - if (entry == NULL) - { - if (create) - { - new_entry = flatpak_db_entry_new (data); - } - else - { - g_dbus_method_invocation_return_error (invocation, - FLATPAK_PORTAL_ERROR, FLATPAK_PORTAL_ERROR_NOT_FOUND, - "Id %s not found", id); - return TRUE; - } - } - else - { - new_entry = flatpak_db_entry_modify_data (entry, data); - } - - flatpak_db_set_entry (table->db, id, new_entry); - emit_changed (object, table_name, id, new_entry); - - ensure_writeout (table, invocation); - - return TRUE; -} - -void -flatpak_permission_store_start (GDBusConnection *connection) -{ - XdgAppPermissionStore *store; - GError *error = NULL; - - tables = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, (GDestroyNotify) table_free); - - store = xdg_app_permission_store_skeleton_new (); - - g_signal_connect (store, "handle-list", G_CALLBACK (handle_list), NULL); - g_signal_connect (store, "handle-lookup", G_CALLBACK (handle_lookup), NULL); - g_signal_connect (store, "handle-set", G_CALLBACK (handle_set), NULL); - g_signal_connect (store, "handle-set-permission", G_CALLBACK (handle_set_permission), NULL); - g_signal_connect (store, "handle-set-value", G_CALLBACK (handle_set_value), NULL); - g_signal_connect (store, "handle-delete", G_CALLBACK (handle_delete), NULL); - - if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (store), - connection, - "/org/freedesktop/Flatpak/PermissionStore", - &error)) - { - g_warning ("error: %s\n", error->message); - g_error_free (error); - } -} diff --git a/session-helper/xdg-app-permission-store.h b/session-helper/xdg-app-permission-store.h deleted file mode 100644 index eb4f32e..0000000 --- a/session-helper/xdg-app-permission-store.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright © 2015 Red Hat, Inc - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - * - * Authors: - * Alexander Larsson - */ - -#ifndef __FLATPAK_PERMISSION_STORE_H__ -#define __FLATPAK_PERMISSION_STORE_H__ - -#include "xdg-app-dbus.h" - -void flatpak_permission_store_start (GDBusConnection *connection); - -#endif /* __FLATPAK_PERMISSION_STORE_H__ */ diff --git a/session-helper/xdg-app-session-helper.c b/session-helper/xdg-app-session-helper.c deleted file mode 100644 index 1b3682c..0000000 --- a/session-helper/xdg-app-session-helper.c +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright © 2014 Red Hat, Inc - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - * - * Authors: - * Alexander Larsson - */ - -#include "config.h" - -#include -#include -#include -#include -#include "xdg-app-dbus.h" -#include "xdg-app-permission-store.h" - -static char *monitor_dir; - -static gboolean -handle_request_monitor (XdgAppSessionHelper *object, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - xdg_app_session_helper_complete_request_monitor (object, invocation, - monitor_dir); - - return TRUE; -} - -static void -on_bus_acquired (GDBusConnection *connection, - const gchar *name, - gpointer user_data) -{ - XdgAppSessionHelper *helper; - GError *error = NULL; - - flatpak_permission_store_start (connection); - - helper = xdg_app_session_helper_skeleton_new (); - - g_signal_connect (helper, "handle-request-monitor", G_CALLBACK (handle_request_monitor), NULL); - - if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (helper), - connection, - "/org/freedesktop/Flatpak/SessionHelper", - &error)) - { - g_warning ("error: %s\n", error->message); - g_error_free (error); - } -} - -static void -on_name_acquired (GDBusConnection *connection, - const gchar *name, - gpointer user_data) -{ -} - -static void -on_name_lost (GDBusConnection *connection, - const gchar *name, - gpointer user_data) -{ - exit (1); -} - -static void -copy_file (const char *source, - const char *target_dir) -{ - char *basename = g_path_get_basename (source); - char *dest = g_build_filename (target_dir, basename, NULL); - gchar *contents = NULL; - gsize len; - - if (g_file_get_contents (source, &contents, &len, NULL)) - g_file_set_contents (dest, contents, len, NULL); - - g_free (basename); - g_free (dest); - g_free (contents); -} - -static void -file_changed (GFileMonitor *monitor, - GFile *file, - GFile *other_file, - GFileMonitorEvent event_type, - char *source) -{ - if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT || - event_type == G_FILE_MONITOR_EVENT_CREATED) - copy_file (source, monitor_dir); -} - -static void -setup_file_monitor (const char *source) -{ - GFile *s = g_file_new_for_path (source); - GFileMonitor *monitor; - - copy_file (source, monitor_dir); - - monitor = g_file_monitor_file (s, G_FILE_MONITOR_NONE, NULL, NULL); - if (monitor) - g_signal_connect (monitor, "changed", G_CALLBACK (file_changed), (char *) source); -} - -int -main (int argc, - char **argv) -{ - guint owner_id; - GMainLoop *loop; - - setlocale (LC_ALL, ""); - - g_setenv ("GIO_USE_VFS", "local", TRUE); - - g_set_prgname (argv[0]); - - monitor_dir = g_build_filename (g_get_user_runtime_dir (), "xdg-app-monitor", NULL); - if (g_mkdir_with_parents (monitor_dir, 0755) != 0) - { - g_print ("Can't create %s\n", monitor_dir); - exit (1); - } - - setup_file_monitor ("/etc/resolv.conf"); - setup_file_monitor ("/etc/localtime"); - - owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, - "org.freedesktop.Flatpak", - G_BUS_NAME_OWNER_FLAGS_NONE, - on_bus_acquired, - on_name_acquired, - on_name_lost, - NULL, - NULL); - - loop = g_main_loop_new (NULL, FALSE); - g_main_loop_run (loop); - - g_bus_unown_name (owner_id); - - return 0; -} -- cgit v1.2.1