Skip to content
Snippets Groups Projects
Commit f22d15cb authored by Recolic K's avatar Recolic K
Browse files

poc

parent 4d439b21
No related branches found
No related tags found
No related merge requests found
...@@ -73,7 +73,7 @@ run `tools/list_keyrings.sh` to check name of your keyrings. The `login` keyring ...@@ -73,7 +73,7 @@ run `tools/list_keyrings.sh` to check name of your keyrings. The `login` keyring
- Working on keyring `Login`: GNOME\_KEYRING\_RESULT\_BAD\_ARGUMENTS. - Working on keyring `Login`: GNOME\_KEYRING\_RESULT\_BAD\_ARGUMENTS.
Seahorse sometimes show an incorrect name for "Login" keyring. It's real name is `login` instead of `Login`. You may confirm this by running `tools/list_keyrings.sh`. Seahorse is showing "label" of your keyring but you need "name" of it. It's real name is `login` instead of `Login`. You may confirm this by running `tools/list_keyrings.sh`.
- It's simply not working. How do I debug this program? - It's simply not working. How do I debug this program?
......
...@@ -6,5 +6,9 @@ EXTRA_FLAGS ?= ...@@ -6,5 +6,9 @@ EXTRA_FLAGS ?=
secret: secret:
mkdir -p ../bin/ mkdir -p ../bin/
$(CXX) unlock_keyrings.cc -o ../bin/unlock_keyrings $(CXXFLAGS) $(EXTRA_FLAGS) $(CXX) unlock_keyrings.cc -DUSE_LIBGNOMEKEYRING -o ../bin/unlock_keyrings $(CXXFLAGS) $(EXTRA_FLAGS)
libsecret_test:
mkdir -p ../bin/
$(CXX) unlock_keyrings.cc -DUSE_LIBSECRET -o ../bin/unlock_keyrings $(CXXFLAGS) $(EXTRA_FLAGS)
#include <gnome-keyring-1/gnome-keyring.h> #include <gnome-keyring-1/gnome-keyring.h>
#include <string> #include <string>
#include <rlib/macro.hpp> #include <rlib/macro.hpp>
using namespace std::literals;
#include <libsecret/secret.h> #include <libsecret/secret.h>
#include <gmodule.h> #include <gmodule.h>
#include <rlib/stdio.hpp> #include <rlib/stdio.hpp>
// TODO: read https://gnome.pages.gitlab.gnome.org/libsecret/class.Service.html #if defined(USE_LIBSECRET)
// https://gnome.pages.gitlab.gnome.org/libsecret/method.Service.prompt.html inline std::string do_unlock(std::string keyring_label, std::string password) {
inline std::string do_unlock_2(std::string keyring, std::string password) {
GError *err = NULL; GError *err = NULL;
SecretService *service_proxy_ptr = secret_service_get_sync(SECRET_SERVICE_LOAD_COLLECTIONS, NULL, &err); SecretService *service_proxy_ptr = secret_service_get_sync(SECRET_SERVICE_LOAD_COLLECTIONS, NULL, &err);
if(err != NULL or service_proxy_ptr == NULL) { if(err != NULL or service_proxy_ptr == NULL) {
...@@ -18,6 +18,8 @@ inline std::string do_unlock_2(std::string keyring, std::string password) { ...@@ -18,6 +18,8 @@ inline std::string do_unlock_2(std::string keyring, std::string password) {
GList *collections = secret_service_get_collections(service_proxy_ptr); GList *collections = secret_service_get_collections(service_proxy_ptr);
if(collections == NULL) return "collection gg"; if(collections == NULL) return "collection gg";
GList *collections_to_unlock = NULL;
GList *collections_unlocked = NULL;
auto curr = collections; auto curr = collections;
while (curr != NULL) while (curr != NULL)
...@@ -27,16 +29,33 @@ inline std::string do_unlock_2(std::string keyring, std::string password) { ...@@ -27,16 +29,33 @@ inline std::string do_unlock_2(std::string keyring, std::string password) {
if(label == NULL) break; if(label == NULL) break;
rlib_defer([&]{g_free(label);}); rlib_defer([&]{g_free(label);});
rlib::println("DEBUG: LABEL=", 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; curr = curr->next;
} }
return ""; if(g_list_length(collections_to_unlock) == 0) {
} return "No such keyring with label: "s + keyring_label;
}
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));
}
}
#define keyringResultToString(r) (r)
constexpr auto UNLOCK_RESULT_SUCCESS = "SUCCESS";
#elif defined(USE_LIBGNOMEKEYRING)
inline GnomeKeyringResult do_unlock(std::string keyring, std::string password) { inline GnomeKeyringResult do_unlock(std::string keyring, std::string password) {
return gnome_keyring_unlock_sync(keyring.c_str(), password.c_str()); return gnome_keyring_unlock_sync(keyring.c_str(), password.c_str());
} }
inline std::string keyringResultToString(GnomeKeyringResult res) { inline std::string keyringResultToString(GnomeKeyringResult res) {
switch(res) { switch(res) {
#define RLIB_IMPL_GEN_RESULT(value) RLIB_IMPL_GEN_RESULT_1(value, RLIB_MACRO_TO_CSTR(value)) #define RLIB_IMPL_GEN_RESULT(value) RLIB_IMPL_GEN_RESULT_1(value, RLIB_MACRO_TO_CSTR(value))
...@@ -47,12 +66,19 @@ inline std::string keyringResultToString(GnomeKeyringResult res) { ...@@ -47,12 +66,19 @@ inline std::string keyringResultToString(GnomeKeyringResult res) {
RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_NO_KEYRING_DAEMON); RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_NO_KEYRING_DAEMON);
RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_ALREADY_UNLOCKED); RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_ALREADY_UNLOCKED);
RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_NO_SUCH_KEYRING); RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_NO_SUCH_KEYRING);
RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_IO_ERROR); RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_IO_ERROR);
RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_CANCELLED); RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_CANCELLED);
RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_KEYRING_ALREADY_EXISTS); RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_KEYRING_ALREADY_EXISTS);
RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_NO_MATCH); RLIB_IMPL_GEN_RESULT(GNOME_KEYRING_RESULT_NO_MATCH);
case GNOME_KEYRING_RESULT_BAD_ARGUMENTS:
return std::string("GNOME_KEYRING_RESULT_BAD_ARGUMENTS, this error usually caused by incorrect keyring name. You need keyring NAME instead of LABEL. ");
default: default:
return std::string("Unknown Result Code: ") + std::to_string(res); return std::string("Unknown Result Code: ") + std::to_string(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
...@@ -41,7 +41,7 @@ int main(int argc, char **argv) { ...@@ -41,7 +41,7 @@ int main(int argc, char **argv) {
auto res = do_unlock(keyring_and_pswd.at(0), keyring_and_pswd.at(1)); auto res = do_unlock(keyring_and_pswd.at(0), keyring_and_pswd.at(1));
auto msg = keyringResultToString(res); auto msg = keyringResultToString(res);
if(res == GNOME_KEYRING_RESULT_OK) if(res == UNLOCK_RESULT_SUCCESS)
rlog.info("line {}: Working on keyring `{}`: {}.", line_num, keyring_and_pswd.at(0), msg); rlog.info("line {}: Working on keyring `{}`: {}.", line_num, keyring_and_pswd.at(0), msg);
else { else {
rlog.error("line {}: Working on keyring `{}`: {}.", line_num, keyring_and_pswd.at(0), msg); rlog.error("line {}: Working on keyring `{}`: {}.", line_num, keyring_and_pswd.at(0), msg);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment