GnuCash 7574033+
Loading...
Searching...
No Matches
GUIUtility

Files

file  gnc-keyring.h
 Functions to save and retrieve passwords.

Functions

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.

Detailed Description

Function Documentation

◆ gnc_keyring_get_password()

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.

This is deliberately generic: the remote service can be a database, website, anything.

If a trusted keystore infrastructure is found (such as the Gnome's keyring or Mac OS X' keychain) this infrastructure will be queried first.

If no such infrastructure is available or the query didn't return a valid result, the user will be prompted for his password.

Warning
When the user is prompted for a password, he can also change the username. So whenever you call this function, read both the username and password values before you continue !
Parameters
parentUsed to transition from in case the user is prompted for a password.
access_methodService type the user attempts to access. Can things like 'mysql', 'postgres' and so on.
serverServer the user wishes to connect to.
portPort the service listens on. If set to 0, it will be ignored in the search for a password.
serviceThe service the user wishes to access on the server. This can be a database name or a path.
userThe user name to access the service. Remember, although you pass it to search for the password, it can have changed when the function returns.
passwordThe password to access the service.
Returns
a boolean indicating whether or not a valid password has been retrieved. The function will return FALSE when the user explicitly cancels the password dialog or if it wasn't called properly. Otherwise it will return TRUE.

access_method, server, port, service and user will be the parameters passed to the trusted keystore (if available) to find the unique password for this service.

Definition at line 321 of file gnc-keyring.c.

328{
329 gboolean password_found = FALSE;
330 gchar *db_path, *heading;
331#ifdef HAVE_LIBSECRET
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;
338#endif
339
340 g_return_val_if_fail (user != NULL, FALSE);
341 g_return_val_if_fail (password != NULL, FALSE);
342
343 *password = NULL;
344
345#ifdef HAVE_LIBSECRET
346 /* Workaround for https://bugs.gnucash.org/show_bug.cgi?id=746873
347 * and by extension for https://bugs.gnucash.org/show_bug.cgi?id=748625
348 * Store a dummy password and delete it again. This forces libsecret
349 * to open the keychain, where only a call to secret_password_lookup_sync
350 * sometimes fails to do so. More details can be found in the bug reports
351 * referenced above. */
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,
357 NULL);
358 secret_password_clear_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
359 "protocol", PROJECT_NAME,
360 "server", PROJECT_NAME,
361 "user", PROJECT_NAME,
362 NULL);
363
364 /* Note: only use the port attribute if it was set by the user. */
365 if (port == 0)
366 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
367 "protocol", access_method,
368 "server", server,
369 "user", *user,
370 NULL);
371 else
372 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
373 "protocol", access_method,
374 "server", server,
375 "port", port,
376 "user", *user,
377 NULL);
378
379 if (libsecret_password != NULL) {
380 *password = g_strdup (libsecret_password);
381 secret_password_free (libsecret_password);
382 return TRUE;
383 }
384
385 /* No password found yet. Perhaps it was written with a port equal to 0.
386 * Gnucash versions prior to 2.6.7 did this unfortunately... */
387 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_GNUCASH, NULL, &error,
388 "protocol", access_method,
389 "server", server,
390 "port", 0,
391 "user", *user,
392 NULL);
393
394 if (libsecret_password != NULL) {
395 *password = g_strdup (libsecret_password);
396 secret_password_free (libsecret_password);
397
398 /* Ok, got an password with 0 port.
399 Store a copy in a more recent gnucash style. */
400 gnc_keyring_set_password(access_method, server, port, service, *user, *password);
401 return TRUE;
402 }
403
404 /* No password was found while querying libsecret using the gnucash schema,
405 Look for a password stored via gnome-keyring instead */
406 if (port == 0)
407 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_COMPAT_NETWORK, NULL, &error,
408 "protocol", access_method,
409 "server", server,
410 "object", service,
411 "user", *user,
412 NULL);
413 else
414 libsecret_password = secret_password_lookup_sync (SECRET_SCHEMA_COMPAT_NETWORK, NULL, &error,
415 "protocol", access_method,
416 "server", server,
417 "port", port,
418 "object", service,
419 "user", *user,
420 NULL);
421
422 if (libsecret_password != NULL) {
423 *password = g_strdup (libsecret_password);
424 secret_password_free (libsecret_password);
425
426 /* Ok, got an old gnome-keyring password.
427 * Store a copy of it in a libsecret compatible format. */
428 gnc_keyring_set_password(access_method, server, port, service, *user, *password);
429 return TRUE;
430 }
431
432 /* Something went wrong while attempting to access libsecret
433 * Log the error message and carry on... */
434 if (error != NULL) {
435 PWARN ("libsecret access failed: %s.", error->message);
436 g_error_free(error);
437 }
438
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 );
443
444 if (gkr_result == GNOME_KEYRING_RESULT_OK)
445 {
446 found = (GnomeKeyringNetworkPasswordData *) found_list->data;
447 if (found->password)
448 *password = g_strdup(found->password);
449 gnome_keyring_network_password_list_free(found_list);
450 return TRUE;
451 }
452
453 /* Something went wrong while attempting to access libsecret
454 * Log the error message and carry on... */
455 PWARN ("Gnome-keyring access failed: %s.",
456 gnome_keyring_result_to_message(gkr_result));
457 gnome_keyring_network_password_list_free(found_list);
458#endif /* HAVE_LIBSECRET or HAVE_GNOME_KEYRING */
459
460#ifdef HAVE_OSX_KEYCHAIN
461 if (*user != NULL)
462 {
463 *password = find_osx_keychain_password (access_method, server, port,
464 service, *user);
465 if (*password != NULL)
466 return TRUE;
467 }
468#endif /* HAVE_OSX_KEYCHAIN */
469
470 /* If we got here, either no proper password store is
471 * available on this system, or we couldn't retrieve
472 * a password from it. In both cases, just ask the user
473 * to enter one
474 */
475
476 if ( port == 0 )
477 db_path = g_strdup_printf ( "%s://%s/%s", access_method, server, service );
478 else
479 db_path = g_strdup_printf ( "%s://%s:%d/%s", access_method, server, port, service );
480 heading = g_strdup_printf ( /* Translators: %s is a path to a database or any other url,
481 like mysql://user@server.somewhere/somedb, https://www.somequotes.com/thequotes */
482 _("Enter a user name and password to connect to: %s"),
483 db_path );
484
485 password_found = gnc_get_username_password ( parent, heading,
486 *user, NULL,
487 user, password );
488 g_free ( db_path );
489 g_free ( heading );
490
491 if ( password_found )
492 {
493 /* User entered new user/password information
494 * Let's try to add it to a password store.
495 */
496 gchar *newuser = g_strdup( *user );
497 gchar *newpassword = g_strdup( *password );
498 gnc_keyring_set_password ( access_method,
499 server,
500 port,
501 service,
502 newuser,
503 newpassword );
504 g_free ( newuser );
505 g_free ( newpassword );
506 }
507
508 return password_found;
509}
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.
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250

