25#include <glib/gi18n.h>
30#include <libsecret/secret.h>
31#elif HAVE_GNOME_KEYRING
32#define GNOME_KEYRING_DEPRECATED
33#define GNOME_KEYRING_DEPRECATED_FOR(x)
34#include <gnome-keyring.h>
36#ifdef HAVE_OSX_KEYCHAIN
37#include <Security/Security.h>
38#include <CoreFoundation/CoreFoundation.h>
42G_GNUC_UNUSED
static QofLogModule log_module = GNC_MOD_GUI;
45const SecretSchema* gnucash_get_secret_schema(
void) G_GNUC_CONST;
46const SecretSchema* gnucash_get_secret_schema(
void)
48 static const SecretSchema secret_schema = {
49 "org.gnucash.password", SECRET_SCHEMA_NONE,
51 {
"protocol", SECRET_SCHEMA_ATTRIBUTE_STRING },
52 {
"server", SECRET_SCHEMA_ATTRIBUTE_STRING },
53 {
"port", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
54 {
"user", SECRET_SCHEMA_ATTRIBUTE_STRING },
59 return &secret_schema;
62#define SECRET_SCHEMA_GNUCASH gnucash_get_secret_schema()
65#ifdef HAVE_OSX_KEYCHAIN
68log_osx_keychain_error (
const gchar *context, OSStatus status)
70 CFStringRef osx_resultstring = SecCopyErrorMessageString (status, NULL);
71 char resultstring[256] =
"";
74 CFStringGetCString (osx_resultstring, resultstring,
75 sizeof resultstring, kCFStringEncodingUTF8);
76 CFRelease (osx_resultstring);
78 PWARN (
"OS X keychain error in %s (status %d): %s",
79 context, (
int) status, resultstring);
84cfdict_set_string_UTF8 (CFMutableDictionaryRef dict, CFStringRef key,
85 const gchar *value_UTF8)
87 CFStringRef str = CFStringCreateWithCString (NULL, value_UTF8,
88 kCFStringEncodingUTF8);
89 CFDictionarySetValue (dict, key, str);
98static CFMutableDictionaryRef
99osx_keychain_query (
const gchar *access_method,
const gchar *server,
100 guint32 port,
const gchar *service,
const gchar *user)
102 CFMutableDictionaryRef query =
103 CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
104 &kCFTypeDictionaryValueCallBacks);
105 CFDictionarySetValue (query, kSecClass, kSecClassInternetPassword);
106 cfdict_set_string_UTF8 (query, kSecAttrServer, server);
107 cfdict_set_string_UTF8 (query, kSecAttrSecurityDomain, access_method);
108 cfdict_set_string_UTF8 (query, kSecAttrAccount, user);
109 cfdict_set_string_UTF8 (query, kSecAttrPath, service);
112 CFNumberRef num = CFNumberCreate (NULL, kCFNumberSInt32Type, &port);
113 CFDictionarySetValue (query, kSecAttrPort, num);
123find_osx_keychain_password (
const gchar *access_method,
const gchar *server,
124 guint32 port,
const gchar *service,
127 gchar *password = NULL;
128 CFDataRef data = NULL;
129 CFMutableDictionaryRef query =
130 osx_keychain_query (access_method, server, port, service, user);
133 CFDictionarySetValue (query, kSecMatchLimit, kSecMatchLimitOne);
134 CFDictionarySetValue (query, kSecReturnData, kCFBooleanTrue);
135 status = SecItemCopyMatching (query, (CFTypeRef *) &data);
136 if (status == errSecSuccess && data)
138 password = g_strndup ((
const gchar *) CFDataGetBytePtr (data),
139 CFDataGetLength (data));
141 else if (status != errSecItemNotFound)
142 log_osx_keychain_error (
"find_osx_keychain_password", status);
152store_osx_keychain_password (
const gchar *access_method,
const gchar *server,
153 guint32 port,
const gchar *service,
154 const gchar *user,
const gchar *password)
156 CFMutableDictionaryRef query =
157 osx_keychain_query (access_method, server, port, service, user);
158 CFDataRef pw = CFDataCreate (NULL, (
const UInt8 *) password,
160 OSStatus status = SecItemCopyMatching (query, NULL);
164 if (status == errSecItemNotFound)
167 CFDictionarySetValue (query, kSecValueData, pw);
168 status = SecItemAdd (query, NULL);
170 else if (status == errSecSuccess)
173 CFMutableDictionaryRef attrs =
174 CFDictionaryCreateMutable (NULL, 1, &kCFTypeDictionaryKeyCallBacks,
175 &kCFTypeDictionaryValueCallBacks);
176 CFDictionarySetValue (attrs, kSecValueData, pw);
177 status = SecItemUpdate (query, attrs);
186#if defined(HAVE_LIBSECRET) || defined(HAVE_GNOME_KEYRING) || defined(HAVE_OSX_KEYCHAIN)
191keyring_lookup_password (
const gchar *access_method,
const gchar *server,
192 guint32 port,
const gchar *service,
const gchar *user)
194 gchar *stored = NULL;
196 GError *error = NULL;
199 found = secret_password_lookup_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
200 "protocol", access_method,
205 found = secret_password_lookup_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
206 "protocol", access_method,
213 stored = g_strdup (found);
214 secret_password_free (found);
217 g_error_free (error);
218#elif HAVE_GNOME_KEYRING
219 GList *found_list = NULL;
220 if (gnome_keyring_find_network_password_sync
221 (user, NULL, server, service, access_method, NULL, port,
222 &found_list) == GNOME_KEYRING_RESULT_OK && found_list)
224 GnomeKeyringNetworkPasswordData *found = found_list->data;
226 stored = g_strdup (found->password);
229 gnome_keyring_network_password_list_free (found_list);
231#ifdef HAVE_OSX_KEYCHAIN
232 stored = find_osx_keychain_password (access_method, server, port, service,
242 const gchar *service,
244 const gchar* password)
246#if defined(HAVE_LIBSECRET) || defined(HAVE_GNOME_KEYRING) || defined(HAVE_OSX_KEYCHAIN)
250 g_return_if_fail (access_method != NULL && server != NULL &&
251 service != NULL && user != NULL && password != NULL);
255 stored = keyring_lookup_password (access_method, server, port, service, user);
256 unchanged = (g_strcmp0 (stored, password) == 0);
263 GError* error = NULL;
266 label = g_strdup_printf(
"GnuCash password for %s://%s@%s", access_method, user, server);
269 secret_password_store_sync (SECRET_SCHEMA_GNUCASH, SECRET_COLLECTION_DEFAULT,
270 label, password, NULL, &error,
271 "protocol", access_method,
276 secret_password_store_sync (SECRET_SCHEMA_GNUCASH, SECRET_COLLECTION_DEFAULT,
277 label, password, NULL, &error,
278 "protocol", access_method,
288 PWARN (
"libsecret error: %s", error->message);
289 PWARN (
"The user will be prompted for a password again next time.");
292#elif HAVE_GNOME_KEYRING
293 GnomeKeyringResult gkr_result;
296 gkr_result = gnome_keyring_set_network_password_sync
297 (NULL, user, NULL, server, service,
298 access_method, NULL, port, password, &item_id);
300 if (gkr_result != GNOME_KEYRING_RESULT_OK)
302 PWARN (
"Gnome-keyring error: %s",
303 gnome_keyring_result_to_message(gkr_result));
304 PWARN (
"The user will be prompted for a password again next time.");
307#ifdef HAVE_OSX_KEYCHAIN
310 status = store_osx_keychain_password (access_method, server, port,
311 service, user, password);
312 if (status != errSecSuccess)
314 log_osx_keychain_error (
"gnc_keyring_set_password", status);
315 PWARN (
"The user will be prompted for a password again next time.");
322 const gchar *access_method,
325 const gchar *service,
329 gboolean password_found = FALSE;
330 gchar *db_path, *heading;
332 GError* error = NULL;
333 char* libsecret_password;
334#elif HAVE_GNOME_KEYRING
335 GnomeKeyringResult gkr_result;
336 GList *found_list = NULL;
337 GnomeKeyringNetworkPasswordData *found;
340 g_return_val_if_fail (user != NULL, FALSE);
341 g_return_val_if_fail (password != NULL, FALSE);
352 secret_password_store_sync (SECRET_SCHEMA_GNUCASH, SECRET_COLLECTION_DEFAULT,
353 "Dummy password",
"dummy", NULL, &error,
354 "protocol", PROJECT_NAME,
355 "server", PROJECT_NAME,
356 "user", PROJECT_NAME,
358 secret_password_clear_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
359 "protocol", PROJECT_NAME,
360 "server", PROJECT_NAME,
361 "user", PROJECT_NAME,
366 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
367 "protocol", access_method,
372 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
373 "protocol", access_method,
379 if (libsecret_password != NULL) {
380 *password = g_strdup (libsecret_password);
381 secret_password_free (libsecret_password);
387 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
388 "protocol", access_method,
394 if (libsecret_password != NULL) {
395 *password = g_strdup (libsecret_password);
396 secret_password_free (libsecret_password);
407 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_COMPAT_NETWORK, NULL, &error,
408 "protocol", access_method,
414 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_COMPAT_NETWORK, NULL, &error,
415 "protocol", access_method,
422 if (libsecret_password != NULL) {
423 *password = g_strdup (libsecret_password);
424 secret_password_free (libsecret_password);
435 PWARN (
"libsecret access failed: %s.", error->message);
439#elif HAVE_GNOME_KEYRING
440 gkr_result = gnome_keyring_find_network_password_sync
441 ( *user, NULL, server, service,
442 access_method, NULL, port, &found_list );
444 if (gkr_result == GNOME_KEYRING_RESULT_OK)
446 found = (GnomeKeyringNetworkPasswordData *) found_list->data;
448 *password = g_strdup(found->password);
449 gnome_keyring_network_password_list_free(found_list);
455 PWARN (
"Gnome-keyring access failed: %s.",
456 gnome_keyring_result_to_message(gkr_result));
457 gnome_keyring_network_password_list_free(found_list);
460#ifdef HAVE_OSX_KEYCHAIN
463 *password = find_osx_keychain_password (access_method, server, port,
465 if (*password != NULL)
477 db_path = g_strdup_printf (
"%s://%s/%s", access_method, server, service );
479 db_path = g_strdup_printf (
"%s://%s:%d/%s", access_method, server, port, service );
480 heading = g_strdup_printf (
482 _(
"Enter a user name and password to connect to: %s"),
485 password_found = gnc_get_username_password ( parent, heading,
491 if ( password_found )
496 gchar *newuser = g_strdup( *user );
497 gchar *newpassword = g_strdup( *password );
505 g_free ( newpassword );
508 return password_found;
Functions to save and retrieve passwords.
void gnc_keyring_set_password(const gchar *access_method, const gchar *server, guint32 port, const gchar *service, const gchar *user, const gchar *password)
Attempt to store a password in some trusted keystore.
gboolean gnc_keyring_get_password(GtkWidget *parent, const gchar *access_method, const gchar *server, guint32 port, const gchar *service, gchar **user, gchar **password)
Attempt to retrieve a password to connect to a remote service.
#define PWARN(format, args...)
Log a warning.