|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using System.Security.Principal; |
| 6 | +using Windows.Win32; |
| 7 | +using Windows.Win32.Foundation; |
| 8 | +using Windows.Win32.System.Com; |
| 9 | +using Windows.Win32.Networking.ActiveDirectory; |
| 10 | +using Windows.Win32.System.Variant; |
| 11 | + |
| 12 | +namespace Files.App.Storage |
| 13 | +{ |
| 14 | + public static unsafe class WindowsObjectPicker |
| 15 | + { |
| 16 | + private const string ObjectSidAttributeName = "objectSid"; |
| 17 | + |
| 18 | + public static Task<string?> OpenObjectPickerAsync(nint ownerHWnd, ILogger? logger) |
| 19 | + => STATask.Run(() => OpenObjectPicker((HWND)ownerHWnd), logger); |
| 20 | + |
| 21 | + private static string? OpenObjectPicker(HWND ownerHWnd) |
| 22 | + { |
| 23 | + Guid CLSID_DsObjectPicker = PInvoke.CLSID_DsObjectPicker; |
| 24 | + |
| 25 | + using ComPtr<IDsObjectPicker> pObjectPicker = default; |
| 26 | + HRESULT hr = pObjectPicker.CoCreateInstance(&CLSID_DsObjectPicker, null, CLSCTX.CLSCTX_INPROC_SERVER); |
| 27 | + if (hr.ThrowIfFailedOnDebug().Failed) |
| 28 | + return null; |
| 29 | + |
| 30 | + DSOP_SCOPE_INIT_INFO* scopeInitInfos = stackalloc DSOP_SCOPE_INIT_INFO[2]; |
| 31 | + scopeInitInfos[0] = CreateScopeInitInfo(PInvoke.DSOP_SCOPE_TYPE_TARGET_COMPUTER, true); |
| 32 | + scopeInitInfos[1] = CreateScopeInitInfo( |
| 33 | + PInvoke.DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN | |
| 34 | + PInvoke.DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN | |
| 35 | + PInvoke.DSOP_SCOPE_TYPE_ENTERPRISE_DOMAIN | |
| 36 | + PInvoke.DSOP_SCOPE_TYPE_GLOBAL_CATALOG | |
| 37 | + PInvoke.DSOP_SCOPE_TYPE_EXTERNAL_UPLEVEL_DOMAIN | |
| 38 | + PInvoke.DSOP_SCOPE_TYPE_EXTERNAL_DOWNLEVEL_DOMAIN | |
| 39 | + PInvoke.DSOP_SCOPE_TYPE_WORKGROUP | |
| 40 | + PInvoke.DSOP_SCOPE_TYPE_USER_ENTERED_UPLEVEL_SCOPE | |
| 41 | + PInvoke.DSOP_SCOPE_TYPE_USER_ENTERED_DOWNLEVEL_SCOPE, |
| 42 | + false); |
| 43 | + |
| 44 | + fixed (char* pszObjectSidAttributeName = ObjectSidAttributeName) |
| 45 | + { |
| 46 | + PCWSTR* attributeNames = stackalloc PCWSTR[1]; |
| 47 | + attributeNames[0] = pszObjectSidAttributeName; |
| 48 | + |
| 49 | + DSOP_INIT_INFO initInfo = default; |
| 50 | + initInfo.cbSize = (uint)sizeof(DSOP_INIT_INFO); |
| 51 | + initInfo.cDsScopeInfos = 2; |
| 52 | + initInfo.aDsScopeInfos = scopeInitInfos; |
| 53 | + initInfo.cAttributesToFetch = 1; |
| 54 | + initInfo.apwzAttributeNames = attributeNames; |
| 55 | + |
| 56 | + hr = pObjectPicker.Get()->Initialize(&initInfo); |
| 57 | + if (hr.ThrowIfFailedOnDebug().Failed) |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + using ComPtr<IDataObject> pSelections = default; |
| 62 | + hr = pObjectPicker.Get()->InvokeDialog(ownerHWnd, pSelections.GetAddressOf()); |
| 63 | + if (hr == HRESULT.S_FALSE || hr.ThrowIfFailedOnDebug().Failed || pSelections.IsNull) |
| 64 | + return null; |
| 65 | + |
| 66 | + return GetSelectedSid(pSelections.Get()); |
| 67 | + } |
| 68 | + |
| 69 | + private static DSOP_SCOPE_INIT_INFO CreateScopeInitInfo(uint scopeType, bool startingScope) |
| 70 | + { |
| 71 | + const uint defaultFilter = |
| 72 | + PInvoke.DSOP_SCOPE_FLAG_DEFAULT_FILTER_USERS | |
| 73 | + PInvoke.DSOP_SCOPE_FLAG_DEFAULT_FILTER_GROUPS; |
| 74 | + |
| 75 | + const uint upLevelFilter = |
| 76 | + PInvoke.DSOP_FILTER_INCLUDE_ADVANCED_VIEW | |
| 77 | + PInvoke.DSOP_FILTER_USERS | |
| 78 | + PInvoke.DSOP_FILTER_BUILTIN_GROUPS | |
| 79 | + PInvoke.DSOP_FILTER_WELL_KNOWN_PRINCIPALS | |
| 80 | + PInvoke.DSOP_FILTER_UNIVERSAL_GROUPS_DL | |
| 81 | + PInvoke.DSOP_FILTER_UNIVERSAL_GROUPS_SE | |
| 82 | + PInvoke.DSOP_FILTER_GLOBAL_GROUPS_DL | |
| 83 | + PInvoke.DSOP_FILTER_GLOBAL_GROUPS_SE | |
| 84 | + PInvoke.DSOP_FILTER_DOMAIN_LOCAL_GROUPS_DL | |
| 85 | + PInvoke.DSOP_FILTER_DOMAIN_LOCAL_GROUPS_SE | |
| 86 | + PInvoke.DSOP_FILTER_CONTACTS | |
| 87 | + PInvoke.DSOP_FILTER_COMPUTERS | |
| 88 | + PInvoke.DSOP_FILTER_SERVICE_ACCOUNTS; |
| 89 | + |
| 90 | + const uint downLevelFilter = |
| 91 | + PInvoke.DSOP_DOWNLEVEL_FILTER_USERS | |
| 92 | + PInvoke.DSOP_DOWNLEVEL_FILTER_LOCAL_GROUPS | |
| 93 | + PInvoke.DSOP_DOWNLEVEL_FILTER_GLOBAL_GROUPS | |
| 94 | + PInvoke.DSOP_DOWNLEVEL_FILTER_COMPUTERS | |
| 95 | + PInvoke.DSOP_DOWNLEVEL_FILTER_ALL_WELLKNOWN_SIDS; |
| 96 | + |
| 97 | + DSOP_SCOPE_INIT_INFO info = default; |
| 98 | + info.cbSize = (uint)sizeof(DSOP_SCOPE_INIT_INFO); |
| 99 | + info.flType = scopeType; |
| 100 | + info.flScope = startingScope |
| 101 | + ? PInvoke.DSOP_SCOPE_FLAG_STARTING_SCOPE | defaultFilter |
| 102 | + : defaultFilter; |
| 103 | + info.FilterFlags.Uplevel.flBothModes = upLevelFilter; |
| 104 | + info.FilterFlags.flDownlevel = downLevelFilter; |
| 105 | + |
| 106 | + return info; |
| 107 | + } |
| 108 | + |
| 109 | + private static string? GetSelectedSid(IDataObject* pSelections) |
| 110 | + { |
| 111 | + uint clipboardFormat; |
| 112 | + fixed (char* pszSelectionListFormatName = PInvoke.CFSTR_DSOP_DS_SELECTION_LIST) |
| 113 | + clipboardFormat = PInvoke.RegisterClipboardFormat(pszSelectionListFormatName); |
| 114 | + |
| 115 | + if (clipboardFormat is 0 || clipboardFormat > ushort.MaxValue) |
| 116 | + return null; |
| 117 | + |
| 118 | + FORMATETC format = default; |
| 119 | + format.cfFormat = (ushort)clipboardFormat; |
| 120 | + format.dwAspect = (uint)DVASPECT.DVASPECT_CONTENT; |
| 121 | + format.lindex = -1; |
| 122 | + format.tymed = (uint)TYMED.TYMED_HGLOBAL; |
| 123 | + |
| 124 | + STGMEDIUM medium = default; |
| 125 | + medium.tymed = TYMED.TYMED_HGLOBAL; |
| 126 | + |
| 127 | + HRESULT hr = pSelections->GetData(&format, &medium); |
| 128 | + if (hr.ThrowIfFailedOnDebug().Failed) |
| 129 | + return null; |
| 130 | + |
| 131 | + void* pvSelectionList = PInvoke.GlobalLock(medium.u.hGlobal); |
| 132 | + if (pvSelectionList is null) |
| 133 | + { |
| 134 | + PInvoke.ReleaseStgMedium(&medium); |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + try |
| 139 | + { |
| 140 | + DS_SELECTION_LIST* pSelectionList = (DS_SELECTION_LIST*)pvSelectionList; |
| 141 | + if (pSelectionList->cItems is 0 || pSelectionList->cFetchedAttributes is 0) |
| 142 | + return null; |
| 143 | + |
| 144 | + var selectionsAsSpan = pSelectionList->aDsSelection.AsSpan((int)pSelectionList->cItems); |
| 145 | + fixed (DS_SELECTION* pSelection = selectionsAsSpan) |
| 146 | + { |
| 147 | + if (pSelection->pvarFetchedAttributes is null) |
| 148 | + return null; |
| 149 | + |
| 150 | + return GetSidString(pSelection->pvarFetchedAttributes); |
| 151 | + } |
| 152 | + } |
| 153 | + finally |
| 154 | + { |
| 155 | + PInvoke.GlobalUnlock(medium.u.hGlobal); |
| 156 | + PInvoke.ReleaseStgMedium(&medium); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private static string? GetSidString(VARIANT* objectSidVariant) |
| 161 | + { |
| 162 | + VARENUM variantType = objectSidVariant->Anonymous.Anonymous.vt; |
| 163 | + if ((variantType & VARENUM.VT_ARRAY) is 0 || (variantType & VARENUM.VT_TYPEMASK) is not VARENUM.VT_UI1) |
| 164 | + return null; |
| 165 | + |
| 166 | + SAFEARRAY* pSafeArray = (variantType & VARENUM.VT_BYREF) is not 0 |
| 167 | + ? *objectSidVariant->Anonymous.Anonymous.Anonymous.pparray |
| 168 | + : objectSidVariant->Anonymous.Anonymous.Anonymous.parray; |
| 169 | + |
| 170 | + if (pSafeArray is null || PInvoke.SafeArrayGetDim(pSafeArray) is not 1) |
| 171 | + return null; |
| 172 | + |
| 173 | + int lowerBound = 0, upperBound = 0; |
| 174 | + if (PInvoke.SafeArrayGetLBound(pSafeArray, 1, &lowerBound).ThrowIfFailedOnDebug().Failed || |
| 175 | + PInvoke.SafeArrayGetUBound(pSafeArray, 1, &upperBound).ThrowIfFailedOnDebug().Failed || |
| 176 | + upperBound < lowerBound) |
| 177 | + return null; |
| 178 | + |
| 179 | + void* pSidBytes; |
| 180 | + if (PInvoke.SafeArrayAccessData(pSafeArray, &pSidBytes).ThrowIfFailedOnDebug().Failed || pSidBytes is null) |
| 181 | + return null; |
| 182 | + |
| 183 | + try |
| 184 | + { |
| 185 | + int length = upperBound - lowerBound + 1; |
| 186 | + byte[] sidBytes = new ReadOnlySpan<byte>((byte*)pSidBytes, length).ToArray(); |
| 187 | + |
| 188 | + return new SecurityIdentifier(sidBytes, 0).Value; |
| 189 | + } |
| 190 | + finally |
| 191 | + { |
| 192 | + PInvoke.SafeArrayUnaccessData(pSafeArray); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments