Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/windows/classes/sun/nio/fs/WindowsAclFileAttributeView.java
41139 views
1
/*
2
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.nio.fs;
27
28
import java.nio.file.ProviderMismatchException;
29
import java.nio.file.attribute.*;
30
import java.util.*;
31
import java.io.IOException;
32
33
import static sun.nio.fs.WindowsNativeDispatcher.*;
34
import static sun.nio.fs.WindowsConstants.*;
35
36
/**
37
* Windows implementation of AclFileAttributeView.
38
*/
39
40
class WindowsAclFileAttributeView
41
extends AbstractAclFileAttributeView
42
{
43
/**
44
* typedef struct _SECURITY_DESCRIPTOR {
45
* BYTE Revision;
46
* BYTE Sbz1;
47
* SECURITY_DESCRIPTOR_CONTROL Control;
48
* PSID Owner;
49
* PSID Group;
50
* PACL Sacl;
51
* PACL Dacl;
52
* } SECURITY_DESCRIPTOR;
53
*/
54
private static final short SIZEOF_SECURITY_DESCRIPTOR = 20;
55
56
private final WindowsPath file;
57
private final boolean followLinks;
58
59
WindowsAclFileAttributeView(WindowsPath file, boolean followLinks) {
60
this.file = file;
61
this.followLinks = followLinks;
62
}
63
64
// permission check
65
private void checkAccess(WindowsPath file,
66
boolean checkRead,
67
boolean checkWrite)
68
{
69
@SuppressWarnings("removal")
70
SecurityManager sm = System.getSecurityManager();
71
if (sm != null) {
72
if (checkRead)
73
sm.checkRead(file.getPathForPermissionCheck());
74
if (checkWrite)
75
sm.checkWrite(file.getPathForPermissionCheck());
76
sm.checkPermission(new RuntimePermission("accessUserInformation"));
77
}
78
}
79
80
// invokes GetFileSecurity to get requested security information
81
static NativeBuffer getFileSecurity(String path, int request)
82
throws IOException
83
{
84
// invoke get to buffer size
85
int size = 0;
86
try {
87
size = GetFileSecurity(path, request, 0L, 0);
88
} catch (WindowsException x) {
89
x.rethrowAsIOException(path);
90
}
91
assert size > 0;
92
93
// allocate buffer and re-invoke to get security information
94
NativeBuffer buffer = NativeBuffers.getNativeBuffer(size);
95
try {
96
for (;;) {
97
int newSize = GetFileSecurity(path, request, buffer.address(), size);
98
if (newSize <= size)
99
return buffer;
100
101
// buffer was insufficient
102
buffer.release();
103
buffer = NativeBuffers.getNativeBuffer(newSize);
104
size = newSize;
105
}
106
} catch (WindowsException x) {
107
buffer.release();
108
x.rethrowAsIOException(path);
109
return null;
110
}
111
}
112
113
@Override
114
public UserPrincipal getOwner()
115
throws IOException
116
{
117
checkAccess(file, true, false);
118
119
// GetFileSecurity does not follow links so when following links we
120
// need the final target
121
String path = WindowsLinkSupport.getFinalPath(file, followLinks);
122
NativeBuffer buffer = getFileSecurity(path, OWNER_SECURITY_INFORMATION);
123
try {
124
// get the address of the SID
125
long sidAddress = GetSecurityDescriptorOwner(buffer.address());
126
if (sidAddress == 0L)
127
throw new IOException("no owner");
128
return WindowsUserPrincipals.fromSid(sidAddress);
129
} catch (WindowsException x) {
130
x.rethrowAsIOException(file);
131
return null;
132
} finally {
133
buffer.release();
134
}
135
}
136
137
@Override
138
public List<AclEntry> getAcl()
139
throws IOException
140
{
141
checkAccess(file, true, false);
142
143
// GetFileSecurity does not follow links so when following links we
144
// need the final target
145
String path = WindowsLinkSupport.getFinalPath(file, followLinks);
146
147
// ALLOW and DENY entries in DACL;
148
// AUDIT entries in SACL (ignore for now as it requires privileges)
149
NativeBuffer buffer = getFileSecurity(path, DACL_SECURITY_INFORMATION);
150
try {
151
return WindowsSecurityDescriptor.getAcl(buffer.address());
152
} finally {
153
buffer.release();
154
}
155
}
156
157
@Override
158
public void setOwner(UserPrincipal obj)
159
throws IOException
160
{
161
if (obj == null)
162
throw new NullPointerException("'owner' is null");
163
if (!(obj instanceof WindowsUserPrincipals.User))
164
throw new ProviderMismatchException();
165
WindowsUserPrincipals.User owner = (WindowsUserPrincipals.User)obj;
166
167
// permission check
168
checkAccess(file, false, true);
169
170
// SetFileSecurity does not follow links so when following links we
171
// need the final target
172
String path = WindowsLinkSupport.getFinalPath(file, followLinks);
173
174
// ConvertStringSidToSid allocates memory for SID so must invoke
175
// LocalFree to free it when we are done
176
long pOwner = 0L;
177
try {
178
pOwner = ConvertStringSidToSid(owner.sidString());
179
} catch (WindowsException x) {
180
throw new IOException("Failed to get SID for " + owner.getName()
181
+ ": " + x.errorString());
182
}
183
184
// Allocate buffer for security descriptor, initialize it, set
185
// owner information and update the file.
186
try {
187
NativeBuffer buffer = NativeBuffers.getNativeBuffer(SIZEOF_SECURITY_DESCRIPTOR);
188
try {
189
InitializeSecurityDescriptor(buffer.address());
190
SetSecurityDescriptorOwner(buffer.address(), pOwner);
191
// may need SeRestorePrivilege to set the owner
192
WindowsSecurity.Privilege priv =
193
WindowsSecurity.enablePrivilege("SeRestorePrivilege");
194
try {
195
SetFileSecurity(path,
196
OWNER_SECURITY_INFORMATION,
197
buffer.address());
198
} finally {
199
priv.drop();
200
}
201
} catch (WindowsException x) {
202
x.rethrowAsIOException(file);
203
} finally {
204
buffer.release();
205
}
206
} finally {
207
LocalFree(pOwner);
208
}
209
}
210
211
@Override
212
public void setAcl(List<AclEntry> acl) throws IOException {
213
checkAccess(file, false, true);
214
215
// SetFileSecurity does not follow links so when following links we
216
// need the final target
217
String path = WindowsLinkSupport.getFinalPath(file, followLinks);
218
WindowsSecurityDescriptor sd = WindowsSecurityDescriptor.create(acl);
219
try {
220
SetFileSecurity(path, DACL_SECURITY_INFORMATION, sd.address());
221
} catch (WindowsException x) {
222
x.rethrowAsIOException(file);
223
} finally {
224
sd.release();
225
}
226
}
227
}
228
229