Skip to content

Commit

Permalink
Add libsecret support
Browse files Browse the repository at this point in the history
This patch adds the option to use libsecret as a password storage backend in
darktable. Because the gnome-keyring API has been deprecated and has been
replaced with the implementation of a secret service that can be accessed via
libsecret it is recommended to use libsecret. Since kwallet already supports the
libsecret API this patch disables the kwallet and gnome-keyring backend if
libsecret is available automatically. If libsecret is not available on the
system it automatically falls back to the gnome-keyring API and kwallet, if
those are available and enabled.

Since attributes are not stored in an encrypted way and therefore are not part
of the secret, this patch converts the passed attributes into the JSON format
and stores the data as the secret. Attributes are only used to lookup the
corresponding secret item correctly. To read the encrypted attributes this patch
then converts the stored JSON data into a hashtable with said attributes.
  • Loading branch information
mlq committed Oct 14, 2014
1 parent b5ead68 commit 8403786
Show file tree
Hide file tree
Showing 9 changed files with 464 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ option(USE_LUA "Build lua scripting support" ON)
option(DONT_USE_INTERNAL_LUA "Never fall back to the intree copy of lua" ON)
option(USE_FLICKR "Enable Flickr support" ON)
option(USE_GLIBJSON "Enable GlibJson support" ON)
option(USE_KWALLET "Build kwallet password storage back-end" ON)
option(USE_LIBSECRET "Build libsecret password storage back-end" ON)
option(USE_GNOME_KEYRING "Build gnome-keyring password storage back-end" ON)
option(USE_UNITY "Use libunity to report progress in the launcher" OFF)
option(USE_SQUISH "Use thumbnail compression via libsquish" ON)
Expand Down
4 changes: 4 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ parse_feature()
flickr)
OPT_FLICKR=$value
;;
libsecret)
OPT_LIBSECRET=$value
;;
kwallet)
OPT_KWALLET=$value
;;
Expand Down Expand Up @@ -208,6 +211,7 @@ cmake_boolean_option()

CMAKE_MORE_OPTIONS=""
cmake_boolean_option USE_FLICKR $OPT_FLICKR
cmake_boolean_option USE_LIBSECRET $OPT_LIBSECRET
cmake_boolean_option USE_KWALLET $OPT_KWALLET
cmake_boolean_option USE_GNOME_KEYRING $OPT_GNOME_KEYRING
cmake_boolean_option USE_OPENMP $OPT_OPENMP
Expand Down
5 changes: 3 additions & 2 deletions data/darktableconfig.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,9 @@
<enum>
<option>auto</option>
<option>none</option>
<option>kwallet</option>
<option>gnome keyring</option>
<option capability="libsecret">libsecret</option>
<option capability="kwallet">kwallet</option>
<option capability="gnome-keyring">gnome keyring</option>
</enum>
</type>
<default>auto</default>
Expand Down
18 changes: 16 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,17 @@ endif(USE_OPENJPEG)
#
# Detect compile of optional pwstorage backends
#
if(USE_GNOME_KEYRING)
if(USE_LIBSECRET)
find_package(Libsecret)
if(LIBSECRET_FOUND)
set(SOURCES ${SOURCES} "common/pwstorage/backend_libsecret.c")
include_directories(SYSTEM ${LIBSECRET_INCLUDE_DIRS})
list(APPEND LIBS ${LIBSECRET_LIBRARIES})
add_definitions("-DHAVE_LIBSECRET")
endif(LIBSECRET_FOUND)
endif(USE_LIBSECRET)

if(USE_GNOME_KEYRING AND NOT USE_LIBSECRET)
find_package(GnomeKeyring)
if(GNOMEKEYRING_FOUND)
if(${GnomeKeyring_VERSION} VERSION_LESS "3.12.0")
Expand All @@ -245,7 +255,11 @@ if(USE_GNOME_KEYRING)
set(GNOMEKEYRING_FOUND FALSE)
endif(${GnomeKeyring_VERSION} VERSION_LESS "3.12.0")
endif(GNOMEKEYRING_FOUND)
endif(USE_GNOME_KEYRING)
endif(USE_GNOME_KEYRING AND NOT USE_LIBSECRET)

if(USE_KWALLET AND NOT USE_LIBSECRET)
add_definitions("-DHAVE_KWALLET")
endif(USE_KWALLET AND NOT USE_LIBSECRET)

