Esempio n. 1
0
static void
_twitter_query_open_view (SwQueryIface          *self,
                          const gchar           *query,
                          GHashTable            *params,
                          DBusGMethodInvocation *context)
{
  SwServiceTwitterPrivate *priv = GET_PRIVATE (self);
  const gchar *object_path;

  if (!_check_query_validity (query, valid_queries))
  {
    dbus_g_method_return_error (context,
                                g_error_new (SW_SERVICE_ERROR,
                                             SW_SERVICE_ERROR_INVALID_QUERY,
                                             "Query '%s' is invalid",
                                             query));
    return;
  }

  if (g_str_equal (query, "x-twitter-stream"))
  {
    SwItemStream *item_stream;
    item_stream = g_object_new (SW_TYPE_TWITTER_ITEM_STREAM,
                                "proxy", priv->proxy,
                                "service", self,
                                "query", query,
                                "params", params,
                                NULL);
    object_path = sw_item_stream_get_object_path (item_stream);
    /* Ensure the object gets disposed when the client goes away */
    sw_client_monitor_add (dbus_g_method_get_sender (context),
                         (GObject *)item_stream);
  } else {
    SwItemView *item_view;
    item_view = g_object_new (SW_TYPE_TWITTER_ITEM_VIEW,
                              "proxy", priv->proxy,
                              "service", self,
                              "query", query,
                              "params", params,
                              NULL);
    object_path = sw_item_view_get_object_path (item_view);
    /* Ensure the object gets disposed when the client goes away */
    sw_client_monitor_add (dbus_g_method_get_sender (context),
                          (GObject *)item_view);
  }

  sw_query_iface_return_from_open_view (context,
                                        object_path);
}
static gboolean
_fprint_device_check_claimed (FprintDevice *rdev,
			      DBusGMethodInvocation *context,
			      GError **error)
{
	FprintDevicePrivate *priv = DEVICE_GET_PRIVATE(rdev);
	char *sender;
	gboolean retval;

	/* The device wasn't claimed, exit */
	if (priv->sender == NULL) {
		g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_CLAIM_DEVICE,
			     _("Device was not claimed before use"));
		return FALSE;
	}

	sender = dbus_g_method_get_sender (context);
	retval = g_str_equal (sender, priv->sender);
	g_free (sender);

	if (retval == FALSE) {
		g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
			     _("Device already in use by another user"));
	}

	return retval;
}
Esempio n. 3
0
static void
_myspace_query_open_view (SwQueryIface          *self,
                          const gchar           *query,
                          GHashTable            *params,
                          DBusGMethodInvocation *context)
{
  SwServiceMySpacePrivate *priv = GET_PRIVATE (self);
  SwItemView *item_view;
  const gchar *object_path;

  if (!_check_query_validity (query))
  {
    dbus_g_method_return_error (context,
                                g_error_new (SW_SERVICE_ERROR,
                                             SW_SERVICE_ERROR_INVALID_QUERY,
                                             "Query '%s' is invalid",
                                             query));
    return;
  }

  item_view = g_object_new (SW_TYPE_MYSPACE_ITEM_VIEW,
                            "proxy", priv->proxy,
                            "service", self,
                            "query", query,
                            "params", params,
                            NULL);

  object_path = sw_item_view_get_object_path (item_view);
  /* Ensure the object gets disposed when the client goes away */
  sw_client_monitor_add (dbus_g_method_get_sender (context),
                         (GObject *)item_view);
  sw_query_iface_return_from_open_view (context,
                                        object_path);
}
Esempio n. 4
0
static void
check_polkit_for_actions (GConfDefaults                   *mechanism,
                          DBusGMethodInvocation           *context,
                          gchar                          **actions,
                          AuthObtainedCallback             auth_obtained_callback,
                          gpointer                         user_data,
			  GDestroyNotify                   destroy)
{
        CheckAuthData *data;

	data = g_new0 (CheckAuthData, 1);
	data->mechanism = g_object_ref (mechanism);
	data->context = context;
	data->actions = actions;
        data->flags = POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION;
	data->id = 0;
	data->auth_obtained_callback = auth_obtained_callback;
	data->check_auth_callback = (GAsyncReadyCallback)check_authorization_callback;
	data->user_data = user_data;
	data->destroy = destroy;
	data->subject = polkit_system_bus_name_new (dbus_g_method_get_sender (context));
	data->challenge = FALSE;

	check_next_action (data);
}
Esempio n. 5
0
static char *
gconf_address_for_caller (GConfDefaults          *mechanism,
			  DBusGMethodInvocation  *context,
			  GError                **gerror)
{
        char *sender;
	DBusConnection *conn;
	uid_t uid;
	struct passwd *pwd;
	char *result;
	DBusError error;

	conn = dbus_g_connection_get_connection (mechanism->priv->system_bus_connection);
        sender = dbus_g_method_get_sender (context);

	dbus_error_init (&error);
	uid = dbus_bus_get_unix_user (conn, sender, &error);
	g_free (sender);
	if (uid == (unsigned)-1) {
		dbus_set_g_error (gerror, &error);
		dbus_error_free (&error);
		return NULL;
	}

	pwd = getpwuid (uid);
	if (pwd == NULL) {
		g_set_error (gerror,
			     0, 0,
			     "Failed to get passwd information for uid %d", uid);
		return NULL;
	}

	result = g_strconcat ("xml:merged:", pwd->pw_dir, "/.gconf", NULL);
	return result;
}
void
_odccm_connection_broker_take_connection (OdccmConnectionBroker *self,
                                          GConn *conn)
{
  OdccmConnectionBrokerPrivate *priv = ODCCM_CONNECTION_BROKER_GET_PRIVATE (self);
  GRand *rnd;
  GIOChannel *chan;
  guint uid;

  g_assert (priv->conn == NULL);

  priv->conn = conn;

  rnd = g_rand_new ();
  priv->filename = g_strdup_printf ("/tmp/odccm-%08x%08x%08x%08x.sock",
      g_rand_int (rnd), g_rand_int (rnd), g_rand_int (rnd), g_rand_int (rnd));
  g_rand_free (rnd);

  priv->server = gnet_unix_socket_server_new (priv->filename);

  _odccm_get_dbus_sender_uid (dbus_g_method_get_sender (priv->ctx), &uid);

  chmod (priv->filename, S_IRUSR | S_IWUSR);
  chown (priv->filename, uid, -1);

  chan = gnet_unix_socket_get_io_channel (priv->server);
  g_io_add_watch (chan, G_IO_IN, server_socket_readable_cb, self);

  dbus_g_method_return (priv->ctx, priv->filename);
  priv->ctx = NULL;
}
Esempio n. 7
0
static void
_twitter_contacts_query_open_view (SwContactsQueryIface  *self,
                                   const gchar           *query,
                                   GHashTable            *params,
                                   DBusGMethodInvocation *context)
{
  SwServiceTwitterPrivate *priv = GET_PRIVATE (self);
  const gchar *object_path;
  SwContactView *contact_view;

  if (!_check_query_validity (query, valid_contact_queries))
  {
    dbus_g_method_return_error (context,
                                g_error_new (SW_SERVICE_ERROR,
                                             SW_SERVICE_ERROR_INVALID_QUERY,
                                             "Query '%s' is invalid",
                                             query));
    return;
  }

  contact_view = g_object_new (SW_TYPE_TWITTER_CONTACT_VIEW,
                               "proxy", priv->proxy,
                               "service", self,
                               "query", query,
                               "params", params,
                               NULL);
  object_path = sw_contact_view_get_object_path (contact_view);
  /* Ensure the object gets disposed when the client goes away */
  sw_client_monitor_add (dbus_g_method_get_sender (context),
                        (GObject *)contact_view);

  sw_contacts_query_iface_return_from_open_view (context,
      object_path);
}
Esempio n. 8
0
static void
check_permissions_only (GConfDefaults                   *mechanism,
                        DBusGMethodInvocation           *context,
                        gchar                          **actions,
                        AuthObtainedCallback             auth_obtained_callback,
                        gpointer                         user_data,
 	 		GDestroyNotify                   destroy)
{
        CheckAuthData *data;

	data = g_new0 (CheckAuthData, 1);
	data->mechanism = g_object_ref (mechanism);
	data->context = context;
	data->actions = actions;
	data->flags = 0;
	data->id = 0;
	data->check_auth_callback = (GAsyncReadyCallback)check_authorization_only_callback;
	data->auth_obtained_callback = NULL;
	data->user_data = NULL;
	data->destroy = NULL;
	data->subject = polkit_system_bus_name_new (dbus_g_method_get_sender (context));
	data->challenge = FALSE;

	check_next_action (data);
}
static char *
_fprint_device_check_for_username (FprintDevice *rdev,
				   DBusGMethodInvocation *context,
				   const char *username,
				   char **ret_sender,
				   GError **error)
{
	DBusConnection *conn;
	DBusError dbus_error;
	char *sender;
	unsigned long uid;
	struct passwd *user;
	char *client_username;

	/* Get details about the current sender, and username/uid */
	conn = dbus_g_connection_get_connection (fprintd_dbus_conn);
	sender = dbus_g_method_get_sender (context);
	dbus_error_init (&dbus_error);
	uid = dbus_bus_get_unix_user (conn, sender, &dbus_error);

	if (dbus_error_is_set(&dbus_error)) {
		g_free (sender);
		dbus_set_g_error (error, &dbus_error);
		return NULL;
	}

	user = getpwuid (uid);
	if (user == NULL) {
		g_free (sender);
		g_set_error(error, FPRINT_ERROR, FPRINT_ERROR_INTERNAL,
			    "Failed to get information about user UID %lu", uid);
		return NULL;
	}
	client_username = g_strdup (user->pw_name);

	/* The current user is usually allowed to access their
	 * own data, this should be followed by PolicyKit checks
	 * anyway */
	if (username == NULL || *username == '\0' || g_str_equal (username, client_username)) {
		if (ret_sender != NULL)
			*ret_sender = sender;
		else
			g_free (sender);
		return client_username;
	}

	/* If we're not allowed to set a different username,
	 * then fail */
	if (_fprint_device_check_polkit_for_action (rdev, context, "net.reactivated.fprint.device.setusername", error) == FALSE) {
		g_free (sender);
		return NULL;
	}

	if (ret_sender != NULL)
		*ret_sender = sender;
	else
		g_free (sender);

	return g_strdup (username);
}
Esempio n. 10
0
/* IN_args contains that path to the GPS device we wish to open */
static void
gypsy_server_create (GypsyServer           *gps,
		     const char            *IN_device_path,
		     DBusGMethodInvocation *context)
{
	GypsyServerPrivate *priv;
	GypsyClient *client;
	char *path, *device_name, *sender;
	GList *list;

	priv = GET_PRIVATE (gps);

	/* We might be in the termination timeout when we receive a new
	   create request, so cancel that timeout */
	if (priv->terminate_id > 0) {
		g_source_remove (priv->terminate_id);
		priv->terminate_id = 0;
	}

	g_debug ("Creating client for %s", IN_device_path);
	device_name = g_path_get_basename (IN_device_path);
	g_debug ("Device name: %s", device_name);
	path = g_strdup_printf ("%s%s", GYPSY_GPS_PATH, 
				g_strdelimit (device_name, ":", '_'));
	g_free (device_name);

	client = (GypsyClient *) dbus_g_connection_lookup_g_object (priv->connection, path);
	if (client == NULL) {
		/* If there isn't already an object registered on that path
		   create and register it */
		client = g_object_new (GYPSY_TYPE_CLIENT, 
				       "device_path", IN_device_path,
				       NULL);
	
		dbus_g_connection_register_g_object (priv->connection, path,
						     G_OBJECT (client));
	} else {
		/* Ref the client so that when one client calls shutdown
		   we won't destroy another clients object */
		g_object_ref (client);
	}

	g_debug ("Registered client on %s", path);

	/* Update the hash of open connnctions */
	sender = dbus_g_method_get_sender (context);
	list = g_hash_table_lookup (priv->connections, sender);
	list = g_list_prepend (list, client);
	g_hash_table_insert (priv->connections, sender, list);

	priv->client_count++;

	dbus_g_method_return (context, path);
	g_free (path);
}
Esempio n. 11
0
/**
 * gpk_dbus_create_task:
 **/
