1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
// 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.
#include "precomp.h"
/********************************************************************
AllocateAcl - allocate an acl and populate it with this user and
permission information user could be user or domain\user
********************************************************************/
HRESULT AllocateAcl(SCA_SMBP* pssp, PACL* ppACL)
{
HRESULT hr = S_OK;
EXPLICIT_ACCESSW* pEA = NULL;
DWORD cEA = 0;
DWORD dwCounter = 0;
PSID psid = NULL;
LPCWSTR wzUser = NULL;
DWORD nPermissions = 0;
DWORD nErrorReturn = 0;
ACCESS_MODE accessMode = NOT_USED_ACCESS;
cEA = pssp->dwUserPermissionCount + 1;
if (cEA >= MAXSIZE_T / sizeof(EXPLICIT_ACCESSW))
{
ExitOnFailure(hr = E_OUTOFMEMORY, "Too many user permissions to allocate: %u", cEA);
}
pEA = static_cast<EXPLICIT_ACCESSW*>(MemAlloc(cEA * sizeof(EXPLICIT_ACCESSW), TRUE));
ExitOnNull(pEA, hr, E_OUTOFMEMORY, "failed to allocate memory for explicit access structure");
// figure out how big the psid is
for (dwCounter = 0; dwCounter < pssp->dwUserPermissionCount; ++dwCounter)
{
wzUser = pssp->pUserPerms[dwCounter].wzUser;
nPermissions = pssp->pUserPerms[dwCounter].nPermissions;
accessMode = pssp->pUserPerms[dwCounter].accessMode;
//
// create the appropriate SID
//
// figure out the right user to put into the access block
if (0 == lstrcmpW(wzUser, L"Everyone"))
{
hr = AclGetWellKnownSid(WinWorldSid, &psid);
}
else if (0 == lstrcmpW(wzUser, L"Administrators"))
{
hr = AclGetWellKnownSid(WinBuiltinAdministratorsSid, &psid);
}
else if (0 == lstrcmpW(wzUser, L"LocalSystem"))
{
hr = AclGetWellKnownSid(WinLocalSystemSid, &psid);
}
else if (0 == lstrcmpW(wzUser, L"LocalService"))
{
hr = AclGetWellKnownSid(WinLocalServiceSid, &psid);
}
else if (0 == lstrcmpW(wzUser, L"NetworkService"))
{
hr = AclGetWellKnownSid(WinNetworkServiceSid, &psid);
}
else if (0 == lstrcmpW(wzUser, L"AuthenticatedUser"))
{
hr = AclGetWellKnownSid(WinAuthenticatedUserSid, &psid);
}
else if (0 == lstrcmpW(wzUser, L"Guests"))
{
hr = AclGetWellKnownSid(WinBuiltinGuestsSid, &psid);
}
else if(0 == lstrcmpW(wzUser, L"CREATOR OWNER"))
{
hr = AclGetWellKnownSid(WinCreatorOwnerSid, &psid);
}
else
{
hr = AclGetAccountSid(NULL, wzUser, &psid);
}
ExitOnFailure(hr, "failed to get sid for account: %ls", wzUser);
// we now have a valid pSid, fill in the EXPLICIT_ACCESS
/* Permissions options: (see sca.sdh for defined sdl options)
#define GENERIC_READ (0x80000000L) 2147483648
#define GENERIC_WRITE (0x40000000L) 1073741824
#define GENERIC_EXECUTE (0x20000000L) 536870912
#define GENERIC_ALL (0x10000000L) 268435456
*/
pEA[dwCounter].grfAccessPermissions = nPermissions;
pEA[dwCounter].grfAccessMode = accessMode;
pEA[dwCounter].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
#pragma prefast(push)
#pragma prefast(disable:25029)
::BuildTrusteeWithSidW(&(pEA[dwCounter].Trustee), psid);
#pragma prefast(pop)
}
// create a new ACL that contains the ACE
*ppACL = NULL;
#pragma prefast(push)
#pragma prefast(disable:25029)
nErrorReturn = ::SetEntriesInAclW(dwCounter, pEA, NULL, ppACL);
#pragma prefast(pop)
ExitOnFailure(hr = HRESULT_FROM_WIN32(nErrorReturn), "failed to allocate ACL");
LExit:
if (psid)
{
AclFreeSid(psid);
}
ReleaseMem(pEA);
return hr;
}
/********************************************************************
FillShareInfo - fill the NetShareAdd data structure
********************************************************************/
void FillShareInfo(SHARE_INFO_502* psi, SCA_SMBP* pssp, PSECURITY_DESCRIPTOR pSD)
{
psi->shi502_netname = pssp->wzKey;
psi->shi502_type = STYPE_DISKTREE;
psi->shi502_remark = pssp->wzDescription;
psi->shi502_permissions = 0; // not used
psi->shi502_max_uses = 0xFFFFFFFF;
psi->shi502_current_uses = 0;
psi->shi502_path = pssp->wzDirectory;
psi->shi502_passwd = NULL; // not file share perms
psi->shi502_reserved = 0;
psi->shi502_security_descriptor = pSD;
}
/* NET_API_STATUS return codes
NERR_Success = 0
NERR_DuplicateShare = 2118
NERR_BufTooSmall = 2123
NERR_NetNameNotFound = 2310
NERR_RedirectedPath = 2117
NERR_UnknownDevDir = 2116
*/
/********************************************************************
DoesShareExists - Does a share of this name exist on this computer?
********************************************************************/
HRESULT DoesShareExist(__in LPWSTR wzShareName)
{
HRESULT hr = S_OK;
NET_API_STATUS s;
SHARE_INFO_502* psi = NULL;
s = ::NetShareGetInfo(NULL, wzShareName, 502, (BYTE**) &psi);
switch (s)
{
case NERR_Success:
hr = S_OK;
break;
case NERR_NetNameNotFound:
hr = E_FILENOTFOUND;
break;
default:
WcaLogError(s, "NetShareGetInfo returned an unexpected value.", NULL);
hr = HRESULT_FROM_WIN32(s);
break;
}
::NetApiBufferFree(psi);
return hr;
}
/********************************************************************
CreateShare - create the file share on this computer
********************************************************************/
HRESULT CreateShare(SCA_SMBP* pssp)
{
if (!pssp || !(pssp->wzKey))
return E_INVALIDARG;
HRESULT hr = S_OK;
PACL pACL = NULL;
SHARE_INFO_502 si;
NET_API_STATUS s;
DWORD dwParamErr = 0;
BOOL fShareExists = SUCCEEDED(DoesShareExist(pssp->wzKey));
PSECURITY_DESCRIPTOR pSD = static_cast<PSECURITY_DESCRIPTOR>(MemAlloc(SECURITY_DESCRIPTOR_MIN_LENGTH, TRUE));
ExitOnNull(pSD, hr, E_OUTOFMEMORY, "Failed to allocate memory for security descriptor");
#pragma prefast(push)
#pragma prefast(disable:25029)
if (!::InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
#pragma prefast(pop)
{
ExitOnLastError(hr, "failed to initialize security descriptor");
}
hr = AllocateAcl(pssp, &pACL);
ExitOnFailure(hr, "Failed to allocate ACL for fileshare");
if (NULL == pACL)
{
WcaLog(LOGMSG_VERBOSE, "Ignoring NULL DACL.");
}
#pragma prefast(push)
#pragma prefast(disable:25028) // We only call this when pACL isn't NULL, so this call is safe according to the docs
// add the ACL to the security descriptor.
else if (!::SetSecurityDescriptorDacl(pSD, TRUE, pACL, FALSE))
{
ExitOnLastError(hr, "Failed to set security descriptor");
}
#pragma prefast(pop)
// all that is left is to create the share
FillShareInfo(&si, pssp, pSD);
// Fail if the directory doesn't exist
if (!DirExists(pssp->wzDirectory, NULL))
ExitOnFailure(hr = HRESULT_FROM_WIN32(ERROR_OBJECT_NOT_FOUND), "Can't create a file share on directory that doesn't exist: %ls.", pssp->wzDirectory);
WcaLog(LOGMSG_VERBOSE, "Creating file share on directory \'%ls\' named \'%ls\'.", pssp->wzDirectory, pssp->wzKey);
if (!fShareExists)
{
s = ::NetShareAdd(NULL, 502, (BYTE*) &si, &dwParamErr);
WcaLog(LOGMSG_VERBOSE, "Adding a new file share.");
}
else
{
// The share exists. Write our new permissions over the top.
s = ::NetShareSetInfo(NULL, pssp->wzKey, 502, (BYTE*) &si, &dwParamErr);
WcaLog(LOGMSG_VERBOSE, "Setting permissions on existing share.");
}
if (NERR_Success != s)
{
hr = E_FAIL;
if (!fShareExists && NERR_DuplicateShare == s)
WcaLog(LOGMSG_VERBOSE, "Duplicate error when existence check failed.");
// error codes listed above.
ExitOnFailure(hr, "Failed to create/modify file share: Err: %d", s);
}
LExit:
if (pACL)
{
::LocalFree(pACL);
}
ReleaseMem(pSD);
return hr;
}
/********************************************************************
ScaEnsureSmbExists
********************************************************************/
HRESULT ScaEnsureSmbExists(SCA_SMBP* pssp)
{
HRESULT hr = S_OK;
// create the share
hr = CreateShare(pssp);
return hr;
}
//
// Delete File Shares - real work
//
/********************************************************************
ScaDropSmb - delete this file share from this computer
********************************************************************/
HRESULT ScaDropSmb(SCA_SMBP* pssp)
{
HRESULT hr = S_OK;
NET_API_STATUS s;
hr = DoesShareExist(pssp->wzKey);
if (E_FILENOTFOUND == hr)
{
WcaLog(LOGMSG_VERBOSE, "Share doesn't exist, share removal skipped. (%ls)", pssp->wzKey);
ExitFunction1(hr = S_OK);
}
ExitOnFailure(hr, "Unable to detect share. (%ls)", pssp->wzKey);
s = ::NetShareDel(NULL, pssp->wzKey, 0);
if (NERR_Success != s)
{
hr = E_FAIL;
ExitOnFailure(hr, "Failed to remove file share: Err: %d", s);
}
LExit:
return hr;
}
|