From ddaceb232c8b537a7d29a9708928d3a3671b98e5 Mon Sep 17 00:00:00 2001 From: haxar Date: Tue, 21 Feb 2012 20:13:04 -0800 Subject: gsd to msd complete rename patch by NiceandGently; code changes commit --- plugins/smartcard/Makefile.am | 18 +- plugins/smartcard/gsd-smartcard-manager.c | 400 +++++++++++++++--------------- plugins/smartcard/gsd-smartcard-manager.h | 76 +++--- plugins/smartcard/gsd-smartcard-plugin.c | 146 +++++------ plugins/smartcard/gsd-smartcard-plugin.h | 28 +-- plugins/smartcard/gsd-smartcard.c | 192 +++++++------- plugins/smartcard/gsd-smartcard.h | 82 +++--- 7 files changed, 471 insertions(+), 471 deletions(-) (limited to 'plugins/smartcard') diff --git a/plugins/smartcard/Makefile.am b/plugins/smartcard/Makefile.am index a70e0a0..4e01be4 100644 --- a/plugins/smartcard/Makefile.am +++ b/plugins/smartcard/Makefile.am @@ -2,19 +2,19 @@ plugin_LTLIBRARIES = \ libsmartcard.la libsmartcard_la_SOURCES = \ - gsd-smartcard-plugin.h \ - gsd-smartcard-plugin.c \ - gsd-smartcard.h \ - gsd-smartcard.c \ - gsd-smartcard-manager.h \ - gsd-smartcard-manager.c + msd-smartcard-plugin.h \ + msd-smartcard-plugin.c \ + msd-smartcard.h \ + msd-smartcard.c \ + msd-smartcard-manager.h \ + msd-smartcard-manager.c libsmartcard_la_CPPFLAGS = \ -I$(top_srcdir)/mate-settings-daemon \ -DMATE_SETTINGS_LOCALEDIR=\""$(datadir)/locale"\" \ -DSYSCONFDIR=\""$(sysconfdir)"\" \ -DLIBDIR=\""$(libdir)"\" \ - -DGSD_SMARTCARD_MANAGER_NSS_DB=\""$(NSS_DATABASE)"\" \ + -DMSD_SMARTCARD_MANAGER_NSS_DB=\""$(NSS_DATABASE)"\" \ $(AM_CPPFLAGS) libsmartcard_la_CFLAGS = \ @@ -24,13 +24,13 @@ libsmartcard_la_CFLAGS = \ $(AM_CFLAGS) libsmartcard_la_LDFLAGS = \ - $(GSD_PLUGIN_LDFLAGS) + $(MSD_PLUGIN_LDFLAGS) libsmartcard_la_LIBADD = \ $(SETTINGS_PLUGIN_LIBS) \ $(NSS_LIBS) -@GSD_INTLTOOL_PLUGIN_RULE@ +@MSD_INTLTOOL_PLUGIN_RULE@ plugin_in_files = \ smartcard.mate-settings-plugin.in diff --git a/plugins/smartcard/gsd-smartcard-manager.c b/plugins/smartcard/gsd-smartcard-manager.c index a231e20..ed823f4 100644 --- a/plugins/smartcard/gsd-smartcard-manager.c +++ b/plugins/smartcard/gsd-smartcard-manager.c @@ -1,4 +1,4 @@ -/* gsd-smartcard-manager.c - object for monitoring smartcard insertion and +/* msd-smartcard-manager.c - object for monitoring smartcard insertion and * removal events * * Copyright (C) 2006, 2009 Red Hat, Inc. @@ -22,10 +22,10 @@ */ #include "config.h" -#include "gsd-smartcard-manager.h" +#include "msd-smartcard-manager.h" #define SMARTCARD_ENABLE_INTERNAL_API -#include "gsd-smartcard.h" +#include "msd-smartcard.h" #include #include @@ -49,22 +49,22 @@ #include #include -#ifndef GSD_SMARTCARD_MANAGER_NSS_DB -#define GSD_SMARTCARD_MANAGER_NSS_DB SYSCONFDIR"/pki/nssdb" +#ifndef MSD_SMARTCARD_MANAGER_NSS_DB +#define MSD_SMARTCARD_MANAGER_NSS_DB SYSCONFDIR"/pki/nssdb" #endif -typedef enum _GsdSmartcardManagerState GsdSmartcardManagerState; -typedef struct _GsdSmartcardManagerWorker GsdSmartcardManagerWorker; +typedef enum _MsdSmartcardManagerState MsdSmartcardManagerState; +typedef struct _MsdSmartcardManagerWorker MsdSmartcardManagerWorker; -enum _GsdSmartcardManagerState { - GSD_SMARTCARD_MANAGER_STATE_STOPPED = 0, - GSD_SMARTCARD_MANAGER_STATE_STARTING, - GSD_SMARTCARD_MANAGER_STATE_STARTED, - GSD_SMARTCARD_MANAGER_STATE_STOPPING, +enum _MsdSmartcardManagerState { + MSD_SMARTCARD_MANAGER_STATE_STOPPED = 0, + MSD_SMARTCARD_MANAGER_STATE_STARTING, + MSD_SMARTCARD_MANAGER_STATE_STARTED, + MSD_SMARTCARD_MANAGER_STATE_STOPPING, }; -struct _GsdSmartcardManagerPrivate { - GsdSmartcardManagerState state; +struct _MsdSmartcardManagerPrivate { + MsdSmartcardManagerState state; SECMODModule *module; char *module_path; @@ -80,7 +80,7 @@ struct _GsdSmartcardManagerPrivate { guint32 nss_is_loaded : 1; }; -struct _GsdSmartcardManagerWorker { +struct _MsdSmartcardManagerWorker { SECMODModule *module; GHashTable *smartcards; int write_fd; @@ -88,36 +88,36 @@ struct _GsdSmartcardManagerWorker { guint32 nss_is_loaded : 1; }; -static void gsd_smartcard_manager_finalize (GObject *object); -static void gsd_smartcard_manager_class_install_signals (GsdSmartcardManagerClass *service_class); -static void gsd_smartcard_manager_class_install_properties (GsdSmartcardManagerClass *service_class); -static void gsd_smartcard_manager_set_property (GObject *object, +static void msd_smartcard_manager_finalize (GObject *object); +static void msd_smartcard_manager_class_install_signals (MsdSmartcardManagerClass *service_class); +static void msd_smartcard_manager_class_install_properties (MsdSmartcardManagerClass *service_class); +static void msd_smartcard_manager_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); -static void gsd_smartcard_manager_get_property (GObject *object, +static void msd_smartcard_manager_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); -static void gsd_smartcard_manager_set_module_path (GsdSmartcardManager *manager, +static void msd_smartcard_manager_set_module_path (MsdSmartcardManager *manager, const char *module_path); -static void gsd_smartcard_manager_card_removed_handler (GsdSmartcardManager *manager, - GsdSmartcard *card); -static void gsd_smartcard_manager_card_inserted_handler (GsdSmartcardManager *manager_class, - GsdSmartcard *card); -static gboolean gsd_smartcard_manager_stop_now (GsdSmartcardManager *manager); -static void gsd_smartcard_manager_queue_stop (GsdSmartcardManager *manager); - -static gboolean gsd_smartcard_manager_create_worker (GsdSmartcardManager *manager, +static void msd_smartcard_manager_card_removed_handler (MsdSmartcardManager *manager, + MsdSmartcard *card); +static void msd_smartcard_manager_card_inserted_handler (MsdSmartcardManager *manager_class, + MsdSmartcard *card); +static gboolean msd_smartcard_manager_stop_now (MsdSmartcardManager *manager); +static void msd_smartcard_manager_queue_stop (MsdSmartcardManager *manager); + +static gboolean msd_smartcard_manager_create_worker (MsdSmartcardManager *manager, int *worker_fd, GThread **worker_thread); -static GsdSmartcardManagerWorker * gsd_smartcard_manager_worker_new (int write_fd); -static void gsd_smartcard_manager_worker_free (GsdSmartcardManagerWorker *worker); +static MsdSmartcardManagerWorker * msd_smartcard_manager_worker_new (int write_fd); +static void msd_smartcard_manager_worker_free (MsdSmartcardManagerWorker *worker); static gboolean open_pipe (int *write_fd, int *read_fd); static gboolean read_bytes (int fd, gpointer bytes, gsize num_bytes); static gboolean write_bytes (int fd, gconstpointer bytes, gsize num_bytes); -static GsdSmartcard *read_smartcard (int fd, SECMODModule *module); -static gboolean write_smartcard (int fd, GsdSmartcard *card); +static MsdSmartcard *read_smartcard (int fd, SECMODModule *module); +static gboolean write_smartcard (int fd, MsdSmartcard *card); enum { PROP_0 = 0, @@ -132,37 +132,37 @@ enum { NUMBER_OF_SIGNALS }; -static guint gsd_smartcard_manager_signals[NUMBER_OF_SIGNALS]; +static guint msd_smartcard_manager_signals[NUMBER_OF_SIGNALS]; -G_DEFINE_TYPE (GsdSmartcardManager, - gsd_smartcard_manager, +G_DEFINE_TYPE (MsdSmartcardManager, + msd_smartcard_manager, G_TYPE_OBJECT); static void -gsd_smartcard_manager_class_init (GsdSmartcardManagerClass *manager_class) +msd_smartcard_manager_class_init (MsdSmartcardManagerClass *manager_class) { GObjectClass *gobject_class; gobject_class = G_OBJECT_CLASS (manager_class); - gobject_class->finalize = gsd_smartcard_manager_finalize; + gobject_class->finalize = msd_smartcard_manager_finalize; - gsd_smartcard_manager_class_install_signals (manager_class); - gsd_smartcard_manager_class_install_properties (manager_class); + msd_smartcard_manager_class_install_signals (manager_class); + msd_smartcard_manager_class_install_properties (manager_class); g_type_class_add_private (manager_class, - sizeof (GsdSmartcardManagerPrivate)); + sizeof (MsdSmartcardManagerPrivate)); } static void -gsd_smartcard_manager_class_install_properties (GsdSmartcardManagerClass *card_class) +msd_smartcard_manager_class_install_properties (MsdSmartcardManagerClass *card_class) { GObjectClass *object_class; GParamSpec *param_spec; object_class = G_OBJECT_CLASS (card_class); - object_class->set_property = gsd_smartcard_manager_set_property; - object_class->get_property = gsd_smartcard_manager_get_property; + object_class->set_property = msd_smartcard_manager_set_property; + object_class->get_property = msd_smartcard_manager_get_property; param_spec = g_param_spec_string ("module-path", _("Module Path"), _("path to smartcard PKCS #11 driver"), @@ -171,16 +171,16 @@ gsd_smartcard_manager_class_install_properties (GsdSmartcardManagerClass *card_c } static void -gsd_smartcard_manager_set_property (GObject *object, +msd_smartcard_manager_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { - GsdSmartcardManager *manager = GSD_SMARTCARD_MANAGER (object); + MsdSmartcardManager *manager = MSD_SMARTCARD_MANAGER (object); switch (prop_id) { case PROP_MODULE_PATH: - gsd_smartcard_manager_set_module_path (manager, + msd_smartcard_manager_set_module_path (manager, g_value_get_string (value)); break; @@ -191,17 +191,17 @@ gsd_smartcard_manager_set_property (GObject *object, } static void -gsd_smartcard_manager_get_property (GObject *object, +msd_smartcard_manager_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { - GsdSmartcardManager *manager = GSD_SMARTCARD_MANAGER (object); + MsdSmartcardManager *manager = MSD_SMARTCARD_MANAGER (object); char *module_path; switch (prop_id) { case PROP_MODULE_PATH: - module_path = gsd_smartcard_manager_get_module_path (manager); + module_path = msd_smartcard_manager_get_module_path (manager); g_value_set_string (value, module_path); g_free (module_path); break; @@ -213,13 +213,13 @@ gsd_smartcard_manager_get_property (GObject *object, } char * -gsd_smartcard_manager_get_module_path (GsdSmartcardManager *manager) +msd_smartcard_manager_get_module_path (MsdSmartcardManager *manager) { return manager->priv->module_path; } static void -gsd_smartcard_manager_set_module_path (GsdSmartcardManager *manager, +msd_smartcard_manager_set_module_path (MsdSmartcardManager *manager, const char *module_path) { if ((manager->priv->module_path == NULL) && (module_path == NULL)) { @@ -236,57 +236,57 @@ gsd_smartcard_manager_set_module_path (GsdSmartcardManager *manager, } static void -gsd_smartcard_manager_card_removed_handler (GsdSmartcardManager *manager, - GsdSmartcard *card) +msd_smartcard_manager_card_removed_handler (MsdSmartcardManager *manager, + MsdSmartcard *card) { g_debug ("informing smartcard of its removal"); - _gsd_smartcard_set_state (card, GSD_SMARTCARD_STATE_REMOVED); + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_REMOVED); g_debug ("done"); } static void -gsd_smartcard_manager_card_inserted_handler (GsdSmartcardManager *manager, - GsdSmartcard *card) +msd_smartcard_manager_card_inserted_handler (MsdSmartcardManager *manager, + MsdSmartcard *card) { g_debug ("informing smartcard of its insertion"); - _gsd_smartcard_set_state (card, GSD_SMARTCARD_STATE_INSERTED); + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_INSERTED); g_debug ("done"); } static void -gsd_smartcard_manager_class_install_signals (GsdSmartcardManagerClass *manager_class) +msd_smartcard_manager_class_install_signals (MsdSmartcardManagerClass *manager_class) { GObjectClass *object_class; object_class = G_OBJECT_CLASS (manager_class); - gsd_smartcard_manager_signals[SMARTCARD_INSERTED] = + msd_smartcard_manager_signals[SMARTCARD_INSERTED] = g_signal_new ("smartcard-inserted", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (GsdSmartcardManagerClass, + G_STRUCT_OFFSET (MsdSmartcardManagerClass, smartcard_inserted), NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER); - manager_class->smartcard_inserted = gsd_smartcard_manager_card_inserted_handler; + manager_class->smartcard_inserted = msd_smartcard_manager_card_inserted_handler; - gsd_smartcard_manager_signals[SMARTCARD_REMOVED] = + msd_smartcard_manager_signals[SMARTCARD_REMOVED] = g_signal_new ("smartcard-removed", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (GsdSmartcardManagerClass, + G_STRUCT_OFFSET (MsdSmartcardManagerClass, smartcard_removed), NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER); - manager_class->smartcard_removed = gsd_smartcard_manager_card_removed_handler; + manager_class->smartcard_removed = msd_smartcard_manager_card_removed_handler; - gsd_smartcard_manager_signals[ERROR] = + msd_smartcard_manager_signals[ERROR] = g_signal_new ("error", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (GsdSmartcardManagerClass, error), + G_STRUCT_OFFSET (MsdSmartcardManagerClass, error), NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER); manager_class->error = NULL; @@ -324,13 +324,13 @@ slot_id_hash (CK_SLOT_ID *slot_id) } static void -gsd_smartcard_manager_init (GsdSmartcardManager *manager) +msd_smartcard_manager_init (MsdSmartcardManager *manager) { g_debug ("initializing smartcard manager"); manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager, - GSD_TYPE_SMARTCARD_MANAGER, - GsdSmartcardManagerPrivate); + MSD_TYPE_SMARTCARD_MANAGER, + MsdSmartcardManagerPrivate); manager->priv->poll_timeout_id = 0; manager->priv->is_unstoppable = FALSE; manager->priv->module = NULL; @@ -348,16 +348,16 @@ gsd_smartcard_manager_init (GsdSmartcardManager *manager) } static void -gsd_smartcard_manager_finalize (GObject *object) +msd_smartcard_manager_finalize (GObject *object) { - GsdSmartcardManager *manager; + MsdSmartcardManager *manager; GObjectClass *gobject_class; - manager = GSD_SMARTCARD_MANAGER (object); + manager = MSD_SMARTCARD_MANAGER (object); gobject_class = - G_OBJECT_CLASS (gsd_smartcard_manager_parent_class); + G_OBJECT_CLASS (msd_smartcard_manager_parent_class); - gsd_smartcard_manager_stop_now (manager); + msd_smartcard_manager_stop_now (manager); g_hash_table_destroy (manager->priv->smartcards); manager->priv->smartcards = NULL; @@ -366,23 +366,23 @@ gsd_smartcard_manager_finalize (GObject *object) } GQuark -gsd_smartcard_manager_error_quark (void) +msd_smartcard_manager_error_quark (void) { static GQuark error_quark = 0; if (error_quark == 0) { - error_quark = g_quark_from_static_string ("gsd-smartcard-manager-error-quark"); + error_quark = g_quark_from_static_string ("msd-smartcard-manager-error-quark"); } return error_quark; } -GsdSmartcardManager * -gsd_smartcard_manager_new (const char *module_path) +MsdSmartcardManager * +msd_smartcard_manager_new (const char *module_path) { - GsdSmartcardManager *instance; + MsdSmartcardManager *instance; - instance = GSD_SMARTCARD_MANAGER (g_object_new (GSD_TYPE_SMARTCARD_MANAGER, + instance = MSD_SMARTCARD_MANAGER (g_object_new (MSD_TYPE_SMARTCARD_MANAGER, "module-path", module_path, NULL)); @@ -390,44 +390,44 @@ gsd_smartcard_manager_new (const char *module_path) } static void -gsd_smartcard_manager_emit_error (GsdSmartcardManager *manager, +msd_smartcard_manager_emit_error (MsdSmartcardManager *manager, GError *error) { manager->priv->is_unstoppable = TRUE; - g_signal_emit (manager, gsd_smartcard_manager_signals[ERROR], 0, + g_signal_emit (manager, msd_smartcard_manager_signals[ERROR], 0, error); manager->priv->is_unstoppable = FALSE; } static void -gsd_smartcard_manager_emit_smartcard_inserted (GsdSmartcardManager *manager, - GsdSmartcard *card) +msd_smartcard_manager_emit_smartcard_inserted (MsdSmartcardManager *manager, + MsdSmartcard *card) { manager->priv->is_unstoppable = TRUE; - g_signal_emit (manager, gsd_smartcard_manager_signals[SMARTCARD_INSERTED], 0, + g_signal_emit (manager, msd_smartcard_manager_signals[SMARTCARD_INSERTED], 0, card); manager->priv->is_unstoppable = FALSE; } static void -gsd_smartcard_manager_emit_smartcard_removed (GsdSmartcardManager *manager, - GsdSmartcard *card) +msd_smartcard_manager_emit_smartcard_removed (MsdSmartcardManager *manager, + MsdSmartcard *card) { - GsdSmartcardManagerState old_state; + MsdSmartcardManagerState old_state; old_state = manager->priv->state; manager->priv->is_unstoppable = TRUE; - g_signal_emit (manager, gsd_smartcard_manager_signals[SMARTCARD_REMOVED], 0, + g_signal_emit (manager, msd_smartcard_manager_signals[SMARTCARD_REMOVED], 0, card); manager->priv->is_unstoppable = FALSE; } static gboolean -gsd_smartcard_manager_check_for_and_process_events (GIOChannel *io_channel, +msd_smartcard_manager_check_for_and_process_events (GIOChannel *io_channel, GIOCondition condition, - GsdSmartcardManager *manager) + MsdSmartcardManager *manager) { - GsdSmartcard *card; + MsdSmartcard *card; gboolean should_stop; gchar event_type; char *card_name; @@ -464,7 +464,7 @@ gsd_smartcard_manager_check_for_and_process_events (GIOChannel *io_chan goto out; } - card_name = gsd_smartcard_get_name (card); + card_name = msd_smartcard_get_name (card); switch (event_type) { case 'I': @@ -472,12 +472,12 @@ gsd_smartcard_manager_check_for_and_process_events (GIOChannel *io_chan card_name, card); card_name = NULL; - gsd_smartcard_manager_emit_smartcard_inserted (manager, card); + msd_smartcard_manager_emit_smartcard_inserted (manager, card); card = NULL; break; case 'R': - gsd_smartcard_manager_emit_smartcard_removed (manager, card); + msd_smartcard_manager_emit_smartcard_removed (manager, card); if (!g_hash_table_remove (manager->priv->smartcards, card_name)) { g_debug ("got removal event of unknown card!"); } @@ -499,13 +499,13 @@ out: if (should_stop) { GError *error; - error = g_error_new (GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, + error = g_error_new (MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, "%s", (condition & G_IO_IN) ? g_strerror (errno) : _("received error or hang up from event source")); - gsd_smartcard_manager_emit_error (manager, error); + msd_smartcard_manager_emit_error (manager, error); g_error_free (error); - gsd_smartcard_manager_stop_now (manager); + msd_smartcard_manager_stop_now (manager); return FALSE; } @@ -513,10 +513,10 @@ out: } static void -gsd_smartcard_manager_event_processing_stopped_handler (GsdSmartcardManager *manager) +msd_smartcard_manager_event_processing_stopped_handler (MsdSmartcardManager *manager) { manager->priv->smartcard_event_source = NULL; - gsd_smartcard_manager_stop_now (manager); + msd_smartcard_manager_stop_now (manager); } static gboolean @@ -551,7 +551,7 @@ open_pipe (int *write_fd, } static void -gsd_smartcard_manager_stop_watching_for_events (GsdSmartcardManager *manager) +msd_smartcard_manager_stop_watching_for_events (MsdSmartcardManager *manager) { if (manager->priv->smartcard_event_source != NULL) { g_source_destroy (manager->priv->smartcard_event_source); @@ -574,9 +574,9 @@ load_nss (GError **error) NSS_INIT_OPTIMIZESPACE | NSS_INIT_PK11RELOAD; g_debug ("attempting to load NSS database '%s'", - GSD_SMARTCARD_MANAGER_NSS_DB); + MSD_SMARTCARD_MANAGER_NSS_DB); - status = NSS_Initialize (GSD_SMARTCARD_MANAGER_NSS_DB, + status = NSS_Initialize (MSD_SMARTCARD_MANAGER_NSS_DB, "", "", SECMOD_DB, flags); if (status != SECSuccess) { @@ -588,8 +588,8 @@ load_nss (GError **error) if (error_message_size == 0) { g_debug ("NSS security system could not be initialized"); g_set_error (error, - GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, _("NSS security system could not be initialized")); goto out; } @@ -598,8 +598,8 @@ load_nss (GError **error) PR_GetErrorText (error_message); g_set_error (error, - GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, "%s", error_message); g_debug ("NSS security system could not be initialized - %s", error_message); @@ -657,8 +657,8 @@ load_driver (char *module_path, if (!module_explicitly_specified && module == NULL) { g_set_error (error, - GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, _("no suitable smartcard driver could be found")); } else if (module == NULL || !module->loaded) { @@ -677,8 +677,8 @@ load_driver (char *module_path, g_debug ("smartcard driver '%s' could not be loaded", module_path); g_set_error (error, - GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, _("smartcard driver '%s' could not be " "loaded"), module_path); goto out; @@ -688,8 +688,8 @@ load_driver (char *module_path, PR_GetErrorText (error_message); g_set_error (error, - GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, "%s", error_message); g_debug ("smartcard driver '%s' could not be loaded - %s", @@ -702,12 +702,12 @@ out: } static void -gsd_smartcard_manager_get_all_cards (GsdSmartcardManager *manager) +msd_smartcard_manager_get_all_cards (MsdSmartcardManager *manager) { int i; for (i = 0; i < manager->priv->module->slotCount; i++) { - GsdSmartcard *card; + MsdSmartcard *card; CK_SLOT_ID slot_id; int slot_series; char *card_name; @@ -715,10 +715,10 @@ gsd_smartcard_manager_get_all_cards (GsdSmartcardManager *manager) slot_id = PK11_GetSlotID (manager->priv->module->slots[i]); slot_series = PK11_GetSlotSeries (manager->priv->module->slots[i]); - card = _gsd_smartcard_new (manager->priv->module, + card = _msd_smartcard_new (manager->priv->module, slot_id, slot_series); - card_name = gsd_smartcard_get_name (card); + card_name = msd_smartcard_get_name (card); g_hash_table_replace (manager->priv->smartcards, card_name, card); @@ -726,7 +726,7 @@ gsd_smartcard_manager_get_all_cards (GsdSmartcardManager *manager) } gboolean -gsd_smartcard_manager_start (GsdSmartcardManager *manager, +msd_smartcard_manager_start (MsdSmartcardManager *manager, GError **error) { GError *watching_error; @@ -737,12 +737,12 @@ gsd_smartcard_manager_start (GsdSmartcardManager *manager, GIOFlags channel_flags; GError *nss_error; - if (manager->priv->state == GSD_SMARTCARD_MANAGER_STATE_STARTED) { + if (manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STARTED) { g_debug ("smartcard manager already started"); return TRUE; } - manager->priv->state = GSD_SMARTCARD_MANAGER_STATE_STARTING; + manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STARTING; worker_fd = -1; worker_pid = 0; @@ -763,11 +763,11 @@ gsd_smartcard_manager_start (GsdSmartcardManager *manager, goto out; } - if (!gsd_smartcard_manager_create_worker (manager, &worker_fd, &manager->priv->worker_thread)) { + if (!msd_smartcard_manager_create_worker (manager, &worker_fd, &manager->priv->worker_thread)) { g_set_error (error, - GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, _("could not watch for incoming card events - %s"), g_strerror (errno)); @@ -787,41 +787,41 @@ gsd_smartcard_manager_start (GsdSmartcardManager *manager, g_source_set_callback (manager->priv->smartcard_event_source, (GSourceFunc) (GIOFunc) - gsd_smartcard_manager_check_for_and_process_events, + msd_smartcard_manager_check_for_and_process_events, manager, (GDestroyNotify) - gsd_smartcard_manager_event_processing_stopped_handler); + msd_smartcard_manager_event_processing_stopped_handler); g_source_attach (manager->priv->smartcard_event_source, NULL); g_source_unref (manager->priv->smartcard_event_source); /* populate the hash with cards that are already inserted */ - gsd_smartcard_manager_get_all_cards (manager); + msd_smartcard_manager_get_all_cards (manager); - manager->priv->state = GSD_SMARTCARD_MANAGER_STATE_STARTED; + manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STARTED; out: /* don't leave it in a half started state */ - if (manager->priv->state != GSD_SMARTCARD_MANAGER_STATE_STARTED) { + if (manager->priv->state != MSD_SMARTCARD_MANAGER_STATE_STARTED) { g_debug ("smartcard manager could not be completely started"); - gsd_smartcard_manager_stop (manager); + msd_smartcard_manager_stop (manager); } else { g_debug ("smartcard manager started"); } - return manager->priv->state == GSD_SMARTCARD_MANAGER_STATE_STARTED; + return manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STARTED; } static gboolean -gsd_smartcard_manager_stop_now (GsdSmartcardManager *manager) +msd_smartcard_manager_stop_now (MsdSmartcardManager *manager) { - if (manager->priv->state == GSD_SMARTCARD_MANAGER_STATE_STOPPED) { + if (manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STOPPED) { return FALSE; } - manager->priv->state = GSD_SMARTCARD_MANAGER_STATE_STOPPED; - gsd_smartcard_manager_stop_watching_for_events (manager); + manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STOPPED; + msd_smartcard_manager_stop_watching_for_events (manager); if (manager->priv->module != NULL) { SECMOD_DestroyModule (manager->priv->module); @@ -839,44 +839,44 @@ gsd_smartcard_manager_stop_now (GsdSmartcardManager *manager) } static void -gsd_smartcard_manager_queue_stop (GsdSmartcardManager *manager) +msd_smartcard_manager_queue_stop (MsdSmartcardManager *manager) { - manager->priv->state = GSD_SMARTCARD_MANAGER_STATE_STOPPING; + manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STOPPING; - g_idle_add ((GSourceFunc) gsd_smartcard_manager_stop_now, manager); + g_idle_add ((GSourceFunc) msd_smartcard_manager_stop_now, manager); } void -gsd_smartcard_manager_stop (GsdSmartcardManager *manager) +msd_smartcard_manager_stop (MsdSmartcardManager *manager) { - if (manager->priv->state == GSD_SMARTCARD_MANAGER_STATE_STOPPED) { + if (manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STOPPED) { return; } if (manager->priv->is_unstoppable) { - gsd_smartcard_manager_queue_stop (manager); + msd_smartcard_manager_queue_stop (manager); return; } - gsd_smartcard_manager_stop_now (manager); + msd_smartcard_manager_stop_now (manager); } static void -gsd_smartcard_manager_check_for_login_card (CK_SLOT_ID slot_id, - GsdSmartcard *card, +msd_smartcard_manager_check_for_login_card (CK_SLOT_ID slot_id, + MsdSmartcard *card, gboolean *is_inserted) { g_assert (is_inserted != NULL); - if (gsd_smartcard_is_login_card (card)) { + if (msd_smartcard_is_login_card (card)) { *is_inserted = TRUE; } } gboolean -gsd_smartcard_manager_login_card_is_inserted (GsdSmartcardManager *manager) +msd_smartcard_manager_login_card_is_inserted (MsdSmartcardManager *manager) { gboolean is_inserted; @@ -884,17 +884,17 @@ gsd_smartcard_manager_login_card_is_inserted (GsdSmartcardManager *manager) is_inserted = FALSE; g_hash_table_foreach (manager->priv->smartcards, (GHFunc) - gsd_smartcard_manager_check_for_login_card, + msd_smartcard_manager_check_for_login_card, &is_inserted); return is_inserted; } -static GsdSmartcardManagerWorker * -gsd_smartcard_manager_worker_new (int write_fd) +static MsdSmartcardManagerWorker * +msd_smartcard_manager_worker_new (int write_fd) { - GsdSmartcardManagerWorker *worker; + MsdSmartcardManagerWorker *worker; - worker = g_slice_new0 (GsdSmartcardManagerWorker); + worker = g_slice_new0 (MsdSmartcardManagerWorker); worker->write_fd = write_fd; worker->module = NULL; @@ -908,14 +908,14 @@ gsd_smartcard_manager_worker_new (int write_fd) } static void -gsd_smartcard_manager_worker_free (GsdSmartcardManagerWorker *worker) +msd_smartcard_manager_worker_free (MsdSmartcardManagerWorker *worker) { if (worker->smartcards != NULL) { g_hash_table_destroy (worker->smartcards); worker->smartcards = NULL; } - g_slice_free (GsdSmartcardManagerWorker, worker); + g_slice_free (MsdSmartcardManagerWorker, worker); } static gboolean @@ -992,11 +992,11 @@ write_bytes (int fd, return TRUE; } -static GsdSmartcard * +static MsdSmartcard * read_smartcard (int fd, SECMODModule *module) { - GsdSmartcard *card; + MsdSmartcard *card; char *card_name; gsize card_name_size; @@ -1010,7 +1010,7 @@ read_smartcard (int fd, g_slice_free1 (card_name_size, card_name); return NULL; } - card = _gsd_smartcard_new_from_name (module, card_name); + card = _msd_smartcard_new_from_name (module, card_name); g_slice_free1 (card_name_size, card_name); return card; @@ -1018,12 +1018,12 @@ read_smartcard (int fd, static gboolean write_smartcard (int fd, - GsdSmartcard *card) + MsdSmartcard *card) { gsize card_name_size; char *card_name; - card_name = gsd_smartcard_get_name (card); + card_name = msd_smartcard_get_name (card); card_name_size = strlen (card_name) + 1; if (!write_bytes (fd, &card_name_size, sizeof (card_name_size))) { @@ -1041,11 +1041,11 @@ write_smartcard (int fd, } static gboolean -gsd_smartcard_manager_worker_emit_smartcard_removed (GsdSmartcardManagerWorker *worker, - GsdSmartcard *card, +msd_smartcard_manager_worker_emit_smartcard_removed (MsdSmartcardManagerWorker *worker, + MsdSmartcard *card, GError **error) { - g_debug ("card '%s' removed!", gsd_smartcard_get_name (card)); + g_debug ("card '%s' removed!", msd_smartcard_get_name (card)); if (!write_bytes (worker->write_fd, "R", 1)) { goto error_out; @@ -1058,21 +1058,21 @@ gsd_smartcard_manager_worker_emit_smartcard_removed (GsdSmartcardManagerWorker return TRUE; error_out: - g_set_error (error, GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS, + g_set_error (error, MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS, "%s", g_strerror (errno)); return FALSE; } static gboolean -gsd_smartcard_manager_worker_emit_smartcard_inserted (GsdSmartcardManagerWorker *worker, - GsdSmartcard *card, +msd_smartcard_manager_worker_emit_smartcard_inserted (MsdSmartcardManagerWorker *worker, + MsdSmartcard *card, GError **error) { GError *write_error; write_error = NULL; - g_debug ("card '%s' inserted!", gsd_smartcard_get_name (card)); + g_debug ("card '%s' inserted!", msd_smartcard_get_name (card)); if (!write_bytes (worker->write_fd, "I", 1)) { goto error_out; } @@ -1084,20 +1084,20 @@ gsd_smartcard_manager_worker_emit_smartcard_inserted (GsdSmartcardManagerWorker return TRUE; error_out: - g_set_error (error, GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS, + g_set_error (error, MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS, "%s", g_strerror (errno)); return FALSE; } static gboolean -gsd_smartcard_manager_worker_watch_for_and_process_event (GsdSmartcardManagerWorker *worker, +msd_smartcard_manager_worker_watch_for_and_process_event (MsdSmartcardManagerWorker *worker, GError **error) { PK11SlotInfo *slot; CK_SLOT_ID slot_id, *key; int slot_series, card_slot_series; - GsdSmartcard *card; + MsdSmartcard *card; GError *processing_error; gboolean ret; @@ -1119,8 +1119,8 @@ gsd_smartcard_manager_worker_watch_for_and_process_event (GsdSmartcardManagerWor /* FIXME: is there a function to convert from a PORT error * code to a translated string? */ - g_set_error (error, GSD_SMARTCARD_MANAGER_ERROR, - GSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, + g_set_error (error, MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, _("encountered unexpected error while " "waiting for smartcard events")); goto out; @@ -1141,7 +1141,7 @@ gsd_smartcard_manager_worker_watch_for_and_process_event (GsdSmartcardManagerWor card = g_hash_table_lookup (worker->smartcards, key); if (card != NULL) { - card_slot_series = gsd_smartcard_get_slot_series (card); + card_slot_series = msd_smartcard_get_slot_series (card); } else { card_slot_series = -1; } @@ -1154,20 +1154,20 @@ gsd_smartcard_manager_worker_watch_for_and_process_event (GsdSmartcardManagerWor */ if ((card != NULL) && card_slot_series != slot_series) { - if (!gsd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { + if (!msd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { g_propagate_error (error, processing_error); goto out; } } - card = _gsd_smartcard_new (worker->module, + card = _msd_smartcard_new (worker->module, slot_id, slot_series); g_hash_table_replace (worker->smartcards, key, card); key = NULL; - if (!gsd_smartcard_manager_worker_emit_smartcard_inserted (worker, + if (!msd_smartcard_manager_worker_emit_smartcard_inserted (worker, card, &processing_error)) { g_propagate_error (error, processing_error); @@ -1188,24 +1188,24 @@ gsd_smartcard_manager_worker_watch_for_and_process_event (GsdSmartcardManagerWor */ if ((slot_series - card_slot_series) > 1) { - if (!gsd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { + if (!msd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { g_propagate_error (error, processing_error); goto out; } g_hash_table_remove (worker->smartcards, key); - card = _gsd_smartcard_new (worker->module, + card = _msd_smartcard_new (worker->module, slot_id, slot_series); g_hash_table_replace (worker->smartcards, key, card); key = NULL; - if (!gsd_smartcard_manager_worker_emit_smartcard_inserted (worker, card, &processing_error)) { + if (!msd_smartcard_manager_worker_emit_smartcard_inserted (worker, card, &processing_error)) { g_propagate_error (error, processing_error); goto out; } } - if (!gsd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { + if (!msd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { g_propagate_error (error, processing_error); goto out; } @@ -1227,29 +1227,29 @@ out: } static void -gsd_smartcard_manager_worker_run (GsdSmartcardManagerWorker *worker) +msd_smartcard_manager_worker_run (MsdSmartcardManagerWorker *worker) { GError *error; error = NULL; - while (gsd_smartcard_manager_worker_watch_for_and_process_event (worker, &error)); + while (msd_smartcard_manager_worker_watch_for_and_process_event (worker, &error)); if (error != NULL) { g_debug ("could not process card event - %s", error->message); g_error_free (error); } - gsd_smartcard_manager_worker_free (worker); + msd_smartcard_manager_worker_free (worker); } static gboolean -gsd_smartcard_manager_create_worker (GsdSmartcardManager *manager, +msd_smartcard_manager_create_worker (MsdSmartcardManager *manager, int *worker_fd, GThread **worker_thread) { - GsdSmartcardManagerWorker *worker; + MsdSmartcardManagerWorker *worker; int write_fd, read_fd; write_fd = -1; @@ -1258,15 +1258,15 @@ gsd_smartcard_manager_create_worker (GsdSmartcardManager *manager, return FALSE; } - worker = gsd_smartcard_manager_worker_new (write_fd); + worker = msd_smartcard_manager_worker_new (write_fd); worker->module = manager->priv->module; *worker_thread = g_thread_create ((GThreadFunc) - gsd_smartcard_manager_worker_run, + msd_smartcard_manager_worker_run, worker, FALSE, NULL); if (*worker_thread == NULL) { - gsd_smartcard_manager_worker_free (worker); + msd_smartcard_manager_worker_free (worker); return FALSE; } @@ -1277,19 +1277,19 @@ gsd_smartcard_manager_create_worker (GsdSmartcardManager *manager, return TRUE; } -#ifdef GSD_SMARTCARD_MANAGER_ENABLE_TEST +#ifdef MSD_SMARTCARD_MANAGER_ENABLE_TEST #include static GMainLoop *event_loop; static gboolean should_exit_on_next_remove = FALSE; static gboolean -on_timeout (GsdSmartcardManager *manager) +on_timeout (MsdSmartcardManager *manager) { GError *error; g_print ("Re-enabling manager.\n"); - if (!gsd_smartcard_manager_start (manager, &error)) { + if (!msd_smartcard_manager_start (manager, &error)) { g_warning ("could not start smartcard manager - %s", error->message); g_error_free (error); @@ -1303,16 +1303,16 @@ on_timeout (GsdSmartcardManager *manager) } static void -on_device_inserted (GsdSmartcardManager *manager, - GsdSmartcard *card) +on_device_inserted (MsdSmartcardManager *manager, + MsdSmartcard *card) { g_print ("smartcard inserted!\n"); g_print ("Please remove it.\n"); } static void -on_device_removed (GsdSmartcardManager *manager, - GsdSmartcard *card) +on_device_removed (MsdSmartcardManager *manager, + MsdSmartcard *card) { g_print ("smartcard removed!\n"); @@ -1320,7 +1320,7 @@ on_device_removed (GsdSmartcardManager *manager, g_main_loop_quit (event_loop); } else { g_print ("disabling manager for 2 seconds\n"); - gsd_smartcard_manager_stop (manager); + msd_smartcard_manager_stop (manager); g_timeout_add (2000, (GSourceFunc) on_timeout, manager); } } @@ -1329,7 +1329,7 @@ int main (int argc, char *argv[]) { - GsdSmartcardManager *manager; + MsdSmartcardManager *manager; GError *error; g_log_set_always_fatal (G_LOG_LEVEL_ERROR @@ -1338,7 +1338,7 @@ main (int argc, g_type_init (); g_message ("creating instance of 'smartcard manager' object..."); - manager = gsd_smartcard_manager_new (NULL); + manager = msd_smartcard_manager_new (NULL); g_message ("'smartcard manager' object created successfully"); g_signal_connect (manager, "smartcard-inserted", @@ -1350,7 +1350,7 @@ main (int argc, g_message ("starting listener..."); error = NULL; - if (!gsd_smartcard_manager_start (manager, &error)) { + if (!msd_smartcard_manager_start (manager, &error)) { g_warning ("could not start smartcard manager - %s", error->message); g_error_free (error); diff --git a/plugins/smartcard/gsd-smartcard-manager.h b/plugins/smartcard/gsd-smartcard-manager.h index 9663124..269e1da 100644 --- a/plugins/smartcard/gsd-smartcard-manager.h +++ b/plugins/smartcard/gsd-smartcard-manager.h @@ -1,4 +1,4 @@ -/* gsd-smartcard-manager.h - object for monitoring smartcard insertion and +/* msd-smartcard-manager.h - object for monitoring smartcard insertion and * removal events * * Copyright (C) 2006, 2009 Red Hat, Inc. @@ -20,11 +20,11 @@ * * Written by: Ray Strode */ -#ifndef GSD_SMARTCARD_MANAGER_H -#define GSD_SMARTCARD_MANAGER_H +#ifndef MSD_SMARTCARD_MANAGER_H +#define MSD_SMARTCARD_MANAGER_H -#define GSD_SMARTCARD_ENABLE_INTERNAL_API -#include "gsd-smartcard.h" +#define MSD_SMARTCARD_ENABLE_INTERNAL_API +#include "msd-smartcard.h" #include #include @@ -32,59 +32,59 @@ #ifdef __cplusplus extern "C" { #endif -#define GSD_TYPE_SMARTCARD_MANAGER (gsd_smartcard_manager_get_type ()) -#define GSD_SMARTCARD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GSD_TYPE_SMARTCARD_MANAGER, GsdSmartcardManager)) -#define GSD_SMARTCARD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GSD_TYPE_SMARTCARD_MANAGER, GsdSmartcardManagerClass)) -#define GSD_IS_SMARTCARD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SC_TYPE_SMARTCARD_MANAGER)) -#define GSD_IS_SMARTCARD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SC_TYPE_SMARTCARD_MANAGER)) -#define GSD_SMARTCARD_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GSD_TYPE_SMARTCARD_MANAGER, GsdSmartcardManagerClass)) -#define GSD_SMARTCARD_MANAGER_ERROR (gsd_smartcard_manager_error_quark ()) -typedef struct _GsdSmartcardManager GsdSmartcardManager; -typedef struct _GsdSmartcardManagerClass GsdSmartcardManagerClass; -typedef struct _GsdSmartcardManagerPrivate GsdSmartcardManagerPrivate; -typedef enum _GsdSmartcardManagerError GsdSmartcardManagerError; +#define MSD_TYPE_SMARTCARD_MANAGER (msd_smartcard_manager_get_type ()) +#define MSD_SMARTCARD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MSD_TYPE_SMARTCARD_MANAGER, MsdSmartcardManager)) +#define MSD_SMARTCARD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MSD_TYPE_SMARTCARD_MANAGER, MsdSmartcardManagerClass)) +#define MSD_IS_SMARTCARD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SC_TYPE_SMARTCARD_MANAGER)) +#define MSD_IS_SMARTCARD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SC_TYPE_SMARTCARD_MANAGER)) +#define MSD_SMARTCARD_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), MSD_TYPE_SMARTCARD_MANAGER, MsdSmartcardManagerClass)) +#define MSD_SMARTCARD_MANAGER_ERROR (msd_smartcard_manager_error_quark ()) +typedef struct _MsdSmartcardManager MsdSmartcardManager; +typedef struct _MsdSmartcardManagerClass MsdSmartcardManagerClass; +typedef struct _MsdSmartcardManagerPrivate MsdSmartcardManagerPrivate; +typedef enum _MsdSmartcardManagerError MsdSmartcardManagerError; -struct _GsdSmartcardManager { +struct _MsdSmartcardManager { GObject parent; /*< private > */ - GsdSmartcardManagerPrivate *priv; + MsdSmartcardManagerPrivate *priv; }; -struct _GsdSmartcardManagerClass { +struct _MsdSmartcardManagerClass { GObjectClass parent_class; /* Signals */ - void (*smartcard_inserted) (GsdSmartcardManager *manager, - GsdSmartcard *token); - void (*smartcard_removed) (GsdSmartcardManager *manager, - GsdSmartcard *token); - void (*error) (GsdSmartcardManager *manager, + void (*smartcard_inserted) (MsdSmartcardManager *manager, + MsdSmartcard *token); + void (*smartcard_removed) (MsdSmartcardManager *manager, + MsdSmartcard *token); + void (*error) (MsdSmartcardManager *manager, GError *error); }; -enum _GsdSmartcardManagerError { - GSD_SMARTCARD_MANAGER_ERROR_GENERIC = 0, - GSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, - GSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, - GSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, - GSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS +enum _MsdSmartcardManagerError { + MSD_SMARTCARD_MANAGER_ERROR_GENERIC = 0, + MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, + MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, + MSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, + MSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS }; -GType gsd_smartcard_manager_get_type (void) G_GNUC_CONST; -GQuark gsd_smartcard_manager_error_quark (void) G_GNUC_CONST; +GType msd_smartcard_manager_get_type (void) G_GNUC_CONST; +GQuark msd_smartcard_manager_error_quark (void) G_GNUC_CONST; -GsdSmartcardManager *gsd_smartcard_manager_new (const char *module); +MsdSmartcardManager *msd_smartcard_manager_new (const char *module); -gboolean gsd_smartcard_manager_start (GsdSmartcardManager *manager, +gboolean msd_smartcard_manager_start (MsdSmartcardManager *manager, GError **error); -void gsd_smartcard_manager_stop (GsdSmartcardManager *manager); +void msd_smartcard_manager_stop (MsdSmartcardManager *manager); -char *gsd_smartcard_manager_get_module_path (GsdSmartcardManager *manager); -gboolean gsd_smartcard_manager_login_card_is_inserted (GsdSmartcardManager *manager); +char *msd_smartcard_manager_get_module_path (MsdSmartcardManager *manager); +gboolean msd_smartcard_manager_login_card_is_inserted (MsdSmartcardManager *manager); #ifdef __cplusplus } #endif -#endif /* GSD_SMARTCARD_MANAGER_H */ +#endif /* MSD_SMARTCARD_MANAGER_H */ diff --git a/plugins/smartcard/gsd-smartcard-plugin.c b/plugins/smartcard/gsd-smartcard-plugin.c index 9e3b4f2..dc4cb8f 100644 --- a/plugins/smartcard/gsd-smartcard-plugin.c +++ b/plugins/smartcard/gsd-smartcard-plugin.c @@ -31,11 +31,11 @@ #include #include "mate-settings-plugin.h" -#include "gsd-smartcard-plugin.h" -#include "gsd-smartcard-manager.h" +#include "msd-smartcard-plugin.h" +#include "msd-smartcard-manager.h" -struct GsdSmartcardPluginPrivate { - GsdSmartcardManager *manager; +struct MsdSmartcardPluginPrivate { + MsdSmartcardManager *manager; DBusGConnection *bus_connection; guint32 is_active : 1; @@ -43,10 +43,10 @@ struct GsdSmartcardPluginPrivate { typedef enum { - GSD_SMARTCARD_REMOVE_ACTION_NONE, - GSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN, - GSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT, -} GsdSmartcardRemoveAction; + MSD_SMARTCARD_REMOVE_ACTION_NONE, + MSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN, + MSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT, +} MsdSmartcardRemoveAction; #define SCREENSAVER_DBUS_NAME "org.mate.ScreenSaver" #define SCREENSAVER_DBUS_PATH "/" @@ -57,19 +57,19 @@ typedef enum #define SM_DBUS_INTERFACE "org.mate.SessionManager" #define SM_LOGOUT_MODE_FORCE 2 -#define GSD_SMARTCARD_KEY "/desktop/mate/peripherals/smartcard" -#define KEY_REMOVE_ACTION GSD_SMARTCARD_KEY "/removal_action" +#define MSD_SMARTCARD_KEY "/desktop/mate/peripherals/smartcard" +#define KEY_REMOVE_ACTION MSD_SMARTCARD_KEY "/removal_action" -#define GSD_SMARTCARD_PLUGIN_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), GSD_TYPE_SMARTCARD_PLUGIN, GsdSmartcardPluginPrivate)) +#define MSD_SMARTCARD_PLUGIN_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPluginPrivate)) -MATE_SETTINGS_PLUGIN_REGISTER (GsdSmartcardPlugin, gsd_smartcard_plugin); +MATE_SETTINGS_PLUGIN_REGISTER (MsdSmartcardPlugin, msd_smartcard_plugin); static void -simulate_user_activity (GsdSmartcardPlugin *plugin) +simulate_user_activity (MsdSmartcardPlugin *plugin) { DBusGProxy *screensaver_proxy; - g_debug ("GsdSmartcardPlugin telling screensaver about smart card insertion"); + g_debug ("MsdSmartcardPlugin telling screensaver about smart card insertion"); screensaver_proxy = dbus_g_proxy_new_for_name (plugin->priv->bus_connection, SCREENSAVER_DBUS_NAME, SCREENSAVER_DBUS_PATH, @@ -83,11 +83,11 @@ simulate_user_activity (GsdSmartcardPlugin *plugin) } static void -lock_screen (GsdSmartcardPlugin *plugin) +lock_screen (MsdSmartcardPlugin *plugin) { DBusGProxy *screensaver_proxy; - g_debug ("GsdSmartcardPlugin telling screensaver to lock screen"); + g_debug ("MsdSmartcardPlugin telling screensaver to lock screen"); screensaver_proxy = dbus_g_proxy_new_for_name (plugin->priv->bus_connection, SCREENSAVER_DBUS_NAME, SCREENSAVER_DBUS_PATH, @@ -101,13 +101,13 @@ lock_screen (GsdSmartcardPlugin *plugin) } static void -force_logout (GsdSmartcardPlugin *plugin) +force_logout (MsdSmartcardPlugin *plugin) { DBusGProxy *sm_proxy; GError *error; gboolean res; - g_debug ("GsdSmartcardPlugin telling session manager to force logout"); + g_debug ("MsdSmartcardPlugin telling session manager to force logout"); sm_proxy = dbus_g_proxy_new_for_name (plugin->priv->bus_connection, SM_DBUS_NAME, SM_DBUS_PATH, @@ -121,7 +121,7 @@ force_logout (GsdSmartcardPlugin *plugin) G_TYPE_INVALID, G_TYPE_INVALID); if (! res) { - g_warning ("GsdSmartcardPlugin Unable to force logout: %s", error->message); + g_warning ("MsdSmartcardPlugin Unable to force logout: %s", error->message); g_error_free (error); } @@ -129,26 +129,26 @@ force_logout (GsdSmartcardPlugin *plugin) } static void -gsd_smartcard_plugin_init (GsdSmartcardPlugin *plugin) +msd_smartcard_plugin_init (MsdSmartcardPlugin *plugin) { - plugin->priv = GSD_SMARTCARD_PLUGIN_GET_PRIVATE (plugin); + plugin->priv = MSD_SMARTCARD_PLUGIN_GET_PRIVATE (plugin); - g_debug ("GsdSmartcardPlugin initializing"); + g_debug ("MsdSmartcardPlugin initializing"); - plugin->priv->manager = gsd_smartcard_manager_new (NULL); + plugin->priv->manager = msd_smartcard_manager_new (NULL); } static void -gsd_smartcard_plugin_finalize (GObject *object) +msd_smartcard_plugin_finalize (GObject *object) { - GsdSmartcardPlugin *plugin; + MsdSmartcardPlugin *plugin; g_return_if_fail (object != NULL); - g_return_if_fail (GSD_IS_SMARTCARD_PLUGIN (object)); + g_return_if_fail (MSD_IS_SMARTCARD_PLUGIN (object)); - g_debug ("GsdSmartcardPlugin finalizing"); + g_debug ("MsdSmartcardPlugin finalizing"); - plugin = GSD_SMARTCARD_PLUGIN (object); + plugin = MSD_SMARTCARD_PLUGIN (object); g_return_if_fail (plugin->priv != NULL); @@ -156,18 +156,18 @@ gsd_smartcard_plugin_finalize (GObject *object) g_object_unref (plugin->priv->manager); } - G_OBJECT_CLASS (gsd_smartcard_plugin_parent_class)->finalize (object); + G_OBJECT_CLASS (msd_smartcard_plugin_parent_class)->finalize (object); } static void -smartcard_inserted_cb (GsdSmartcardManager *card_monitor, - GsdSmartcard *card, - GsdSmartcardPlugin *plugin) +smartcard_inserted_cb (MsdSmartcardManager *card_monitor, + MsdSmartcard *card, + MsdSmartcardPlugin *plugin) { char *name; - name = gsd_smartcard_get_name (card); - g_debug ("GsdSmartcardPlugin smart card '%s' inserted", name); + name = msd_smartcard_get_name (card); + g_debug ("MsdSmartcardPlugin smart card '%s' inserted", name); g_free (name); simulate_user_activity (plugin); @@ -179,29 +179,29 @@ user_logged_in_with_smartcard (void) return g_getenv ("PKCS11_LOGIN_TOKEN_NAME") != NULL; } -static GsdSmartcardRemoveAction -get_configured_remove_action (GsdSmartcardPlugin *plugin) +static MsdSmartcardRemoveAction +get_configured_remove_action (MsdSmartcardPlugin *plugin) { MateConfClient *client; char *remove_action_string; - GsdSmartcardRemoveAction remove_action; + MsdSmartcardRemoveAction remove_action; client = mateconf_client_get_default (); remove_action_string = mateconf_client_get_string (client, KEY_REMOVE_ACTION, NULL); if (remove_action_string == NULL) { - g_warning ("GsdSmartcardPlugin unable to get smartcard remove action"); - remove_action = GSD_SMARTCARD_REMOVE_ACTION_NONE; + g_warning ("MsdSmartcardPlugin unable to get smartcard remove action"); + remove_action = MSD_SMARTCARD_REMOVE_ACTION_NONE; } else if (strcmp (remove_action_string, "none") == 0) { - remove_action = GSD_SMARTCARD_REMOVE_ACTION_NONE; + remove_action = MSD_SMARTCARD_REMOVE_ACTION_NONE; } else if (strcmp (remove_action_string, "lock_screen") == 0) { - remove_action = GSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN; + remove_action = MSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN; } else if (strcmp (remove_action_string, "force_logout") == 0) { - remove_action = GSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT; + remove_action = MSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT; } else { - g_warning ("GsdSmartcardPlugin unknown smartcard remove action"); - remove_action = GSD_SMARTCARD_REMOVE_ACTION_NONE; + g_warning ("MsdSmartcardPlugin unknown smartcard remove action"); + remove_action = MSD_SMARTCARD_REMOVE_ACTION_NONE; } g_object_unref (client); @@ -210,40 +210,40 @@ get_configured_remove_action (GsdSmartcardPlugin *plugin) } static void -process_smartcard_removal (GsdSmartcardPlugin *plugin) +process_smartcard_removal (MsdSmartcardPlugin *plugin) { - GsdSmartcardRemoveAction remove_action; + MsdSmartcardRemoveAction remove_action; - g_debug ("GsdSmartcardPlugin processing smartcard removal"); + g_debug ("MsdSmartcardPlugin processing smartcard removal"); remove_action = get_configured_remove_action (plugin); switch (remove_action) { - case GSD_SMARTCARD_REMOVE_ACTION_NONE: + case MSD_SMARTCARD_REMOVE_ACTION_NONE: return; - case GSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN: + case MSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN: lock_screen (plugin); break; - case GSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT: + case MSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT: force_logout (plugin); break; } } static void -smartcard_removed_cb (GsdSmartcardManager *card_monitor, - GsdSmartcard *card, - GsdSmartcardPlugin *plugin) +smartcard_removed_cb (MsdSmartcardManager *card_monitor, + MsdSmartcard *card, + MsdSmartcardPlugin *plugin) { char *name; - name = gsd_smartcard_get_name (card); - g_debug ("GsdSmartcardPlugin smart card '%s' removed", name); + name = msd_smartcard_get_name (card); + g_debug ("MsdSmartcardPlugin smart card '%s' removed", name); g_free (name); - if (!gsd_smartcard_is_login_card (card)) { - g_debug ("GsdSmartcardPlugin removed smart card was not used to login"); + if (!msd_smartcard_is_login_card (card)) { + g_debug ("MsdSmartcardPlugin removed smart card was not used to login"); return; } @@ -254,33 +254,33 @@ static void impl_activate (MateSettingsPlugin *plugin) { GError *error; - GsdSmartcardPlugin *smartcard_plugin = GSD_SMARTCARD_PLUGIN (plugin); + MsdSmartcardPlugin *smartcard_plugin = MSD_SMARTCARD_PLUGIN (plugin); if (smartcard_plugin->priv->is_active) { - g_debug ("GsdSmartcardPlugin Not activating smartcard plugin, because it's " + g_debug ("MsdSmartcardPlugin Not activating smartcard plugin, because it's " "already active"); return; } if (!user_logged_in_with_smartcard ()) { - g_debug ("GsdSmartcardPlugin Not activating smartcard plugin, because user didn't use " + g_debug ("MsdSmartcardPlugin Not activating smartcard plugin, because user didn't use " " smartcard to log in"); smartcard_plugin->priv->is_active = FALSE; return; } - g_debug ("GsdSmartcardPlugin Activating smartcard plugin"); + g_debug ("MsdSmartcardPlugin Activating smartcard plugin"); error = NULL; smartcard_plugin->priv->bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (smartcard_plugin->priv->bus_connection == NULL) { - g_warning ("GsdSmartcardPlugin Unable to connect to session bus: %s", error->message); + g_warning ("MsdSmartcardPlugin Unable to connect to session bus: %s", error->message); return; } - if (!gsd_smartcard_manager_start (smartcard_plugin->priv->manager, &error)) { - g_warning ("GsdSmartcardPlugin Unable to start smartcard manager: %s", error->message); + if (!msd_smartcard_manager_start (smartcard_plugin->priv->manager, &error)) { + g_warning ("MsdSmartcardPlugin Unable to start smartcard manager: %s", error->message); g_error_free (error); } @@ -292,8 +292,8 @@ impl_activate (MateSettingsPlugin *plugin) "smartcard-inserted", G_CALLBACK (smartcard_inserted_cb), smartcard_plugin); - if (!gsd_smartcard_manager_login_card_is_inserted (smartcard_plugin->priv->manager)) { - g_debug ("GsdSmartcardPlugin processing smartcard removal immediately user logged in with smartcard " + if (!msd_smartcard_manager_login_card_is_inserted (smartcard_plugin->priv->manager)) { + g_debug ("MsdSmartcardPlugin processing smartcard removal immediately user logged in with smartcard " "and it's not inserted"); process_smartcard_removal (smartcard_plugin); } @@ -304,17 +304,17 @@ impl_activate (MateSettingsPlugin *plugin) static void impl_deactivate (MateSettingsPlugin *plugin) { - GsdSmartcardPlugin *smartcard_plugin = GSD_SMARTCARD_PLUGIN (plugin); + MsdSmartcardPlugin *smartcard_plugin = MSD_SMARTCARD_PLUGIN (plugin); if (!smartcard_plugin->priv->is_active) { - g_debug ("GsdSmartcardPlugin Not deactivating smartcard plugin, " + g_debug ("MsdSmartcardPlugin Not deactivating smartcard plugin, " "because it's already inactive"); return; } - g_debug ("GsdSmartcardPlugin Deactivating smartcard plugin"); + g_debug ("MsdSmartcardPlugin Deactivating smartcard plugin"); - gsd_smartcard_manager_stop (smartcard_plugin->priv->manager); + msd_smartcard_manager_stop (smartcard_plugin->priv->manager); g_signal_handlers_disconnect_by_func (smartcard_plugin->priv->manager, smartcard_removed_cb, smartcard_plugin); @@ -326,15 +326,15 @@ impl_deactivate (MateSettingsPlugin *plugin) } static void -gsd_smartcard_plugin_class_init (GsdSmartcardPluginClass *klass) +msd_smartcard_plugin_class_init (MsdSmartcardPluginClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); MateSettingsPluginClass *plugin_class = MATE_SETTINGS_PLUGIN_CLASS (klass); - object_class->finalize = gsd_smartcard_plugin_finalize; + object_class->finalize = msd_smartcard_plugin_finalize; plugin_class->activate = impl_activate; plugin_class->deactivate = impl_deactivate; - g_type_class_add_private (klass, sizeof (GsdSmartcardPluginPrivate)); + g_type_class_add_private (klass, sizeof (MsdSmartcardPluginPrivate)); } diff --git a/plugins/smartcard/gsd-smartcard-plugin.h b/plugins/smartcard/gsd-smartcard-plugin.h index 4c61686..206e58d 100644 --- a/plugins/smartcard/gsd-smartcard-plugin.h +++ b/plugins/smartcard/gsd-smartcard-plugin.h @@ -18,8 +18,8 @@ * */ -#ifndef __GSD_SMARTCARD_PLUGIN_H__ -#define __GSD_SMARTCARD_PLUGIN_H__ +#ifndef __MSD_SMARTCARD_PLUGIN_H__ +#define __MSD_SMARTCARD_PLUGIN_H__ #include #include @@ -31,27 +31,27 @@ extern "C" { #endif -#define GSD_TYPE_SMARTCARD_PLUGIN (gsd_smartcard_plugin_get_type ()) -#define GSD_SMARTCARD_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GSD_TYPE_SMARTCARD_PLUGIN, GsdSmartcardPlugin)) -#define GSD_SMARTCARD_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GSD_TYPE_SMARTCARD_PLUGIN, GsdSmartcardPluginClass)) -#define GSD_IS_SMARTCARD_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GSD_TYPE_SMARTCARD_PLUGIN)) -#define GSD_IS_SMARTCARD_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GSD_TYPE_SMARTCARD_PLUGIN)) -#define GSD_SMARTCARD_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GSD_TYPE_SMARTCARD_PLUGIN, GsdSmartcardPluginClass)) +#define MSD_TYPE_SMARTCARD_PLUGIN (msd_smartcard_plugin_get_type ()) +#define MSD_SMARTCARD_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPlugin)) +#define MSD_SMARTCARD_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPluginClass)) +#define MSD_IS_SMARTCARD_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MSD_TYPE_SMARTCARD_PLUGIN)) +#define MSD_IS_SMARTCARD_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), MSD_TYPE_SMARTCARD_PLUGIN)) +#define MSD_SMARTCARD_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPluginClass)) -typedef struct GsdSmartcardPluginPrivate GsdSmartcardPluginPrivate; +typedef struct MsdSmartcardPluginPrivate MsdSmartcardPluginPrivate; typedef struct { MateSettingsPlugin parent; - GsdSmartcardPluginPrivate *priv; -} GsdSmartcardPlugin; + MsdSmartcardPluginPrivate *priv; +} MsdSmartcardPlugin; typedef struct { MateSettingsPluginClass parent_class; -} GsdSmartcardPluginClass; +} MsdSmartcardPluginClass; -GType gsd_smartcard_plugin_get_type (void) G_GNUC_CONST; +GType msd_smartcard_plugin_get_type (void) G_GNUC_CONST; /* All the plugins must implement this function */ G_MODULE_EXPORT GType register_mate_settings_plugin (GTypeModule *module); @@ -60,4 +60,4 @@ G_MODULE_EXPORT GType register_mate_settings_plugin (GTypeModule *module); } #endif -#endif /* __GSD_SMARTCARD_PLUGIN_H__ */ +#endif /* __MSD_SMARTCARD_PLUGIN_H__ */ diff --git a/plugins/smartcard/gsd-smartcard.c b/plugins/smartcard/gsd-smartcard.c index 22f4e12..9154200 100644 --- a/plugins/smartcard/gsd-smartcard.c +++ b/plugins/smartcard/gsd-smartcard.c @@ -1,4 +1,4 @@ -/* gsd-smartcard.c - smartcard object +/* msd-smartcard.c - smartcard object * * Copyright (C) 2006 Ray Strode * @@ -17,8 +17,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ -#define GSD_SMARTCARD_ENABLE_INTERNAL_API -#include "gsd-smartcard.h" +#define MSD_SMARTCARD_ENABLE_INTERNAL_API +#include "msd-smartcard.h" #include #include @@ -34,9 +34,9 @@ #include #include -struct _GsdSmartcardPrivate { +struct _MsdSmartcardPrivate { SECMODModule *module; - GsdSmartcardState state; + MsdSmartcardState state; CK_SLOT_ID slot_id; int slot_series; @@ -48,36 +48,36 @@ struct _GsdSmartcardPrivate { CERTCertificate *encryption_certificate; }; -static void gsd_smartcard_finalize (GObject *object); -static void gsd_smartcard_class_install_signals (GsdSmartcardClass *card_class); -static void gsd_smartcard_class_install_properties (GsdSmartcardClass *card_class); -static void gsd_smartcard_set_property (GObject *object, +static void msd_smartcard_finalize (GObject *object); +static void msd_smartcard_class_install_signals (MsdSmartcardClass *card_class); +static void msd_smartcard_class_install_properties (MsdSmartcardClass *card_class); +static void msd_smartcard_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); -static void gsd_smartcard_get_property (GObject *object, +static void msd_smartcard_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); -static void gsd_smartcard_set_name (GsdSmartcard *card, const char *name); -static void gsd_smartcard_set_slot_id (GsdSmartcard *card, +static void msd_smartcard_set_name (MsdSmartcard *card, const char *name); +static void msd_smartcard_set_slot_id (MsdSmartcard *card, int slot_id); -static void gsd_smartcard_set_slot_series (GsdSmartcard *card, +static void msd_smartcard_set_slot_series (MsdSmartcard *card, int slot_series); -static void gsd_smartcard_set_module (GsdSmartcard *card, +static void msd_smartcard_set_module (MsdSmartcard *card, SECMODModule *module); -static PK11SlotInfo *gsd_smartcard_find_slot_from_id (GsdSmartcard *card, +static PK11SlotInfo *msd_smartcard_find_slot_from_id (MsdSmartcard *card, int slot_id); -static PK11SlotInfo *gsd_smartcard_find_slot_from_card_name (GsdSmartcard *card, +static PK11SlotInfo *msd_smartcard_find_slot_from_card_name (MsdSmartcard *card, const char *card_name); -#ifndef GSD_SMARTCARD_DEFAULT_SLOT_ID -#define GSD_SMARTCARD_DEFAULT_SLOT_ID ((gulong) -1) +#ifndef MSD_SMARTCARD_DEFAULT_SLOT_ID +#define MSD_SMARTCARD_DEFAULT_SLOT_ID ((gulong) -1) #endif -#ifndef GSD_SMARTCARD_DEFAULT_SLOT_SERIES -#define GSD_SMARTCARD_DEFAULT_SLOT_SERIES -1 +#ifndef MSD_SMARTCARD_DEFAULT_SLOT_SERIES +#define MSD_SMARTCARD_DEFAULT_SLOT_SERIES -1 #endif enum { @@ -95,73 +95,73 @@ enum { NUMBER_OF_SIGNALS }; -static guint gsd_smartcard_signals[NUMBER_OF_SIGNALS]; +static guint msd_smartcard_signals[NUMBER_OF_SIGNALS]; -G_DEFINE_TYPE (GsdSmartcard, gsd_smartcard, G_TYPE_OBJECT); +G_DEFINE_TYPE (MsdSmartcard, msd_smartcard, G_TYPE_OBJECT); static void -gsd_smartcard_class_init (GsdSmartcardClass *card_class) +msd_smartcard_class_init (MsdSmartcardClass *card_class) { GObjectClass *gobject_class; gobject_class = G_OBJECT_CLASS (card_class); - gobject_class->finalize = gsd_smartcard_finalize; + gobject_class->finalize = msd_smartcard_finalize; - gsd_smartcard_class_install_signals (card_class); - gsd_smartcard_class_install_properties (card_class); + msd_smartcard_class_install_signals (card_class); + msd_smartcard_class_install_properties (card_class); g_type_class_add_private (card_class, - sizeof (GsdSmartcardPrivate)); + sizeof (MsdSmartcardPrivate)); } static void -gsd_smartcard_class_install_signals (GsdSmartcardClass *card_class) +msd_smartcard_class_install_signals (MsdSmartcardClass *card_class) { GObjectClass *object_class; object_class = G_OBJECT_CLASS (card_class); - gsd_smartcard_signals[INSERTED] = + msd_smartcard_signals[INSERTED] = g_signal_new ("inserted", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (GsdSmartcardClass, + G_STRUCT_OFFSET (MsdSmartcardClass, inserted), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); - gsd_smartcard_signals[REMOVED] = + msd_smartcard_signals[REMOVED] = g_signal_new ("removed", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (GsdSmartcardClass, + G_STRUCT_OFFSET (MsdSmartcardClass, removed), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); } static void -gsd_smartcard_class_install_properties (GsdSmartcardClass *card_class) +msd_smartcard_class_install_properties (MsdSmartcardClass *card_class) { GObjectClass *object_class; GParamSpec *param_spec; object_class = G_OBJECT_CLASS (card_class); - object_class->set_property = gsd_smartcard_set_property; - object_class->get_property = gsd_smartcard_get_property; + object_class->set_property = msd_smartcard_set_property; + object_class->get_property = msd_smartcard_get_property; param_spec = g_param_spec_ulong ("slot-id", _("Slot ID"), _("The slot the card is in"), 1, G_MAXULONG, - GSD_SMARTCARD_DEFAULT_SLOT_ID, + MSD_SMARTCARD_DEFAULT_SLOT_ID, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); g_object_class_install_property (object_class, PROP_SLOT_ID, param_spec); param_spec = g_param_spec_int ("slot-series", _("Slot Series"), _("per-slot card identifier"), -1, G_MAXINT, - GSD_SMARTCARD_DEFAULT_SLOT_SERIES, + MSD_SMARTCARD_DEFAULT_SLOT_SERIES, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); g_object_class_install_property (object_class, PROP_SLOT_SERIES, param_spec); @@ -177,30 +177,30 @@ gsd_smartcard_class_install_properties (GsdSmartcardClass *card_class) } static void -gsd_smartcard_set_property (GObject *object, +msd_smartcard_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { - GsdSmartcard *card = GSD_SMARTCARD (object); + MsdSmartcard *card = MSD_SMARTCARD (object); switch (prop_id) { case PROP_NAME: - gsd_smartcard_set_name (card, g_value_get_string (value)); + msd_smartcard_set_name (card, g_value_get_string (value)); break; case PROP_SLOT_ID: - gsd_smartcard_set_slot_id (card, + msd_smartcard_set_slot_id (card, g_value_get_ulong (value)); break; case PROP_SLOT_SERIES: - gsd_smartcard_set_slot_series (card, + msd_smartcard_set_slot_series (card, g_value_get_int (value)); break; case PROP_MODULE: - gsd_smartcard_set_module (card, + msd_smartcard_set_module (card, (SECMODModule *) g_value_get_pointer (value)); break; @@ -211,25 +211,25 @@ gsd_smartcard_set_property (GObject *object, } CK_SLOT_ID -gsd_smartcard_get_slot_id (GsdSmartcard *card) +msd_smartcard_get_slot_id (MsdSmartcard *card) { return card->priv->slot_id; } -GsdSmartcardState -gsd_smartcard_get_state (GsdSmartcard *card) +MsdSmartcardState +msd_smartcard_get_state (MsdSmartcard *card) { return card->priv->state; } char * -gsd_smartcard_get_name (GsdSmartcard *card) +msd_smartcard_get_name (MsdSmartcard *card) { return g_strdup (card->priv->name); } gboolean -gsd_smartcard_is_login_card (GsdSmartcard *card) +msd_smartcard_is_login_card (MsdSmartcard *card) { const char *login_card_name; login_card_name = g_getenv ("PKCS11_LOGIN_TOKEN_NAME"); @@ -246,27 +246,27 @@ gsd_smartcard_is_login_card (GsdSmartcard *card) } static void -gsd_smartcard_get_property (GObject *object, +msd_smartcard_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { - GsdSmartcard *card = GSD_SMARTCARD (object); + MsdSmartcard *card = MSD_SMARTCARD (object); switch (prop_id) { case PROP_NAME: g_value_take_string (value, - gsd_smartcard_get_name (card)); + msd_smartcard_get_name (card)); break; case PROP_SLOT_ID: g_value_set_ulong (value, - (gulong) gsd_smartcard_get_slot_id (card)); + (gulong) msd_smartcard_get_slot_id (card)); break; case PROP_SLOT_SERIES: g_value_set_int (value, - gsd_smartcard_get_slot_series (card)); + msd_smartcard_get_slot_series (card)); break; default: @@ -275,7 +275,7 @@ gsd_smartcard_get_property (GObject *object, } static void -gsd_smartcard_set_name (GsdSmartcard *card, +msd_smartcard_set_name (MsdSmartcard *card, const char *name) { if (name == NULL) { @@ -288,7 +288,7 @@ gsd_smartcard_set_name (GsdSmartcard *card, card->priv->name = g_strdup (name); if (card->priv->slot == NULL) { - card->priv->slot = gsd_smartcard_find_slot_from_card_name (card, + card->priv->slot = msd_smartcard_find_slot_from_card_name (card, card->priv->name); if (card->priv->slot != NULL) { @@ -296,17 +296,17 @@ gsd_smartcard_set_name (GsdSmartcard *card, slot_id = PK11_GetSlotID (card->priv->slot); if (slot_id != card->priv->slot_id) { - gsd_smartcard_set_slot_id (card, slot_id); + msd_smartcard_set_slot_id (card, slot_id); } slot_series = PK11_GetSlotSeries (card->priv->slot); if (slot_series != card->priv->slot_series) { - gsd_smartcard_set_slot_series (card, slot_series); + msd_smartcard_set_slot_series (card, slot_series); } - _gsd_smartcard_set_state (card, GSD_SMARTCARD_STATE_INSERTED); + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_INSERTED); } else { - _gsd_smartcard_set_state (card, GSD_SMARTCARD_STATE_REMOVED); + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_REMOVED); } } @@ -315,14 +315,14 @@ gsd_smartcard_set_name (GsdSmartcard *card, } static void -gsd_smartcard_set_slot_id (GsdSmartcard *card, +msd_smartcard_set_slot_id (MsdSmartcard *card, int slot_id) { if (card->priv->slot_id != slot_id) { card->priv->slot_id = slot_id; if (card->priv->slot == NULL) { - card->priv->slot = gsd_smartcard_find_slot_from_id (card, + card->priv->slot = msd_smartcard_find_slot_from_id (card, card->priv->slot_id); if (card->priv->slot != NULL) { @@ -332,12 +332,12 @@ gsd_smartcard_set_slot_id (GsdSmartcard *card, if ((card->priv->name == NULL) || ((card_name != NULL) && (strcmp (card_name, card->priv->name) != 0))) { - gsd_smartcard_set_name (card, card_name); + msd_smartcard_set_name (card, card_name); } - _gsd_smartcard_set_state (card, GSD_SMARTCARD_STATE_INSERTED); + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_INSERTED); } else { - _gsd_smartcard_set_state (card, GSD_SMARTCARD_STATE_REMOVED); + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_REMOVED); } } @@ -346,7 +346,7 @@ gsd_smartcard_set_slot_id (GsdSmartcard *card, } static void -gsd_smartcard_set_slot_series (GsdSmartcard *card, +msd_smartcard_set_slot_series (MsdSmartcard *card, int slot_series) { if (card->priv->slot_series != slot_series) { @@ -356,7 +356,7 @@ gsd_smartcard_set_slot_series (GsdSmartcard *card, } static void -gsd_smartcard_set_module (GsdSmartcard *card, +msd_smartcard_set_module (MsdSmartcard *card, SECMODModule *module) { gboolean should_notify; @@ -382,66 +382,66 @@ gsd_smartcard_set_module (GsdSmartcard *card, } int -gsd_smartcard_get_slot_series (GsdSmartcard *card) +msd_smartcard_get_slot_series (MsdSmartcard *card) { return card->priv->slot_series; } static void -gsd_smartcard_init (GsdSmartcard *card) +msd_smartcard_init (MsdSmartcard *card) { g_debug ("initializing smartcard "); card->priv = G_TYPE_INSTANCE_GET_PRIVATE (card, - GSD_TYPE_SMARTCARD, - GsdSmartcardPrivate); + MSD_TYPE_SMARTCARD, + MsdSmartcardPrivate); if (card->priv->slot != NULL) { card->priv->name = g_strdup (PK11_GetTokenName (card->priv->slot)); } } -static void gsd_smartcard_finalize (GObject *object) +static void msd_smartcard_finalize (GObject *object) { - GsdSmartcard *card; + MsdSmartcard *card; GObjectClass *gobject_class; - card = GSD_SMARTCARD (object); + card = MSD_SMARTCARD (object); g_free (card->priv->name); - gsd_smartcard_set_module (card, NULL); + msd_smartcard_set_module (card, NULL); - gobject_class = G_OBJECT_CLASS (gsd_smartcard_parent_class); + gobject_class = G_OBJECT_CLASS (msd_smartcard_parent_class); gobject_class->finalize (object); } -GQuark gsd_smartcard_error_quark (void) +GQuark msd_smartcard_error_quark (void) { static GQuark error_quark = 0; if (error_quark == 0) { - error_quark = g_quark_from_static_string ("gsd-smartcard-error-quark"); + error_quark = g_quark_from_static_string ("msd-smartcard-error-quark"); } return error_quark; } -GsdSmartcard * -_gsd_smartcard_new (SECMODModule *module, +MsdSmartcard * +_msd_smartcard_new (SECMODModule *module, CK_SLOT_ID slot_id, int slot_series) { - GsdSmartcard *card; + MsdSmartcard *card; g_return_val_if_fail (module != NULL, NULL); g_return_val_if_fail (slot_id >= 1, NULL); g_return_val_if_fail (slot_series > 0, NULL); g_return_val_if_fail (sizeof (gulong) == sizeof (slot_id), NULL); - card = GSD_SMARTCARD (g_object_new (GSD_TYPE_SMARTCARD, + card = MSD_SMARTCARD (g_object_new (MSD_TYPE_SMARTCARD, "module", module, "slot-id", (gulong) slot_id, "slot-series", slot_series, @@ -449,16 +449,16 @@ _gsd_smartcard_new (SECMODModule *module, return card; } -GsdSmartcard * -_gsd_smartcard_new_from_name (SECMODModule *module, +MsdSmartcard * +_msd_smartcard_new_from_name (SECMODModule *module, const char *name) { - GsdSmartcard *card; + MsdSmartcard *card; g_return_val_if_fail (module != NULL, NULL); g_return_val_if_fail (name != NULL, NULL); - card = GSD_SMARTCARD (g_object_new (GSD_TYPE_SMARTCARD, + card = MSD_SMARTCARD (g_object_new (MSD_TYPE_SMARTCARD, "module", module, "name", name, NULL)); @@ -466,16 +466,16 @@ _gsd_smartcard_new_from_name (SECMODModule *module, } void -_gsd_smartcard_set_state (GsdSmartcard *card, - GsdSmartcardState state) +_msd_smartcard_set_state (MsdSmartcard *card, + MsdSmartcardState state) { if (card->priv->state != state) { card->priv->state = state; - if (state == GSD_SMARTCARD_STATE_INSERTED) { - g_signal_emit (card, gsd_smartcard_signals[INSERTED], 0); - } else if (state == GSD_SMARTCARD_STATE_REMOVED) { - g_signal_emit (card, gsd_smartcard_signals[REMOVED], 0); + if (state == MSD_SMARTCARD_STATE_INSERTED) { + g_signal_emit (card, msd_smartcard_signals[INSERTED], 0); + } else if (state == MSD_SMARTCARD_STATE_REMOVED) { + g_signal_emit (card, msd_smartcard_signals[REMOVED], 0); } else { g_assert_not_reached (); } @@ -489,7 +489,7 @@ _gsd_smartcard_set_state (GsdSmartcard *card, * and strdup it using NSPR's memory allocation routines. */ static char * -gsd_smartcard_password_handler (PK11SlotInfo *slot, +msd_smartcard_password_handler (PK11SlotInfo *slot, PRBool is_retrying, const char *password) { @@ -501,12 +501,12 @@ gsd_smartcard_password_handler (PK11SlotInfo *slot, } gboolean -gsd_smartcard_unlock (GsdSmartcard *card, +msd_smartcard_unlock (MsdSmartcard *card, const char *password) { SECStatus status; - PK11_SetPasswordFunc ((PK11PasswordFunc) gsd_smartcard_password_handler); + PK11_SetPasswordFunc ((PK11PasswordFunc) msd_smartcard_password_handler); /* we pass PR_TRUE to load certificates */ @@ -520,7 +520,7 @@ gsd_smartcard_unlock (GsdSmartcard *card, } static PK11SlotInfo * -gsd_smartcard_find_slot_from_card_name (GsdSmartcard *card, +msd_smartcard_find_slot_from_card_name (MsdSmartcard *card, const char *card_name) { int i; @@ -540,7 +540,7 @@ gsd_smartcard_find_slot_from_card_name (GsdSmartcard *card, } static PK11SlotInfo * -gsd_smartcard_find_slot_from_id (GsdSmartcard *card, +msd_smartcard_find_slot_from_id (MsdSmartcard *card, int slot_id) { int i; diff --git a/plugins/smartcard/gsd-smartcard.h b/plugins/smartcard/gsd-smartcard.h index c8c1ea7..d00b8af 100644 --- a/plugins/smartcard/gsd-smartcard.h +++ b/plugins/smartcard/gsd-smartcard.h @@ -17,8 +17,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ -#ifndef GSD_SMARTCARD_H -#define GSD_SMARTCARD_H +#ifndef MSD_SMARTCARD_H +#define MSD_SMARTCARD_H #include #include @@ -28,71 +28,71 @@ #ifdef __cplusplus extern "C" { #endif -#define GSD_TYPE_SMARTCARD (gsd_smartcard_get_type ()) -#define GSD_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GSD_TYPE_SMARTCARD, GsdSmartcard)) -#define GSD_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GSD_TYPE_SMARTCARD, GsdSmartcardClass)) -#define GSD_IS_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GSD_TYPE_SMARTCARD)) -#define GSD_IS_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GSD_TYPE_SMARTCARD)) -#define GSD_SMARTCARD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GSD_TYPE_SMARTCARD, GsdSmartcardClass)) -#define GSD_SMARTCARD_ERROR (gsd_smartcard_error_quark ()) -typedef struct _GsdSmartcardClass GsdSmartcardClass; -typedef struct _GsdSmartcard GsdSmartcard; -typedef struct _GsdSmartcardPrivate GsdSmartcardPrivate; -typedef enum _GsdSmartcardError GsdSmartcardError; -typedef enum _GsdSmartcardState GsdSmartcardState; - -typedef struct _GsdSmartcardRequest GsdSmartcardRequest; - -struct _GsdSmartcard { +#define MSD_TYPE_SMARTCARD (msd_smartcard_get_type ()) +#define MSD_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MSD_TYPE_SMARTCARD, MsdSmartcard)) +#define MSD_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MSD_TYPE_SMARTCARD, MsdSmartcardClass)) +#define MSD_IS_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MSD_TYPE_SMARTCARD)) +#define MSD_IS_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MSD_TYPE_SMARTCARD)) +#define MSD_SMARTCARD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), MSD_TYPE_SMARTCARD, MsdSmartcardClass)) +#define MSD_SMARTCARD_ERROR (msd_smartcard_error_quark ()) +typedef struct _MsdSmartcardClass MsdSmartcardClass; +typedef struct _MsdSmartcard MsdSmartcard; +typedef struct _MsdSmartcardPrivate MsdSmartcardPrivate; +typedef enum _MsdSmartcardError MsdSmartcardError; +typedef enum _MsdSmartcardState MsdSmartcardState; + +typedef struct _MsdSmartcardRequest MsdSmartcardRequest; + +struct _MsdSmartcard { GObject parent; /*< private > */ - GsdSmartcardPrivate *priv; + MsdSmartcardPrivate *priv; }; -struct _GsdSmartcardClass { +struct _MsdSmartcardClass { GObjectClass parent_class; - void (* inserted) (GsdSmartcard *card); - void (* removed) (GsdSmartcard *card); + void (* inserted) (MsdSmartcard *card); + void (* removed) (MsdSmartcard *card); }; -enum _GsdSmartcardError { - GSD_SMARTCARD_ERROR_GENERIC = 0, +enum _MsdSmartcardError { + MSD_SMARTCARD_ERROR_GENERIC = 0, }; -enum _GsdSmartcardState { - GSD_SMARTCARD_STATE_INSERTED = 0, - GSD_SMARTCARD_STATE_REMOVED, +enum _MsdSmartcardState { + MSD_SMARTCARD_STATE_INSERTED = 0, + MSD_SMARTCARD_STATE_REMOVED, }; -GType gsd_smartcard_get_type (void) G_GNUC_CONST; -GQuark gsd_smartcard_error_quark (void) G_GNUC_CONST; +GType msd_smartcard_get_type (void) G_GNUC_CONST; +GQuark msd_smartcard_error_quark (void) G_GNUC_CONST; -CK_SLOT_ID gsd_smartcard_get_slot_id (GsdSmartcard *card); -gint gsd_smartcard_get_slot_series (GsdSmartcard *card); -GsdSmartcardState gsd_smartcard_get_state (GsdSmartcard *card); +CK_SLOT_ID msd_smartcard_get_slot_id (MsdSmartcard *card); +gint msd_smartcard_get_slot_series (MsdSmartcard *card); +MsdSmartcardState msd_smartcard_get_state (MsdSmartcard *card); -char *gsd_smartcard_get_name (GsdSmartcard *card); -gboolean gsd_smartcard_is_login_card (GsdSmartcard *card); +char *msd_smartcard_get_name (MsdSmartcard *card); +gboolean msd_smartcard_is_login_card (MsdSmartcard *card); -gboolean gsd_smartcard_unlock (GsdSmartcard *card, +gboolean msd_smartcard_unlock (MsdSmartcard *card, const char *password); /* don't under any circumstances call these functions */ -#ifdef GSD_SMARTCARD_ENABLE_INTERNAL_API +#ifdef MSD_SMARTCARD_ENABLE_INTERNAL_API -GsdSmartcard *_gsd_smartcard_new (SECMODModule *module, +MsdSmartcard *_msd_smartcard_new (SECMODModule *module, CK_SLOT_ID slot_id, gint slot_series); -GsdSmartcard *_gsd_smartcard_new_from_name (SECMODModule *module, +MsdSmartcard *_msd_smartcard_new_from_name (SECMODModule *module, const char *name); -void _gsd_smartcard_set_state (GsdSmartcard *card, - GsdSmartcardState state); +void _msd_smartcard_set_state (MsdSmartcard *card, + MsdSmartcardState state); #endif #ifdef __cplusplus } #endif -#endif /* GSD_SMARTCARD_H */ +#endif /* MSD_SMARTCARD_H */ -- cgit v1.2.1 From e46b4adef5c6c6805b3ca6dbfbe99a4299252514 Mon Sep 17 00:00:00 2001 From: haxar Date: Tue, 21 Feb 2012 20:14:01 -0800 Subject: gsd to msd complete rename patch by NiceandGently; file rename commit --- plugins/smartcard/gsd-smartcard-manager.c | 1372 ----------------------------- plugins/smartcard/gsd-smartcard-manager.h | 90 -- plugins/smartcard/gsd-smartcard-plugin.c | 340 ------- plugins/smartcard/gsd-smartcard-plugin.h | 63 -- plugins/smartcard/gsd-smartcard.c | 555 ------------ plugins/smartcard/gsd-smartcard.h | 98 --- plugins/smartcard/msd-smartcard-manager.c | 1372 +++++++++++++++++++++++++++++ plugins/smartcard/msd-smartcard-manager.h | 90 ++ plugins/smartcard/msd-smartcard-plugin.c | 340 +++++++ plugins/smartcard/msd-smartcard-plugin.h | 63 ++ plugins/smartcard/msd-smartcard.c | 555 ++++++++++++ plugins/smartcard/msd-smartcard.h | 98 +++ 12 files changed, 2518 insertions(+), 2518 deletions(-) delete mode 100644 plugins/smartcard/gsd-smartcard-manager.c delete mode 100644 plugins/smartcard/gsd-smartcard-manager.h delete mode 100644 plugins/smartcard/gsd-smartcard-plugin.c delete mode 100644 plugins/smartcard/gsd-smartcard-plugin.h delete mode 100644 plugins/smartcard/gsd-smartcard.c delete mode 100644 plugins/smartcard/gsd-smartcard.h create mode 100644 plugins/smartcard/msd-smartcard-manager.c create mode 100644 plugins/smartcard/msd-smartcard-manager.h create mode 100644 plugins/smartcard/msd-smartcard-plugin.c create mode 100644 plugins/smartcard/msd-smartcard-plugin.h create mode 100644 plugins/smartcard/msd-smartcard.c create mode 100644 plugins/smartcard/msd-smartcard.h (limited to 'plugins/smartcard') diff --git a/plugins/smartcard/gsd-smartcard-manager.c b/plugins/smartcard/gsd-smartcard-manager.c deleted file mode 100644 index ed823f4..0000000 --- a/plugins/smartcard/gsd-smartcard-manager.c +++ /dev/null @@ -1,1372 +0,0 @@ -/* msd-smartcard-manager.c - object for monitoring smartcard insertion and - * removal events - * - * Copyright (C) 2006, 2009 Red Hat, Inc. - * - * 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, 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. - * - * Written By: Ray Strode - */ -#include "config.h" - -#include "msd-smartcard-manager.h" - -#define SMARTCARD_ENABLE_INTERNAL_API -#include "msd-smartcard.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - -#ifndef MSD_SMARTCARD_MANAGER_NSS_DB -#define MSD_SMARTCARD_MANAGER_NSS_DB SYSCONFDIR"/pki/nssdb" -#endif - -typedef enum _MsdSmartcardManagerState MsdSmartcardManagerState; -typedef struct _MsdSmartcardManagerWorker MsdSmartcardManagerWorker; - -enum _MsdSmartcardManagerState { - MSD_SMARTCARD_MANAGER_STATE_STOPPED = 0, - MSD_SMARTCARD_MANAGER_STATE_STARTING, - MSD_SMARTCARD_MANAGER_STATE_STARTED, - MSD_SMARTCARD_MANAGER_STATE_STOPPING, -}; - -struct _MsdSmartcardManagerPrivate { - MsdSmartcardManagerState state; - SECMODModule *module; - char *module_path; - - GSource *smartcard_event_source; - GPid smartcard_event_watcher_pid; - GHashTable *smartcards; - - GThread *worker_thread; - - guint poll_timeout_id; - - guint32 is_unstoppable : 1; - guint32 nss_is_loaded : 1; -}; - -struct _MsdSmartcardManagerWorker { - SECMODModule *module; - GHashTable *smartcards; - int write_fd; - - guint32 nss_is_loaded : 1; -}; - -static void msd_smartcard_manager_finalize (GObject *object); -static void msd_smartcard_manager_class_install_signals (MsdSmartcardManagerClass *service_class); -static void msd_smartcard_manager_class_install_properties (MsdSmartcardManagerClass *service_class); -static void msd_smartcard_manager_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec); -static void msd_smartcard_manager_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec); -static void msd_smartcard_manager_set_module_path (MsdSmartcardManager *manager, - const char *module_path); -static void msd_smartcard_manager_card_removed_handler (MsdSmartcardManager *manager, - MsdSmartcard *card); -static void msd_smartcard_manager_card_inserted_handler (MsdSmartcardManager *manager_class, - MsdSmartcard *card); -static gboolean msd_smartcard_manager_stop_now (MsdSmartcardManager *manager); -static void msd_smartcard_manager_queue_stop (MsdSmartcardManager *manager); - -static gboolean msd_smartcard_manager_create_worker (MsdSmartcardManager *manager, - int *worker_fd, GThread **worker_thread); - -static MsdSmartcardManagerWorker * msd_smartcard_manager_worker_new (int write_fd); -static void msd_smartcard_manager_worker_free (MsdSmartcardManagerWorker *worker); -static gboolean open_pipe (int *write_fd, int *read_fd); -static gboolean read_bytes (int fd, gpointer bytes, gsize num_bytes); -static gboolean write_bytes (int fd, gconstpointer bytes, gsize num_bytes); -static MsdSmartcard *read_smartcard (int fd, SECMODModule *module); -static gboolean write_smartcard (int fd, MsdSmartcard *card); - -enum { - PROP_0 = 0, - PROP_MODULE_PATH, - NUMBER_OF_PROPERTIES -}; - -enum { - SMARTCARD_INSERTED = 0, - SMARTCARD_REMOVED, - ERROR, - NUMBER_OF_SIGNALS -}; - -static guint msd_smartcard_manager_signals[NUMBER_OF_SIGNALS]; - -G_DEFINE_TYPE (MsdSmartcardManager, - msd_smartcard_manager, - G_TYPE_OBJECT); - -static void -msd_smartcard_manager_class_init (MsdSmartcardManagerClass *manager_class) -{ - GObjectClass *gobject_class; - - gobject_class = G_OBJECT_CLASS (manager_class); - - gobject_class->finalize = msd_smartcard_manager_finalize; - - msd_smartcard_manager_class_install_signals (manager_class); - msd_smartcard_manager_class_install_properties (manager_class); - - g_type_class_add_private (manager_class, - sizeof (MsdSmartcardManagerPrivate)); -} - -static void -msd_smartcard_manager_class_install_properties (MsdSmartcardManagerClass *card_class) -{ - GObjectClass *object_class; - GParamSpec *param_spec; - - object_class = G_OBJECT_CLASS (card_class); - object_class->set_property = msd_smartcard_manager_set_property; - object_class->get_property = msd_smartcard_manager_get_property; - - param_spec = g_param_spec_string ("module-path", _("Module Path"), - _("path to smartcard PKCS #11 driver"), - NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_property (object_class, PROP_MODULE_PATH, param_spec); -} - -static void -msd_smartcard_manager_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - MsdSmartcardManager *manager = MSD_SMARTCARD_MANAGER (object); - - switch (prop_id) { - case PROP_MODULE_PATH: - msd_smartcard_manager_set_module_path (manager, - g_value_get_string (value)); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -msd_smartcard_manager_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - MsdSmartcardManager *manager = MSD_SMARTCARD_MANAGER (object); - char *module_path; - - switch (prop_id) { - case PROP_MODULE_PATH: - module_path = msd_smartcard_manager_get_module_path (manager); - g_value_set_string (value, module_path); - g_free (module_path); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -char * -msd_smartcard_manager_get_module_path (MsdSmartcardManager *manager) -{ - return manager->priv->module_path; -} - -static void -msd_smartcard_manager_set_module_path (MsdSmartcardManager *manager, - const char *module_path) -{ - if ((manager->priv->module_path == NULL) && (module_path == NULL)) { - return; - } - - if (((manager->priv->module_path == NULL) || - (module_path == NULL) || - (strcmp (manager->priv->module_path, module_path) != 0))) { - g_free (manager->priv->module_path); - manager->priv->module_path = g_strdup (module_path); - g_object_notify (G_OBJECT (manager), "module-path"); - } -} - -static void -msd_smartcard_manager_card_removed_handler (MsdSmartcardManager *manager, - MsdSmartcard *card) -{ - g_debug ("informing smartcard of its removal"); - _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_REMOVED); - g_debug ("done"); -} - -static void -msd_smartcard_manager_card_inserted_handler (MsdSmartcardManager *manager, - MsdSmartcard *card) -{ - g_debug ("informing smartcard of its insertion"); - - _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_INSERTED); - g_debug ("done"); - -} - -static void -msd_smartcard_manager_class_install_signals (MsdSmartcardManagerClass *manager_class) -{ - GObjectClass *object_class; - - object_class = G_OBJECT_CLASS (manager_class); - - msd_smartcard_manager_signals[SMARTCARD_INSERTED] = - g_signal_new ("smartcard-inserted", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (MsdSmartcardManagerClass, - smartcard_inserted), - NULL, NULL, g_cclosure_marshal_VOID__POINTER, - G_TYPE_NONE, 1, G_TYPE_POINTER); - manager_class->smartcard_inserted = msd_smartcard_manager_card_inserted_handler; - - msd_smartcard_manager_signals[SMARTCARD_REMOVED] = - g_signal_new ("smartcard-removed", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (MsdSmartcardManagerClass, - smartcard_removed), - NULL, NULL, g_cclosure_marshal_VOID__POINTER, - G_TYPE_NONE, 1, G_TYPE_POINTER); - manager_class->smartcard_removed = msd_smartcard_manager_card_removed_handler; - - msd_smartcard_manager_signals[ERROR] = - g_signal_new ("error", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (MsdSmartcardManagerClass, error), - NULL, NULL, g_cclosure_marshal_VOID__POINTER, - G_TYPE_NONE, 1, G_TYPE_POINTER); - manager_class->error = NULL; -} - -static gboolean -slot_id_equal (CK_SLOT_ID *slot_id_1, - CK_SLOT_ID *slot_id_2) -{ - g_assert (slot_id_1 != NULL); - g_assert (slot_id_2 != NULL); - - return *slot_id_1 == *slot_id_2; -} - -static gboolean -slot_id_hash (CK_SLOT_ID *slot_id) -{ - guint32 upper_bits, lower_bits; - int temp; - - if (sizeof (CK_SLOT_ID) == sizeof (int)) { - return g_int_hash (slot_id); - } - - upper_bits = ((*slot_id) >> 31) - 1; - lower_bits = (*slot_id) & 0xffffffff; - - /* The upper bits are almost certainly always zero, - * so let's degenerate to g_int_hash for the - * (very) common case - */ - temp = lower_bits + upper_bits; - return upper_bits + g_int_hash (&temp); -} - -static void -msd_smartcard_manager_init (MsdSmartcardManager *manager) -{ - g_debug ("initializing smartcard manager"); - - manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager, - MSD_TYPE_SMARTCARD_MANAGER, - MsdSmartcardManagerPrivate); - manager->priv->poll_timeout_id = 0; - manager->priv->is_unstoppable = FALSE; - manager->priv->module = NULL; - - manager->priv->smartcards = - g_hash_table_new_full (g_str_hash, - g_str_equal, - (GDestroyNotify) g_free, - (GDestroyNotify) g_object_unref); - - if (!g_thread_supported ()) { - g_thread_init (NULL); - } - -} - -static void -msd_smartcard_manager_finalize (GObject *object) -{ - MsdSmartcardManager *manager; - GObjectClass *gobject_class; - - manager = MSD_SMARTCARD_MANAGER (object); - gobject_class = - G_OBJECT_CLASS (msd_smartcard_manager_parent_class); - - msd_smartcard_manager_stop_now (manager); - - g_hash_table_destroy (manager->priv->smartcards); - manager->priv->smartcards = NULL; - - gobject_class->finalize (object); -} - -GQuark -msd_smartcard_manager_error_quark (void) -{ - static GQuark error_quark = 0; - - if (error_quark == 0) { - error_quark = g_quark_from_static_string ("msd-smartcard-manager-error-quark"); - } - - return error_quark; -} - -MsdSmartcardManager * -msd_smartcard_manager_new (const char *module_path) -{ - MsdSmartcardManager *instance; - - instance = MSD_SMARTCARD_MANAGER (g_object_new (MSD_TYPE_SMARTCARD_MANAGER, - "module-path", module_path, - NULL)); - - return instance; -} - -static void -msd_smartcard_manager_emit_error (MsdSmartcardManager *manager, - GError *error) -{ - manager->priv->is_unstoppable = TRUE; - g_signal_emit (manager, msd_smartcard_manager_signals[ERROR], 0, - error); - manager->priv->is_unstoppable = FALSE; -} - -static void -msd_smartcard_manager_emit_smartcard_inserted (MsdSmartcardManager *manager, - MsdSmartcard *card) -{ - manager->priv->is_unstoppable = TRUE; - g_signal_emit (manager, msd_smartcard_manager_signals[SMARTCARD_INSERTED], 0, - card); - manager->priv->is_unstoppable = FALSE; -} - -static void -msd_smartcard_manager_emit_smartcard_removed (MsdSmartcardManager *manager, - MsdSmartcard *card) -{ - MsdSmartcardManagerState old_state; - - old_state = manager->priv->state; - manager->priv->is_unstoppable = TRUE; - g_signal_emit (manager, msd_smartcard_manager_signals[SMARTCARD_REMOVED], 0, - card); - manager->priv->is_unstoppable = FALSE; -} - -static gboolean -msd_smartcard_manager_check_for_and_process_events (GIOChannel *io_channel, - GIOCondition condition, - MsdSmartcardManager *manager) -{ - MsdSmartcard *card; - gboolean should_stop; - gchar event_type; - char *card_name; - int fd; - - card = NULL; - should_stop = (condition & G_IO_HUP) || (condition & G_IO_ERR); - - if (should_stop) { - g_debug ("received %s on event socket, stopping " - "manager...", - (condition & G_IO_HUP) && (condition & G_IO_ERR)? - "error and hangup" : - (condition & G_IO_HUP)? - "hangup" : "error"); - } - - if (!(condition & G_IO_IN)) { - goto out; - } - - fd = g_io_channel_unix_get_fd (io_channel); - - event_type = '\0'; - if (!read_bytes (fd, &event_type, 1)) { - should_stop = TRUE; - goto out; - } - - card = read_smartcard (fd, manager->priv->module); - - if (card == NULL) { - should_stop = TRUE; - goto out; - } - - card_name = msd_smartcard_get_name (card); - - switch (event_type) { - case 'I': - g_hash_table_replace (manager->priv->smartcards, - card_name, card); - card_name = NULL; - - msd_smartcard_manager_emit_smartcard_inserted (manager, card); - card = NULL; - break; - - case 'R': - msd_smartcard_manager_emit_smartcard_removed (manager, card); - if (!g_hash_table_remove (manager->priv->smartcards, card_name)) { - g_debug ("got removal event of unknown card!"); - } - g_free (card_name); - card_name = NULL; - card = NULL; - break; - - default: - g_free (card_name); - card_name = NULL; - g_object_unref (card); - - should_stop = TRUE; - break; - } - -out: - if (should_stop) { - GError *error; - - error = g_error_new (MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, - "%s", (condition & G_IO_IN) ? g_strerror (errno) : _("received error or hang up from event source")); - - msd_smartcard_manager_emit_error (manager, error); - g_error_free (error); - msd_smartcard_manager_stop_now (manager); - return FALSE; - } - - return TRUE; -} - -static void -msd_smartcard_manager_event_processing_stopped_handler (MsdSmartcardManager *manager) -{ - manager->priv->smartcard_event_source = NULL; - msd_smartcard_manager_stop_now (manager); -} - -static gboolean -open_pipe (int *write_fd, - int *read_fd) -{ - int pipe_fds[2] = { -1, -1 }; - - g_assert (write_fd != NULL); - g_assert (read_fd != NULL); - - if (pipe (pipe_fds) < 0) { - return FALSE; - } - - if (fcntl (pipe_fds[0], F_SETFD, FD_CLOEXEC) < 0) { - close (pipe_fds[0]); - close (pipe_fds[1]); - return FALSE; - } - - if (fcntl (pipe_fds[1], F_SETFD, FD_CLOEXEC) < 0) { - close (pipe_fds[0]); - close (pipe_fds[1]); - return FALSE; - } - - *read_fd = pipe_fds[0]; - *write_fd = pipe_fds[1]; - - return TRUE; -} - -static void -msd_smartcard_manager_stop_watching_for_events (MsdSmartcardManager *manager) -{ - if (manager->priv->smartcard_event_source != NULL) { - g_source_destroy (manager->priv->smartcard_event_source); - manager->priv->smartcard_event_source = NULL; - } - - if (manager->priv->worker_thread != NULL) { - SECMOD_CancelWait (manager->priv->module); - manager->priv->worker_thread = NULL; - } -} - -static gboolean -load_nss (GError **error) -{ - SECStatus status = SECSuccess; - static const guint32 flags = - NSS_INIT_READONLY | NSS_INIT_NOCERTDB | NSS_INIT_NOMODDB | - NSS_INIT_FORCEOPEN | NSS_INIT_NOROOTINIT | - NSS_INIT_OPTIMIZESPACE | NSS_INIT_PK11RELOAD; - - g_debug ("attempting to load NSS database '%s'", - MSD_SMARTCARD_MANAGER_NSS_DB); - - status = NSS_Initialize (MSD_SMARTCARD_MANAGER_NSS_DB, - "", "", SECMOD_DB, flags); - - if (status != SECSuccess) { - gsize error_message_size; - char *error_message; - - error_message_size = PR_GetErrorTextLength (); - - if (error_message_size == 0) { - g_debug ("NSS security system could not be initialized"); - g_set_error (error, - MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, - _("NSS security system could not be initialized")); - goto out; - } - - error_message = g_slice_alloc0 (error_message_size); - PR_GetErrorText (error_message); - - g_set_error (error, - MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, - "%s", error_message); - g_debug ("NSS security system could not be initialized - %s", - error_message); - - g_slice_free1 (error_message_size, error_message); - - goto out; - } - - g_debug ("NSS database sucessfully loaded"); - return TRUE; - -out: - g_debug ("NSS database couldn't be sucessfully loaded"); - return FALSE; -} - -static SECMODModule * -load_driver (char *module_path, - GError **error) -{ - SECMODModule *module; - char *module_spec; - gboolean module_explicitly_specified; - - g_debug ("attempting to load driver..."); - - module = NULL; - module_explicitly_specified = module_path != NULL; - if (module_explicitly_specified) { - module_spec = g_strdup_printf ("library=\"%s\"", module_path); - g_debug ("loading smartcard driver using spec '%s'", - module_spec); - - module = SECMOD_LoadUserModule (module_spec, - NULL /* parent */, - FALSE /* recurse */); - g_free (module_spec); - module_spec = NULL; - - } else { - SECMODModuleList *modules, *tmp; - - modules = SECMOD_GetDefaultModuleList (); - - for (tmp = modules; tmp != NULL; tmp = tmp->next) { - if (!SECMOD_HasRemovableSlots (tmp->module) || - !tmp->module->loaded) - continue; - - module = SECMOD_ReferenceModule (tmp->module); - break; - } - } - - if (!module_explicitly_specified && module == NULL) { - g_set_error (error, - MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, - _("no suitable smartcard driver could be found")); - } else if (module == NULL || !module->loaded) { - - gsize error_message_size; - char *error_message; - - if (module != NULL && !module->loaded) { - g_debug ("module found but not loaded?!"); - SECMOD_DestroyModule (module); - module = NULL; - } - - error_message_size = PR_GetErrorTextLength (); - - if (error_message_size == 0) { - g_debug ("smartcard driver '%s' could not be loaded", - module_path); - g_set_error (error, - MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, - _("smartcard driver '%s' could not be " - "loaded"), module_path); - goto out; - } - - error_message = g_slice_alloc0 (error_message_size); - PR_GetErrorText (error_message); - - g_set_error (error, - MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, - "%s", error_message); - - g_debug ("smartcard driver '%s' could not be loaded - %s", - module_path, error_message); - g_slice_free1 (error_message_size, error_message); - } - -out: - return module; -} - -static void -msd_smartcard_manager_get_all_cards (MsdSmartcardManager *manager) -{ - int i; - - for (i = 0; i < manager->priv->module->slotCount; i++) { - MsdSmartcard *card; - CK_SLOT_ID slot_id; - int slot_series; - char *card_name; - - slot_id = PK11_GetSlotID (manager->priv->module->slots[i]); - slot_series = PK11_GetSlotSeries (manager->priv->module->slots[i]); - - card = _msd_smartcard_new (manager->priv->module, - slot_id, slot_series); - - card_name = msd_smartcard_get_name (card); - - g_hash_table_replace (manager->priv->smartcards, - card_name, card); - } -} - -gboolean -msd_smartcard_manager_start (MsdSmartcardManager *manager, - GError **error) -{ - GError *watching_error; - int worker_fd; - GPid worker_pid; - GIOChannel *io_channel; - GSource *source; - GIOFlags channel_flags; - GError *nss_error; - - if (manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STARTED) { - g_debug ("smartcard manager already started"); - return TRUE; - } - - manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STARTING; - - worker_fd = -1; - worker_pid = 0; - - nss_error = NULL; - if (!manager->priv->nss_is_loaded && !load_nss (&nss_error)) { - g_propagate_error (error, nss_error); - goto out; - } - manager->priv->nss_is_loaded = TRUE; - - if (manager->priv->module == NULL) { - manager->priv->module = load_driver (manager->priv->module_path, &nss_error); - } - - if (manager->priv->module == NULL) { - g_propagate_error (error, nss_error); - goto out; - } - - if (!msd_smartcard_manager_create_worker (manager, &worker_fd, &manager->priv->worker_thread)) { - - g_set_error (error, - MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, - _("could not watch for incoming card events - %s"), - g_strerror (errno)); - - goto out; - } - - io_channel = g_io_channel_unix_new (worker_fd); - - channel_flags = g_io_channel_get_flags (io_channel); - watching_error = NULL; - - source = g_io_create_watch (io_channel, G_IO_IN | G_IO_HUP); - g_io_channel_unref (io_channel); - io_channel = NULL; - - manager->priv->smartcard_event_source = source; - - g_source_set_callback (manager->priv->smartcard_event_source, - (GSourceFunc) (GIOFunc) - msd_smartcard_manager_check_for_and_process_events, - manager, - (GDestroyNotify) - msd_smartcard_manager_event_processing_stopped_handler); - g_source_attach (manager->priv->smartcard_event_source, NULL); - g_source_unref (manager->priv->smartcard_event_source); - - /* populate the hash with cards that are already inserted - */ - msd_smartcard_manager_get_all_cards (manager); - - manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STARTED; - -out: - /* don't leave it in a half started state - */ - if (manager->priv->state != MSD_SMARTCARD_MANAGER_STATE_STARTED) { - g_debug ("smartcard manager could not be completely started"); - msd_smartcard_manager_stop (manager); - } else { - g_debug ("smartcard manager started"); - } - - return manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STARTED; -} - -static gboolean -msd_smartcard_manager_stop_now (MsdSmartcardManager *manager) -{ - if (manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STOPPED) { - return FALSE; - } - - manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STOPPED; - msd_smartcard_manager_stop_watching_for_events (manager); - - if (manager->priv->module != NULL) { - SECMOD_DestroyModule (manager->priv->module); - manager->priv->module = NULL; - } - - if (manager->priv->nss_is_loaded) { - NSS_Shutdown (); - manager->priv->nss_is_loaded = FALSE; - } - - g_debug ("smartcard manager stopped"); - - return FALSE; -} - -static void -msd_smartcard_manager_queue_stop (MsdSmartcardManager *manager) -{ - - manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STOPPING; - - g_idle_add ((GSourceFunc) msd_smartcard_manager_stop_now, manager); -} - -void -msd_smartcard_manager_stop (MsdSmartcardManager *manager) -{ - if (manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STOPPED) { - return; - } - - if (manager->priv->is_unstoppable) { - msd_smartcard_manager_queue_stop (manager); - return; - } - - msd_smartcard_manager_stop_now (manager); -} - -static void -msd_smartcard_manager_check_for_login_card (CK_SLOT_ID slot_id, - MsdSmartcard *card, - gboolean *is_inserted) -{ - g_assert (is_inserted != NULL); - - if (msd_smartcard_is_login_card (card)) { - *is_inserted = TRUE; - } - -} - -gboolean -msd_smartcard_manager_login_card_is_inserted (MsdSmartcardManager *manager) - -{ - gboolean is_inserted; - - is_inserted = FALSE; - g_hash_table_foreach (manager->priv->smartcards, - (GHFunc) - msd_smartcard_manager_check_for_login_card, - &is_inserted); - return is_inserted; -} - -static MsdSmartcardManagerWorker * -msd_smartcard_manager_worker_new (int write_fd) -{ - MsdSmartcardManagerWorker *worker; - - worker = g_slice_new0 (MsdSmartcardManagerWorker); - worker->write_fd = write_fd; - worker->module = NULL; - - worker->smartcards = - g_hash_table_new_full ((GHashFunc) slot_id_hash, - (GEqualFunc) slot_id_equal, - (GDestroyNotify) g_free, - (GDestroyNotify) g_object_unref); - - return worker; -} - -static void -msd_smartcard_manager_worker_free (MsdSmartcardManagerWorker *worker) -{ - if (worker->smartcards != NULL) { - g_hash_table_destroy (worker->smartcards); - worker->smartcards = NULL; - } - - g_slice_free (MsdSmartcardManagerWorker, worker); -} - -static gboolean -read_bytes (int fd, - gpointer bytes, - gsize num_bytes) -{ - size_t bytes_left; - size_t total_bytes_read; - ssize_t bytes_read; - - bytes_left = (size_t) num_bytes; - total_bytes_read = 0; - - do { - bytes_read = read (fd, - (char *) bytes + total_bytes_read, - bytes_left); - g_assert (bytes_read <= (ssize_t) bytes_left); - - if (bytes_read <= 0) { - if ((bytes_read < 0) && (errno == EINTR || errno == EAGAIN)) { - continue; - } - - bytes_left = 0; - } else { - bytes_left -= bytes_read; - total_bytes_read += bytes_read; - } - } while (bytes_left > 0); - - if (total_bytes_read < (size_t) num_bytes) { - return FALSE; - } - - return TRUE; -} - -static gboolean -write_bytes (int fd, - gconstpointer bytes, - gsize num_bytes) -{ - size_t bytes_left; - size_t total_bytes_written; - ssize_t bytes_written; - - bytes_left = (size_t) num_bytes; - total_bytes_written = 0; - - do { - bytes_written = write (fd, - (char *) bytes + total_bytes_written, - bytes_left); - g_assert (bytes_written <= (ssize_t) bytes_left); - - if (bytes_written <= 0) { - if ((bytes_written < 0) && (errno == EINTR || errno == EAGAIN)) { - continue; - } - - bytes_left = 0; - } else { - bytes_left -= bytes_written; - total_bytes_written += bytes_written; - } - } while (bytes_left > 0); - - if (total_bytes_written < (size_t) num_bytes) { - return FALSE; - } - - return TRUE; -} - -static MsdSmartcard * -read_smartcard (int fd, - SECMODModule *module) -{ - MsdSmartcard *card; - char *card_name; - gsize card_name_size; - - card_name_size = 0; - if (!read_bytes (fd, &card_name_size, sizeof (card_name_size))) { - return NULL; - } - - card_name = g_slice_alloc0 (card_name_size); - if (!read_bytes (fd, card_name, card_name_size)) { - g_slice_free1 (card_name_size, card_name); - return NULL; - } - card = _msd_smartcard_new_from_name (module, card_name); - g_slice_free1 (card_name_size, card_name); - - return card; -} - -static gboolean -write_smartcard (int fd, - MsdSmartcard *card) -{ - gsize card_name_size; - char *card_name; - - card_name = msd_smartcard_get_name (card); - card_name_size = strlen (card_name) + 1; - - if (!write_bytes (fd, &card_name_size, sizeof (card_name_size))) { - g_free (card_name); - return FALSE; - } - - if (!write_bytes (fd, card_name, card_name_size)) { - g_free (card_name); - return FALSE; - } - g_free (card_name); - - return TRUE; -} - -static gboolean -msd_smartcard_manager_worker_emit_smartcard_removed (MsdSmartcardManagerWorker *worker, - MsdSmartcard *card, - GError **error) -{ - g_debug ("card '%s' removed!", msd_smartcard_get_name (card)); - - if (!write_bytes (worker->write_fd, "R", 1)) { - goto error_out; - } - - if (!write_smartcard (worker->write_fd, card)) { - goto error_out; - } - - return TRUE; - -error_out: - g_set_error (error, MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS, - "%s", g_strerror (errno)); - return FALSE; -} - -static gboolean -msd_smartcard_manager_worker_emit_smartcard_inserted (MsdSmartcardManagerWorker *worker, - MsdSmartcard *card, - GError **error) -{ - GError *write_error; - - write_error = NULL; - g_debug ("card '%s' inserted!", msd_smartcard_get_name (card)); - if (!write_bytes (worker->write_fd, "I", 1)) { - goto error_out; - } - - if (!write_smartcard (worker->write_fd, card)) { - goto error_out; - } - - return TRUE; - -error_out: - g_set_error (error, MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS, - "%s", g_strerror (errno)); - return FALSE; -} - -static gboolean -msd_smartcard_manager_worker_watch_for_and_process_event (MsdSmartcardManagerWorker *worker, - GError **error) -{ - PK11SlotInfo *slot; - CK_SLOT_ID slot_id, *key; - int slot_series, card_slot_series; - MsdSmartcard *card; - GError *processing_error; - gboolean ret; - - g_debug ("waiting for card event"); - ret = FALSE; - - slot = SECMOD_WaitForAnyTokenEvent (worker->module, 0, PR_SecondsToInterval (1)); - processing_error = NULL; - - if (slot == NULL) { - int error_code; - - error_code = PORT_GetError (); - if ((error_code == 0) || (error_code == SEC_ERROR_NO_EVENT)) { - g_debug ("spurrious event occurred"); - return TRUE; - } - - /* FIXME: is there a function to convert from a PORT error - * code to a translated string? - */ - g_set_error (error, MSD_SMARTCARD_MANAGER_ERROR, - MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, - _("encountered unexpected error while " - "waiting for smartcard events")); - goto out; - } - - /* the slot id and series together uniquely identify a card. - * You can never have two cards with the same slot id at the - * same time, however (I think), so we can key off of it. - */ - slot_id = PK11_GetSlotID (slot); - slot_series = PK11_GetSlotSeries (slot); - - /* First check to see if there is a card that we're currently - * tracking in the slot. - */ - key = g_new (CK_SLOT_ID, 1); - *key = slot_id; - card = g_hash_table_lookup (worker->smartcards, key); - - if (card != NULL) { - card_slot_series = msd_smartcard_get_slot_series (card); - } else { - card_slot_series = -1; - } - - if (PK11_IsPresent (slot)) { - /* Now, check to see if their is a new card in the slot. - * If there was a different card in the slot now than - * there was before, then we need to emit a removed signal - * for the old card (we don't want unpaired insertion events). - */ - if ((card != NULL) && - card_slot_series != slot_series) { - if (!msd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { - g_propagate_error (error, processing_error); - goto out; - } - } - - card = _msd_smartcard_new (worker->module, - slot_id, slot_series); - - g_hash_table_replace (worker->smartcards, - key, card); - key = NULL; - - if (!msd_smartcard_manager_worker_emit_smartcard_inserted (worker, - card, - &processing_error)) { - g_propagate_error (error, processing_error); - goto out; - } - } else { - /* if we aren't tracking the card, just discard the event. - * We don't want unpaired remove events. Note on startup - * NSS will generate an "insertion" event if a card is - * already inserted in the slot. - */ - if ((card != NULL)) { - /* FIXME: i'm not sure about this code. Maybe we - * shouldn't do this at all, or maybe we should do it - * n times (where n = slot_series - card_slot_series + 1) - * - * Right now, i'm just doing it once. - */ - if ((slot_series - card_slot_series) > 1) { - - if (!msd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { - g_propagate_error (error, processing_error); - goto out; - } - g_hash_table_remove (worker->smartcards, key); - - card = _msd_smartcard_new (worker->module, - slot_id, slot_series); - g_hash_table_replace (worker->smartcards, - key, card); - key = NULL; - if (!msd_smartcard_manager_worker_emit_smartcard_inserted (worker, card, &processing_error)) { - g_propagate_error (error, processing_error); - goto out; - } - } - - if (!msd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { - g_propagate_error (error, processing_error); - goto out; - } - - g_hash_table_remove (worker->smartcards, key); - card = NULL; - } else { - g_debug ("got spurious remove event"); - } - } - - ret = TRUE; - -out: - g_free (key); - PK11_FreeSlot (slot); - - return ret; -} - -static void -msd_smartcard_manager_worker_run (MsdSmartcardManagerWorker *worker) -{ - GError *error; - - - error = NULL; - - while (msd_smartcard_manager_worker_watch_for_and_process_event (worker, &error)); - - if (error != NULL) { - g_debug ("could not process card event - %s", error->message); - g_error_free (error); - } - - msd_smartcard_manager_worker_free (worker); -} - -static gboolean -msd_smartcard_manager_create_worker (MsdSmartcardManager *manager, - int *worker_fd, - GThread **worker_thread) -{ - MsdSmartcardManagerWorker *worker; - int write_fd, read_fd; - - write_fd = -1; - read_fd = -1; - if (!open_pipe (&write_fd, &read_fd)) { - return FALSE; - } - - worker = msd_smartcard_manager_worker_new (write_fd); - worker->module = manager->priv->module; - - *worker_thread = g_thread_create ((GThreadFunc) - msd_smartcard_manager_worker_run, - worker, FALSE, NULL); - - if (*worker_thread == NULL) { - msd_smartcard_manager_worker_free (worker); - return FALSE; - } - - if (worker_fd) { - *worker_fd = read_fd; - } - - return TRUE; -} - -#ifdef MSD_SMARTCARD_MANAGER_ENABLE_TEST -#include - -static GMainLoop *event_loop; -static gboolean should_exit_on_next_remove = FALSE; - -static gboolean -on_timeout (MsdSmartcardManager *manager) -{ - GError *error; - g_print ("Re-enabling manager.\n"); - - if (!msd_smartcard_manager_start (manager, &error)) { - g_warning ("could not start smartcard manager - %s", - error->message); - g_error_free (error); - return 1; - } - g_print ("Please re-insert smartcard\n"); - - should_exit_on_next_remove = TRUE; - - return FALSE; -} - -static void -on_device_inserted (MsdSmartcardManager *manager, - MsdSmartcard *card) -{ - g_print ("smartcard inserted!\n"); - g_print ("Please remove it.\n"); -} - -static void -on_device_removed (MsdSmartcardManager *manager, - MsdSmartcard *card) -{ - g_print ("smartcard removed!\n"); - - if (should_exit_on_next_remove) { - g_main_loop_quit (event_loop); - } else { - g_print ("disabling manager for 2 seconds\n"); - msd_smartcard_manager_stop (manager); - g_timeout_add (2000, (GSourceFunc) on_timeout, manager); - } -} - -int -main (int argc, - char *argv[]) -{ - MsdSmartcardManager *manager; - GError *error; - - g_log_set_always_fatal (G_LOG_LEVEL_ERROR - | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING); - - g_type_init (); - - g_message ("creating instance of 'smartcard manager' object..."); - manager = msd_smartcard_manager_new (NULL); - g_message ("'smartcard manager' object created successfully"); - - g_signal_connect (manager, "smartcard-inserted", - G_CALLBACK (on_device_inserted), NULL); - - g_signal_connect (manager, "smartcard-removed", - G_CALLBACK (on_device_removed), NULL); - - g_message ("starting listener..."); - - error = NULL; - if (!msd_smartcard_manager_start (manager, &error)) { - g_warning ("could not start smartcard manager - %s", - error->message); - g_error_free (error); - return 1; - } - - event_loop = g_main_loop_new (NULL, FALSE); - g_main_loop_run (event_loop); - g_main_loop_unref (event_loop); - event_loop = NULL; - - g_message ("destroying previously created 'smartcard manager' object..."); - g_object_unref (manager); - manager = NULL; - g_message ("'smartcard manager' object destroyed successfully"); - - return 0; -} -#endif diff --git a/plugins/smartcard/gsd-smartcard-manager.h b/plugins/smartcard/gsd-smartcard-manager.h deleted file mode 100644 index 269e1da..0000000 --- a/plugins/smartcard/gsd-smartcard-manager.h +++ /dev/null @@ -1,90 +0,0 @@ -/* msd-smartcard-manager.h - object for monitoring smartcard insertion and - * removal events - * - * Copyright (C) 2006, 2009 Red Hat, Inc. - * - * 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, 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. - * - * Written by: Ray Strode - */ -#ifndef MSD_SMARTCARD_MANAGER_H -#define MSD_SMARTCARD_MANAGER_H - -#define MSD_SMARTCARD_ENABLE_INTERNAL_API -#include "msd-smartcard.h" - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif -#define MSD_TYPE_SMARTCARD_MANAGER (msd_smartcard_manager_get_type ()) -#define MSD_SMARTCARD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MSD_TYPE_SMARTCARD_MANAGER, MsdSmartcardManager)) -#define MSD_SMARTCARD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MSD_TYPE_SMARTCARD_MANAGER, MsdSmartcardManagerClass)) -#define MSD_IS_SMARTCARD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SC_TYPE_SMARTCARD_MANAGER)) -#define MSD_IS_SMARTCARD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SC_TYPE_SMARTCARD_MANAGER)) -#define MSD_SMARTCARD_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), MSD_TYPE_SMARTCARD_MANAGER, MsdSmartcardManagerClass)) -#define MSD_SMARTCARD_MANAGER_ERROR (msd_smartcard_manager_error_quark ()) -typedef struct _MsdSmartcardManager MsdSmartcardManager; -typedef struct _MsdSmartcardManagerClass MsdSmartcardManagerClass; -typedef struct _MsdSmartcardManagerPrivate MsdSmartcardManagerPrivate; -typedef enum _MsdSmartcardManagerError MsdSmartcardManagerError; - -struct _MsdSmartcardManager { - GObject parent; - - /*< private > */ - MsdSmartcardManagerPrivate *priv; -}; - -struct _MsdSmartcardManagerClass { - GObjectClass parent_class; - - /* Signals */ - void (*smartcard_inserted) (MsdSmartcardManager *manager, - MsdSmartcard *token); - void (*smartcard_removed) (MsdSmartcardManager *manager, - MsdSmartcard *token); - void (*error) (MsdSmartcardManager *manager, - GError *error); -}; - -enum _MsdSmartcardManagerError { - MSD_SMARTCARD_MANAGER_ERROR_GENERIC = 0, - MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, - MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, - MSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, - MSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS -}; - -GType msd_smartcard_manager_get_type (void) G_GNUC_CONST; -GQuark msd_smartcard_manager_error_quark (void) G_GNUC_CONST; - -MsdSmartcardManager *msd_smartcard_manager_new (const char *module); - -gboolean msd_smartcard_manager_start (MsdSmartcardManager *manager, - GError **error); - -void msd_smartcard_manager_stop (MsdSmartcardManager *manager); - -char *msd_smartcard_manager_get_module_path (MsdSmartcardManager *manager); -gboolean msd_smartcard_manager_login_card_is_inserted (MsdSmartcardManager *manager); - -#ifdef __cplusplus -} -#endif -#endif /* MSD_SMARTCARD_MANAGER_H */ diff --git a/plugins/smartcard/gsd-smartcard-plugin.c b/plugins/smartcard/gsd-smartcard-plugin.c deleted file mode 100644 index dc4cb8f..0000000 --- a/plugins/smartcard/gsd-smartcard-plugin.c +++ /dev/null @@ -1,340 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * - * Copyright (C) 2010 Red Hat, Inc. - * - * 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, 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#include "config.h" - -#include -#include - -#include -#include - -#include - -#include - -#include "mate-settings-plugin.h" -#include "msd-smartcard-plugin.h" -#include "msd-smartcard-manager.h" - -struct MsdSmartcardPluginPrivate { - MsdSmartcardManager *manager; - DBusGConnection *bus_connection; - - guint32 is_active : 1; -}; - -typedef enum -{ - MSD_SMARTCARD_REMOVE_ACTION_NONE, - MSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN, - MSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT, -} MsdSmartcardRemoveAction; - -#define SCREENSAVER_DBUS_NAME "org.mate.ScreenSaver" -#define SCREENSAVER_DBUS_PATH "/" -#define SCREENSAVER_DBUS_INTERFACE "org.mate.ScreenSaver" - -#define SM_DBUS_NAME "org.mate.SessionManager" -#define SM_DBUS_PATH "/org/mate/SessionManager" -#define SM_DBUS_INTERFACE "org.mate.SessionManager" -#define SM_LOGOUT_MODE_FORCE 2 - -#define MSD_SMARTCARD_KEY "/desktop/mate/peripherals/smartcard" -#define KEY_REMOVE_ACTION MSD_SMARTCARD_KEY "/removal_action" - -#define MSD_SMARTCARD_PLUGIN_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPluginPrivate)) - -MATE_SETTINGS_PLUGIN_REGISTER (MsdSmartcardPlugin, msd_smartcard_plugin); - -static void -simulate_user_activity (MsdSmartcardPlugin *plugin) -{ - DBusGProxy *screensaver_proxy; - - g_debug ("MsdSmartcardPlugin telling screensaver about smart card insertion"); - screensaver_proxy = dbus_g_proxy_new_for_name (plugin->priv->bus_connection, - SCREENSAVER_DBUS_NAME, - SCREENSAVER_DBUS_PATH, - SCREENSAVER_DBUS_INTERFACE); - - dbus_g_proxy_call_no_reply (screensaver_proxy, - "SimulateUserActivity", - G_TYPE_INVALID, G_TYPE_INVALID); - - g_object_unref (screensaver_proxy); -} - -static void -lock_screen (MsdSmartcardPlugin *plugin) -{ - DBusGProxy *screensaver_proxy; - - g_debug ("MsdSmartcardPlugin telling screensaver to lock screen"); - screensaver_proxy = dbus_g_proxy_new_for_name (plugin->priv->bus_connection, - SCREENSAVER_DBUS_NAME, - SCREENSAVER_DBUS_PATH, - SCREENSAVER_DBUS_INTERFACE); - - dbus_g_proxy_call_no_reply (screensaver_proxy, - "Lock", - G_TYPE_INVALID, G_TYPE_INVALID); - - g_object_unref (screensaver_proxy); -} - -static void -force_logout (MsdSmartcardPlugin *plugin) -{ - DBusGProxy *sm_proxy; - GError *error; - gboolean res; - - g_debug ("MsdSmartcardPlugin telling session manager to force logout"); - sm_proxy = dbus_g_proxy_new_for_name (plugin->priv->bus_connection, - SM_DBUS_NAME, - SM_DBUS_PATH, - SM_DBUS_INTERFACE); - - error = NULL; - res = dbus_g_proxy_call (sm_proxy, - "Logout", - &error, - G_TYPE_UINT, SM_LOGOUT_MODE_FORCE, - G_TYPE_INVALID, G_TYPE_INVALID); - - if (! res) { - g_warning ("MsdSmartcardPlugin Unable to force logout: %s", error->message); - g_error_free (error); - } - - g_object_unref (sm_proxy); -} - -static void -msd_smartcard_plugin_init (MsdSmartcardPlugin *plugin) -{ - plugin->priv = MSD_SMARTCARD_PLUGIN_GET_PRIVATE (plugin); - - g_debug ("MsdSmartcardPlugin initializing"); - - plugin->priv->manager = msd_smartcard_manager_new (NULL); -} - -static void -msd_smartcard_plugin_finalize (GObject *object) -{ - MsdSmartcardPlugin *plugin; - - g_return_if_fail (object != NULL); - g_return_if_fail (MSD_IS_SMARTCARD_PLUGIN (object)); - - g_debug ("MsdSmartcardPlugin finalizing"); - - plugin = MSD_SMARTCARD_PLUGIN (object); - - g_return_if_fail (plugin->priv != NULL); - - if (plugin->priv->manager != NULL) { - g_object_unref (plugin->priv->manager); - } - - G_OBJECT_CLASS (msd_smartcard_plugin_parent_class)->finalize (object); -} - -static void -smartcard_inserted_cb (MsdSmartcardManager *card_monitor, - MsdSmartcard *card, - MsdSmartcardPlugin *plugin) -{ - char *name; - - name = msd_smartcard_get_name (card); - g_debug ("MsdSmartcardPlugin smart card '%s' inserted", name); - g_free (name); - - simulate_user_activity (plugin); -} - -static gboolean -user_logged_in_with_smartcard (void) -{ - return g_getenv ("PKCS11_LOGIN_TOKEN_NAME") != NULL; -} - -static MsdSmartcardRemoveAction -get_configured_remove_action (MsdSmartcardPlugin *plugin) -{ - MateConfClient *client; - char *remove_action_string; - MsdSmartcardRemoveAction remove_action; - - client = mateconf_client_get_default (); - remove_action_string = mateconf_client_get_string (client, - KEY_REMOVE_ACTION, NULL); - - if (remove_action_string == NULL) { - g_warning ("MsdSmartcardPlugin unable to get smartcard remove action"); - remove_action = MSD_SMARTCARD_REMOVE_ACTION_NONE; - } else if (strcmp (remove_action_string, "none") == 0) { - remove_action = MSD_SMARTCARD_REMOVE_ACTION_NONE; - } else if (strcmp (remove_action_string, "lock_screen") == 0) { - remove_action = MSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN; - } else if (strcmp (remove_action_string, "force_logout") == 0) { - remove_action = MSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT; - } else { - g_warning ("MsdSmartcardPlugin unknown smartcard remove action"); - remove_action = MSD_SMARTCARD_REMOVE_ACTION_NONE; - } - - g_object_unref (client); - - return remove_action; -} - -static void -process_smartcard_removal (MsdSmartcardPlugin *plugin) -{ - MsdSmartcardRemoveAction remove_action; - - g_debug ("MsdSmartcardPlugin processing smartcard removal"); - remove_action = get_configured_remove_action (plugin); - - switch (remove_action) - { - case MSD_SMARTCARD_REMOVE_ACTION_NONE: - return; - case MSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN: - lock_screen (plugin); - break; - case MSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT: - force_logout (plugin); - break; - } -} - -static void -smartcard_removed_cb (MsdSmartcardManager *card_monitor, - MsdSmartcard *card, - MsdSmartcardPlugin *plugin) -{ - - char *name; - - name = msd_smartcard_get_name (card); - g_debug ("MsdSmartcardPlugin smart card '%s' removed", name); - g_free (name); - - if (!msd_smartcard_is_login_card (card)) { - g_debug ("MsdSmartcardPlugin removed smart card was not used to login"); - return; - } - - process_smartcard_removal (plugin); -} - -static void -impl_activate (MateSettingsPlugin *plugin) -{ - GError *error; - MsdSmartcardPlugin *smartcard_plugin = MSD_SMARTCARD_PLUGIN (plugin); - - if (smartcard_plugin->priv->is_active) { - g_debug ("MsdSmartcardPlugin Not activating smartcard plugin, because it's " - "already active"); - return; - } - - if (!user_logged_in_with_smartcard ()) { - g_debug ("MsdSmartcardPlugin Not activating smartcard plugin, because user didn't use " - " smartcard to log in"); - smartcard_plugin->priv->is_active = FALSE; - return; - } - - g_debug ("MsdSmartcardPlugin Activating smartcard plugin"); - - error = NULL; - smartcard_plugin->priv->bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); - - if (smartcard_plugin->priv->bus_connection == NULL) { - g_warning ("MsdSmartcardPlugin Unable to connect to session bus: %s", error->message); - return; - } - - if (!msd_smartcard_manager_start (smartcard_plugin->priv->manager, &error)) { - g_warning ("MsdSmartcardPlugin Unable to start smartcard manager: %s", error->message); - g_error_free (error); - } - - g_signal_connect (smartcard_plugin->priv->manager, - "smartcard-removed", - G_CALLBACK (smartcard_removed_cb), smartcard_plugin); - - g_signal_connect (smartcard_plugin->priv->manager, - "smartcard-inserted", - G_CALLBACK (smartcard_inserted_cb), smartcard_plugin); - - if (!msd_smartcard_manager_login_card_is_inserted (smartcard_plugin->priv->manager)) { - g_debug ("MsdSmartcardPlugin processing smartcard removal immediately user logged in with smartcard " - "and it's not inserted"); - process_smartcard_removal (smartcard_plugin); - } - - smartcard_plugin->priv->is_active = TRUE; -} - -static void -impl_deactivate (MateSettingsPlugin *plugin) -{ - MsdSmartcardPlugin *smartcard_plugin = MSD_SMARTCARD_PLUGIN (plugin); - - if (!smartcard_plugin->priv->is_active) { - g_debug ("MsdSmartcardPlugin Not deactivating smartcard plugin, " - "because it's already inactive"); - return; - } - - g_debug ("MsdSmartcardPlugin Deactivating smartcard plugin"); - - msd_smartcard_manager_stop (smartcard_plugin->priv->manager); - - g_signal_handlers_disconnect_by_func (smartcard_plugin->priv->manager, - smartcard_removed_cb, smartcard_plugin); - - g_signal_handlers_disconnect_by_func (smartcard_plugin->priv->manager, - smartcard_inserted_cb, smartcard_plugin); - smartcard_plugin->priv->bus_connection = NULL; - smartcard_plugin->priv->is_active = FALSE; -} - -static void -msd_smartcard_plugin_class_init (MsdSmartcardPluginClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - MateSettingsPluginClass *plugin_class = MATE_SETTINGS_PLUGIN_CLASS (klass); - - object_class->finalize = msd_smartcard_plugin_finalize; - - plugin_class->activate = impl_activate; - plugin_class->deactivate = impl_deactivate; - - g_type_class_add_private (klass, sizeof (MsdSmartcardPluginPrivate)); -} diff --git a/plugins/smartcard/gsd-smartcard-plugin.h b/plugins/smartcard/gsd-smartcard-plugin.h deleted file mode 100644 index 206e58d..0000000 --- a/plugins/smartcard/gsd-smartcard-plugin.h +++ /dev/null @@ -1,63 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- - * - * Copyright (C) 2010 Red Hat, Inc. - * - * 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, 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#ifndef __MSD_SMARTCARD_PLUGIN_H__ -#define __MSD_SMARTCARD_PLUGIN_H__ - -#include -#include -#include - -#include "mate-settings-plugin.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define MSD_TYPE_SMARTCARD_PLUGIN (msd_smartcard_plugin_get_type ()) -#define MSD_SMARTCARD_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPlugin)) -#define MSD_SMARTCARD_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPluginClass)) -#define MSD_IS_SMARTCARD_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MSD_TYPE_SMARTCARD_PLUGIN)) -#define MSD_IS_SMARTCARD_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), MSD_TYPE_SMARTCARD_PLUGIN)) -#define MSD_SMARTCARD_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPluginClass)) - -typedef struct MsdSmartcardPluginPrivate MsdSmartcardPluginPrivate; - -typedef struct -{ - MateSettingsPlugin parent; - MsdSmartcardPluginPrivate *priv; -} MsdSmartcardPlugin; - -typedef struct -{ - MateSettingsPluginClass parent_class; -} MsdSmartcardPluginClass; - -GType msd_smartcard_plugin_get_type (void) G_GNUC_CONST; - -/* All the plugins must implement this function */ -G_MODULE_EXPORT GType register_mate_settings_plugin (GTypeModule *module); - -#ifdef __cplusplus -} -#endif - -#endif /* __MSD_SMARTCARD_PLUGIN_H__ */ diff --git a/plugins/smartcard/gsd-smartcard.c b/plugins/smartcard/gsd-smartcard.c deleted file mode 100644 index 9154200..0000000 --- a/plugins/smartcard/gsd-smartcard.c +++ /dev/null @@ -1,555 +0,0 @@ -/* msd-smartcard.c - smartcard object - * - * Copyright (C) 2006 Ray Strode - * - * 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, 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. - */ -#define MSD_SMARTCARD_ENABLE_INTERNAL_API -#include "msd-smartcard.h" - -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -struct _MsdSmartcardPrivate { - SECMODModule *module; - MsdSmartcardState state; - - CK_SLOT_ID slot_id; - int slot_series; - - PK11SlotInfo *slot; - char *name; - - CERTCertificate *signing_certificate; - CERTCertificate *encryption_certificate; -}; - -static void msd_smartcard_finalize (GObject *object); -static void msd_smartcard_class_install_signals (MsdSmartcardClass *card_class); -static void msd_smartcard_class_install_properties (MsdSmartcardClass *card_class); -static void msd_smartcard_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec); -static void msd_smartcard_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec); -static void msd_smartcard_set_name (MsdSmartcard *card, const char *name); -static void msd_smartcard_set_slot_id (MsdSmartcard *card, - int slot_id); -static void msd_smartcard_set_slot_series (MsdSmartcard *card, - int slot_series); -static void msd_smartcard_set_module (MsdSmartcard *card, - SECMODModule *module); - -static PK11SlotInfo *msd_smartcard_find_slot_from_id (MsdSmartcard *card, - int slot_id); - -static PK11SlotInfo *msd_smartcard_find_slot_from_card_name (MsdSmartcard *card, - const char *card_name); -#ifndef MSD_SMARTCARD_DEFAULT_SLOT_ID -#define MSD_SMARTCARD_DEFAULT_SLOT_ID ((gulong) -1) -#endif - -#ifndef MSD_SMARTCARD_DEFAULT_SLOT_SERIES -#define MSD_SMARTCARD_DEFAULT_SLOT_SERIES -1 -#endif - -enum { - PROP_0 = 0, - PROP_NAME, - PROP_SLOT_ID, - PROP_SLOT_SERIES, - PROP_MODULE, - NUMBER_OF_PROPERTIES -}; - -enum { - INSERTED, - REMOVED, - NUMBER_OF_SIGNALS -}; - -static guint msd_smartcard_signals[NUMBER_OF_SIGNALS]; - -G_DEFINE_TYPE (MsdSmartcard, msd_smartcard, G_TYPE_OBJECT); - -static void -msd_smartcard_class_init (MsdSmartcardClass *card_class) -{ - GObjectClass *gobject_class; - - gobject_class = G_OBJECT_CLASS (card_class); - - gobject_class->finalize = msd_smartcard_finalize; - - msd_smartcard_class_install_signals (card_class); - msd_smartcard_class_install_properties (card_class); - - g_type_class_add_private (card_class, - sizeof (MsdSmartcardPrivate)); -} - -static void -msd_smartcard_class_install_signals (MsdSmartcardClass *card_class) -{ - GObjectClass *object_class; - - object_class = G_OBJECT_CLASS (card_class); - - msd_smartcard_signals[INSERTED] = - g_signal_new ("inserted", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (MsdSmartcardClass, - inserted), - NULL, NULL, g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0); - - msd_smartcard_signals[REMOVED] = - g_signal_new ("removed", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (MsdSmartcardClass, - removed), - NULL, NULL, g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0); -} - -static void -msd_smartcard_class_install_properties (MsdSmartcardClass *card_class) -{ - GObjectClass *object_class; - GParamSpec *param_spec; - - object_class = G_OBJECT_CLASS (card_class); - object_class->set_property = msd_smartcard_set_property; - object_class->get_property = msd_smartcard_get_property; - - param_spec = g_param_spec_ulong ("slot-id", _("Slot ID"), - _("The slot the card is in"), - 1, G_MAXULONG, - MSD_SMARTCARD_DEFAULT_SLOT_ID, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_property (object_class, PROP_SLOT_ID, param_spec); - - param_spec = g_param_spec_int ("slot-series", _("Slot Series"), - _("per-slot card identifier"), - -1, G_MAXINT, - MSD_SMARTCARD_DEFAULT_SLOT_SERIES, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_property (object_class, PROP_SLOT_SERIES, param_spec); - - param_spec = g_param_spec_string ("name", _("name"), - _("name"), NULL, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_property (object_class, PROP_NAME, param_spec); - - param_spec = g_param_spec_pointer ("module", _("Module"), - _("smartcard driver"), - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_property (object_class, PROP_MODULE, param_spec); -} - -static void -msd_smartcard_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - MsdSmartcard *card = MSD_SMARTCARD (object); - - switch (prop_id) { - case PROP_NAME: - msd_smartcard_set_name (card, g_value_get_string (value)); - break; - - case PROP_SLOT_ID: - msd_smartcard_set_slot_id (card, - g_value_get_ulong (value)); - break; - - case PROP_SLOT_SERIES: - msd_smartcard_set_slot_series (card, - g_value_get_int (value)); - break; - - case PROP_MODULE: - msd_smartcard_set_module (card, - (SECMODModule *) - g_value_get_pointer (value)); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - } -} - -CK_SLOT_ID -msd_smartcard_get_slot_id (MsdSmartcard *card) -{ - return card->priv->slot_id; -} - -MsdSmartcardState -msd_smartcard_get_state (MsdSmartcard *card) -{ - return card->priv->state; -} - -char * -msd_smartcard_get_name (MsdSmartcard *card) -{ - return g_strdup (card->priv->name); -} - -gboolean -msd_smartcard_is_login_card (MsdSmartcard *card) -{ - const char *login_card_name; - login_card_name = g_getenv ("PKCS11_LOGIN_TOKEN_NAME"); - - if ((login_card_name == NULL) || (card->priv->name == NULL)) { - return FALSE; - } - - if (strcmp (card->priv->name, login_card_name) == 0) { - return TRUE; - } - - return FALSE; -} - -static void -msd_smartcard_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - MsdSmartcard *card = MSD_SMARTCARD (object); - - switch (prop_id) { - case PROP_NAME: - g_value_take_string (value, - msd_smartcard_get_name (card)); - break; - - case PROP_SLOT_ID: - g_value_set_ulong (value, - (gulong) msd_smartcard_get_slot_id (card)); - break; - - case PROP_SLOT_SERIES: - g_value_set_int (value, - msd_smartcard_get_slot_series (card)); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - } -} - -static void -msd_smartcard_set_name (MsdSmartcard *card, - const char *name) -{ - if (name == NULL) { - return; - } - - if ((card->priv->name == NULL) || - (strcmp (card->priv->name, name) != 0)) { - g_free (card->priv->name); - card->priv->name = g_strdup (name); - - if (card->priv->slot == NULL) { - card->priv->slot = msd_smartcard_find_slot_from_card_name (card, - card->priv->name); - - if (card->priv->slot != NULL) { - int slot_id, slot_series; - - slot_id = PK11_GetSlotID (card->priv->slot); - if (slot_id != card->priv->slot_id) { - msd_smartcard_set_slot_id (card, slot_id); - } - - slot_series = PK11_GetSlotSeries (card->priv->slot); - if (slot_series != card->priv->slot_series) { - msd_smartcard_set_slot_series (card, slot_series); - } - - _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_INSERTED); - } else { - _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_REMOVED); - } - } - - g_object_notify (G_OBJECT (card), "name"); - } -} - -static void -msd_smartcard_set_slot_id (MsdSmartcard *card, - int slot_id) -{ - if (card->priv->slot_id != slot_id) { - card->priv->slot_id = slot_id; - - if (card->priv->slot == NULL) { - card->priv->slot = msd_smartcard_find_slot_from_id (card, - card->priv->slot_id); - - if (card->priv->slot != NULL) { - const char *card_name; - - card_name = PK11_GetTokenName (card->priv->slot); - if ((card->priv->name == NULL) || - ((card_name != NULL) && - (strcmp (card_name, card->priv->name) != 0))) { - msd_smartcard_set_name (card, card_name); - } - - _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_INSERTED); - } else { - _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_REMOVED); - } - } - - g_object_notify (G_OBJECT (card), "slot-id"); - } -} - -static void -msd_smartcard_set_slot_series (MsdSmartcard *card, - int slot_series) -{ - if (card->priv->slot_series != slot_series) { - card->priv->slot_series = slot_series; - g_object_notify (G_OBJECT (card), "slot-series"); - } -} - -static void -msd_smartcard_set_module (MsdSmartcard *card, - SECMODModule *module) -{ - gboolean should_notify; - - if (card->priv->module != module) { - should_notify = TRUE; - } else { - should_notify = FALSE; - } - - if (card->priv->module != NULL) { - SECMOD_DestroyModule (card->priv->module); - card->priv->module = NULL; - } - - if (module != NULL) { - card->priv->module = SECMOD_ReferenceModule (module); - } - - if (should_notify) { - g_object_notify (G_OBJECT (card), "module"); - } -} - -int -msd_smartcard_get_slot_series (MsdSmartcard *card) -{ - return card->priv->slot_series; -} - -static void -msd_smartcard_init (MsdSmartcard *card) -{ - - g_debug ("initializing smartcard "); - - card->priv = G_TYPE_INSTANCE_GET_PRIVATE (card, - MSD_TYPE_SMARTCARD, - MsdSmartcardPrivate); - - if (card->priv->slot != NULL) { - card->priv->name = g_strdup (PK11_GetTokenName (card->priv->slot)); - } -} - -static void msd_smartcard_finalize (GObject *object) -{ - MsdSmartcard *card; - GObjectClass *gobject_class; - - card = MSD_SMARTCARD (object); - - g_free (card->priv->name); - - msd_smartcard_set_module (card, NULL); - - gobject_class = G_OBJECT_CLASS (msd_smartcard_parent_class); - - gobject_class->finalize (object); -} - -GQuark msd_smartcard_error_quark (void) -{ - static GQuark error_quark = 0; - - if (error_quark == 0) { - error_quark = g_quark_from_static_string ("msd-smartcard-error-quark"); - } - - return error_quark; -} - -MsdSmartcard * -_msd_smartcard_new (SECMODModule *module, - CK_SLOT_ID slot_id, - int slot_series) -{ - MsdSmartcard *card; - - g_return_val_if_fail (module != NULL, NULL); - g_return_val_if_fail (slot_id >= 1, NULL); - g_return_val_if_fail (slot_series > 0, NULL); - g_return_val_if_fail (sizeof (gulong) == sizeof (slot_id), NULL); - - card = MSD_SMARTCARD (g_object_new (MSD_TYPE_SMARTCARD, - "module", module, - "slot-id", (gulong) slot_id, - "slot-series", slot_series, - NULL)); - return card; -} - -MsdSmartcard * -_msd_smartcard_new_from_name (SECMODModule *module, - const char *name) -{ - MsdSmartcard *card; - - g_return_val_if_fail (module != NULL, NULL); - g_return_val_if_fail (name != NULL, NULL); - - card = MSD_SMARTCARD (g_object_new (MSD_TYPE_SMARTCARD, - "module", module, - "name", name, - NULL)); - return card; -} - -void -_msd_smartcard_set_state (MsdSmartcard *card, - MsdSmartcardState state) -{ - if (card->priv->state != state) { - card->priv->state = state; - - if (state == MSD_SMARTCARD_STATE_INSERTED) { - g_signal_emit (card, msd_smartcard_signals[INSERTED], 0); - } else if (state == MSD_SMARTCARD_STATE_REMOVED) { - g_signal_emit (card, msd_smartcard_signals[REMOVED], 0); - } else { - g_assert_not_reached (); - } - } -} - -/* So we could conceivably make the closure data a pointer to the card - * or something similiar and then emit signals when we want passwords, - * but it's probably easier to just get the password up front and use - * it. So we just take the passed in g_malloc'd (well probably, who knows) - * and strdup it using NSPR's memory allocation routines. - */ -static char * -msd_smartcard_password_handler (PK11SlotInfo *slot, - PRBool is_retrying, - const char *password) -{ - if (is_retrying) { - return NULL; - } - - return password != NULL? PL_strdup (password): NULL; -} - -gboolean -msd_smartcard_unlock (MsdSmartcard *card, - const char *password) -{ - SECStatus status; - - PK11_SetPasswordFunc ((PK11PasswordFunc) msd_smartcard_password_handler); - - /* we pass PR_TRUE to load certificates - */ - status = PK11_Authenticate (card->priv->slot, PR_TRUE, (gpointer) password); - - if (status != SECSuccess) { - g_debug ("could not unlock card - %d", status); - return FALSE; - } - return TRUE; -} - -static PK11SlotInfo * -msd_smartcard_find_slot_from_card_name (MsdSmartcard *card, - const char *card_name) -{ - int i; - - for (i = 0; i < card->priv->module->slotCount; i++) { - const char *slot_card_name; - - slot_card_name = PK11_GetTokenName (card->priv->module->slots[i]); - - if ((slot_card_name != NULL) && - (strcmp (slot_card_name, card_name) == 0)) { - return card->priv->module->slots[i]; - } - } - - return NULL; -} - -static PK11SlotInfo * -msd_smartcard_find_slot_from_id (MsdSmartcard *card, - int slot_id) -{ - int i; - - for (i = 0; i < card->priv->module->slotCount; i++) { - if (PK11_GetSlotID (card->priv->module->slots[i]) == slot_id) { - return card->priv->module->slots[i]; - } - } - - return NULL; -} diff --git a/plugins/smartcard/gsd-smartcard.h b/plugins/smartcard/gsd-smartcard.h deleted file mode 100644 index d00b8af..0000000 --- a/plugins/smartcard/gsd-smartcard.h +++ /dev/null @@ -1,98 +0,0 @@ -/* securitycard.h - api for reading and writing data to a security card - * - * Copyright (C) 2006 Ray Strode - * - * 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, 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. - */ -#ifndef MSD_SMARTCARD_H -#define MSD_SMARTCARD_H - -#include -#include - -#include - -#ifdef __cplusplus -extern "C" { -#endif -#define MSD_TYPE_SMARTCARD (msd_smartcard_get_type ()) -#define MSD_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MSD_TYPE_SMARTCARD, MsdSmartcard)) -#define MSD_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MSD_TYPE_SMARTCARD, MsdSmartcardClass)) -#define MSD_IS_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MSD_TYPE_SMARTCARD)) -#define MSD_IS_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MSD_TYPE_SMARTCARD)) -#define MSD_SMARTCARD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), MSD_TYPE_SMARTCARD, MsdSmartcardClass)) -#define MSD_SMARTCARD_ERROR (msd_smartcard_error_quark ()) -typedef struct _MsdSmartcardClass MsdSmartcardClass; -typedef struct _MsdSmartcard MsdSmartcard; -typedef struct _MsdSmartcardPrivate MsdSmartcardPrivate; -typedef enum _MsdSmartcardError MsdSmartcardError; -typedef enum _MsdSmartcardState MsdSmartcardState; - -typedef struct _MsdSmartcardRequest MsdSmartcardRequest; - -struct _MsdSmartcard { - GObject parent; - - /*< private > */ - MsdSmartcardPrivate *priv; -}; - -struct _MsdSmartcardClass { - GObjectClass parent_class; - - void (* inserted) (MsdSmartcard *card); - void (* removed) (MsdSmartcard *card); -}; - -enum _MsdSmartcardError { - MSD_SMARTCARD_ERROR_GENERIC = 0, -}; - -enum _MsdSmartcardState { - MSD_SMARTCARD_STATE_INSERTED = 0, - MSD_SMARTCARD_STATE_REMOVED, -}; - -GType msd_smartcard_get_type (void) G_GNUC_CONST; -GQuark msd_smartcard_error_quark (void) G_GNUC_CONST; - -CK_SLOT_ID msd_smartcard_get_slot_id (MsdSmartcard *card); -gint msd_smartcard_get_slot_series (MsdSmartcard *card); -MsdSmartcardState msd_smartcard_get_state (MsdSmartcard *card); - -char *msd_smartcard_get_name (MsdSmartcard *card); -gboolean msd_smartcard_is_login_card (MsdSmartcard *card); - -gboolean msd_smartcard_unlock (MsdSmartcard *card, - const char *password); - -/* don't under any circumstances call these functions */ -#ifdef MSD_SMARTCARD_ENABLE_INTERNAL_API - -MsdSmartcard *_msd_smartcard_new (SECMODModule *module, - CK_SLOT_ID slot_id, - gint slot_series); -MsdSmartcard *_msd_smartcard_new_from_name (SECMODModule *module, - const char *name); - -void _msd_smartcard_set_state (MsdSmartcard *card, - MsdSmartcardState state); -#endif - -#ifdef __cplusplus -} -#endif -#endif /* MSD_SMARTCARD_H */ diff --git a/plugins/smartcard/msd-smartcard-manager.c b/plugins/smartcard/msd-smartcard-manager.c new file mode 100644 index 0000000..ed823f4 --- /dev/null +++ b/plugins/smartcard/msd-smartcard-manager.c @@ -0,0 +1,1372 @@ +/* msd-smartcard-manager.c - object for monitoring smartcard insertion and + * removal events + * + * Copyright (C) 2006, 2009 Red Hat, Inc. + * + * 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, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Written By: Ray Strode + */ +#include "config.h" + +#include "msd-smartcard-manager.h" + +#define SMARTCARD_ENABLE_INTERNAL_API +#include "msd-smartcard.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#ifndef MSD_SMARTCARD_MANAGER_NSS_DB +#define MSD_SMARTCARD_MANAGER_NSS_DB SYSCONFDIR"/pki/nssdb" +#endif + +typedef enum _MsdSmartcardManagerState MsdSmartcardManagerState; +typedef struct _MsdSmartcardManagerWorker MsdSmartcardManagerWorker; + +enum _MsdSmartcardManagerState { + MSD_SMARTCARD_MANAGER_STATE_STOPPED = 0, + MSD_SMARTCARD_MANAGER_STATE_STARTING, + MSD_SMARTCARD_MANAGER_STATE_STARTED, + MSD_SMARTCARD_MANAGER_STATE_STOPPING, +}; + +struct _MsdSmartcardManagerPrivate { + MsdSmartcardManagerState state; + SECMODModule *module; + char *module_path; + + GSource *smartcard_event_source; + GPid smartcard_event_watcher_pid; + GHashTable *smartcards; + + GThread *worker_thread; + + guint poll_timeout_id; + + guint32 is_unstoppable : 1; + guint32 nss_is_loaded : 1; +}; + +struct _MsdSmartcardManagerWorker { + SECMODModule *module; + GHashTable *smartcards; + int write_fd; + + guint32 nss_is_loaded : 1; +}; + +static void msd_smartcard_manager_finalize (GObject *object); +static void msd_smartcard_manager_class_install_signals (MsdSmartcardManagerClass *service_class); +static void msd_smartcard_manager_class_install_properties (MsdSmartcardManagerClass *service_class); +static void msd_smartcard_manager_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void msd_smartcard_manager_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); +static void msd_smartcard_manager_set_module_path (MsdSmartcardManager *manager, + const char *module_path); +static void msd_smartcard_manager_card_removed_handler (MsdSmartcardManager *manager, + MsdSmartcard *card); +static void msd_smartcard_manager_card_inserted_handler (MsdSmartcardManager *manager_class, + MsdSmartcard *card); +static gboolean msd_smartcard_manager_stop_now (MsdSmartcardManager *manager); +static void msd_smartcard_manager_queue_stop (MsdSmartcardManager *manager); + +static gboolean msd_smartcard_manager_create_worker (MsdSmartcardManager *manager, + int *worker_fd, GThread **worker_thread); + +static MsdSmartcardManagerWorker * msd_smartcard_manager_worker_new (int write_fd); +static void msd_smartcard_manager_worker_free (MsdSmartcardManagerWorker *worker); +static gboolean open_pipe (int *write_fd, int *read_fd); +static gboolean read_bytes (int fd, gpointer bytes, gsize num_bytes); +static gboolean write_bytes (int fd, gconstpointer bytes, gsize num_bytes); +static MsdSmartcard *read_smartcard (int fd, SECMODModule *module); +static gboolean write_smartcard (int fd, MsdSmartcard *card); + +enum { + PROP_0 = 0, + PROP_MODULE_PATH, + NUMBER_OF_PROPERTIES +}; + +enum { + SMARTCARD_INSERTED = 0, + SMARTCARD_REMOVED, + ERROR, + NUMBER_OF_SIGNALS +}; + +static guint msd_smartcard_manager_signals[NUMBER_OF_SIGNALS]; + +G_DEFINE_TYPE (MsdSmartcardManager, + msd_smartcard_manager, + G_TYPE_OBJECT); + +static void +msd_smartcard_manager_class_init (MsdSmartcardManagerClass *manager_class) +{ + GObjectClass *gobject_class; + + gobject_class = G_OBJECT_CLASS (manager_class); + + gobject_class->finalize = msd_smartcard_manager_finalize; + + msd_smartcard_manager_class_install_signals (manager_class); + msd_smartcard_manager_class_install_properties (manager_class); + + g_type_class_add_private (manager_class, + sizeof (MsdSmartcardManagerPrivate)); +} + +static void +msd_smartcard_manager_class_install_properties (MsdSmartcardManagerClass *card_class) +{ + GObjectClass *object_class; + GParamSpec *param_spec; + + object_class = G_OBJECT_CLASS (card_class); + object_class->set_property = msd_smartcard_manager_set_property; + object_class->get_property = msd_smartcard_manager_get_property; + + param_spec = g_param_spec_string ("module-path", _("Module Path"), + _("path to smartcard PKCS #11 driver"), + NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (object_class, PROP_MODULE_PATH, param_spec); +} + +static void +msd_smartcard_manager_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + MsdSmartcardManager *manager = MSD_SMARTCARD_MANAGER (object); + + switch (prop_id) { + case PROP_MODULE_PATH: + msd_smartcard_manager_set_module_path (manager, + g_value_get_string (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +msd_smartcard_manager_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + MsdSmartcardManager *manager = MSD_SMARTCARD_MANAGER (object); + char *module_path; + + switch (prop_id) { + case PROP_MODULE_PATH: + module_path = msd_smartcard_manager_get_module_path (manager); + g_value_set_string (value, module_path); + g_free (module_path); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +char * +msd_smartcard_manager_get_module_path (MsdSmartcardManager *manager) +{ + return manager->priv->module_path; +} + +static void +msd_smartcard_manager_set_module_path (MsdSmartcardManager *manager, + const char *module_path) +{ + if ((manager->priv->module_path == NULL) && (module_path == NULL)) { + return; + } + + if (((manager->priv->module_path == NULL) || + (module_path == NULL) || + (strcmp (manager->priv->module_path, module_path) != 0))) { + g_free (manager->priv->module_path); + manager->priv->module_path = g_strdup (module_path); + g_object_notify (G_OBJECT (manager), "module-path"); + } +} + +static void +msd_smartcard_manager_card_removed_handler (MsdSmartcardManager *manager, + MsdSmartcard *card) +{ + g_debug ("informing smartcard of its removal"); + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_REMOVED); + g_debug ("done"); +} + +static void +msd_smartcard_manager_card_inserted_handler (MsdSmartcardManager *manager, + MsdSmartcard *card) +{ + g_debug ("informing smartcard of its insertion"); + + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_INSERTED); + g_debug ("done"); + +} + +static void +msd_smartcard_manager_class_install_signals (MsdSmartcardManagerClass *manager_class) +{ + GObjectClass *object_class; + + object_class = G_OBJECT_CLASS (manager_class); + + msd_smartcard_manager_signals[SMARTCARD_INSERTED] = + g_signal_new ("smartcard-inserted", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (MsdSmartcardManagerClass, + smartcard_inserted), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); + manager_class->smartcard_inserted = msd_smartcard_manager_card_inserted_handler; + + msd_smartcard_manager_signals[SMARTCARD_REMOVED] = + g_signal_new ("smartcard-removed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (MsdSmartcardManagerClass, + smartcard_removed), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); + manager_class->smartcard_removed = msd_smartcard_manager_card_removed_handler; + + msd_smartcard_manager_signals[ERROR] = + g_signal_new ("error", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (MsdSmartcardManagerClass, error), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); + manager_class->error = NULL; +} + +static gboolean +slot_id_equal (CK_SLOT_ID *slot_id_1, + CK_SLOT_ID *slot_id_2) +{ + g_assert (slot_id_1 != NULL); + g_assert (slot_id_2 != NULL); + + return *slot_id_1 == *slot_id_2; +} + +static gboolean +slot_id_hash (CK_SLOT_ID *slot_id) +{ + guint32 upper_bits, lower_bits; + int temp; + + if (sizeof (CK_SLOT_ID) == sizeof (int)) { + return g_int_hash (slot_id); + } + + upper_bits = ((*slot_id) >> 31) - 1; + lower_bits = (*slot_id) & 0xffffffff; + + /* The upper bits are almost certainly always zero, + * so let's degenerate to g_int_hash for the + * (very) common case + */ + temp = lower_bits + upper_bits; + return upper_bits + g_int_hash (&temp); +} + +static void +msd_smartcard_manager_init (MsdSmartcardManager *manager) +{ + g_debug ("initializing smartcard manager"); + + manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager, + MSD_TYPE_SMARTCARD_MANAGER, + MsdSmartcardManagerPrivate); + manager->priv->poll_timeout_id = 0; + manager->priv->is_unstoppable = FALSE; + manager->priv->module = NULL; + + manager->priv->smartcards = + g_hash_table_new_full (g_str_hash, + g_str_equal, + (GDestroyNotify) g_free, + (GDestroyNotify) g_object_unref); + + if (!g_thread_supported ()) { + g_thread_init (NULL); + } + +} + +static void +msd_smartcard_manager_finalize (GObject *object) +{ + MsdSmartcardManager *manager; + GObjectClass *gobject_class; + + manager = MSD_SMARTCARD_MANAGER (object); + gobject_class = + G_OBJECT_CLASS (msd_smartcard_manager_parent_class); + + msd_smartcard_manager_stop_now (manager); + + g_hash_table_destroy (manager->priv->smartcards); + manager->priv->smartcards = NULL; + + gobject_class->finalize (object); +} + +GQuark +msd_smartcard_manager_error_quark (void) +{ + static GQuark error_quark = 0; + + if (error_quark == 0) { + error_quark = g_quark_from_static_string ("msd-smartcard-manager-error-quark"); + } + + return error_quark; +} + +MsdSmartcardManager * +msd_smartcard_manager_new (const char *module_path) +{ + MsdSmartcardManager *instance; + + instance = MSD_SMARTCARD_MANAGER (g_object_new (MSD_TYPE_SMARTCARD_MANAGER, + "module-path", module_path, + NULL)); + + return instance; +} + +static void +msd_smartcard_manager_emit_error (MsdSmartcardManager *manager, + GError *error) +{ + manager->priv->is_unstoppable = TRUE; + g_signal_emit (manager, msd_smartcard_manager_signals[ERROR], 0, + error); + manager->priv->is_unstoppable = FALSE; +} + +static void +msd_smartcard_manager_emit_smartcard_inserted (MsdSmartcardManager *manager, + MsdSmartcard *card) +{ + manager->priv->is_unstoppable = TRUE; + g_signal_emit (manager, msd_smartcard_manager_signals[SMARTCARD_INSERTED], 0, + card); + manager->priv->is_unstoppable = FALSE; +} + +static void +msd_smartcard_manager_emit_smartcard_removed (MsdSmartcardManager *manager, + MsdSmartcard *card) +{ + MsdSmartcardManagerState old_state; + + old_state = manager->priv->state; + manager->priv->is_unstoppable = TRUE; + g_signal_emit (manager, msd_smartcard_manager_signals[SMARTCARD_REMOVED], 0, + card); + manager->priv->is_unstoppable = FALSE; +} + +static gboolean +msd_smartcard_manager_check_for_and_process_events (GIOChannel *io_channel, + GIOCondition condition, + MsdSmartcardManager *manager) +{ + MsdSmartcard *card; + gboolean should_stop; + gchar event_type; + char *card_name; + int fd; + + card = NULL; + should_stop = (condition & G_IO_HUP) || (condition & G_IO_ERR); + + if (should_stop) { + g_debug ("received %s on event socket, stopping " + "manager...", + (condition & G_IO_HUP) && (condition & G_IO_ERR)? + "error and hangup" : + (condition & G_IO_HUP)? + "hangup" : "error"); + } + + if (!(condition & G_IO_IN)) { + goto out; + } + + fd = g_io_channel_unix_get_fd (io_channel); + + event_type = '\0'; + if (!read_bytes (fd, &event_type, 1)) { + should_stop = TRUE; + goto out; + } + + card = read_smartcard (fd, manager->priv->module); + + if (card == NULL) { + should_stop = TRUE; + goto out; + } + + card_name = msd_smartcard_get_name (card); + + switch (event_type) { + case 'I': + g_hash_table_replace (manager->priv->smartcards, + card_name, card); + card_name = NULL; + + msd_smartcard_manager_emit_smartcard_inserted (manager, card); + card = NULL; + break; + + case 'R': + msd_smartcard_manager_emit_smartcard_removed (manager, card); + if (!g_hash_table_remove (manager->priv->smartcards, card_name)) { + g_debug ("got removal event of unknown card!"); + } + g_free (card_name); + card_name = NULL; + card = NULL; + break; + + default: + g_free (card_name); + card_name = NULL; + g_object_unref (card); + + should_stop = TRUE; + break; + } + +out: + if (should_stop) { + GError *error; + + error = g_error_new (MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, + "%s", (condition & G_IO_IN) ? g_strerror (errno) : _("received error or hang up from event source")); + + msd_smartcard_manager_emit_error (manager, error); + g_error_free (error); + msd_smartcard_manager_stop_now (manager); + return FALSE; + } + + return TRUE; +} + +static void +msd_smartcard_manager_event_processing_stopped_handler (MsdSmartcardManager *manager) +{ + manager->priv->smartcard_event_source = NULL; + msd_smartcard_manager_stop_now (manager); +} + +static gboolean +open_pipe (int *write_fd, + int *read_fd) +{ + int pipe_fds[2] = { -1, -1 }; + + g_assert (write_fd != NULL); + g_assert (read_fd != NULL); + + if (pipe (pipe_fds) < 0) { + return FALSE; + } + + if (fcntl (pipe_fds[0], F_SETFD, FD_CLOEXEC) < 0) { + close (pipe_fds[0]); + close (pipe_fds[1]); + return FALSE; + } + + if (fcntl (pipe_fds[1], F_SETFD, FD_CLOEXEC) < 0) { + close (pipe_fds[0]); + close (pipe_fds[1]); + return FALSE; + } + + *read_fd = pipe_fds[0]; + *write_fd = pipe_fds[1]; + + return TRUE; +} + +static void +msd_smartcard_manager_stop_watching_for_events (MsdSmartcardManager *manager) +{ + if (manager->priv->smartcard_event_source != NULL) { + g_source_destroy (manager->priv->smartcard_event_source); + manager->priv->smartcard_event_source = NULL; + } + + if (manager->priv->worker_thread != NULL) { + SECMOD_CancelWait (manager->priv->module); + manager->priv->worker_thread = NULL; + } +} + +static gboolean +load_nss (GError **error) +{ + SECStatus status = SECSuccess; + static const guint32 flags = + NSS_INIT_READONLY | NSS_INIT_NOCERTDB | NSS_INIT_NOMODDB | + NSS_INIT_FORCEOPEN | NSS_INIT_NOROOTINIT | + NSS_INIT_OPTIMIZESPACE | NSS_INIT_PK11RELOAD; + + g_debug ("attempting to load NSS database '%s'", + MSD_SMARTCARD_MANAGER_NSS_DB); + + status = NSS_Initialize (MSD_SMARTCARD_MANAGER_NSS_DB, + "", "", SECMOD_DB, flags); + + if (status != SECSuccess) { + gsize error_message_size; + char *error_message; + + error_message_size = PR_GetErrorTextLength (); + + if (error_message_size == 0) { + g_debug ("NSS security system could not be initialized"); + g_set_error (error, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, + _("NSS security system could not be initialized")); + goto out; + } + + error_message = g_slice_alloc0 (error_message_size); + PR_GetErrorText (error_message); + + g_set_error (error, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, + "%s", error_message); + g_debug ("NSS security system could not be initialized - %s", + error_message); + + g_slice_free1 (error_message_size, error_message); + + goto out; + } + + g_debug ("NSS database sucessfully loaded"); + return TRUE; + +out: + g_debug ("NSS database couldn't be sucessfully loaded"); + return FALSE; +} + +static SECMODModule * +load_driver (char *module_path, + GError **error) +{ + SECMODModule *module; + char *module_spec; + gboolean module_explicitly_specified; + + g_debug ("attempting to load driver..."); + + module = NULL; + module_explicitly_specified = module_path != NULL; + if (module_explicitly_specified) { + module_spec = g_strdup_printf ("library=\"%s\"", module_path); + g_debug ("loading smartcard driver using spec '%s'", + module_spec); + + module = SECMOD_LoadUserModule (module_spec, + NULL /* parent */, + FALSE /* recurse */); + g_free (module_spec); + module_spec = NULL; + + } else { + SECMODModuleList *modules, *tmp; + + modules = SECMOD_GetDefaultModuleList (); + + for (tmp = modules; tmp != NULL; tmp = tmp->next) { + if (!SECMOD_HasRemovableSlots (tmp->module) || + !tmp->module->loaded) + continue; + + module = SECMOD_ReferenceModule (tmp->module); + break; + } + } + + if (!module_explicitly_specified && module == NULL) { + g_set_error (error, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, + _("no suitable smartcard driver could be found")); + } else if (module == NULL || !module->loaded) { + + gsize error_message_size; + char *error_message; + + if (module != NULL && !module->loaded) { + g_debug ("module found but not loaded?!"); + SECMOD_DestroyModule (module); + module = NULL; + } + + error_message_size = PR_GetErrorTextLength (); + + if (error_message_size == 0) { + g_debug ("smartcard driver '%s' could not be loaded", + module_path); + g_set_error (error, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, + _("smartcard driver '%s' could not be " + "loaded"), module_path); + goto out; + } + + error_message = g_slice_alloc0 (error_message_size); + PR_GetErrorText (error_message); + + g_set_error (error, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, + "%s", error_message); + + g_debug ("smartcard driver '%s' could not be loaded - %s", + module_path, error_message); + g_slice_free1 (error_message_size, error_message); + } + +out: + return module; +} + +static void +msd_smartcard_manager_get_all_cards (MsdSmartcardManager *manager) +{ + int i; + + for (i = 0; i < manager->priv->module->slotCount; i++) { + MsdSmartcard *card; + CK_SLOT_ID slot_id; + int slot_series; + char *card_name; + + slot_id = PK11_GetSlotID (manager->priv->module->slots[i]); + slot_series = PK11_GetSlotSeries (manager->priv->module->slots[i]); + + card = _msd_smartcard_new (manager->priv->module, + slot_id, slot_series); + + card_name = msd_smartcard_get_name (card); + + g_hash_table_replace (manager->priv->smartcards, + card_name, card); + } +} + +gboolean +msd_smartcard_manager_start (MsdSmartcardManager *manager, + GError **error) +{ + GError *watching_error; + int worker_fd; + GPid worker_pid; + GIOChannel *io_channel; + GSource *source; + GIOFlags channel_flags; + GError *nss_error; + + if (manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STARTED) { + g_debug ("smartcard manager already started"); + return TRUE; + } + + manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STARTING; + + worker_fd = -1; + worker_pid = 0; + + nss_error = NULL; + if (!manager->priv->nss_is_loaded && !load_nss (&nss_error)) { + g_propagate_error (error, nss_error); + goto out; + } + manager->priv->nss_is_loaded = TRUE; + + if (manager->priv->module == NULL) { + manager->priv->module = load_driver (manager->priv->module_path, &nss_error); + } + + if (manager->priv->module == NULL) { + g_propagate_error (error, nss_error); + goto out; + } + + if (!msd_smartcard_manager_create_worker (manager, &worker_fd, &manager->priv->worker_thread)) { + + g_set_error (error, + MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, + _("could not watch for incoming card events - %s"), + g_strerror (errno)); + + goto out; + } + + io_channel = g_io_channel_unix_new (worker_fd); + + channel_flags = g_io_channel_get_flags (io_channel); + watching_error = NULL; + + source = g_io_create_watch (io_channel, G_IO_IN | G_IO_HUP); + g_io_channel_unref (io_channel); + io_channel = NULL; + + manager->priv->smartcard_event_source = source; + + g_source_set_callback (manager->priv->smartcard_event_source, + (GSourceFunc) (GIOFunc) + msd_smartcard_manager_check_for_and_process_events, + manager, + (GDestroyNotify) + msd_smartcard_manager_event_processing_stopped_handler); + g_source_attach (manager->priv->smartcard_event_source, NULL); + g_source_unref (manager->priv->smartcard_event_source); + + /* populate the hash with cards that are already inserted + */ + msd_smartcard_manager_get_all_cards (manager); + + manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STARTED; + +out: + /* don't leave it in a half started state + */ + if (manager->priv->state != MSD_SMARTCARD_MANAGER_STATE_STARTED) { + g_debug ("smartcard manager could not be completely started"); + msd_smartcard_manager_stop (manager); + } else { + g_debug ("smartcard manager started"); + } + + return manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STARTED; +} + +static gboolean +msd_smartcard_manager_stop_now (MsdSmartcardManager *manager) +{ + if (manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STOPPED) { + return FALSE; + } + + manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STOPPED; + msd_smartcard_manager_stop_watching_for_events (manager); + + if (manager->priv->module != NULL) { + SECMOD_DestroyModule (manager->priv->module); + manager->priv->module = NULL; + } + + if (manager->priv->nss_is_loaded) { + NSS_Shutdown (); + manager->priv->nss_is_loaded = FALSE; + } + + g_debug ("smartcard manager stopped"); + + return FALSE; +} + +static void +msd_smartcard_manager_queue_stop (MsdSmartcardManager *manager) +{ + + manager->priv->state = MSD_SMARTCARD_MANAGER_STATE_STOPPING; + + g_idle_add ((GSourceFunc) msd_smartcard_manager_stop_now, manager); +} + +void +msd_smartcard_manager_stop (MsdSmartcardManager *manager) +{ + if (manager->priv->state == MSD_SMARTCARD_MANAGER_STATE_STOPPED) { + return; + } + + if (manager->priv->is_unstoppable) { + msd_smartcard_manager_queue_stop (manager); + return; + } + + msd_smartcard_manager_stop_now (manager); +} + +static void +msd_smartcard_manager_check_for_login_card (CK_SLOT_ID slot_id, + MsdSmartcard *card, + gboolean *is_inserted) +{ + g_assert (is_inserted != NULL); + + if (msd_smartcard_is_login_card (card)) { + *is_inserted = TRUE; + } + +} + +gboolean +msd_smartcard_manager_login_card_is_inserted (MsdSmartcardManager *manager) + +{ + gboolean is_inserted; + + is_inserted = FALSE; + g_hash_table_foreach (manager->priv->smartcards, + (GHFunc) + msd_smartcard_manager_check_for_login_card, + &is_inserted); + return is_inserted; +} + +static MsdSmartcardManagerWorker * +msd_smartcard_manager_worker_new (int write_fd) +{ + MsdSmartcardManagerWorker *worker; + + worker = g_slice_new0 (MsdSmartcardManagerWorker); + worker->write_fd = write_fd; + worker->module = NULL; + + worker->smartcards = + g_hash_table_new_full ((GHashFunc) slot_id_hash, + (GEqualFunc) slot_id_equal, + (GDestroyNotify) g_free, + (GDestroyNotify) g_object_unref); + + return worker; +} + +static void +msd_smartcard_manager_worker_free (MsdSmartcardManagerWorker *worker) +{ + if (worker->smartcards != NULL) { + g_hash_table_destroy (worker->smartcards); + worker->smartcards = NULL; + } + + g_slice_free (MsdSmartcardManagerWorker, worker); +} + +static gboolean +read_bytes (int fd, + gpointer bytes, + gsize num_bytes) +{ + size_t bytes_left; + size_t total_bytes_read; + ssize_t bytes_read; + + bytes_left = (size_t) num_bytes; + total_bytes_read = 0; + + do { + bytes_read = read (fd, + (char *) bytes + total_bytes_read, + bytes_left); + g_assert (bytes_read <= (ssize_t) bytes_left); + + if (bytes_read <= 0) { + if ((bytes_read < 0) && (errno == EINTR || errno == EAGAIN)) { + continue; + } + + bytes_left = 0; + } else { + bytes_left -= bytes_read; + total_bytes_read += bytes_read; + } + } while (bytes_left > 0); + + if (total_bytes_read < (size_t) num_bytes) { + return FALSE; + } + + return TRUE; +} + +static gboolean +write_bytes (int fd, + gconstpointer bytes, + gsize num_bytes) +{ + size_t bytes_left; + size_t total_bytes_written; + ssize_t bytes_written; + + bytes_left = (size_t) num_bytes; + total_bytes_written = 0; + + do { + bytes_written = write (fd, + (char *) bytes + total_bytes_written, + bytes_left); + g_assert (bytes_written <= (ssize_t) bytes_left); + + if (bytes_written <= 0) { + if ((bytes_written < 0) && (errno == EINTR || errno == EAGAIN)) { + continue; + } + + bytes_left = 0; + } else { + bytes_left -= bytes_written; + total_bytes_written += bytes_written; + } + } while (bytes_left > 0); + + if (total_bytes_written < (size_t) num_bytes) { + return FALSE; + } + + return TRUE; +} + +static MsdSmartcard * +read_smartcard (int fd, + SECMODModule *module) +{ + MsdSmartcard *card; + char *card_name; + gsize card_name_size; + + card_name_size = 0; + if (!read_bytes (fd, &card_name_size, sizeof (card_name_size))) { + return NULL; + } + + card_name = g_slice_alloc0 (card_name_size); + if (!read_bytes (fd, card_name, card_name_size)) { + g_slice_free1 (card_name_size, card_name); + return NULL; + } + card = _msd_smartcard_new_from_name (module, card_name); + g_slice_free1 (card_name_size, card_name); + + return card; +} + +static gboolean +write_smartcard (int fd, + MsdSmartcard *card) +{ + gsize card_name_size; + char *card_name; + + card_name = msd_smartcard_get_name (card); + card_name_size = strlen (card_name) + 1; + + if (!write_bytes (fd, &card_name_size, sizeof (card_name_size))) { + g_free (card_name); + return FALSE; + } + + if (!write_bytes (fd, card_name, card_name_size)) { + g_free (card_name); + return FALSE; + } + g_free (card_name); + + return TRUE; +} + +static gboolean +msd_smartcard_manager_worker_emit_smartcard_removed (MsdSmartcardManagerWorker *worker, + MsdSmartcard *card, + GError **error) +{ + g_debug ("card '%s' removed!", msd_smartcard_get_name (card)); + + if (!write_bytes (worker->write_fd, "R", 1)) { + goto error_out; + } + + if (!write_smartcard (worker->write_fd, card)) { + goto error_out; + } + + return TRUE; + +error_out: + g_set_error (error, MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS, + "%s", g_strerror (errno)); + return FALSE; +} + +static gboolean +msd_smartcard_manager_worker_emit_smartcard_inserted (MsdSmartcardManagerWorker *worker, + MsdSmartcard *card, + GError **error) +{ + GError *write_error; + + write_error = NULL; + g_debug ("card '%s' inserted!", msd_smartcard_get_name (card)); + if (!write_bytes (worker->write_fd, "I", 1)) { + goto error_out; + } + + if (!write_smartcard (worker->write_fd, card)) { + goto error_out; + } + + return TRUE; + +error_out: + g_set_error (error, MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS, + "%s", g_strerror (errno)); + return FALSE; +} + +static gboolean +msd_smartcard_manager_worker_watch_for_and_process_event (MsdSmartcardManagerWorker *worker, + GError **error) +{ + PK11SlotInfo *slot; + CK_SLOT_ID slot_id, *key; + int slot_series, card_slot_series; + MsdSmartcard *card; + GError *processing_error; + gboolean ret; + + g_debug ("waiting for card event"); + ret = FALSE; + + slot = SECMOD_WaitForAnyTokenEvent (worker->module, 0, PR_SecondsToInterval (1)); + processing_error = NULL; + + if (slot == NULL) { + int error_code; + + error_code = PORT_GetError (); + if ((error_code == 0) || (error_code == SEC_ERROR_NO_EVENT)) { + g_debug ("spurrious event occurred"); + return TRUE; + } + + /* FIXME: is there a function to convert from a PORT error + * code to a translated string? + */ + g_set_error (error, MSD_SMARTCARD_MANAGER_ERROR, + MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, + _("encountered unexpected error while " + "waiting for smartcard events")); + goto out; + } + + /* the slot id and series together uniquely identify a card. + * You can never have two cards with the same slot id at the + * same time, however (I think), so we can key off of it. + */ + slot_id = PK11_GetSlotID (slot); + slot_series = PK11_GetSlotSeries (slot); + + /* First check to see if there is a card that we're currently + * tracking in the slot. + */ + key = g_new (CK_SLOT_ID, 1); + *key = slot_id; + card = g_hash_table_lookup (worker->smartcards, key); + + if (card != NULL) { + card_slot_series = msd_smartcard_get_slot_series (card); + } else { + card_slot_series = -1; + } + + if (PK11_IsPresent (slot)) { + /* Now, check to see if their is a new card in the slot. + * If there was a different card in the slot now than + * there was before, then we need to emit a removed signal + * for the old card (we don't want unpaired insertion events). + */ + if ((card != NULL) && + card_slot_series != slot_series) { + if (!msd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { + g_propagate_error (error, processing_error); + goto out; + } + } + + card = _msd_smartcard_new (worker->module, + slot_id, slot_series); + + g_hash_table_replace (worker->smartcards, + key, card); + key = NULL; + + if (!msd_smartcard_manager_worker_emit_smartcard_inserted (worker, + card, + &processing_error)) { + g_propagate_error (error, processing_error); + goto out; + } + } else { + /* if we aren't tracking the card, just discard the event. + * We don't want unpaired remove events. Note on startup + * NSS will generate an "insertion" event if a card is + * already inserted in the slot. + */ + if ((card != NULL)) { + /* FIXME: i'm not sure about this code. Maybe we + * shouldn't do this at all, or maybe we should do it + * n times (where n = slot_series - card_slot_series + 1) + * + * Right now, i'm just doing it once. + */ + if ((slot_series - card_slot_series) > 1) { + + if (!msd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { + g_propagate_error (error, processing_error); + goto out; + } + g_hash_table_remove (worker->smartcards, key); + + card = _msd_smartcard_new (worker->module, + slot_id, slot_series); + g_hash_table_replace (worker->smartcards, + key, card); + key = NULL; + if (!msd_smartcard_manager_worker_emit_smartcard_inserted (worker, card, &processing_error)) { + g_propagate_error (error, processing_error); + goto out; + } + } + + if (!msd_smartcard_manager_worker_emit_smartcard_removed (worker, card, &processing_error)) { + g_propagate_error (error, processing_error); + goto out; + } + + g_hash_table_remove (worker->smartcards, key); + card = NULL; + } else { + g_debug ("got spurious remove event"); + } + } + + ret = TRUE; + +out: + g_free (key); + PK11_FreeSlot (slot); + + return ret; +} + +static void +msd_smartcard_manager_worker_run (MsdSmartcardManagerWorker *worker) +{ + GError *error; + + + error = NULL; + + while (msd_smartcard_manager_worker_watch_for_and_process_event (worker, &error)); + + if (error != NULL) { + g_debug ("could not process card event - %s", error->message); + g_error_free (error); + } + + msd_smartcard_manager_worker_free (worker); +} + +static gboolean +msd_smartcard_manager_create_worker (MsdSmartcardManager *manager, + int *worker_fd, + GThread **worker_thread) +{ + MsdSmartcardManagerWorker *worker; + int write_fd, read_fd; + + write_fd = -1; + read_fd = -1; + if (!open_pipe (&write_fd, &read_fd)) { + return FALSE; + } + + worker = msd_smartcard_manager_worker_new (write_fd); + worker->module = manager->priv->module; + + *worker_thread = g_thread_create ((GThreadFunc) + msd_smartcard_manager_worker_run, + worker, FALSE, NULL); + + if (*worker_thread == NULL) { + msd_smartcard_manager_worker_free (worker); + return FALSE; + } + + if (worker_fd) { + *worker_fd = read_fd; + } + + return TRUE; +} + +#ifdef MSD_SMARTCARD_MANAGER_ENABLE_TEST +#include + +static GMainLoop *event_loop; +static gboolean should_exit_on_next_remove = FALSE; + +static gboolean +on_timeout (MsdSmartcardManager *manager) +{ + GError *error; + g_print ("Re-enabling manager.\n"); + + if (!msd_smartcard_manager_start (manager, &error)) { + g_warning ("could not start smartcard manager - %s", + error->message); + g_error_free (error); + return 1; + } + g_print ("Please re-insert smartcard\n"); + + should_exit_on_next_remove = TRUE; + + return FALSE; +} + +static void +on_device_inserted (MsdSmartcardManager *manager, + MsdSmartcard *card) +{ + g_print ("smartcard inserted!\n"); + g_print ("Please remove it.\n"); +} + +static void +on_device_removed (MsdSmartcardManager *manager, + MsdSmartcard *card) +{ + g_print ("smartcard removed!\n"); + + if (should_exit_on_next_remove) { + g_main_loop_quit (event_loop); + } else { + g_print ("disabling manager for 2 seconds\n"); + msd_smartcard_manager_stop (manager); + g_timeout_add (2000, (GSourceFunc) on_timeout, manager); + } +} + +int +main (int argc, + char *argv[]) +{ + MsdSmartcardManager *manager; + GError *error; + + g_log_set_always_fatal (G_LOG_LEVEL_ERROR + | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING); + + g_type_init (); + + g_message ("creating instance of 'smartcard manager' object..."); + manager = msd_smartcard_manager_new (NULL); + g_message ("'smartcard manager' object created successfully"); + + g_signal_connect (manager, "smartcard-inserted", + G_CALLBACK (on_device_inserted), NULL); + + g_signal_connect (manager, "smartcard-removed", + G_CALLBACK (on_device_removed), NULL); + + g_message ("starting listener..."); + + error = NULL; + if (!msd_smartcard_manager_start (manager, &error)) { + g_warning ("could not start smartcard manager - %s", + error->message); + g_error_free (error); + return 1; + } + + event_loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (event_loop); + g_main_loop_unref (event_loop); + event_loop = NULL; + + g_message ("destroying previously created 'smartcard manager' object..."); + g_object_unref (manager); + manager = NULL; + g_message ("'smartcard manager' object destroyed successfully"); + + return 0; +} +#endif diff --git a/plugins/smartcard/msd-smartcard-manager.h b/plugins/smartcard/msd-smartcard-manager.h new file mode 100644 index 0000000..269e1da --- /dev/null +++ b/plugins/smartcard/msd-smartcard-manager.h @@ -0,0 +1,90 @@ +/* msd-smartcard-manager.h - object for monitoring smartcard insertion and + * removal events + * + * Copyright (C) 2006, 2009 Red Hat, Inc. + * + * 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, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Written by: Ray Strode + */ +#ifndef MSD_SMARTCARD_MANAGER_H +#define MSD_SMARTCARD_MANAGER_H + +#define MSD_SMARTCARD_ENABLE_INTERNAL_API +#include "msd-smartcard.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif +#define MSD_TYPE_SMARTCARD_MANAGER (msd_smartcard_manager_get_type ()) +#define MSD_SMARTCARD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MSD_TYPE_SMARTCARD_MANAGER, MsdSmartcardManager)) +#define MSD_SMARTCARD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MSD_TYPE_SMARTCARD_MANAGER, MsdSmartcardManagerClass)) +#define MSD_IS_SMARTCARD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SC_TYPE_SMARTCARD_MANAGER)) +#define MSD_IS_SMARTCARD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SC_TYPE_SMARTCARD_MANAGER)) +#define MSD_SMARTCARD_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), MSD_TYPE_SMARTCARD_MANAGER, MsdSmartcardManagerClass)) +#define MSD_SMARTCARD_MANAGER_ERROR (msd_smartcard_manager_error_quark ()) +typedef struct _MsdSmartcardManager MsdSmartcardManager; +typedef struct _MsdSmartcardManagerClass MsdSmartcardManagerClass; +typedef struct _MsdSmartcardManagerPrivate MsdSmartcardManagerPrivate; +typedef enum _MsdSmartcardManagerError MsdSmartcardManagerError; + +struct _MsdSmartcardManager { + GObject parent; + + /*< private > */ + MsdSmartcardManagerPrivate *priv; +}; + +struct _MsdSmartcardManagerClass { + GObjectClass parent_class; + + /* Signals */ + void (*smartcard_inserted) (MsdSmartcardManager *manager, + MsdSmartcard *token); + void (*smartcard_removed) (MsdSmartcardManager *manager, + MsdSmartcard *token); + void (*error) (MsdSmartcardManager *manager, + GError *error); +}; + +enum _MsdSmartcardManagerError { + MSD_SMARTCARD_MANAGER_ERROR_GENERIC = 0, + MSD_SMARTCARD_MANAGER_ERROR_WITH_NSS, + MSD_SMARTCARD_MANAGER_ERROR_LOADING_DRIVER, + MSD_SMARTCARD_MANAGER_ERROR_WATCHING_FOR_EVENTS, + MSD_SMARTCARD_MANAGER_ERROR_REPORTING_EVENTS +}; + +GType msd_smartcard_manager_get_type (void) G_GNUC_CONST; +GQuark msd_smartcard_manager_error_quark (void) G_GNUC_CONST; + +MsdSmartcardManager *msd_smartcard_manager_new (const char *module); + +gboolean msd_smartcard_manager_start (MsdSmartcardManager *manager, + GError **error); + +void msd_smartcard_manager_stop (MsdSmartcardManager *manager); + +char *msd_smartcard_manager_get_module_path (MsdSmartcardManager *manager); +gboolean msd_smartcard_manager_login_card_is_inserted (MsdSmartcardManager *manager); + +#ifdef __cplusplus +} +#endif +#endif /* MSD_SMARTCARD_MANAGER_H */ diff --git a/plugins/smartcard/msd-smartcard-plugin.c b/plugins/smartcard/msd-smartcard-plugin.c new file mode 100644 index 0000000..dc4cb8f --- /dev/null +++ b/plugins/smartcard/msd-smartcard-plugin.c @@ -0,0 +1,340 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2010 Red Hat, Inc. + * + * 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, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include "config.h" + +#include +#include + +#include +#include + +#include + +#include + +#include "mate-settings-plugin.h" +#include "msd-smartcard-plugin.h" +#include "msd-smartcard-manager.h" + +struct MsdSmartcardPluginPrivate { + MsdSmartcardManager *manager; + DBusGConnection *bus_connection; + + guint32 is_active : 1; +}; + +typedef enum +{ + MSD_SMARTCARD_REMOVE_ACTION_NONE, + MSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN, + MSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT, +} MsdSmartcardRemoveAction; + +#define SCREENSAVER_DBUS_NAME "org.mate.ScreenSaver" +#define SCREENSAVER_DBUS_PATH "/" +#define SCREENSAVER_DBUS_INTERFACE "org.mate.ScreenSaver" + +#define SM_DBUS_NAME "org.mate.SessionManager" +#define SM_DBUS_PATH "/org/mate/SessionManager" +#define SM_DBUS_INTERFACE "org.mate.SessionManager" +#define SM_LOGOUT_MODE_FORCE 2 + +#define MSD_SMARTCARD_KEY "/desktop/mate/peripherals/smartcard" +#define KEY_REMOVE_ACTION MSD_SMARTCARD_KEY "/removal_action" + +#define MSD_SMARTCARD_PLUGIN_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPluginPrivate)) + +MATE_SETTINGS_PLUGIN_REGISTER (MsdSmartcardPlugin, msd_smartcard_plugin); + +static void +simulate_user_activity (MsdSmartcardPlugin *plugin) +{ + DBusGProxy *screensaver_proxy; + + g_debug ("MsdSmartcardPlugin telling screensaver about smart card insertion"); + screensaver_proxy = dbus_g_proxy_new_for_name (plugin->priv->bus_connection, + SCREENSAVER_DBUS_NAME, + SCREENSAVER_DBUS_PATH, + SCREENSAVER_DBUS_INTERFACE); + + dbus_g_proxy_call_no_reply (screensaver_proxy, + "SimulateUserActivity", + G_TYPE_INVALID, G_TYPE_INVALID); + + g_object_unref (screensaver_proxy); +} + +static void +lock_screen (MsdSmartcardPlugin *plugin) +{ + DBusGProxy *screensaver_proxy; + + g_debug ("MsdSmartcardPlugin telling screensaver to lock screen"); + screensaver_proxy = dbus_g_proxy_new_for_name (plugin->priv->bus_connection, + SCREENSAVER_DBUS_NAME, + SCREENSAVER_DBUS_PATH, + SCREENSAVER_DBUS_INTERFACE); + + dbus_g_proxy_call_no_reply (screensaver_proxy, + "Lock", + G_TYPE_INVALID, G_TYPE_INVALID); + + g_object_unref (screensaver_proxy); +} + +static void +force_logout (MsdSmartcardPlugin *plugin) +{ + DBusGProxy *sm_proxy; + GError *error; + gboolean res; + + g_debug ("MsdSmartcardPlugin telling session manager to force logout"); + sm_proxy = dbus_g_proxy_new_for_name (plugin->priv->bus_connection, + SM_DBUS_NAME, + SM_DBUS_PATH, + SM_DBUS_INTERFACE); + + error = NULL; + res = dbus_g_proxy_call (sm_proxy, + "Logout", + &error, + G_TYPE_UINT, SM_LOGOUT_MODE_FORCE, + G_TYPE_INVALID, G_TYPE_INVALID); + + if (! res) { + g_warning ("MsdSmartcardPlugin Unable to force logout: %s", error->message); + g_error_free (error); + } + + g_object_unref (sm_proxy); +} + +static void +msd_smartcard_plugin_init (MsdSmartcardPlugin *plugin) +{ + plugin->priv = MSD_SMARTCARD_PLUGIN_GET_PRIVATE (plugin); + + g_debug ("MsdSmartcardPlugin initializing"); + + plugin->priv->manager = msd_smartcard_manager_new (NULL); +} + +static void +msd_smartcard_plugin_finalize (GObject *object) +{ + MsdSmartcardPlugin *plugin; + + g_return_if_fail (object != NULL); + g_return_if_fail (MSD_IS_SMARTCARD_PLUGIN (object)); + + g_debug ("MsdSmartcardPlugin finalizing"); + + plugin = MSD_SMARTCARD_PLUGIN (object); + + g_return_if_fail (plugin->priv != NULL); + + if (plugin->priv->manager != NULL) { + g_object_unref (plugin->priv->manager); + } + + G_OBJECT_CLASS (msd_smartcard_plugin_parent_class)->finalize (object); +} + +static void +smartcard_inserted_cb (MsdSmartcardManager *card_monitor, + MsdSmartcard *card, + MsdSmartcardPlugin *plugin) +{ + char *name; + + name = msd_smartcard_get_name (card); + g_debug ("MsdSmartcardPlugin smart card '%s' inserted", name); + g_free (name); + + simulate_user_activity (plugin); +} + +static gboolean +user_logged_in_with_smartcard (void) +{ + return g_getenv ("PKCS11_LOGIN_TOKEN_NAME") != NULL; +} + +static MsdSmartcardRemoveAction +get_configured_remove_action (MsdSmartcardPlugin *plugin) +{ + MateConfClient *client; + char *remove_action_string; + MsdSmartcardRemoveAction remove_action; + + client = mateconf_client_get_default (); + remove_action_string = mateconf_client_get_string (client, + KEY_REMOVE_ACTION, NULL); + + if (remove_action_string == NULL) { + g_warning ("MsdSmartcardPlugin unable to get smartcard remove action"); + remove_action = MSD_SMARTCARD_REMOVE_ACTION_NONE; + } else if (strcmp (remove_action_string, "none") == 0) { + remove_action = MSD_SMARTCARD_REMOVE_ACTION_NONE; + } else if (strcmp (remove_action_string, "lock_screen") == 0) { + remove_action = MSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN; + } else if (strcmp (remove_action_string, "force_logout") == 0) { + remove_action = MSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT; + } else { + g_warning ("MsdSmartcardPlugin unknown smartcard remove action"); + remove_action = MSD_SMARTCARD_REMOVE_ACTION_NONE; + } + + g_object_unref (client); + + return remove_action; +} + +static void +process_smartcard_removal (MsdSmartcardPlugin *plugin) +{ + MsdSmartcardRemoveAction remove_action; + + g_debug ("MsdSmartcardPlugin processing smartcard removal"); + remove_action = get_configured_remove_action (plugin); + + switch (remove_action) + { + case MSD_SMARTCARD_REMOVE_ACTION_NONE: + return; + case MSD_SMARTCARD_REMOVE_ACTION_LOCK_SCREEN: + lock_screen (plugin); + break; + case MSD_SMARTCARD_REMOVE_ACTION_FORCE_LOGOUT: + force_logout (plugin); + break; + } +} + +static void +smartcard_removed_cb (MsdSmartcardManager *card_monitor, + MsdSmartcard *card, + MsdSmartcardPlugin *plugin) +{ + + char *name; + + name = msd_smartcard_get_name (card); + g_debug ("MsdSmartcardPlugin smart card '%s' removed", name); + g_free (name); + + if (!msd_smartcard_is_login_card (card)) { + g_debug ("MsdSmartcardPlugin removed smart card was not used to login"); + return; + } + + process_smartcard_removal (plugin); +} + +static void +impl_activate (MateSettingsPlugin *plugin) +{ + GError *error; + MsdSmartcardPlugin *smartcard_plugin = MSD_SMARTCARD_PLUGIN (plugin); + + if (smartcard_plugin->priv->is_active) { + g_debug ("MsdSmartcardPlugin Not activating smartcard plugin, because it's " + "already active"); + return; + } + + if (!user_logged_in_with_smartcard ()) { + g_debug ("MsdSmartcardPlugin Not activating smartcard plugin, because user didn't use " + " smartcard to log in"); + smartcard_plugin->priv->is_active = FALSE; + return; + } + + g_debug ("MsdSmartcardPlugin Activating smartcard plugin"); + + error = NULL; + smartcard_plugin->priv->bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); + + if (smartcard_plugin->priv->bus_connection == NULL) { + g_warning ("MsdSmartcardPlugin Unable to connect to session bus: %s", error->message); + return; + } + + if (!msd_smartcard_manager_start (smartcard_plugin->priv->manager, &error)) { + g_warning ("MsdSmartcardPlugin Unable to start smartcard manager: %s", error->message); + g_error_free (error); + } + + g_signal_connect (smartcard_plugin->priv->manager, + "smartcard-removed", + G_CALLBACK (smartcard_removed_cb), smartcard_plugin); + + g_signal_connect (smartcard_plugin->priv->manager, + "smartcard-inserted", + G_CALLBACK (smartcard_inserted_cb), smartcard_plugin); + + if (!msd_smartcard_manager_login_card_is_inserted (smartcard_plugin->priv->manager)) { + g_debug ("MsdSmartcardPlugin processing smartcard removal immediately user logged in with smartcard " + "and it's not inserted"); + process_smartcard_removal (smartcard_plugin); + } + + smartcard_plugin->priv->is_active = TRUE; +} + +static void +impl_deactivate (MateSettingsPlugin *plugin) +{ + MsdSmartcardPlugin *smartcard_plugin = MSD_SMARTCARD_PLUGIN (plugin); + + if (!smartcard_plugin->priv->is_active) { + g_debug ("MsdSmartcardPlugin Not deactivating smartcard plugin, " + "because it's already inactive"); + return; + } + + g_debug ("MsdSmartcardPlugin Deactivating smartcard plugin"); + + msd_smartcard_manager_stop (smartcard_plugin->priv->manager); + + g_signal_handlers_disconnect_by_func (smartcard_plugin->priv->manager, + smartcard_removed_cb, smartcard_plugin); + + g_signal_handlers_disconnect_by_func (smartcard_plugin->priv->manager, + smartcard_inserted_cb, smartcard_plugin); + smartcard_plugin->priv->bus_connection = NULL; + smartcard_plugin->priv->is_active = FALSE; +} + +static void +msd_smartcard_plugin_class_init (MsdSmartcardPluginClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + MateSettingsPluginClass *plugin_class = MATE_SETTINGS_PLUGIN_CLASS (klass); + + object_class->finalize = msd_smartcard_plugin_finalize; + + plugin_class->activate = impl_activate; + plugin_class->deactivate = impl_deactivate; + + g_type_class_add_private (klass, sizeof (MsdSmartcardPluginPrivate)); +} diff --git a/plugins/smartcard/msd-smartcard-plugin.h b/plugins/smartcard/msd-smartcard-plugin.h new file mode 100644 index 0000000..206e58d --- /dev/null +++ b/plugins/smartcard/msd-smartcard-plugin.h @@ -0,0 +1,63 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2010 Red Hat, Inc. + * + * 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, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#ifndef __MSD_SMARTCARD_PLUGIN_H__ +#define __MSD_SMARTCARD_PLUGIN_H__ + +#include +#include +#include + +#include "mate-settings-plugin.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define MSD_TYPE_SMARTCARD_PLUGIN (msd_smartcard_plugin_get_type ()) +#define MSD_SMARTCARD_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPlugin)) +#define MSD_SMARTCARD_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPluginClass)) +#define MSD_IS_SMARTCARD_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MSD_TYPE_SMARTCARD_PLUGIN)) +#define MSD_IS_SMARTCARD_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), MSD_TYPE_SMARTCARD_PLUGIN)) +#define MSD_SMARTCARD_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MSD_TYPE_SMARTCARD_PLUGIN, MsdSmartcardPluginClass)) + +typedef struct MsdSmartcardPluginPrivate MsdSmartcardPluginPrivate; + +typedef struct +{ + MateSettingsPlugin parent; + MsdSmartcardPluginPrivate *priv; +} MsdSmartcardPlugin; + +typedef struct +{ + MateSettingsPluginClass parent_class; +} MsdSmartcardPluginClass; + +GType msd_smartcard_plugin_get_type (void) G_GNUC_CONST; + +/* All the plugins must implement this function */ +G_MODULE_EXPORT GType register_mate_settings_plugin (GTypeModule *module); + +#ifdef __cplusplus +} +#endif + +#endif /* __MSD_SMARTCARD_PLUGIN_H__ */ diff --git a/plugins/smartcard/msd-smartcard.c b/plugins/smartcard/msd-smartcard.c new file mode 100644 index 0000000..9154200 --- /dev/null +++ b/plugins/smartcard/msd-smartcard.c @@ -0,0 +1,555 @@ +/* msd-smartcard.c - smartcard object + * + * Copyright (C) 2006 Ray Strode + * + * 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, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#define MSD_SMARTCARD_ENABLE_INTERNAL_API +#include "msd-smartcard.h" + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +struct _MsdSmartcardPrivate { + SECMODModule *module; + MsdSmartcardState state; + + CK_SLOT_ID slot_id; + int slot_series; + + PK11SlotInfo *slot; + char *name; + + CERTCertificate *signing_certificate; + CERTCertificate *encryption_certificate; +}; + +static void msd_smartcard_finalize (GObject *object); +static void msd_smartcard_class_install_signals (MsdSmartcardClass *card_class); +static void msd_smartcard_class_install_properties (MsdSmartcardClass *card_class); +static void msd_smartcard_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void msd_smartcard_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); +static void msd_smartcard_set_name (MsdSmartcard *card, const char *name); +static void msd_smartcard_set_slot_id (MsdSmartcard *card, + int slot_id); +static void msd_smartcard_set_slot_series (MsdSmartcard *card, + int slot_series); +static void msd_smartcard_set_module (MsdSmartcard *card, + SECMODModule *module); + +static PK11SlotInfo *msd_smartcard_find_slot_from_id (MsdSmartcard *card, + int slot_id); + +static PK11SlotInfo *msd_smartcard_find_slot_from_card_name (MsdSmartcard *card, + const char *card_name); +#ifndef MSD_SMARTCARD_DEFAULT_SLOT_ID +#define MSD_SMARTCARD_DEFAULT_SLOT_ID ((gulong) -1) +#endif + +#ifndef MSD_SMARTCARD_DEFAULT_SLOT_SERIES +#define MSD_SMARTCARD_DEFAULT_SLOT_SERIES -1 +#endif + +enum { + PROP_0 = 0, + PROP_NAME, + PROP_SLOT_ID, + PROP_SLOT_SERIES, + PROP_MODULE, + NUMBER_OF_PROPERTIES +}; + +enum { + INSERTED, + REMOVED, + NUMBER_OF_SIGNALS +}; + +static guint msd_smartcard_signals[NUMBER_OF_SIGNALS]; + +G_DEFINE_TYPE (MsdSmartcard, msd_smartcard, G_TYPE_OBJECT); + +static void +msd_smartcard_class_init (MsdSmartcardClass *card_class) +{ + GObjectClass *gobject_class; + + gobject_class = G_OBJECT_CLASS (card_class); + + gobject_class->finalize = msd_smartcard_finalize; + + msd_smartcard_class_install_signals (card_class); + msd_smartcard_class_install_properties (card_class); + + g_type_class_add_private (card_class, + sizeof (MsdSmartcardPrivate)); +} + +static void +msd_smartcard_class_install_signals (MsdSmartcardClass *card_class) +{ + GObjectClass *object_class; + + object_class = G_OBJECT_CLASS (card_class); + + msd_smartcard_signals[INSERTED] = + g_signal_new ("inserted", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (MsdSmartcardClass, + inserted), + NULL, NULL, g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + msd_smartcard_signals[REMOVED] = + g_signal_new ("removed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (MsdSmartcardClass, + removed), + NULL, NULL, g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +static void +msd_smartcard_class_install_properties (MsdSmartcardClass *card_class) +{ + GObjectClass *object_class; + GParamSpec *param_spec; + + object_class = G_OBJECT_CLASS (card_class); + object_class->set_property = msd_smartcard_set_property; + object_class->get_property = msd_smartcard_get_property; + + param_spec = g_param_spec_ulong ("slot-id", _("Slot ID"), + _("The slot the card is in"), + 1, G_MAXULONG, + MSD_SMARTCARD_DEFAULT_SLOT_ID, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (object_class, PROP_SLOT_ID, param_spec); + + param_spec = g_param_spec_int ("slot-series", _("Slot Series"), + _("per-slot card identifier"), + -1, G_MAXINT, + MSD_SMARTCARD_DEFAULT_SLOT_SERIES, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (object_class, PROP_SLOT_SERIES, param_spec); + + param_spec = g_param_spec_string ("name", _("name"), + _("name"), NULL, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (object_class, PROP_NAME, param_spec); + + param_spec = g_param_spec_pointer ("module", _("Module"), + _("smartcard driver"), + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (object_class, PROP_MODULE, param_spec); +} + +static void +msd_smartcard_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + MsdSmartcard *card = MSD_SMARTCARD (object); + + switch (prop_id) { + case PROP_NAME: + msd_smartcard_set_name (card, g_value_get_string (value)); + break; + + case PROP_SLOT_ID: + msd_smartcard_set_slot_id (card, + g_value_get_ulong (value)); + break; + + case PROP_SLOT_SERIES: + msd_smartcard_set_slot_series (card, + g_value_get_int (value)); + break; + + case PROP_MODULE: + msd_smartcard_set_module (card, + (SECMODModule *) + g_value_get_pointer (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +CK_SLOT_ID +msd_smartcard_get_slot_id (MsdSmartcard *card) +{ + return card->priv->slot_id; +} + +MsdSmartcardState +msd_smartcard_get_state (MsdSmartcard *card) +{ + return card->priv->state; +} + +char * +msd_smartcard_get_name (MsdSmartcard *card) +{ + return g_strdup (card->priv->name); +} + +gboolean +msd_smartcard_is_login_card (MsdSmartcard *card) +{ + const char *login_card_name; + login_card_name = g_getenv ("PKCS11_LOGIN_TOKEN_NAME"); + + if ((login_card_name == NULL) || (card->priv->name == NULL)) { + return FALSE; + } + + if (strcmp (card->priv->name, login_card_name) == 0) { + return TRUE; + } + + return FALSE; +} + +static void +msd_smartcard_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + MsdSmartcard *card = MSD_SMARTCARD (object); + + switch (prop_id) { + case PROP_NAME: + g_value_take_string (value, + msd_smartcard_get_name (card)); + break; + + case PROP_SLOT_ID: + g_value_set_ulong (value, + (gulong) msd_smartcard_get_slot_id (card)); + break; + + case PROP_SLOT_SERIES: + g_value_set_int (value, + msd_smartcard_get_slot_series (card)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +msd_smartcard_set_name (MsdSmartcard *card, + const char *name) +{ + if (name == NULL) { + return; + } + + if ((card->priv->name == NULL) || + (strcmp (card->priv->name, name) != 0)) { + g_free (card->priv->name); + card->priv->name = g_strdup (name); + + if (card->priv->slot == NULL) { + card->priv->slot = msd_smartcard_find_slot_from_card_name (card, + card->priv->name); + + if (card->priv->slot != NULL) { + int slot_id, slot_series; + + slot_id = PK11_GetSlotID (card->priv->slot); + if (slot_id != card->priv->slot_id) { + msd_smartcard_set_slot_id (card, slot_id); + } + + slot_series = PK11_GetSlotSeries (card->priv->slot); + if (slot_series != card->priv->slot_series) { + msd_smartcard_set_slot_series (card, slot_series); + } + + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_INSERTED); + } else { + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_REMOVED); + } + } + + g_object_notify (G_OBJECT (card), "name"); + } +} + +static void +msd_smartcard_set_slot_id (MsdSmartcard *card, + int slot_id) +{ + if (card->priv->slot_id != slot_id) { + card->priv->slot_id = slot_id; + + if (card->priv->slot == NULL) { + card->priv->slot = msd_smartcard_find_slot_from_id (card, + card->priv->slot_id); + + if (card->priv->slot != NULL) { + const char *card_name; + + card_name = PK11_GetTokenName (card->priv->slot); + if ((card->priv->name == NULL) || + ((card_name != NULL) && + (strcmp (card_name, card->priv->name) != 0))) { + msd_smartcard_set_name (card, card_name); + } + + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_INSERTED); + } else { + _msd_smartcard_set_state (card, MSD_SMARTCARD_STATE_REMOVED); + } + } + + g_object_notify (G_OBJECT (card), "slot-id"); + } +} + +static void +msd_smartcard_set_slot_series (MsdSmartcard *card, + int slot_series) +{ + if (card->priv->slot_series != slot_series) { + card->priv->slot_series = slot_series; + g_object_notify (G_OBJECT (card), "slot-series"); + } +} + +static void +msd_smartcard_set_module (MsdSmartcard *card, + SECMODModule *module) +{ + gboolean should_notify; + + if (card->priv->module != module) { + should_notify = TRUE; + } else { + should_notify = FALSE; + } + + if (card->priv->module != NULL) { + SECMOD_DestroyModule (card->priv->module); + card->priv->module = NULL; + } + + if (module != NULL) { + card->priv->module = SECMOD_ReferenceModule (module); + } + + if (should_notify) { + g_object_notify (G_OBJECT (card), "module"); + } +} + +int +msd_smartcard_get_slot_series (MsdSmartcard *card) +{ + return card->priv->slot_series; +} + +static void +msd_smartcard_init (MsdSmartcard *card) +{ + + g_debug ("initializing smartcard "); + + card->priv = G_TYPE_INSTANCE_GET_PRIVATE (card, + MSD_TYPE_SMARTCARD, + MsdSmartcardPrivate); + + if (card->priv->slot != NULL) { + card->priv->name = g_strdup (PK11_GetTokenName (card->priv->slot)); + } +} + +static void msd_smartcard_finalize (GObject *object) +{ + MsdSmartcard *card; + GObjectClass *gobject_class; + + card = MSD_SMARTCARD (object); + + g_free (card->priv->name); + + msd_smartcard_set_module (card, NULL); + + gobject_class = G_OBJECT_CLASS (msd_smartcard_parent_class); + + gobject_class->finalize (object); +} + +GQuark msd_smartcard_error_quark (void) +{ + static GQuark error_quark = 0; + + if (error_quark == 0) { + error_quark = g_quark_from_static_string ("msd-smartcard-error-quark"); + } + + return error_quark; +} + +MsdSmartcard * +_msd_smartcard_new (SECMODModule *module, + CK_SLOT_ID slot_id, + int slot_series) +{ + MsdSmartcard *card; + + g_return_val_if_fail (module != NULL, NULL); + g_return_val_if_fail (slot_id >= 1, NULL); + g_return_val_if_fail (slot_series > 0, NULL); + g_return_val_if_fail (sizeof (gulong) == sizeof (slot_id), NULL); + + card = MSD_SMARTCARD (g_object_new (MSD_TYPE_SMARTCARD, + "module", module, + "slot-id", (gulong) slot_id, + "slot-series", slot_series, + NULL)); + return card; +} + +MsdSmartcard * +_msd_smartcard_new_from_name (SECMODModule *module, + const char *name) +{ + MsdSmartcard *card; + + g_return_val_if_fail (module != NULL, NULL); + g_return_val_if_fail (name != NULL, NULL); + + card = MSD_SMARTCARD (g_object_new (MSD_TYPE_SMARTCARD, + "module", module, + "name", name, + NULL)); + return card; +} + +void +_msd_smartcard_set_state (MsdSmartcard *card, + MsdSmartcardState state) +{ + if (card->priv->state != state) { + card->priv->state = state; + + if (state == MSD_SMARTCARD_STATE_INSERTED) { + g_signal_emit (card, msd_smartcard_signals[INSERTED], 0); + } else if (state == MSD_SMARTCARD_STATE_REMOVED) { + g_signal_emit (card, msd_smartcard_signals[REMOVED], 0); + } else { + g_assert_not_reached (); + } + } +} + +/* So we could conceivably make the closure data a pointer to the card + * or something similiar and then emit signals when we want passwords, + * but it's probably easier to just get the password up front and use + * it. So we just take the passed in g_malloc'd (well probably, who knows) + * and strdup it using NSPR's memory allocation routines. + */ +static char * +msd_smartcard_password_handler (PK11SlotInfo *slot, + PRBool is_retrying, + const char *password) +{ + if (is_retrying) { + return NULL; + } + + return password != NULL? PL_strdup (password): NULL; +} + +gboolean +msd_smartcard_unlock (MsdSmartcard *card, + const char *password) +{ + SECStatus status; + + PK11_SetPasswordFunc ((PK11PasswordFunc) msd_smartcard_password_handler); + + /* we pass PR_TRUE to load certificates + */ + status = PK11_Authenticate (card->priv->slot, PR_TRUE, (gpointer) password); + + if (status != SECSuccess) { + g_debug ("could not unlock card - %d", status); + return FALSE; + } + return TRUE; +} + +static PK11SlotInfo * +msd_smartcard_find_slot_from_card_name (MsdSmartcard *card, + const char *card_name) +{ + int i; + + for (i = 0; i < card->priv->module->slotCount; i++) { + const char *slot_card_name; + + slot_card_name = PK11_GetTokenName (card->priv->module->slots[i]); + + if ((slot_card_name != NULL) && + (strcmp (slot_card_name, card_name) == 0)) { + return card->priv->module->slots[i]; + } + } + + return NULL; +} + +static PK11SlotInfo * +msd_smartcard_find_slot_from_id (MsdSmartcard *card, + int slot_id) +{ + int i; + + for (i = 0; i < card->priv->module->slotCount; i++) { + if (PK11_GetSlotID (card->priv->module->slots[i]) == slot_id) { + return card->priv->module->slots[i]; + } + } + + return NULL; +} diff --git a/plugins/smartcard/msd-smartcard.h b/plugins/smartcard/msd-smartcard.h new file mode 100644 index 0000000..d00b8af --- /dev/null +++ b/plugins/smartcard/msd-smartcard.h @@ -0,0 +1,98 @@ +/* securitycard.h - api for reading and writing data to a security card + * + * Copyright (C) 2006 Ray Strode + * + * 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, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#ifndef MSD_SMARTCARD_H +#define MSD_SMARTCARD_H + +#include +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif +#define MSD_TYPE_SMARTCARD (msd_smartcard_get_type ()) +#define MSD_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MSD_TYPE_SMARTCARD, MsdSmartcard)) +#define MSD_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MSD_TYPE_SMARTCARD, MsdSmartcardClass)) +#define MSD_IS_SMARTCARD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MSD_TYPE_SMARTCARD)) +#define MSD_IS_SMARTCARD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MSD_TYPE_SMARTCARD)) +#define MSD_SMARTCARD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), MSD_TYPE_SMARTCARD, MsdSmartcardClass)) +#define MSD_SMARTCARD_ERROR (msd_smartcard_error_quark ()) +typedef struct _MsdSmartcardClass MsdSmartcardClass; +typedef struct _MsdSmartcard MsdSmartcard; +typedef struct _MsdSmartcardPrivate MsdSmartcardPrivate; +typedef enum _MsdSmartcardError MsdSmartcardError; +typedef enum _MsdSmartcardState MsdSmartcardState; + +typedef struct _MsdSmartcardRequest MsdSmartcardRequest; + +struct _MsdSmartcard { + GObject parent; + + /*< private > */ + MsdSmartcardPrivate *priv; +}; + +struct _MsdSmartcardClass { + GObjectClass parent_class; + + void (* inserted) (MsdSmartcard *card); + void (* removed) (MsdSmartcard *card); +}; + +enum _MsdSmartcardError { + MSD_SMARTCARD_ERROR_GENERIC = 0, +}; + +enum _MsdSmartcardState { + MSD_SMARTCARD_STATE_INSERTED = 0, + MSD_SMARTCARD_STATE_REMOVED, +}; + +GType msd_smartcard_get_type (void) G_GNUC_CONST; +GQuark msd_smartcard_error_quark (void) G_GNUC_CONST; + +CK_SLOT_ID msd_smartcard_get_slot_id (MsdSmartcard *card); +gint msd_smartcard_get_slot_series (MsdSmartcard *card); +MsdSmartcardState msd_smartcard_get_state (MsdSmartcard *card); + +char *msd_smartcard_get_name (MsdSmartcard *card); +gboolean msd_smartcard_is_login_card (MsdSmartcard *card); + +gboolean msd_smartcard_unlock (MsdSmartcard *card, + const char *password); + +/* don't under any circumstances call these functions */ +#ifdef MSD_SMARTCARD_ENABLE_INTERNAL_API + +MsdSmartcard *_msd_smartcard_new (SECMODModule *module, + CK_SLOT_ID slot_id, + gint slot_series); +MsdSmartcard *_msd_smartcard_new_from_name (SECMODModule *module, + const char *name); + +void _msd_smartcard_set_state (MsdSmartcard *card, + MsdSmartcardState state); +#endif + +#ifdef __cplusplus +} +#endif +#endif /* MSD_SMARTCARD_H */ -- cgit v1.2.1