auth: refactor and simplify authentication related setup

We get rid of the MMAuthRequests; and we leave up to the caller and user of the
MMAuthProvider the handling of request cancellations through the provided
GCancellable.

Also made GIO-async-friendly methods.
This commit is contained in:
Aleksander Morgado
2012-02-21 12:45:50 +01:00
parent 7316f4645b
commit 089a983850
11 changed files with 294 additions and 914 deletions

View File

@@ -118,21 +118,6 @@ if WITH_POLKIT
modem_manager_LDADD += $(POLKIT_LIBS)
endif
auth_sources = \
mm-auth-request.c \
mm-auth-request.h \
mm-auth-provider.h \
mm-auth-provider.c \
mm-auth-provider-factory.c
if WITH_POLKIT
auth_sources += \
mm-auth-request-polkit.c \
mm-auth-request-polkit.h \
mm-auth-provider-polkit.c \
mm-auth-provider-polkit.h
endif
modem_manager_SOURCES = \
main.c \
mm-log.c \
@@ -141,7 +126,10 @@ modem_manager_SOURCES = \
mm-daemon-enums-types.c \
mm-private-boxed-types.h \
mm-private-boxed-types.c \
$(auth_sources) \
mm-auth.h \
mm-auth.c \
mm-auth-provider.h \
mm-auth-provider.c \
mm-manager.c \
mm-manager.h \
mm-plugin-manager.c \
@@ -193,6 +181,12 @@ modem_manager_SOURCES = \
mm-plugin-base.c \
mm-plugin-base.h
if WITH_POLKIT
modem_manager_SOURCES += \
mm-auth-provider-polkit.c \
mm-auth-provider-polkit.h
endif
CLEANFILES = \
mm-private-enums-types.h \
mm-private-enums-types.c

View File

