| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Runtime.InteropServices; |
| | 6 | |
|
| | 7 | | namespace Azure.Identity |
| | 8 | | { |
| | 9 | | internal static class LinuxNativeMethods |
| | 10 | | { |
| | 11 | | public const string SECRET_COLLECTION_SESSION = "session"; |
| | 12 | |
|
| | 13 | | public enum SecretSchemaAttributeType |
| | 14 | | { |
| | 15 | | SECRET_SCHEMA_ATTRIBUTE_STRING = 0, |
| | 16 | | SECRET_SCHEMA_ATTRIBUTE_INTEGER = 1, |
| | 17 | | SECRET_SCHEMA_ATTRIBUTE_BOOLEAN = 2, |
| | 18 | | } |
| | 19 | |
|
| | 20 | | public enum SecretSchemaFlags |
| | 21 | | { |
| | 22 | | SECRET_SCHEMA_NONE = 0, |
| | 23 | | SECRET_SCHEMA_DONT_MATCH_NAME = 2, |
| | 24 | | } |
| | 25 | |
|
| | 26 | | internal struct GError |
| | 27 | | { |
| | 28 | | public uint Domain; |
| | 29 | | public int Code; |
| | 30 | | public string Message; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public static IntPtr secret_schema_new(string name, SecretSchemaFlags flags, string attribute1, SecretSchemaAttr |
| | 34 | | { |
| 0 | 35 | | return Imports.secret_schema_new(name, (int)flags, attribute1, (int)attribute1Type, attribute2, (int)attribu |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public static string secret_password_lookup_sync(IntPtr schemaPtr, IntPtr cancellable, string attribute1Type, st |
| | 39 | | { |
| 0 | 40 | | IntPtr passwordPtr = Imports.secret_password_lookup_sync(schemaPtr, cancellable, out IntPtr errorPtr, attrib |
| 0 | 41 | | HandleError(errorPtr, "An error was encountered while reading secret from keyring"); |
| 0 | 42 | | return passwordPtr != IntPtr.Zero ? Marshal.PtrToStringAnsi(passwordPtr) : null; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public static void secret_password_store_sync(IntPtr schemaPtr, string collection, string label, string password |
| | 46 | | { |
| 0 | 47 | | _ = Imports.secret_password_store_sync(schemaPtr, collection, label, password, cancellable, out IntPtr error |
| 0 | 48 | | HandleError(errorPtr, "An error was encountered while writing secret to keyring"); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public static void secret_password_clear_sync(IntPtr schemaPtr, IntPtr cancellable, string attribute1Type, strin |
| | 52 | | { |
| 0 | 53 | | _ = Imports.secret_password_clear_sync(schemaPtr, cancellable, out IntPtr errorPtr, attribute1Type, attribut |
| 0 | 54 | | HandleError(errorPtr, "An error was encountered while clearing secret from keyring "); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public static void secret_password_free(IntPtr passwordPtr) |
| | 58 | | { |
| 0 | 59 | | if (passwordPtr != IntPtr.Zero) |
| | 60 | | { |
| 0 | 61 | | Imports.secret_password_free(passwordPtr); |
| | 62 | | } |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public static void secret_schema_unref(IntPtr schemaPtr) |
| | 66 | | { |
| 0 | 67 | | if (schemaPtr != IntPtr.Zero) |
| | 68 | | { |
| 0 | 69 | | Imports.secret_schema_unref(schemaPtr); |
| | 70 | | } |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | private static void HandleError(IntPtr errorPtr, string errorMessage) |
| | 74 | | { |
| 0 | 75 | | if (errorPtr == IntPtr.Zero) |
| | 76 | | { |
| 0 | 77 | | return; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | GError error; |
| | 81 | | try |
| | 82 | | { |
| 0 | 83 | | error = Marshal.PtrToStructure<GError>(errorPtr); |
| 0 | 84 | | } |
| 0 | 85 | | catch (Exception ex) |
| | 86 | | { |
| 0 | 87 | | throw new InvalidOperationException($"An exception was encountered while processing libsecret error: {ex |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | throw new InvalidOperationException($"{errorMessage}, domain:'{error.Domain}', code:'{error.Code}', message: |
| | 91 | | } |
| | 92 | |
|
| | 93 | | private static class Imports |
| | 94 | | { |
| | 95 | | [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFi |
| | 96 | | [DefaultDllImportSearchPaths(DllImportSearchPath.System32 | DllImportSearchPath.AssemblyDirectory)] |
| | 97 | | public static extern IntPtr secret_schema_new(string name, int flags, string attribute1, int attribute1Type, |
| | 98 | |
|
| | 99 | | [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall)] |
| | 100 | | public static extern void secret_schema_unref (IntPtr schema); |
| | 101 | |
|
| | 102 | | [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFi |
| | 103 | | public static extern IntPtr secret_password_lookup_sync(IntPtr schema, IntPtr cancellable, out IntPtr error, |
| | 104 | |
|
| | 105 | | [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFi |
| | 106 | | public static extern int secret_password_store_sync(IntPtr schema, string collection, string label, string p |
| | 107 | |
|
| | 108 | | [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFi |
| | 109 | | public static extern int secret_password_clear_sync(IntPtr schema, IntPtr cancellable, out IntPtr error, str |
| | 110 | |
|
| | 111 | | [DllImport("libsecret-1.so.0", CallingConvention = CallingConvention.StdCall)] |
| | 112 | | public static extern void secret_password_free(IntPtr password); |
| | 113 | | } |
| | 114 | | } |
| | 115 | | } |