Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsAclFileAttributeView.java
32288 views
/*1* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.nio.file.ProviderMismatchException;28import java.nio.file.attribute.*;29import java.util.*;30import java.io.IOException;3132import static sun.nio.fs.WindowsNativeDispatcher.*;33import static sun.nio.fs.WindowsConstants.*;3435/**36* Windows implementation of AclFileAttributeView.37*/3839class WindowsAclFileAttributeView40extends AbstractAclFileAttributeView41{42/**43* typedef struct _SECURITY_DESCRIPTOR {44* BYTE Revision;45* BYTE Sbz1;46* SECURITY_DESCRIPTOR_CONTROL Control;47* PSID Owner;48* PSID Group;49* PACL Sacl;50* PACL Dacl;51* } SECURITY_DESCRIPTOR;52*/53private static final short SIZEOF_SECURITY_DESCRIPTOR = 20;5455private final WindowsPath file;56private final boolean followLinks;5758WindowsAclFileAttributeView(WindowsPath file, boolean followLinks) {59this.file = file;60this.followLinks = followLinks;61}6263// permision check64private void checkAccess(WindowsPath file,65boolean checkRead,66boolean checkWrite)67{68SecurityManager sm = System.getSecurityManager();69if (sm != null) {70if (checkRead)71sm.checkRead(file.getPathForPermissionCheck());72if (checkWrite)73sm.checkWrite(file.getPathForPermissionCheck());74sm.checkPermission(new RuntimePermission("accessUserInformation"));75}76}7778// invokes GetFileSecurity to get requested security information79static NativeBuffer getFileSecurity(String path, int request)80throws IOException81{82// invoke get to buffer size83int size = 0;84try {85size = GetFileSecurity(path, request, 0L, 0);86} catch (WindowsException x) {87x.rethrowAsIOException(path);88}89assert size > 0;9091// allocate buffer and re-invoke to get security information92NativeBuffer buffer = NativeBuffers.getNativeBuffer(size);93try {94for (;;) {95int newSize = GetFileSecurity(path, request, buffer.address(), size);96if (newSize <= size)97return buffer;9899// buffer was insufficient100buffer.release();101buffer = NativeBuffers.getNativeBuffer(newSize);102size = newSize;103}104} catch (WindowsException x) {105buffer.release();106x.rethrowAsIOException(path);107return null;108}109}110111@Override112public UserPrincipal getOwner()113throws IOException114{115checkAccess(file, true, false);116117// GetFileSecurity does not follow links so when following links we118// need the final target119String path = WindowsLinkSupport.getFinalPath(file, followLinks);120NativeBuffer buffer = getFileSecurity(path, OWNER_SECURITY_INFORMATION);121try {122// get the address of the SID123long sidAddress = GetSecurityDescriptorOwner(buffer.address());124if (sidAddress == 0L)125throw new IOException("no owner");126return WindowsUserPrincipals.fromSid(sidAddress);127} catch (WindowsException x) {128x.rethrowAsIOException(file);129return null;130} finally {131buffer.release();132}133}134135@Override136public List<AclEntry> getAcl()137throws IOException138{139checkAccess(file, true, false);140141// GetFileSecurity does not follow links so when following links we142// need the final target143String path = WindowsLinkSupport.getFinalPath(file, followLinks);144145// ALLOW and DENY entries in DACL;146// AUDIT entries in SACL (ignore for now as it requires privileges)147NativeBuffer buffer = getFileSecurity(path, DACL_SECURITY_INFORMATION);148try {149return WindowsSecurityDescriptor.getAcl(buffer.address());150} finally {151buffer.release();152}153}154155@Override156public void setOwner(UserPrincipal obj)157throws IOException158{159if (obj == null)160throw new NullPointerException("'owner' is null");161if (!(obj instanceof WindowsUserPrincipals.User))162throw new ProviderMismatchException();163WindowsUserPrincipals.User owner = (WindowsUserPrincipals.User)obj;164165// permission check166checkAccess(file, false, true);167168// SetFileSecurity does not follow links so when following links we169// need the final target170String path = WindowsLinkSupport.getFinalPath(file, followLinks);171172// ConvertStringSidToSid allocates memory for SID so must invoke173// LocalFree to free it when we are done174long pOwner = 0L;175try {176pOwner = ConvertStringSidToSid(owner.sidString());177} catch (WindowsException x) {178throw new IOException("Failed to get SID for " + owner.getName()179+ ": " + x.errorString());180}181182// Allocate buffer for security descriptor, initialize it, set183// owner information and update the file.184try {185NativeBuffer buffer = NativeBuffers.getNativeBuffer(SIZEOF_SECURITY_DESCRIPTOR);186try {187InitializeSecurityDescriptor(buffer.address());188SetSecurityDescriptorOwner(buffer.address(), pOwner);189// may need SeRestorePrivilege to set the owner190WindowsSecurity.Privilege priv =191WindowsSecurity.enablePrivilege("SeRestorePrivilege");192try {193SetFileSecurity(path,194OWNER_SECURITY_INFORMATION,195buffer.address());196} finally {197priv.drop();198}199} catch (WindowsException x) {200x.rethrowAsIOException(file);201} finally {202buffer.release();203}204} finally {205LocalFree(pOwner);206}207}208209@Override210public void setAcl(List<AclEntry> acl) throws IOException {211checkAccess(file, false, true);212213// SetFileSecurity does not follow links so when following links we214// need the final target215String path = WindowsLinkSupport.getFinalPath(file, followLinks);216WindowsSecurityDescriptor sd = WindowsSecurityDescriptor.create(acl);217try {218SetFileSecurity(path, DACL_SECURITY_INFORMATION, sd.address());219} catch (WindowsException x) {220x.rethrowAsIOException(file);221} finally {222sd.release();223}224}225}226227228