diff --git a/src/Makefile b/src/Makefile index 1332f60a87550f886dbf8c3635c76d5da1e35636..1c4810cf49414b143c38775ecb9f9ea2a5d002f7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,14 +1,14 @@ CXX ?= g++ # Accepts CXXSTD >= C++14 -CXXFLAGS := $(shell pkg-config --cflags --libs gnome-keyring-1) $(shell pkg-config --cflags --libs libsecret-1) -I ./lib -I . -std=c++14 +CXXFLAGS := -I ./lib -I . -std=c++14 EXTRA_FLAGS ?= secret: mkdir -p ../bin/ - $(CXX) unlock_keyrings.cc -DUSE_LIBGNOMEKEYRING -o ../bin/unlock_keyrings $(CXXFLAGS) $(EXTRA_FLAGS) + $(CXX) unlock_keyrings.cc -DUSE_LIBGNOMEKEYRING -o ../bin/unlock_keyrings $(CXXFLAGS) $(EXTRA_FLAGS) $(shell pkg-config --cflags --libs gnome-keyring-1) libsecret_test: mkdir -p ../bin/ - $(CXX) unlock_keyrings.cc -DUSE_LIBSECRET -o ../bin/unlock_keyrings $(CXXFLAGS) $(EXTRA_FLAGS) + $(CXX) unlock_keyrings.cc -DUSE_LIBSECRET -o ../bin/unlock_keyrings $(CXXFLAGS) $(EXTRA_FLAGS) $(shell pkg-config --cflags --libs libsecret-1) diff --git a/src/keyring_op.hpp b/src/keyring_op.hpp index ad24e1c31a2f7a939768520aa97eb4e296583fe7..fcbe4bcb76336a19bb345633fc8756aee1f9cfaf 100644 --- a/src/keyring_op.hpp +++ b/src/keyring_op.hpp @@ -1,13 +1,11 @@ -#include <gnome-keyring-1/gnome-keyring.h> #include <string> #include <rlib/macro.hpp> using namespace std::literals; +#if defined(USE_LIBSECRET) #include <libsecret/secret.h> #include <gmodule.h> -#include <rlib/stdio.hpp> -#if defined(USE_LIBSECRET) inline std::string do_unlock(std::string keyring_label, std::string password) { GError *err = NULL; SecretService *service_proxy_ptr = secret_service_get_sync(SECRET_SERVICE_LOAD_COLLECTIONS, NULL, &err); @@ -28,31 +26,33 @@ inline std::string do_unlock(std::string keyring_label, std::string password) { auto label = secret_collection_get_label(iter); if(label == NULL) break; rlib_defer([&]{g_free(label);}); - rlib::println("DEBUG: LABEL=", label); if(std::string(label) == keyring_label) { collections_to_unlock = g_list_append(collections_to_unlock, iter); - rlib::println("APPEND!"); } curr = curr->next; } if(g_list_length(collections_to_unlock) == 0) { - return "No such keyring with label: "s + keyring_label; + return "No such keyring with label: "s + keyring_label + ". You need keyring LABEL instead of NAME. "; } auto unlocked_count = secret_service_unlock_sync(service_proxy_ptr, collections_to_unlock, NULL, &collections_unlocked, &err); if(unlocked_count == g_list_length(collections_to_unlock)) return "SUCCESS"; else { - if(err != NULL) - return rlib::string("{}/{} collections(keyrings) successfully unlocked, some of them failed. Error message: {}").format(unlocked_count, g_list_length(collections_to_unlock), err->message); - else - return rlib::string("{}/{} collections(keyrings) successfully unlocked, some of them failed. No error message from libsecret").format(unlocked_count, g_list_length(collections_to_unlock)); + std::string err_msg_pass = err == NULL ? "No error message from libsecret" : err->message; + std::string err_msg_stat = unlocked_count == 0 ? "Failed. " : + "Some requested keyrings failed to unlock. "+std::to_string(unlocked_count)+"/"+std::to_string(g_list_length(collections_to_unlock))+" collections(keyrings) successfully unlocked. "; + + return err_msg_stat + err_msg_pass; } } #define keyringResultToString(r) (r) constexpr auto UNLOCK_RESULT_SUCCESS = "SUCCESS"; + #elif defined(USE_LIBGNOMEKEYRING) +#include <gnome-keyring-1/gnome-keyring.h> + inline GnomeKeyringResult do_unlock(std::string keyring, std::string password) { return gnome_keyring_unlock_sync(keyring.c_str(), password.c_str()); } @@ -78,7 +78,10 @@ inline std::string keyringResultToString(GnomeKeyringResult res) { } } constexpr auto UNLOCK_RESULT_SUCCESS = GNOME_KEYRING_RESULT_OK; + #else + #error You must define either USE_LIBGNOMEKEYRING(deprecated) or USE_LIBSECRET(new), to select which backend implementation to use + #endif