Exemple #1
0
static PolkitCheckAuthorizationFlags
lookup_invocation_flags (GDBusMethodInvocation *invocation,
                         const GDBusMethodInfo *info)
{
  gboolean auth_no_user_interaction;
  GVariant *params;
  GVariant *options;
  gint i;

  auth_no_user_interaction = FALSE;

  /* Find an options, a{sv} */
  if (info->in_args)
    {
      for (i = 0; info->in_args[i] != NULL; i++)
        {
          if (g_str_equal (info->in_args[i]->name, "options") &&
              g_str_equal (info->in_args[i]->signature, "a{sv}"))
            {
              params = g_dbus_method_invocation_get_parameters (invocation);
              g_variant_get_child (params, i, "@a{sv}", &options);
              g_variant_lookup (options, "auth.no_user_interaction", "b",
                                &auth_no_user_interaction);
              g_variant_unref (options);
            }
        }
    }

  return auth_no_user_interaction ?
      POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE :
      POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION;
}
static void
open_content_chooser (GDBusMethodInvocation *invocation,
                      const char            *app_id,
                      gboolean               create)
{
  GSubprocess *subprocess;
  g_autoptr(GError) error = NULL;
  g_autoptr(GPtrArray) args = NULL;
  ContentChooserData *data;
  GVariant *parameters;
  const char **types = NULL;
  const char *title = NULL;
  int i;

  parameters = g_dbus_method_invocation_get_parameters (invocation);
  if (create)
    g_variant_get (parameters, "(&s)", &title);
  else
    g_variant_get (parameters, "(^a&s)", &types);

  args = g_ptr_array_new ();
  g_ptr_array_add (args, LIBEXECDIR "/xdg-content-chooser");
  g_ptr_array_add (args, "--action");
  g_ptr_array_add (args, create ? "create" : "open");
  if (app_id && app_id[0])
    {
      g_ptr_array_add (args, "--caller");
      g_ptr_array_add (args, (gpointer)app_id);
    }
  if (title && title[0])
    {
      g_ptr_array_add (args, "--title");
      g_ptr_array_add (args, (gpointer)title);
    }
  if (types != NULL)
    {
      for (i = 0; types[i]; i++)
        g_ptr_array_add (args, (gpointer)types[i]);
    }
  g_ptr_array_add (args, NULL);

  subprocess = g_subprocess_newv ((const char **)args->pdata, G_SUBPROCESS_FLAGS_STDOUT_PIPE, &error);
  if (subprocess == NULL)
    {
      g_dbus_method_invocation_return_error (invocation,
                                             XDP_ERROR, XDP_ERROR_FAILED,
                                             "Failed to start content chooser: %s", error->message);
      return;
    }

  data = g_new0 (ContentChooserData, 1);
  data->invocation = invocation;
  data->app_id = app_id;
  data->create = create;

  g_subprocess_communicate_async (subprocess, NULL, NULL, content_chooser_done, data);
}
Exemple #3
0
static void
got_app_id_cb (GObject *source_object,
               GAsyncResult *res,
               gpointer user_data)
{
  GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (source_object);
  g_autoptr(GError) error = NULL;
  g_autofree char *app_id = NULL;
  PortalMethod portal_method = user_data;

  app_id = xdg_app_invocation_lookup_app_id_finish (invocation, res, &error);

  if (app_id == NULL)
    g_dbus_method_invocation_return_gerror (invocation, error);
  else
    portal_method (invocation, g_dbus_method_invocation_get_parameters (invocation), app_id);
}
static gboolean
flatpak_authorize_method_handler (GDBusInterfaceSkeleton *interface,
                                  GDBusMethodInvocation  *invocation,
                                  gpointer                user_data)
{
  const gchar *method_name = g_dbus_method_invocation_get_method_name (invocation);
  const gchar *sender = g_dbus_method_invocation_get_sender (invocation);
  GVariant *parameters = g_dbus_method_invocation_get_parameters (invocation);
  g_autoptr(AutoPolkitSubject) subject = polkit_system_bus_name_new (sender);
  g_autoptr(AutoPolkitDetails) details = polkit_details_new ();
  const gchar *action = NULL;
  gboolean authorized = FALSE;

  /* Ensure we don't idle exit */
  schedule_idle_callback ();

  if (on_session_bus)
    {
      /* This is test code, make sure it never runs with privileges */
      g_assert (geteuid () != 0);
      g_assert (getuid () != 0);
      g_assert (getegid () != 0);
      g_assert (getgid () != 0);
      authorized = TRUE;
    }
  else if (g_strcmp0 (method_name, "Deploy") == 0)
    {
      const char *ref, *origin;
      guint32 flags;
      gboolean is_update, is_app;

      g_variant_get_child (parameters, 1, "u", &flags);
      g_variant_get_child (parameters, 2, "&s", &ref);
      g_variant_get_child (parameters, 3, "&s", &origin);

      is_update = (flags & FLATPAK_HELPER_DEPLOY_FLAGS_UPDATE) != 0;
      is_app = g_str_has_prefix (ref, "app/");

      if (is_update)
        {
          if (is_app)
            action = "org.freedesktop.Flatpak.app-update";
          else
            action = "org.freedesktop.Flatpak.runtime-update";
        }
      else
        {
          if (is_app)
            action = "org.freedesktop.Flatpak.app-install";
          else
            action = "org.freedesktop.Flatpak.runtime-install";
        }

      polkit_details_insert (details, "origin", origin);
      polkit_details_insert (details, "ref", ref);
    }
  else if (g_strcmp0 (method_name, "DeployAppstream") == 0)
    {
      const char *arch, *origin;

      g_variant_get_child (parameters, 1, "&s", &origin);
      g_variant_get_child (parameters, 2, "&s", &arch);

      action = "org.freedesktop.Flatpak.appstream-update";

      polkit_details_insert (details, "origin", origin);
      polkit_details_insert (details, "arch", arch);
    }
  else if (g_strcmp0 (method_name, "InstallBundle") == 0)
    {
      const char *path;

      g_variant_get_child (parameters, 0, "^&ay", &path);

      action = "org.freedesktop.Flatpak.install-bundle";

      polkit_details_insert (details, "path", path);
    }
  else if (g_strcmp0 (method_name, "Uninstall") == 0)
    {
      const char *ref;
      gboolean is_app;

      g_variant_get_child (parameters, 1, "&s", &ref);

      is_app = g_str_has_prefix (ref, "app/");
      if (is_app)
        action = "org.freedesktop.Flatpak.app-uninstall";
      else
        action = "org.freedesktop.Flatpak.runtime-uninstall";

      polkit_details_insert (details, "ref", ref);
    }
  else if (g_strcmp0 (method_name, "ConfigureRemote") == 0)
    {
      const char *remote;

      g_variant_get_child (parameters, 1, "&s", &remote);

      action = "org.freedesktop.Flatpak.configure-remote";

      polkit_details_insert (details, "remote", remote);
    }

  if (action)
    {
      g_autoptr(AutoPolkitAuthorizationResult) result;
      g_autoptr(GError) error = NULL;

      result = polkit_authority_check_authorization_sync (authority, subject,
                                                          action, details,
                                                          POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
                                                          NULL, &error);
      if (result == NULL)
        {
          g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
                                                 "Authorization error: %s", error->message);
          return FALSE;
        }

      authorized = polkit_authorization_result_get_is_authorized (result);
    }

  if (!authorized)
    {
      g_dbus_method_invocation_return_error (invocation,
                                             G_DBUS_ERROR,
                                             G_DBUS_ERROR_ACCESS_DENIED,
                                             "Flatpak system operation %s not allowed for user", method_name);
    }

  return authorized;
}