@@ -10,32 +10,28 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
* Copyright (C) 2010 - 2012 Red Hat, Inc.
* Copyright (C) 2012 Google, Inc.
*/
#include <polkit/polkit.h>
#include <config.h>
#include "mm-auth-request-polkit.h"
#include <libmm-common.h>
#include "mm-log.h"
#include "mm-auth-provider-polkit.h"
G_DEFINE_TYPE (MMAuthProviderPolkit, mm_auth_provider_polkit, MM_TYPE_AUTH_PROVIDER)
#define MM_AUTH_PROVIDER_POLKIT_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_AUTH_PROVIDER_POLKIT, MMAuthProviderPolkitPrivate))
typedef struct {
struct _MMAuthProviderPolkitPrivate {
PolkitAuthority *authority;
guint auth_changed_id;
} MMAuthProviderPolkitPrivate;
enum {
PROP_NAME = 1000,
};
/*****************************************************************************/
GObject *
MMAuthProvider *
mm_auth_provider_polkit_new (void)
{
return g_object_new (MM_TYPE_AUTH_PROVIDER_POLKIT, NULL);
@@ -43,112 +39,155 @@ mm_auth_provider_polkit_new (void)
/*****************************************************************************/
typedef struct {
MMAuthProvider *self;
GCancellable *cancellable;
PolkitSubject *subject;
gchar *authorization;
GDBusMethodInvocation *invocation;
GSimpleAsyncResult *result;
} AuthorizeContext;
static void
pk_authority_changed_cb (GObject *object, gpointer user_data)
authorize_context_complete_and_free (AuthorizeContext *ctx)
{
/* Let clients know they should re-check their authorization */
g_simple_async_result_complete (ctx->result);
g_object_unref (ctx->result);
if (ctx->cancellable)
g_object_unref (ctx->cancellable);
g_object_unref (ctx->invocation);
g_object_unref (ctx->subject);
g_object_unref (ctx->self);
g_free (ctx->authorization);
g_free (ctx);
}
/*****************************************************************************/
static MMAuthRequest *
real_create_request (MMAuthProvider *provider,
const char *authorization,
GObject *owner,
GDBusMethodInvocation *context,
MMAuthRequestCb callback,
gpointer callback_data,
GDestroyNotify notify)
static gboolean
authorize_finish (MMAuthProvider *self,
GAsyncResult *res,
GError **error)
{
MMAuthProviderPolkitPrivate *priv = MM_AUTH_PROVIDER_POLKIT_GET_PRIVATE (provider);
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}
return (MMAuthRequest *) mm_auth_request_polkit_new (priv->authority,
authorization,
owner,
context,
static void
check_authorization_ready (PolkitAuthority *authority,
GAsyncResult *res,
AuthorizeContext *ctx)
{
PolkitAuthorizationResult *pk_result;
GError *error = NULL;
if (g_cancellable_is_cancelled (ctx->cancellable)) {
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_CANCELLED,
"PolicyKit authorization attempt cancelled");
authorize_context_complete_and_free (ctx);
return;
}
pk_result = polkit_authority_check_authorization_finish (authority, res, &error);
if (!pk_result) {
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"PolicyKit authorization failed: '%s'",
error->message);
g_error_free (error);
} else {
if (polkit_authorization_result_get_is_authorized (pk_result))
/* Good! */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
else if (polkit_authorization_result_get_is_challenge (pk_result))
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_UNAUTHORIZED,
"PolicyKit authorization failed: challenge needed for '%s'",
ctx->authorization);
else
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_UNAUTHORIZED,
"PolicyKit authorization failed: not authorized for '%s'",
ctx->authorization);
g_object_unref (pk_result);
}
authorize_context_complete_and_free (ctx);
}
static void
authorize (MMAuthProvider *self,
GDBusMethodInvocation *invocation,
const gchar *authorization,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
MMAuthProviderPolkit *polkit = MM_AUTH_PROVIDER_POLKIT (self);
AuthorizeContext *ctx;
/* When creating the object, we actually allowed errors when looking for the
* authority. If that is the case, we'll just forbid any incoming
* authentication request */
if (!polkit->priv->authority) {
g_simple_async_report_error_in_idle (G_OBJECT (self),
callback,
callback_data,
notify);
user_data,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"PolicyKit authorization error: "
"'authority not found'");
return;
}
ctx = g_new (AuthorizeContext, 1);
ctx->self = g_object_ref (self);
ctx->invocation = g_object_ref (invocation);
ctx->authorization = g_strdup (authorization);
ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
authorize);
ctx->subject = polkit_system_bus_name_new (g_dbus_method_invocation_get_sender (ctx->invocation));
polkit_authority_check_authorization (polkit->priv->authority,
ctx->subject,
authorization,
NULL, /* details */
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
ctx->cancellable,
(GAsyncReadyCallback)check_authorization_ready,
ctx);
}
/*****************************************************************************/
/* Fix for polkit 0.97 and later */
#if !HAVE_POLKIT_AUTHORITY_GET_SYNC
static inline PolkitAuthority *
polkit_authority_get_sync (GCancellable *cancellable, GError **error)
{
PolkitAuthority *authority;
authority = polkit_authority_get ();
if (!authority)
g_set_error (error, 0, 0, "failed to get the PolicyKit authority");
return authority;
}
#endif
static void
mm_auth_provider_polkit_init (MMAuthProviderPolkit *self)
{
MMAuthProviderPolkitPrivate *priv = MM_AUTH_PROVIDER_POLKIT_GET_PRIVATE (self);
GError *error = NULL;
priv->authority = polkit_authority_get_sync (NULL, &error);
if (priv->authority) {
priv->auth_changed_id = g_signal_connect (priv->authority,
"changed",
G_CALLBACK (pk_authority_changed_cb),
self);
} else {
g_warning ("%s: failed to create PolicyKit authority: (%d) %s",
__func__,
error ? error->code : -1,
error && error->message ? error->message : "(unknown)");
self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
MM_TYPE_AUTH_PROVIDER_POLKIT,
MMAuthProviderPolkitPrivate);
self->priv->authority = polkit_authority_get_sync (NULL, &error);
if (!self->priv->authority) {
/* NOTE: we failed to create the polkit authority, but we still create
* our AuthProvider. Every request will fail, though. */
mm_warn ("failed to create PolicyKit authority: '%s'",
error ? error->message : "unknown");
g_clear_error (&error);
}
}
static void
set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id) {
case PROP_NAME:
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
switch (prop_id) {
case PROP_NAME:
g_value_set_string (value, "polkit");
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
dispose (GObject *object)
{
MMAuthProviderPolkit *self = MM_AUTH_PROVIDER_POLKIT (object);
MMAuthProviderPolkitPrivate *priv = MM_AUTH_PROVIDER_POLKIT_GET_PRIVATE (self);
if (priv->auth_changed_id) {
g_signal_handler_disconnect (priv->authority, priv->auth_changed_id);
priv->auth_changed_id = 0;
}
g_clear_object (MM_AUTH_PROVIDER_POLKIT (object)->priv->authority);
G_OBJECT_CLASS (mm_auth_provider_polkit_parent_class)->dispose (object);
}
@@ -157,17 +196,12 @@ static void
mm_auth_provider_polkit_class_init (MMAuthProviderPolkitClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
MMAuthProviderClass *ap_class = MM_AUTH_PROVIDER_CLASS (class);
MMAuthProviderClass *auth_provider_class = MM_AUTH_PROVIDER_CLASS (class);
mm_auth_provider_polkit_parent_class = g_type_class_peek_parent (class);
g_type_class_add_private (class, sizeof (MMAuthProviderPolkitPrivate));
/* Virtual methods */
object_class->set_property = set_property;
object_class->get_property = get_property;
object_class->dispose = dispose;
ap_class->create_request = real_create_request;
/* Properties */
g_object_class_override_property (object_class, PROP_NAME, MM_AUTH_PROVIDER_NAME);
auth_provider_class->authorize = authorize;
auth_provider_class->authorize_finish = authorize_finish;
}

View File

@@ -10,14 +10,13 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
* Copyright (C) 2010 - 2012 Red Hat, Inc.
* Copyright (C) 2012 Google, Inc.
*/
#ifndef MM_AUTH_PROVIDER_POLKIT_H
#define MM_AUTH_PROVIDER_POLKIT_H
#include <glib-object.h>
#include "mm-auth-provider.h"
#define MM_TYPE_AUTH_PROVIDER_POLKIT (mm_auth_provider_polkit_get_type ())
@@ -27,17 +26,21 @@
#define MM_IS_AUTH_PROVIDER_POLKIT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_AUTH_PROVIDER_POLKIT))
#define MM_AUTH_PROVIDER_POLKIT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_AUTH_PROVIDER_POLKIT, MMAuthProviderPolkitClass))
typedef struct {
MMAuthProvider parent;
} MMAuthProviderPolkit;
typedef struct _MMAuthProviderPolkit MMAuthProviderPolkit;
typedef struct _MMAuthProviderPolkitClass MMAuthProviderPolkitClass;
typedef struct _MMAuthProviderPolkitPrivate MMAuthProviderPolkitPrivate;
typedef struct {
struct _MMAuthProviderPolkit {
MMAuthProvider parent;
MMAuthProviderPolkitPrivate *priv;
};
struct _MMAuthProviderPolkitClass {
MMAuthProviderClass parent;
} MMAuthProviderPolkitClass;
};
GType mm_auth_provider_polkit_get_type (void);
GObject *mm_auth_provider_polkit_new (void);
MMAuthProvider *mm_auth_provider_polkit_new (void);
#endif /* MM_AUTH_PROVIDER_POLKIT_H */

View File

@@ -10,34 +10,17 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
* Copyright (C) 2010 - 2012 Red Hat, Inc.
* Copyright (C) 2012 Google, Inc.
*/
#include <string.h>
#include "mm-marshal.h"
#include "mm-auth-provider.h"
GObject *mm_auth_provider_new (void);
G_DEFINE_TYPE (MMAuthProvider, mm_auth_provider, G_TYPE_OBJECT)
#define MM_AUTH_PROVIDER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_AUTH_PROVIDER, MMAuthProviderPrivate))
typedef struct {
GHashTable *requests;
guint process_id;
} MMAuthProviderPrivate;
enum {
PROP_0,
PROP_NAME,
LAST_PROP
};
/*****************************************************************************/
GObject *
MMAuthProvider *
mm_auth_provider_new (void)
{
return g_object_new (MM_TYPE_AUTH_PROVIDER, NULL);
@@ -45,255 +28,75 @@ mm_auth_provider_new (void)
/*****************************************************************************/
static void
remove_requests (MMAuthProvider *self, GSList *remove)
{
MMAuthProviderPrivate *priv = MM_AUTH_PROVIDER_GET_PRIVATE (self);
MMAuthRequest *req;
while (remove) {
req = MM_AUTH_REQUEST (remove->data);
g_hash_table_remove (priv->requests, req);
remove = g_slist_remove (remove, req);
}
}
void
mm_auth_provider_cancel_request (MMAuthProvider *provider, MMAuthRequest *req)
{
MMAuthProviderPrivate *priv;
g_return_if_fail (provider != NULL);
g_return_if_fail (MM_IS_AUTH_PROVIDER (provider));
g_return_if_fail (req != NULL);
priv = MM_AUTH_PROVIDER_GET_PRIVATE (provider);
g_return_if_fail (g_hash_table_lookup (priv->requests, req) != NULL);
g_hash_table_remove (priv->requests, req);
}
void
mm_auth_provider_cancel_for_owner (MMAuthProvider *self, GObject *owner)
{
MMAuthProviderPrivate *priv;
GHashTableIter iter;
MMAuthRequest *req;
gpointer value;
GSList *remove = NULL;
g_return_if_fail (self != NULL);
g_return_if_fail (MM_IS_AUTH_PROVIDER (self));
/* Find all requests from this owner */
priv = MM_AUTH_PROVIDER_GET_PRIVATE (self);
g_hash_table_iter_init (&iter, priv->requests);
while (g_hash_table_iter_next (&iter, NULL, &value)) {
req = MM_AUTH_REQUEST (value);
if (mm_auth_request_get_owner (req) == owner)
remove = g_slist_prepend (remove, req);
}
/* And cancel/remove them */
remove_requests (self, remove);
}
/*****************************************************************************/
static MMAuthRequest *
real_create_request (MMAuthProvider *provider,
const char *authorization,
GObject *owner,
GDBusMethodInvocation *context,
MMAuthRequestCb callback,
gpointer callback_data,
GDestroyNotify notify)
{
return (MMAuthRequest *) mm_auth_request_new (0,
authorization,
owner,
context,
callback,
callback_data,
notify);
}
static gboolean
process_complete_requests (gpointer user_data)
{
MMAuthProvider *self = MM_AUTH_PROVIDER (user_data);
MMAuthProviderPrivate *priv = MM_AUTH_PROVIDER_GET_PRIVATE (self);
GHashTableIter iter;
gpointer value;
GSList *remove = NULL;
MMAuthRequest *req;
priv->process_id = 0;
/* Call finished request's callbacks */
g_hash_table_iter_init (&iter, priv->requests);
while (g_hash_table_iter_next (&iter, NULL, &value)) {
req = MM_AUTH_REQUEST (value);
if (mm_auth_request_get_authorization (req) != MM_AUTH_RESULT_UNKNOWN) {
mm_auth_request_callback (req);
remove = g_slist_prepend (remove, req);
}
}
/* And remove those requests from our pending request list */
remove_requests (self, remove);
return FALSE;
}
static void
auth_result_cb (MMAuthRequest *req, gpointer user_data)
{
MMAuthProvider *self = MM_AUTH_PROVIDER (user_data);
MMAuthProviderPrivate *priv = MM_AUTH_PROVIDER_GET_PRIVATE (self);
/* Process results from an idle handler */
if (priv->process_id == 0)
priv->process_id = g_idle_add (process_complete_requests, self);
}
#define RESULT_SIGID_TAG "result-sigid"
MMAuthRequest *
mm_auth_provider_request_auth (MMAuthProvider *self,
const char *authorization,
GObject *owner,
GDBusMethodInvocation *context,
MMAuthRequestCb callback,
gpointer callback_data,
GDestroyNotify notify,
gboolean
mm_auth_provider_authorize_finish (MMAuthProvider *self,
GAsyncResult *res,
GError **error)
{
MMAuthProviderPrivate *priv;
MMAuthRequest *req;
guint32 sigid;
g_return_val_if_fail (MM_IS_AUTH_PROVIDER (self), FALSE);
g_return_val_if_fail (self != NULL, 0);
g_return_val_if_fail (MM_IS_AUTH_PROVIDER (self), 0);
g_return_val_if_fail (authorization != NULL, 0);
g_return_val_if_fail (callback != NULL, 0);
priv = MM_AUTH_PROVIDER_GET_PRIVATE (self);
req = MM_AUTH_PROVIDER_GET_CLASS (self)->create_request (self,
authorization,
owner,
context,
callback,
callback_data,
notify);
g_assert (req);
sigid = g_signal_connect (req, "result", G_CALLBACK (auth_result_cb), self);
g_object_set_data (G_OBJECT (req), RESULT_SIGID_TAG, GUINT_TO_POINTER (sigid));
g_hash_table_insert (priv->requests, req, req);
if (!mm_auth_request_authenticate (req, error)) {
/* Error */
g_hash_table_remove (priv->requests, req);
return NULL;
return MM_AUTH_PROVIDER_GET_CLASS (self)->authorize_finish (self, res, error);
}
return req;
void
mm_auth_provider_authorize (MMAuthProvider *self,
GDBusMethodInvocation *invocation,
const gchar *authorization,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_return_if_fail (MM_IS_AUTH_PROVIDER (self));
MM_AUTH_PROVIDER_GET_CLASS (self)->authorize (self,
invocation,
authorization,
cancellable,
callback,
user_data);
}
/*****************************************************************************/
static void
dispose_auth_request (gpointer data)
static gboolean
authorize_finish (MMAuthProvider *self,
GAsyncResult *res,
GError **error)
{
MMAuthRequest *req = MM_AUTH_REQUEST (data);
guint sigid;
sigid = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (req), RESULT_SIGID_TAG));
if (sigid)
g_signal_handler_disconnect (req, sigid);
mm_auth_request_dispose (req);
g_object_unref (req);
/* Null auth; everything passes */
return TRUE;
}
static void
authorize (MMAuthProvider *self,
GDBusMethodInvocation *invocation,
const gchar *authorization,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *result;
/* Just create the result and complete it */
result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
authorize);
g_simple_async_result_complete_in_idle (result);
g_object_unref (result);
}
/*****************************************************************************/
static void
mm_auth_provider_init (MMAuthProvider *self)
{
MMAuthProviderPrivate *priv = MM_AUTH_PROVIDER_GET_PRIVATE (self);
priv->requests = g_hash_table_new_full (g_direct_hash,
g_direct_equal,
NULL,
dispose_auth_request);
}
static void
set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id) {
case PROP_NAME:
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
#define NULL_PROVIDER "open"
static void
get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
switch (prop_id) {
case PROP_NAME:
g_value_set_string (value, NULL_PROVIDER);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
dispose (GObject *object)
{
MMAuthProviderPrivate *priv = MM_AUTH_PROVIDER_GET_PRIVATE (object);
if (priv->process_id)
g_source_remove (priv->process_id);
g_hash_table_destroy (priv->requests);
G_OBJECT_CLASS (mm_auth_provider_parent_class)->dispose (object);
}
static void
mm_auth_provider_class_init (MMAuthProviderClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
mm_auth_provider_parent_class = g_type_class_peek_parent (class);
g_type_class_add_private (class, sizeof (MMAuthProviderPrivate));
/* Virtual methods */
object_class->set_property = set_property;
object_class->get_property = get_property;
object_class->dispose = dispose;
class->create_request = real_create_request;
/* Properties */
g_object_class_install_property (object_class, PROP_NAME,
g_param_spec_string (MM_AUTH_PROVIDER_NAME,
"Name",
"Provider name",
NULL_PROVIDER,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
class->authorize = authorize;
class->authorize_finish = authorize_finish;
}

View File

@@ -10,7 +10,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
* Copyright (C) 2010 - 2012 Red Hat, Inc.
* Copyright (C) 2012 Google, Inc.
*/
#ifndef MM_AUTH_PROVIDER_H
@@ -18,20 +19,6 @@
#include <gio/gio.h>
#include "mm-auth-request.h"
/* Authorizations */
#define MM_AUTHORIZATION_MANAGER_CONTROL "org.freedesktop.ModemManager.Control"
#define MM_AUTHORIZATION_DEVICE_INFO "org.freedesktop.ModemManager.Device.Info"
#define MM_AUTHORIZATION_DEVICE_CONTROL "org.freedesktop.ModemManager.Device.Control"
#define MM_AUTHORIZATION_CONTACTS "org.freedesktop.ModemManager.Contacts"
#define MM_AUTHORIZATION_SMS "org.freedesktop.ModemManager.SMS"
#define MM_AUTHORIZATION_USSD "org.freedesktop.ModemManager.USSD"
#define MM_AUTHORIZATION_LOCATION "org.freedesktop.ModemManager.Location"
#define MM_AUTHORIZATION_FIRMWARE "org.freedesktop.ModemManager.Firmware"
/******************/
#define MM_TYPE_AUTH_PROVIDER (mm_auth_provider_get_type ())
#define MM_AUTH_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_AUTH_PROVIDER, MMAuthProvider))
#define MM_AUTH_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_AUTH_PROVIDER, MMAuthProviderClass))
@@ -39,49 +26,50 @@
#define MM_IS_AUTH_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_AUTH_PROVIDER))
#define MM_AUTH_PROVIDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_AUTH_PROVIDER, MMAuthProviderClass))
#define MM_AUTH_PROVIDER_NAME "name"
/* Authorizations */
#define MM_AUTHORIZATION_MANAGER_CONTROL "org.freedesktop.ModemManager1.Control"
#define MM_AUTHORIZATION_DEVICE_CONTROL "org.freedesktop.ModemManager1.Device.Control"
#define MM_AUTHORIZATION_CONTACTS "org.freedesktop.ModemManager1.Contacts"
#define MM_AUTHORIZATION_MESSAGING "org.freedesktop.ModemManager1.Messaging"
#define MM_AUTHORIZATION_USSD "org.freedesktop.ModemManager1.USSD"
#define MM_AUTHORIZATION_LOCATION "org.freedesktop.ModemManager1.Location"
#define MM_AUTHORIZATION_FIRMWARE "org.freedesktop.ModemManager1.Firmware"
typedef struct {
typedef struct _MMAuthProvider MMAuthProvider;
typedef struct _MMAuthProviderClass MMAuthProviderClass;
struct _MMAuthProvider {
GObject parent;
} MMAuthProvider;
};
typedef struct {
struct _MMAuthProviderClass {
GObjectClass parent;
MMAuthRequest * (*create_request) (MMAuthProvider *provider,
const char *authorization,
GObject *owner,
GDBusMethodInvocation *context,
MMAuthRequestCb callback,
gpointer callback_data,
GDestroyNotify notify);
} MMAuthProviderClass;
/* Perform authorization checks in this request (async).
* Returns TRUE if authorized, FALSE if error is set. */
void (* authorize) (MMAuthProvider *self,
GDBusMethodInvocation *invocation,
const gchar *authorization,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
gboolean (* authorize_finish) (MMAuthProvider *self,
GAsyncResult *res,
GError **error);
};
GType mm_auth_provider_get_type (void);
/* Don't do anything clever from the notify callback... */
MMAuthRequest *mm_auth_provider_request_auth (MMAuthProvider *provider,
const char *authorization,
GObject *owner,
GDBusMethodInvocation *context,
MMAuthRequestCb callback,
gpointer callback_data,
GDestroyNotify notify,
MMAuthProvider *mm_auth_provider_new (void);
void mm_auth_provider_authorize (MMAuthProvider *self,
GDBusMethodInvocation *invocation,
const gchar *authorization,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
gboolean mm_auth_provider_authorize_finish (MMAuthProvider *self,
GAsyncResult *res,
GError **error);
void mm_auth_provider_cancel_for_owner (MMAuthProvider *provider,
GObject *owner);
/* Subclass API */
/* To get an auth provider instance, implemented in mm-auth-provider-factory.c */
MMAuthProvider *mm_auth_provider_get (void);
/* schedules the request's completion */
void mm_auth_provider_finish_request (MMAuthProvider *provider,
MMAuthRequest *req,
MMAuthResult result);
void mm_auth_provider_cancel_request (MMAuthProvider *provider, MMAuthRequest *req);
#endif /* MM_AUTH_PROVIDER_H */

View File

@@ -1,174 +0,0 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
*/
#include <glib.h>
#include <gio/gio.h>
#include "mm-auth-request-polkit.h"
G_DEFINE_TYPE (MMAuthRequestPolkit, mm_auth_request_polkit, MM_TYPE_AUTH_REQUEST)
#define MM_AUTH_REQUEST_POLKIT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_AUTH_REQUEST_POLKIT, MMAuthRequestPolkitPrivate))
typedef struct {
PolkitAuthority *authority;
GCancellable *cancellable;
PolkitSubject *subject;
} MMAuthRequestPolkitPrivate;
/*****************************************************************************/
GObject *
mm_auth_request_polkit_new (PolkitAuthority *authority,
const char *authorization,
GObject *owner,
GDBusMethodInvocation *context,
MMAuthRequestCb callback,
gpointer callback_data,
GDestroyNotify notify)
{
GObject *obj;
MMAuthRequestPolkitPrivate *priv;
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) {
const gchar *sender;
priv = MM_AUTH_REQUEST_POLKIT_GET_PRIVATE (obj);
priv->authority = authority;
priv->cancellable = g_cancellable_new ();
sender = g_dbus_method_invocation_get_sender (context);
priv->subject = polkit_system_bus_name_new (sender);
}
return obj;
}
/*****************************************************************************/
static void
pk_auth_cb (GObject *object, GAsyncResult *result, gpointer user_data)
{
MMAuthRequestPolkit *self = user_data;
MMAuthRequestPolkitPrivate *priv;
PolkitAuthorizationResult *pk_result;
GError *error = NULL;
g_return_if_fail (self != NULL);
g_return_if_fail (MM_IS_AUTH_REQUEST_POLKIT (self));
priv = MM_AUTH_REQUEST_POLKIT_GET_PRIVATE (self);
if (!g_cancellable_is_cancelled (priv->cancellable)) {
pk_result = polkit_authority_check_authorization_finish (priv->authority,
result,
&error);
if (error) {
mm_auth_request_set_result (MM_AUTH_REQUEST (self), MM_AUTH_RESULT_INTERNAL_FAILURE);
g_warning ("%s: PolicyKit authentication error: (%d) %s",
__func__,
error ? error->code : -1,
error && error->message ? error->message : "(unknown)");
} else if (polkit_authorization_result_get_is_authorized (pk_result))
mm_auth_request_set_result (MM_AUTH_REQUEST (self), MM_AUTH_RESULT_AUTHORIZED);
else if (polkit_authorization_result_get_is_challenge (pk_result))
mm_auth_request_set_result (MM_AUTH_REQUEST (self), MM_AUTH_RESULT_CHALLENGE);
else
mm_auth_request_set_result (MM_AUTH_REQUEST (self), MM_AUTH_RESULT_NOT_AUTHORIZED);
g_signal_emit_by_name (self, "result");
}
g_object_unref (self);
}
static gboolean
real_authenticate (MMAuthRequest *self, GError **error)
{
MMAuthRequestPolkitPrivate *priv;
g_return_val_if_fail (self != NULL, FALSE);
g_return_val_if_fail (MM_IS_AUTH_REQUEST_POLKIT (self), FALSE);
/* We ref ourselves across the polkit call, because we can't get
* disposed of while the call is still in-progress, and even if we
* cancel ourselves we'll still get the callback.
*/
g_object_ref (self);
priv = MM_AUTH_REQUEST_POLKIT_GET_PRIVATE (self);
polkit_authority_check_authorization (priv->authority,
priv->subject,
mm_auth_request_get_authorization (MM_AUTH_REQUEST (self)),
NULL,
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
priv->cancellable,
pk_auth_cb,
self);
return TRUE;
}
static void
real_dispose (MMAuthRequest *req)
{
g_return_if_fail (req != NULL);
g_return_if_fail (MM_IS_AUTH_REQUEST_POLKIT (req));
g_cancellable_cancel (MM_AUTH_REQUEST_POLKIT_GET_PRIVATE (req)->cancellable);
}
/*****************************************************************************/
static void
mm_auth_request_polkit_init (MMAuthRequestPolkit *self)
{
}
static void
dispose (GObject *object)
{
MMAuthRequestPolkitPrivate *priv = MM_AUTH_REQUEST_POLKIT_GET_PRIVATE (object);
g_object_unref (priv->cancellable);
g_object_unref (priv->subject);
G_OBJECT_CLASS (mm_auth_request_polkit_parent_class)->dispose (object);
}
static void
mm_auth_request_polkit_class_init (MMAuthRequestPolkitClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
MMAuthRequestClass *ar_class = MM_AUTH_REQUEST_CLASS (class);
mm_auth_request_polkit_parent_class = g_type_class_peek_parent (class);
g_type_class_add_private (class, sizeof (MMAuthRequestPolkitPrivate));
/* Virtual methods */
object_class->dispose = dispose;
ar_class->authenticate = real_authenticate;
ar_class->dispose = real_dispose;
}

View File

@@ -1,51 +0,0 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
*/
#ifndef MM_AUTH_REQUEST_POLKIT_H
#define MM_AUTH_REQUEST_POLKIT_H
#include <gio/gio.h>
#include <polkit/polkit.h>
#include "mm-auth-request.h"
#define MM_TYPE_AUTH_REQUEST_POLKIT (mm_auth_request_polkit_get_type ())
#define MM_AUTH_REQUEST_POLKIT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_AUTH_REQUEST_POLKIT, MMAuthRequestPolkit))
#define MM_AUTH_REQUEST_POLKIT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_AUTH_REQUEST_POLKIT, MMAuthRequestPolkitClass))
#define MM_IS_AUTH_REQUEST_POLKIT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_AUTH_REQUEST_POLKIT))
#define MM_IS_AUTH_REQUEST_POLKIT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_AUTH_REQUEST_POLKIT))
#define MM_AUTH_REQUEST_POLKIT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_AUTH_REQUEST_POLKIT, MMAuthRequestPolkitClass))
typedef struct {
MMAuthRequest parent;
} MMAuthRequestPolkit;
typedef struct {
MMAuthRequestClass parent;
} MMAuthRequestPolkitClass;
GType mm_auth_request_polkit_get_type (void);
GObject *mm_auth_request_polkit_new (PolkitAuthority *authority,
const char *authorization,
GObject *owner,
GDBusMethodInvocation *context,
MMAuthRequestCb callback,
gpointer callback_data,
GDestroyNotify notify);
void mm_auth_request_polkit_cancel (MMAuthRequestPolkit *self);
#endif /* MM_AUTH_REQUEST_POLKIT_H */

View File

@@ -1,182 +0,0 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
*/
#include "mm-auth-request.h"
G_DEFINE_TYPE (MMAuthRequest, mm_auth_request, G_TYPE_OBJECT)
#define MM_AUTH_REQUEST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_AUTH_REQUEST, MMAuthRequestPrivate))
typedef struct {
GObject *owner;
char *auth;
GDBusMethodInvocation *context;
MMAuthRequestCb callback;
gpointer callback_data;
MMAuthResult result;
} MMAuthRequestPrivate;
/*****************************************************************************/
GObject *
mm_auth_request_new (GType atype,
const char *authorization,
GObject *owner,
GDBusMethodInvocation *context,
MMAuthRequestCb callback,
gpointer callback_data,
GDestroyNotify notify)
{
GObject *obj;
MMAuthRequestPrivate *priv;
g_return_val_if_fail (authorization != NULL, NULL);
g_return_val_if_fail (owner != NULL, NULL);
g_return_val_if_fail (callback != NULL, NULL);
obj = g_object_new (atype ? atype : MM_TYPE_AUTH_REQUEST, NULL);
if (obj) {
priv = MM_AUTH_REQUEST_GET_PRIVATE (obj);
priv->owner = owner; /* not reffed */
priv->context = context;
priv->auth = g_strdup (authorization);
priv->callback = callback;
priv->callback_data = callback_data;
g_object_set_data_full (obj, "caller-data", callback_data, notify);
}
return obj;
}
/*****************************************************************************/
const char *
mm_auth_request_get_authorization (MMAuthRequest *self)
{
g_return_val_if_fail (self != NULL, NULL);
g_return_val_if_fail (MM_IS_AUTH_REQUEST (self), NULL);
return MM_AUTH_REQUEST_GET_PRIVATE (self)->auth;
}
GObject *
mm_auth_request_get_owner (MMAuthRequest *self)
{
g_return_val_if_fail (self != NULL, NULL);
g_return_val_if_fail (MM_IS_AUTH_REQUEST (self), NULL);
return MM_AUTH_REQUEST_GET_PRIVATE (self)->owner;
}
MMAuthResult
mm_auth_request_get_result (MMAuthRequest *self)
{
g_return_val_if_fail (self != NULL, MM_AUTH_RESULT_UNKNOWN);
g_return_val_if_fail (MM_IS_AUTH_REQUEST (self), MM_AUTH_RESULT_UNKNOWN);
return MM_AUTH_REQUEST_GET_PRIVATE (self)->result;
}
void
mm_auth_request_set_result (MMAuthRequest *self, MMAuthResult result)
{
g_return_if_fail (self != NULL);
g_return_if_fail (MM_IS_AUTH_REQUEST (self));
g_return_if_fail (result != MM_AUTH_RESULT_UNKNOWN);
MM_AUTH_REQUEST_GET_PRIVATE (self)->result = result;
}
gboolean
mm_auth_request_authenticate (MMAuthRequest *self, GError **error)
{
return MM_AUTH_REQUEST_GET_CLASS (self)->authenticate (self, error);
}
void
mm_auth_request_callback (MMAuthRequest *self)
{
MMAuthRequestPrivate *priv;
g_return_if_fail (self != NULL);
g_return_if_fail (MM_IS_AUTH_REQUEST (self));
priv = MM_AUTH_REQUEST_GET_PRIVATE (self);
g_warn_if_fail (priv->result != MM_AUTH_RESULT_UNKNOWN);
if (priv->callback)
priv->callback (self, priv->owner, priv->context, priv->callback_data);
}
void
mm_auth_request_dispose (MMAuthRequest *self)
{
g_return_if_fail (self != NULL);
g_return_if_fail (MM_IS_AUTH_REQUEST (self));
if (MM_AUTH_REQUEST_GET_CLASS (self)->dispose)
MM_AUTH_REQUEST_GET_CLASS (self)->dispose (self);
}
/*****************************************************************************/
static gboolean
real_authenticate (MMAuthRequest *self, GError **error)
{
/* Null auth; everything passes */
mm_auth_request_set_result (self, MM_AUTH_RESULT_AUTHORIZED);
g_signal_emit_by_name (self, "result");
return TRUE;
}
/*****************************************************************************/
static void
mm_auth_request_init (MMAuthRequest *self)
{
}
static void
dispose (GObject *object)
{
MMAuthRequestPrivate *priv = MM_AUTH_REQUEST_GET_PRIVATE (object);
g_free (priv->auth);
G_OBJECT_CLASS (mm_auth_request_parent_class)->dispose (object);
}
static void
mm_auth_request_class_init (MMAuthRequestClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
mm_auth_request_parent_class = g_type_class_peek_parent (class);
g_type_class_add_private (class, sizeof (MMAuthRequestPrivate));
/* Virtual methods */
object_class->dispose = dispose;
class->authenticate = real_authenticate;
g_signal_new ("result",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
0, NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, G_TYPE_NONE);
}

View File

@@ -1,70 +0,0 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
*/
#ifndef MM_AUTH_REQUEST_H
#define MM_AUTH_REQUEST_H
#include <gio/gio.h>
#define MM_TYPE_AUTH_REQUEST (mm_auth_request_get_type ())
#define MM_AUTH_REQUEST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_AUTH_REQUEST, MMAuthRequest))
#define MM_AUTH_REQUEST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_AUTH_REQUEST, MMAuthRequestClass))
#define MM_IS_AUTH_REQUEST(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_AUTH_REQUEST))
#define MM_IS_AUTH_REQUEST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_AUTH_REQUEST))
#define MM_AUTH_REQUEST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_AUTH_REQUEST, MMAuthRequestClass))
typedef enum MMAuthResult {
MM_AUTH_RESULT_UNKNOWN = 0,
MM_AUTH_RESULT_INTERNAL_FAILURE,
MM_AUTH_RESULT_NOT_AUTHORIZED,
MM_AUTH_RESULT_CHALLENGE,
MM_AUTH_RESULT_AUTHORIZED
} MMAuthResult;
typedef struct {
GObject parent;
} MMAuthRequest;
typedef struct {
GObjectClass parent;
gboolean (*authenticate) (MMAuthRequest *self, GError **error);
void (*dispose) (MMAuthRequest *self);
} MMAuthRequestClass;
GType mm_auth_request_get_type (void);
typedef void (*MMAuthRequestCb) (MMAuthRequest *req,
GObject *owner,
GDBusMethodInvocation *context,
gpointer user_data);
GObject *mm_auth_request_new (GType atype,
const char *authorization,
GObject *owner,
GDBusMethodInvocation *context,
MMAuthRequestCb callback,
gpointer callback_data,
GDestroyNotify notify);
const char * mm_auth_request_get_authorization (MMAuthRequest *req);
GObject * mm_auth_request_get_owner (MMAuthRequest *req);
MMAuthResult mm_auth_request_get_result (MMAuthRequest *req);
void mm_auth_request_set_result (MMAuthRequest *req, MMAuthResult result);
gboolean mm_auth_request_authenticate (MMAuthRequest *req, GError **error);
void mm_auth_request_callback (MMAuthRequest *req);
void mm_auth_request_dispose (MMAuthRequest *req);
#endif /* MM_AUTH_REQUEST_H */

View File

@@ -10,36 +10,44 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2010 Red Hat, Inc.
* Copyright (C) 2010 - 2012 Red Hat, Inc.
* Copyright (C) 2012 Google, Inc.
*/
#include <string.h>
#include "config.h"
#include "mm-auth.h"
#include "mm-auth-provider.h"
GObject *mm_auth_provider_new (void);
#ifdef WITH_POLKIT
#define IN_AUTH_PROVIDER_FACTORY_C
#include "mm-auth-provider-polkit.h"
#undef IN_AUTH_PROVIDER_FACTORY_C
#endif
static MMAuthProvider *authp = NULL;
MMAuthProvider *
mm_auth_provider_get (void)
mm_auth_get_provider (void)
{
static MMAuthProvider *singleton;
if (!singleton) {
if (!authp) {
#if WITH_POLKIT
singleton = (MMAuthProvider *) mm_auth_provider_polkit_new ();
authp = mm_auth_provider_polkit_new ();
#else
singleton = (MMAuthProvider *) mm_auth_provider_new ();
authp = mm_auth_provider_new ();
#endif
}
g_assert (singleton);
return singleton;
g_assert (authp);
/* We'll keep the refcount of this object controlled, in order to have
* clean shutdowns */
return g_object_ref (authp);
}
void
mm_auth_shutdown (void)
{
/* Clear the last reference of the auth provider if it was ever set */
g_clear_object (&authp);
}

27
src/mm-auth.h Normal file
View File

@@ -0,0 +1,27 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details:
*
* Copyright (C) 2010 - 2012 Red Hat, Inc.
* Copyright (C) 2012 Google, Inc.
*/
#ifndef MM_AUTH_H
#define MM_AUTH_H
#include "mm-auth-provider.h"
/* Get the default provider */
MMAuthProvider *mm_auth_get_provider (void);
void mm_auth_shutdown (void);
#endif /* MM_AUTH_H */