From 1fe1c38cb73d39edbda4d63b3580f3b571442966 Mon Sep 17 00:00:00 2001 From: Matthew John Cheetham Date: Thu, 3 Dec 2020 11:53:35 +0000 Subject: [PATCH] libsecret: use downlevel functions to get secrets Replace our use of `secret_value_unref_to_password` which was only introduced in libsecret 0.19.0, with `secret_value_get` and a separate `secret_value_unref` call which is available in all versions. This will allow us to work on older distributions that may only include older versions of libsecret, specifically Ubuntu 18.04 LTS which includes libsecret 0.18. Version 0.20 of libsecret was only introduced from Ubuntu 19.04. --- .../Interop/Linux/SecretServiceCollection.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/shared/Microsoft.Git.CredentialManager/Interop/Linux/SecretServiceCollection.cs b/src/shared/Microsoft.Git.CredentialManager/Interop/Linux/SecretServiceCollection.cs index 09ce5e19a..41e235e72 100644 --- a/src/shared/Microsoft.Git.CredentialManager/Interop/Linux/SecretServiceCollection.cs +++ b/src/shared/Microsoft.Git.CredentialManager/Interop/Linux/SecretServiceCollection.cs @@ -245,6 +245,7 @@ private static unsafe ICredential CreateCredentialFromItem(SecretItem* item) GHashTable* secretAttrs = null; IntPtr serviceKeyPtr = IntPtr.Zero; IntPtr accountKeyPtr = IntPtr.Zero; + SecretValue* value = null; IntPtr passwordPtr = IntPtr.Zero; GError* error = null; @@ -264,14 +265,14 @@ private static unsafe ICredential CreateCredentialFromItem(SecretItem* item) // Load the secret value secret_item_load_secret_sync(item, IntPtr.Zero, out error); - SecretValue* value = secret_item_get_secret(item); + value = secret_item_get_secret(item); if (value == null) { throw new InteropException("Failed to load secret", -1); } // Extract the secret/password - passwordPtr = secret_value_unref_to_password(value, out int passwordLength); + passwordPtr = secret_value_get(value, out int passwordLength); string password = Marshal.PtrToStringAuto(passwordPtr, passwordLength); return new SecretServiceCredential(service, account, password); @@ -281,6 +282,7 @@ private static unsafe ICredential CreateCredentialFromItem(SecretItem* item) if (secretAttrs != null) g_hash_table_unref(secretAttrs); if (accountKeyPtr != IntPtr.Zero) Marshal.FreeHGlobal(accountKeyPtr); if (serviceKeyPtr != IntPtr.Zero) Marshal.FreeHGlobal(serviceKeyPtr); + if (value != null) secret_value_unref(value); if (passwordPtr != IntPtr.Zero) secret_password_free(passwordPtr); if (error != null) g_error_free(error); }