diff options
Diffstat (limited to 'src/winterop/winterop.cpp')
-rw-r--r-- | src/winterop/winterop.cpp | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/src/winterop/winterop.cpp b/src/winterop/winterop.cpp new file mode 100644 index 00000000..12d8ca3f --- /dev/null +++ b/src/winterop/winterop.cpp | |||
@@ -0,0 +1,216 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
2 | |||
3 | #include "precomp.h" | ||
4 | |||
5 | |||
6 | HRESULT HashPublicKeyInfo( | ||
7 | __in PCERT_CONTEXT pCertContext, | ||
8 | __in_ecount(*pcbSubjectKeyIndentifier) BYTE* rgbSubjectKeyIdentifier, | ||
9 | __inout DWORD* pcbSubjectKeyIndentifier | ||
10 | ) | ||
11 | { | ||
12 | HRESULT hr = S_OK; | ||
13 | |||
14 | if (!::CryptHashPublicKeyInfo(NULL, CALG_SHA1, 0, X509_ASN_ENCODING, &pCertContext->pCertInfo->SubjectPublicKeyInfo, rgbSubjectKeyIdentifier, pcbSubjectKeyIndentifier)) | ||
15 | { | ||
16 | ExitWithLastError(hr, "Failed to hash public key information."); | ||
17 | } | ||
18 | |||
19 | LExit: | ||
20 | return hr; | ||
21 | } | ||
22 | |||
23 | HRESULT ResetAcls( | ||
24 | __in LPCWSTR pwzFiles[], | ||
25 | __in DWORD cFiles | ||
26 | ) | ||
27 | { | ||
28 | HRESULT hr = S_OK; | ||
29 | ACL* pacl = NULL; | ||
30 | DWORD cbAcl = sizeof(ACL); | ||
31 | |||
32 | OSVERSIONINFO osvi; | ||
33 | |||
34 | osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | ||
35 | if (!::GetVersionExA(&osvi)) | ||
36 | { | ||
37 | ExitOnLastError(hr, "failed to get OS version"); | ||
38 | } | ||
39 | |||
40 | // If we're running on NT 4 or earlier, or ME or earlier, don't reset ACLs. | ||
41 | if (4 >= osvi.dwMajorVersion) | ||
42 | { | ||
43 | ExitFunction1(hr = S_FALSE); | ||
44 | } | ||
45 | |||
46 | // create an empty (not NULL!) ACL to use on all the files | ||
47 | pacl = static_cast<ACL*>(MemAlloc(cbAcl, FALSE)); | ||
48 | ExitOnNull(pacl, hr, E_OUTOFMEMORY, "failed to allocate ACL"); | ||
49 | |||
50 | #pragma prefast(push) | ||
51 | #pragma prefast(disable:25029) | ||
52 | if (!::InitializeAcl(pacl, cbAcl, ACL_REVISION)) | ||
53 | #pragma prefast(op) | ||
54 | { | ||
55 | ExitOnLastError(hr, "failed to initialize ACL"); | ||
56 | } | ||
57 | |||
58 | // reset the existing security permissions on each file | ||
59 | for (DWORD i = 0; i < cFiles; ++i) | ||
60 | { | ||
61 | hr = ::SetNamedSecurityInfoW(const_cast<LPWSTR>(pwzFiles[i]), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION, NULL, NULL, pacl, NULL); | ||
62 | if (ERROR_FILE_NOT_FOUND != hr && ERROR_PATH_NOT_FOUND != hr) | ||
63 | { | ||
64 | ExitOnFailure(hr = HRESULT_FROM_WIN32(hr), "failed to set security descriptor for file: %S", pwzFiles[i]); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | // Setting to S_OK because we could end with ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND as valid return values. | ||
69 | hr = S_OK; | ||
70 | |||
71 | AssertSz(::IsValidAcl(pacl), "ResetAcls() - created invalid ACL"); | ||
72 | |||
73 | LExit: | ||
74 | if (pacl) | ||
75 | { | ||
76 | MemFree(pacl); | ||
77 | } | ||
78 | |||
79 | return hr; | ||
80 | } | ||
81 | |||
82 | |||
83 | HRESULT CreateCabBegin( | ||
84 | __in LPCWSTR wzCab, | ||
85 | __in LPCWSTR wzCabDir, | ||
86 | __in DWORD dwMaxFiles, | ||
87 | __in DWORD dwMaxSize, | ||
88 | __in DWORD dwMaxThresh, | ||
89 | __in COMPRESSION_TYPE ct, | ||
90 | __out HANDLE *phContext | ||
91 | ) | ||
92 | { | ||
93 | return CabCBegin(wzCab, wzCabDir, dwMaxFiles, dwMaxSize, dwMaxThresh, ct, phContext); | ||
94 | } | ||
95 | |||
96 | |||
97 | HRESULT CreateCabAddFile( | ||
98 | __in LPCWSTR wzFile, | ||
99 | __in_opt LPCWSTR wzToken, | ||
100 | __in_opt PMSIFILEHASHINFO pmfHash, | ||
101 | __in HANDLE hContext | ||
102 | ) | ||
103 | { | ||
104 | return CabCAddFile(wzFile, wzToken, pmfHash, hContext); | ||
105 | } | ||
106 | |||
107 | |||
108 | HRESULT CreateCabAddFiles( | ||
109 | __in LPCWSTR pwzFiles[], | ||
110 | __in LPCWSTR pwzTokens[], | ||
111 | __in PMSIFILEHASHINFO pmfHash[], | ||
112 | __in DWORD cFiles, | ||
113 | __in HANDLE hContext | ||
114 | ) | ||
115 | { | ||
116 | HRESULT hr = S_OK; | ||
117 | DWORD i; | ||
118 | |||
119 | Assert(pwzFiles); | ||
120 | Assert(hContext); | ||
121 | |||
122 | for (i = 0; i < cFiles; i++) | ||
123 | { | ||
124 | hr = CreateCabAddFile( | ||
125 | pwzFiles[i], | ||
126 | pwzTokens ? pwzTokens[i] : NULL, | ||
127 | pmfHash[i], | ||
128 | hContext | ||
129 | ); | ||
130 | ExitOnFailure(hr, "Failed to add file %S to cab", pwzFiles[i]); | ||
131 | } | ||
132 | |||
133 | LExit: | ||
134 | return hr; | ||
135 | } | ||
136 | |||
137 | |||
138 | HRESULT CreateCabFinish( | ||
139 | __in HANDLE hContext, | ||
140 | __in_opt FileSplitCabNamesCallback newCabNamesCallBackAddress | ||
141 | ) | ||
142 | { | ||
143 | // Convert address into Binder callback function | ||
144 | return CabCFinish(hContext, newCabNamesCallBackAddress); | ||
145 | } | ||
146 | |||
147 | |||
148 | void CreateCabCancel( | ||
149 | __in HANDLE hContext | ||
150 | ) | ||
151 | { | ||
152 | CabCCancel(hContext); | ||
153 | } | ||
154 | |||
155 | |||
156 | HRESULT ExtractCabBegin() | ||
157 | { | ||
158 | return CabInitialize(FALSE); | ||
159 | } | ||
160 | |||
161 | |||
162 | HRESULT ExtractCab( | ||
163 | __in LPCWSTR wzCabinet, | ||
164 | __in LPCWSTR wzExtractDir | ||
165 | ) | ||
166 | { | ||
167 | return CabExtract(wzCabinet, L"*", wzExtractDir, NULL, NULL, 0); | ||
168 | } | ||
169 | |||
170 | |||
171 | void ExtractCabFinish() | ||
172 | { | ||
173 | CabUninitialize(); | ||
174 | return; | ||
175 | } | ||
176 | |||
177 | |||
178 | HRESULT EnumerateCabBegin() | ||
179 | { | ||
180 | return CabInitialize(FALSE); | ||
181 | } | ||
182 | |||
183 | |||
184 | HRESULT EnumerateCab( | ||
185 | __in LPCWSTR wzCabinet, | ||
186 | __in STDCALL_PFNFDINOTIFY pfnNotify | ||
187 | ) | ||
188 | { | ||
189 | return CabEnumerate(wzCabinet, L"*", pfnNotify, 0); | ||
190 | } | ||
191 | |||
192 | |||
193 | void EnumerateCabFinish() | ||
194 | { | ||
195 | CabUninitialize(); | ||
196 | return; | ||
197 | } | ||
198 | |||
199 | |||
200 | BOOL WINAPI DllMain( | ||
201 | __in HINSTANCE /*hInstance*/, | ||
202 | __in DWORD dwReason, | ||
203 | __in LPVOID /*lpvReserved*/ | ||
204 | ) | ||
205 | { | ||
206 | switch(dwReason) | ||
207 | { | ||
208 | case DLL_PROCESS_ATTACH: | ||
209 | case DLL_PROCESS_DETACH: | ||
210 | case DLL_THREAD_ATTACH: | ||
211 | case DLL_THREAD_DETACH: | ||
212 | break; | ||
213 | } | ||
214 | |||
215 | return TRUE; | ||
216 | } | ||