static GpkDbusTask *
gpk_dbus_create_task (GpkDbus *dbus, guint32 xid, const gchar *interaction, DBusGMethodInvocation *context)
{
	GpkDbusTask *task;
	PkBitfield interact = 0;
	gint timeout = 0;
	gchar *sender;
	gchar *exec;
	guint timestamp = 0;
	gboolean ret;

	task = gpk_dbus_task_new ();

	/* work out what interaction the task should use */
	gpk_dbus_parse_interaction (dbus, interaction, &interact, &timeout);

	/* set interaction mode */
	g_debug ("interact=%i", (gint) interact);
	gpk_dbus_task_set_interaction (task, interact);

	/* try to get the user time of the window */
	if (xid != 0) {
		ret = gpk_x11_set_xid (dbus->priv->x11, xid);
		if (ret)
			timestamp = gpk_x11_get_user_time (dbus->priv->x11);
	}

	/* set the context for the return values */
	gpk_dbus_task_set_context (task, context);

	/* set the last interaction */
	gpk_dbus_task_set_timestamp (task, timestamp);

	/* set the window for the modal and timestamp */
        if (xid != 0)
        	gpk_dbus_task_set_xid (task, xid);

	/* get the program name and set */
	sender = dbus_g_method_get_sender (context);
	exec = gpk_dbus_get_exec_for_sender (dbus, sender);
	if (exec != NULL)
		gpk_dbus_task_set_exec (task, exec);

	/* unref on delete */
	//g_signal_connect...

	/* reset time */
	g_timer_reset (dbus->priv->timer);
	dbus->priv->refcount++;

	g_free (sender);
	g_free (exec);
	return task;
}
Esempio n. 12
0
static gboolean
check_polkit_for_action (GConfDefaults         *mechanism,
                         DBusGMethodInvocation *context,
                         const char            *action)
{
	const char *sender;
	GError *error;
	DBusError dbus_error;
	PolKitCaller *pk_caller;
	PolKitAction *pk_action;
	PolKitResult pk_result;

	error = NULL;

	/* Check that caller is privileged */
	sender = dbus_g_method_get_sender (context);
	dbus_error_init (&dbus_error);
	pk_caller = polkit_caller_new_from_dbus_name (
	dbus_g_connection_get_connection (mechanism->priv->system_bus_connection),
					  sender,
					  &dbus_error);
	if (pk_caller == NULL) {
		error = g_error_new (GCONF_DEFAULTS_ERROR,
				     GCONF_DEFAULTS_ERROR_GENERAL,
				     "Error getting information about caller: %s: %s",
				     dbus_error.name, dbus_error.message);
		dbus_error_free (&dbus_error);
		dbus_g_method_return_error (context, error);
		g_error_free (error);
		return FALSE;
	}

	pk_action = polkit_action_new ();
	polkit_action_set_action_id (pk_action, action);
	pk_result = polkit_context_is_caller_authorized (mechanism->priv->pol_ctx, pk_action, pk_caller, TRUE, NULL);
	polkit_caller_unref (pk_caller);

	if (pk_result != POLKIT_RESULT_YES) {
		dbus_error_init (&dbus_error);
		polkit_dbus_error_generate (pk_action, pk_result, &dbus_error);
		dbus_set_g_error (&error, &dbus_error);
		dbus_g_method_return_error (context, error);
		dbus_error_free (&dbus_error);
		g_error_free (error);
		polkit_action_unref (pk_action);
		return FALSE;
	}

	polkit_action_unref (pk_action);
	return TRUE;
}
Esempio n. 13
0
static void
gypsy_server_shutdown (GypsyServer           *gps,
		       const char            *IN_device_path,
		       DBusGMethodInvocation *context)
{
	GypsyServerPrivate *priv;
	GypsyClient *client;
	GList *list, *owner;
	char *path, *device_name, *sender;

	priv = GET_PRIVATE (gps);

	g_debug ("Finding client for %s", IN_device_path);
	device_name = g_path_get_basename (IN_device_path);
	g_debug ("Device name: %s", device_name);
	path = g_strdup_printf ("%s%s", GYPSY_GPS_PATH, device_name);

	client = (GypsyClient *) dbus_g_connection_lookup_g_object (priv->connection, path);
	g_free (path);

	if (client == NULL) {
		dbus_g_method_return_error (context,
					    g_error_new (GYPSY_SERVER_ERROR,
							 GYPSY_SERVER_ERROR_NO_CLIENT,
							 "No such client: %s",
							 device_name));
	} else {
		if (--priv->client_count == 0) {
			if (priv->terminate_id == 0) {
				priv->terminate_id = g_timeout_add (TERMINATE_TIMEOUT,
								    gypsy_terminate,
								    gps);
			}
		}

		/* Update the hash of open connnctions */
		sender = dbus_g_method_get_sender (context);
		list = g_hash_table_lookup (priv->connections, sender);
		owner = g_list_find (list, client);
		if (owner) {
			g_object_unref (owner->data);
		}
		list = g_list_remove (list, client);
		g_hash_table_insert (priv->connections, sender, list);

		dbus_g_method_return (context);

	}

	g_free (device_name);
}
static gboolean
_check_polkit_for_action (GnomeClockAppletMechanism *mechanism, DBusGMethodInvocation *context, const char *action)
{
        const char *sender;
        GError *error;
        DBusError dbus_error;
        PolKitCaller *pk_caller;
        PolKitAction *pk_action;
        PolKitResult pk_result;

        error = NULL;

        /* Check that caller is privileged */
        sender = dbus_g_method_get_sender (context);
        dbus_error_init (&dbus_error);
        pk_caller = polkit_caller_new_from_dbus_name (
                dbus_g_connection_get_connection (mechanism->priv->system_bus_connection),
                sender, 
                &dbus_error);
        if (pk_caller == NULL) {
                error = g_error_new (GNOME_CLOCK_APPLET_MECHANISM_ERROR,
                                     GNOME_CLOCK_APPLET_MECHANISM_ERROR_GENERAL,
                                     "Error getting information about caller: %s: %s",
                                     dbus_error.name, dbus_error.message);
                dbus_error_free (&dbus_error);
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                return FALSE;
        }

        pk_action = polkit_action_new ();
        polkit_action_set_action_id (pk_action, action);
        pk_result = polkit_context_can_caller_do_action (mechanism->priv->pol_ctx, pk_action, pk_caller);
        polkit_caller_unref (pk_caller);
        polkit_action_unref (pk_action);

        if (pk_result != POLKIT_RESULT_YES) {
                error = g_error_new (GNOME_CLOCK_APPLET_MECHANISM_ERROR,
                                     GNOME_CLOCK_APPLET_MECHANISM_ERROR_NOT_PRIVILEGED,
                                     "%s %s <-- (action, result)",
                                     action,
                                     polkit_result_to_string_representation (pk_result));
                dbus_error_free (&dbus_error);
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                return FALSE;
        }

        return TRUE;
}
Esempio n. 15
0
static void 
remove_reference (GcIfaceGeoclue *geoclue,
                  DBusGMethodInvocation *context)
{
	GcProvider *provider = GC_PROVIDER (geoclue);
	char *sender;
	
	sender = dbus_g_method_get_sender (context);
	if (!gc_provider_remove_client (provider, sender)) {
		g_warning ("Unreffed by client that has not been referenced");
	}

	g_free (sender);
	
	dbus_g_method_return (context);
}
Esempio n. 16
0
gboolean
bm_auth_get_caller_uid (DBusGMethodInvocation *context,
                        BMDBusManager *dbus_mgr,
                        gulong *out_uid,
                        const char **out_error_desc)
{
	DBusConnection *connection;
	char *sender = NULL;
	gboolean success = FALSE;
	DBusError dbus_error;

	g_return_val_if_fail (context != NULL, FALSE);
	g_return_val_if_fail (dbus_mgr != NULL, FALSE);
	g_return_val_if_fail (out_uid != NULL, FALSE);

	*out_uid = G_MAXULONG;

	sender = dbus_g_method_get_sender (context);
	if (!sender) {
		if (out_error_desc)
			*out_error_desc = "Could not determine D-Bus requestor";
		goto out;
	}

	connection = bm_dbus_manager_get_dbus_connection (dbus_mgr);
	if (!connection) {
		if (out_error_desc)
			*out_error_desc = "Could not get the D-Bus system bus";
		goto out;
	}

	dbus_error_init (&dbus_error);
	/* FIXME: do this async */
	*out_uid = dbus_bus_get_unix_user (connection, sender, &dbus_error);
	if (dbus_error_is_set (&dbus_error)) {
		if (out_error_desc)
			*out_error_desc = "Could not determine the user ID of the requestor";
		dbus_error_free (&dbus_error);
		*out_uid = G_MAXULONG;
	} else
		success = TRUE;

out:
	g_free (sender);
	return success;
}
gboolean
cpufreq_selector_service_can_set (CPUFreqSelectorService *service,
				  DBusGMethodInvocation  *context)
{
	PolkitSubject             *subject;
	PolkitAuthorizationResult *result;
	gchar                     *sender;
	gboolean                   ret;
        GError                    *error = NULL;

	reset_killtimer ();

	sender = dbus_g_method_get_sender (context);
	subject = polkit_system_bus_name_new (sender);
	g_free (sender);

	result = polkit_authority_check_authorization_sync (service->authority,
                                                            subject,
                                                            "org.consort.cpufreqselector",
                                                            NULL,
                                                            0,
                                                            NULL,
							    &error);
        g_object_unref (subject);

	if (error) {
		dbus_g_method_return_error (context, error);
		g_error_free (error);

		return FALSE;
	}

        if (polkit_authorization_result_get_is_authorized (result)) {
		ret = TRUE;
	} else if (polkit_authorization_result_get_is_challenge (result)) {
		ret = TRUE;
	} else {
		ret = FALSE;
	}

	g_object_unref (result);

        dbus_g_method_return (context, ret);

	return TRUE;
}
static gboolean
_check_polkit_for_action (GsdDatetimeMechanism *mechanism, DBusGMethodInvocation *context)
{
        const char *action = "org.gnome.settingsdaemon.datetimemechanism.configure";
        const char *sender;
        GError *error;
        PolkitSubject *subject;
        PolkitAuthorizationResult *result;

        error = NULL;

        /* Check that caller is privileged */
        sender = dbus_g_method_get_sender (context);
        subject = polkit_system_bus_name_new (sender);

        result = polkit_authority_check_authorization_sync (mechanism->priv->auth,
                                                            subject,
                                                            action,
                                                            NULL,
                                                            POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
                                                            NULL, &error);
        g_object_unref (subject);

        if (error) {
                dbus_g_method_return_error (context, error);
                g_error_free (error);

                return FALSE;
        }

        if (!polkit_authorization_result_get_is_authorized (result)) {
                error = g_error_new (GSD_DATETIME_MECHANISM_ERROR,
                                     GSD_DATETIME_MECHANISM_ERROR_NOT_PRIVILEGED,
                                     "Not Authorized for action %s", action);
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                g_object_unref (result);

                return FALSE;
        }

        g_object_unref (result);

        return TRUE;
}
Esempio n. 19
0
static void
add_reference (GcIfaceGeoclue *geoclue,
               DBusGMethodInvocation *context)
{
	GcProviderPrivate *priv = GET_PRIVATE (geoclue);
	char *sender;
	int *pcount;
	
	/* Update the hash of open connections */
	sender = dbus_g_method_get_sender (context);
	pcount = g_hash_table_lookup (priv->connections, sender);
	if (!pcount) {
		pcount = g_malloc0 (sizeof (int));
		g_hash_table_insert (priv->connections, sender, pcount);
	}
	(*pcount)++;
	
	dbus_g_method_return (context);
}
static void
check_can_do (GsdDatetimeMechanism  *mechanism,
              const char            *action,
              DBusGMethodInvocation *context)
{
        const char *sender;
        PolkitSubject *subject;
        PolkitAuthorizationResult *result;
        GError *error;

        /* Check that caller is privileged */
        sender = dbus_g_method_get_sender (context);
        subject = polkit_system_bus_name_new (sender);

        error = NULL;
        result = polkit_authority_check_authorization_sync (mechanism->priv->auth,
                                                            subject,
                                                            action,
                                                            NULL,
                                                            0,
                                                            NULL,
                                                            &error);
        g_object_unref (subject);

        if (error) {
                dbus_g_method_return_error (context, error);
                g_error_free (error);
                return;
        }

        if (polkit_authorization_result_get_is_authorized (result)) {
                dbus_g_method_return (context, 2);
        }
        else if (polkit_authorization_result_get_is_challenge (result)) {
                dbus_g_method_return (context, 1);
        }
        else {
                dbus_g_method_return (context, 0);
        }

        g_object_unref (result);
}
/* PolicyKit */
static gboolean
cpufreq_selector_service_check_policy (CPUFreqSelectorService *service,
				       DBusGMethodInvocation  *context,
				       GError                **error)
{
	PolkitSubject             *subject;
	PolkitAuthorizationResult *result;
	gchar                     *sender;
	gboolean                   ret;

	sender = dbus_g_method_get_sender (context);
	subject = polkit_system_bus_name_new (sender);
	g_free (sender);

	result = polkit_authority_check_authorization_sync (service->authority,
                                                            subject,
                                                            "org.consort.cpufreqselector",
                                                            NULL,
                                                            POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
                                                            NULL, error);
        g_object_unref (subject);

        if (*error) {
		g_warning ("Check policy: %s", (*error)->message);
		g_object_unref (result);

		return FALSE;
	}

	ret = polkit_authorization_result_get_is_authorized (result);
	if (!ret) {
		g_set_error (error,
			     CPUFREQ_SELECTOR_SERVICE_ERROR,
			     SERVICE_ERROR_NOT_AUTHORIZED,
			     "Caller is not authorized");
	}

	g_object_unref (result);

	return ret;
}
static gboolean
_fprint_device_check_polkit_for_action (FprintDevice *rdev, DBusGMethodInvocation *context, const char *action, GError **error)
{
	FprintDevicePrivate *priv = DEVICE_GET_PRIVATE(rdev);
	const char *sender;
	PolkitSubject *subject;
	PolkitAuthorizationResult *result;
	GError *_error = NULL;

	/* Check that caller is privileged */
	sender = dbus_g_method_get_sender (context);
	subject = polkit_system_bus_name_new (sender);

	result = polkit_authority_check_authorization_sync (priv->auth,
                                                            subject,
                                                            action,
							    NULL,
                                                            POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
					                    NULL, &_error);
	g_object_unref (subject);

	if (result == NULL) {
		g_set_error (error, FPRINT_ERROR,
			     FPRINT_ERROR_PERMISSION_DENIED,
			     "Not Authorized: %s", _error->message);
		g_error_free (_error);
		return FALSE;
	}

	if (!polkit_authorization_result_get_is_authorized (result)) {
		g_set_error (error, FPRINT_ERROR,
			     FPRINT_ERROR_PERMISSION_DENIED,
			     "Not Authorized: %s", action);
		g_object_unref (result);
		return FALSE;
	}

	g_object_unref (result);

	return TRUE;
}
GObject *
mm_auth_request_polkit_new (PolkitAuthority *authority,
                            const char *authorization,
                            GObject *owner,
                            DBusGMethodInvocation *context,
                            MMAuthRequestCb callback,
                            gpointer callback_data,
                            GDestroyNotify notify)
{
    GObject *obj;
    MMAuthRequestPolkitPrivate *priv;
    char *sender;

    g_return_val_if_fail (authorization != NULL, NULL);
    g_return_val_if_fail (owner != NULL, NULL);
    g_return_val_if_fail (callback != NULL, NULL);
    g_return_val_if_fail (context != NULL, NULL);

    obj = mm_auth_request_new (MM_TYPE_AUTH_REQUEST_POLKIT,
                               authorization,
                               owner,
                               context,
                               callback,
                               callback_data,
                               notify);
    if (obj) {
        priv = MM_AUTH_REQUEST_POLKIT_GET_PRIVATE (obj);
        priv->authority = authority;
        priv->cancellable = g_cancellable_new ();

        sender = dbus_g_method_get_sender (context);
        priv->subject = polkit_system_bus_name_new (sender);
    	g_free (sender);
    }

    return obj;
}
Esempio n. 24
0
static BMAuthChain *
_auth_chain_new (PolkitAuthority *authority,
                 DBusGMethodInvocation *context,
                 DBusGProxy *proxy,
                 DBusMessage *message,
                 BMAuthChainResultFunc done_func,
                 gpointer user_data)
{
	BMAuthChain *self;

	g_return_val_if_fail (context || proxy || message, NULL);

	self = g_malloc0 (sizeof (BMAuthChain));
	self->refcount = 1;
	self->authority = g_object_ref (authority);
	self->data = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_data);
	self->done_func = done_func;
	self->call_func = /* call_func ? call_func : */ default_call_func;
	self->user_data = user_data;
	self->context = context;

	if (proxy)
		self->owner = g_strdup (dbus_g_proxy_get_bus_name (proxy));
	else if (context)
		self->owner = dbus_g_method_get_sender (context);
	else if (message)
		self->owner = g_strdup (dbus_message_get_sender (message));

	if (!self->owner) {
		/* Need an owner */
		g_warn_if_fail (self->owner);
		bm_auth_chain_unref (self);
		self = NULL;
	}

	return self;
}
Esempio n. 25
0
gboolean
gsm_manager_inhibit (GsmManager            *manager,
                     const char            *app_id,
                     guint                  toplevel_xid,
                     const char            *reason,
                     guint                  flags,
                     DBusGMethodInvocation *context)
{
    GsmInhibitor *inhibitor;
    guint         cookie;

    g_return_val_if_fail (GSM_IS_MANAGER (manager), FALSE);

    g_debug ("GsmManager: Inhibit xid=%u app_id=%s reason=%s flags=%u",
             toplevel_xid,
             app_id,
             reason,
             flags);

    if (IS_STRING_EMPTY (app_id)) {
        GError *new_error;

        new_error = g_error_new (GSM_MANAGER_ERROR,
                                 GSM_MANAGER_ERROR_GENERAL,
                                 "Application ID not specified");
        g_debug ("GsmManager: Unable to inhibit: %s", new_error->message);
        dbus_g_method_return_error (context, new_error);
        g_error_free (new_error);
        return FALSE;
    }

    if (IS_STRING_EMPTY (reason)) {
        GError *new_error;

        new_error = g_error_new (GSM_MANAGER_ERROR,
                                 GSM_MANAGER_ERROR_GENERAL,
                                 "Reason not specified");
        g_debug ("GsmManager: Unable to inhibit: %s", new_error->message);
        dbus_g_method_return_error (context, new_error);
        g_error_free (new_error);
        return FALSE;
    }

    if (flags == 0) {
        GError *new_error;

        new_error = g_error_new (GSM_MANAGER_ERROR,
                                 GSM_MANAGER_ERROR_GENERAL,
                                 "Invalid inhibit flags");
        g_debug ("GsmManager: Unable to inhibit: %s", new_error->message);
        dbus_g_method_return_error (context, new_error);
        g_error_free (new_error);
        return FALSE;
    }

    cookie = _generate_unique_cookie (manager);
    inhibitor = gsm_inhibitor_new (app_id,
                                   toplevel_xid,
                                   flags,
                                   reason,
                                   dbus_g_method_get_sender (context),
                                   cookie);
    gsm_store_add (manager->priv->inhibitors, gsm_inhibitor_peek_id (inhibitor), G_OBJECT (inhibitor));
    g_object_unref (inhibitor);

    dbus_g_method_return (context, cookie);

    return TRUE;
}