if(USE_MAC_INTEGRATION)
find_package(MacIntegration)
Expand Down
6 changes: 3 additions & 3 deletions src/common/darktable.c
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,6 @@ int dt_init(int argc, char *argv[], const int init_gui,lua_State *L)
darktable.thumbnail_height /= 16;
darktable.thumbnail_height *= 16;

// Initialize the password storage engine
darktable.pwstorage=dt_pwstorage_new();

// FIXME: move there into dt_database_t
dt_pthread_mutex_init(&(darktable.db_insert), NULL);
dt_pthread_mutex_init(&(darktable.plugin_threadsafe), NULL);
Expand Down Expand Up @@ -780,6 +777,9 @@ int dt_init(int argc, char *argv[], const int init_gui,lua_State *L)
/* capabilities set to NULL */
darktable.capabilities = NULL;

// Initialize the password storage engine
darktable.pwstorage=dt_pwstorage_new();

#ifdef HAVE_GRAPHICSMAGICK
/* GraphicsMagick init */
InitializeMagick(darktable.progname);
Expand Down
281 changes: 281 additions & 0 deletions src/common/pwstorage/backend_libsecret.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
// This file is part of darktable
//
// Copyright (c) 2014 Moritz Lipp <[email protected]>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "backend_libsecret.h"
#include "control/conf.h"

#include <libsecret/secret.h>
#include <glib.h>
#include <json-glib/json-glib.h>

#define DARKTABLE_KEYRING PACKAGE_NAME

#define GFOREACH(item, list) for(GList *__glist = list; __glist && (item = __glist->data, true); __glist = __glist->next)

const SecretSchema * secret_darktable_get_schema (void) G_GNUC_CONST;
#define SECRET_SCHEMA_DARKTABLE secret_darktable_get_schema ()

static GHashTable* secret_to_attributes(SecretValue* value);
static SecretValue* attributes_to_secret(GHashTable* attributes);

const SecretSchema *
secret_darktable_get_schema (void)
{
static const SecretSchema darktable_schema = {
"org.darktable.Password", SECRET_SCHEMA_NONE,
{
{ "slot", SECRET_SCHEMA_ATTRIBUTE_STRING },
{ "magic", SECRET_SCHEMA_ATTRIBUTE_STRING },
{ "NULL", 0 },
}
};

return &darktable_schema;
}

const backend_libsecret_context_t*
dt_pwstorage_libsecret_new()
{
backend_libsecret_context_t* context = g_malloc(sizeof(backend_libsecret_context_t));

context->secret_service = secret_service_get_sync(SECRET_SERVICE_NONE, NULL, NULL);
if (context->secret_service == NULL) {
return NULL;
}

/* Ensure to load all collections */
if (secret_service_load_collections_sync(context->secret_service, NULL, NULL) == FALSE) {
dt_pwstorage_libsecret_destroy(context);
return NULL;
}

GList* collections = secret_service_get_collections(context->secret_service);
SecretCollection* item = NULL;

gboolean collection_exists = FALSE;
GFOREACH(item, collections) {
if (g_strcmp0(secret_collection_get_label(item), DARKTABLE_KEYRING)) {
context->secret_collection = item;
collection_exists = TRUE;
break;
}
}

if (collection_exists == FALSE) {
context->secret_collection =
secret_collection_create_sync(context->secret_service, DARKTABLE_KEYRING,
NULL, SECRET_COLLECTION_CREATE_NONE, NULL, NULL);

if (context->secret_collection == NULL) {
dt_pwstorage_libsecret_destroy(context);
return NULL;
}
}

return context;
}

void
dt_pwstorage_libsecret_destroy(const backend_libsecret_context_t *context)
{
if (context == NULL) {
return;
}

if (context->secret_service != NULL) {
g_object_unref(context->secret_service);
}

if (context->secret_collection != NULL) {
g_object_unref(context->secret_collection);
}

g_free((backend_libsecret_context_t*) context);
}