◆ gnc_keyring_set_password()

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.

At this point that can be Gnome's keyring or Mac OS X' keychain. If no such keystore is available, this function does nothing.

All the parameters passed (except for the password) will be used to create a unique key, so the password can later be retrieved again with the same parameters.

Parameters
access_methodService type the user attempts to access. Can things like 'mysql', 'postgres' and so on.
serverServer the user wishes to connect to.
portPort the service listens on. If set to 0, it will be ignored in the search for a password.
serviceThe service the user wishes to access on the server. This can be a database name or a path.
userThe username to access the service.
passwordThe password to access the service.

Definition at line 239 of file gnc-keyring.c.

245{
246#if defined(HAVE_LIBSECRET) || defined(HAVE_GNOME_KEYRING) || defined(HAVE_OSX_KEYCHAIN)
247 gchar *stored;
248 gboolean unchanged;
249
250 g_return_if_fail (access_method != NULL && server != NULL &&
251 service != NULL && user != NULL && password != NULL);
252
253 /* Only (re)write the keyring when the stored password is missing or has
254 * actually changed. */
255 stored = keyring_lookup_password (access_method, server, port, service, user);
256 unchanged = (g_strcmp0 (stored, password) == 0);
257 g_free (stored);
258 if (unchanged)
259 return;
260#endif
261
262#ifdef HAVE_LIBSECRET
263 GError* error = NULL;
264 gchar* label = NULL;
265
266 label = g_strdup_printf("GnuCash password for %s://%s@%s", access_method, user, server);
267
268 if (port == 0)
269 secret_password_store_sync (SECRET_SCHEMA_GNUCASH, SECRET_COLLECTION_DEFAULT,
270 label, password, NULL, &error,
271 "protocol", access_method,
272 "server", server,
273 "user", user,
274 NULL);
275 else
276 secret_password_store_sync (SECRET_SCHEMA_GNUCASH, SECRET_COLLECTION_DEFAULT,
277 label, password, NULL, &error,
278 "protocol", access_method,
279 "server", server,
280 "port", port,
281 "user", user,
282 NULL);
283
284 g_free(label);
285
286 if (error != NULL)
287 {
288 PWARN ("libsecret error: %s", error->message);
289 PWARN ("The user will be prompted for a password again next time.");
290 g_error_free(error);
291 }
292#elif HAVE_GNOME_KEYRING
293 GnomeKeyringResult gkr_result;
294 guint32 item_id = 0;
295
296 gkr_result = gnome_keyring_set_network_password_sync
297 (NULL, user, NULL, server, service,
298 access_method, NULL, port, password, &item_id);
299
300 if (gkr_result != GNOME_KEYRING_RESULT_OK)
301 {
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.");
305 }
306#endif /* HAVE_GNOME_KEYRING */
307#ifdef HAVE_OSX_KEYCHAIN
308 OSStatus status;
309
310 status = store_osx_keychain_password (access_method, server, port,
311 service, user, password);
312 if (status != errSecSuccess)
313 {
314 log_osx_keychain_error ("gnc_keyring_set_password", status);
315 PWARN ("The user will be prompted for a password again next time.");
316 }
317#endif /* HAVE_OSX_KEYCHAIN */
318}