gboolean dt_pwstorage_libsecret_set(const backend_libsecret_context_t* context,
const gchar* slot, GHashTable* attributes)
{
if (context == NULL || slot == NULL || strlen(slot) == 0 || attributes == NULL) {
return FALSE;
}

/* Convert attributes to secret */
SecretValue* secret_value = attributes_to_secret(attributes);

if (secret_value == NULL) {
return FALSE;
}

/* Insert slot as a attribute */
GHashTable* secret_attributes = secret_attributes_build(SECRET_SCHEMA_DARKTABLE,
"slot", slot, "magic", PACKAGE_NAME, NULL);

/* Save the item */
gchar* label = g_strdup_printf("darktable@%s", slot);

GError* error = NULL;
SecretItem* item = secret_item_create_sync(
context->secret_collection,
SECRET_SCHEMA_DARKTABLE,
secret_attributes,
label,
secret_value,
SECRET_ITEM_CREATE_REPLACE,
NULL,
&error);

if (item == NULL) {
return FALSE;
} else {
return TRUE;
}
}

GHashTable* dt_pwstorage_libsecret_get(const backend_libsecret_context_t*
context, const gchar* slot)
{
if (context == NULL || slot == NULL || strlen(slot) == 0) {
goto error_out;
}

/* Setup search attributes */
GHashTable* secret_attributes = secret_attributes_build(SECRET_SCHEMA_DARKTABLE,
"slot", slot, "magic", PACKAGE_NAME, NULL);

/* Search for item */
GError* error = NULL;
GList* items = secret_collection_search_sync(
context->secret_collection,
SECRET_SCHEMA_DARKTABLE,
secret_attributes,
SECRET_SEARCH_NONE,
NULL,
&error);

/* Since the search flag is set to SECRET_SEARCH_NONE only one
* matching item is returned. */
if (items == NULL || g_list_length(items) != 1) {
goto error_out;
}

SecretItem* item = (SecretItem*) g_list_nth_data(items, 0);

if (item == NULL) {
goto error_out;
}

/* Load secret */
secret_item_load_secret_sync(item, NULL, NULL);

SecretValue* value = secret_item_get_secret(item);

if (value == NULL) {
goto error_out;
}

GHashTable* attributes = secret_to_attributes(value);

if (attributes == NULL) {
secret_value_unref(value);
goto error_out;
}

secret_value_unref(value);

return attributes;

error_out:

return g_hash_table_new(g_str_hash, g_str_equal);
}

static void append_pair_to_json(gpointer key, gpointer value, gpointer data)
{
JsonBuilder* json_builder = (JsonBuilder*) data;

json_builder_set_member_name(json_builder, (char*) key);
json_builder_add_string_value(json_builder, (char*) value);
}

static SecretValue* attributes_to_secret(GHashTable* attributes)
{
/* Build JSON */
JsonBuilder* json_builder = json_builder_new();
json_builder_begin_object(json_builder);
g_hash_table_foreach(attributes, append_pair_to_json, json_builder);
json_builder_end_object(json_builder);

/* Generate JSON */
JsonGenerator* json_generator = json_generator_new();
json_generator_set_root(json_generator, json_builder_get_root(json_builder));
gchar *json_data = json_generator_to_data(json_generator, 0);

/* Create secret */
SecretValue* secret = secret_value_new(json_data, -1, "text/plain");

g_object_unref(json_generator);
g_object_unref(json_builder);

return secret;
}

static GHashTable* secret_to_attributes(SecretValue* secret)
{
if (secret == NULL) {
return NULL;
}

/* Parse JSON from data */
JsonParser* json_parser = json_parser_new();

if (json_parser_load_from_data(json_parser, secret_value_get_text(secret), -1, NULL) == FALSE) {
g_object_unref(json_parser);
return NULL;
}

/* Read JSON */
JsonNode* json_root = json_parser_get_root(json_parser);
JsonReader* json_reader = json_reader_new(json_root);

GHashTable* attributes = g_hash_table_new(g_str_hash, g_str_equal);

/* Save each element as an attribute pair */
gint n_attributes = json_reader_count_members(json_reader);
for (gint i = 0; i < n_attributes; i++) {
if (json_reader_read_element(json_reader, i) == FALSE) {
continue;
}

const gchar* key = json_reader_get_member_name(json_reader);
const gchar* value = json_reader_get_string_value(json_reader);

g_hash_table_insert(attributes, (gpointer) g_strdup(key), (gpointer) g_strdup(value));

json_reader_end_element(json_reader);
}

g_object_unref(json_reader);
g_object_unref(json_parser);

return attributes;
}
Loading

0 comments on commit 8403786

Please sign in to comment.