893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
893N/Aimport java.nio.file.ProviderMismatchException;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.util.*;
893N/Aimport java.io.IOException;
893N/Aimport sun.misc.Unsafe;
893N/A
893N/Aimport static sun.nio.fs.WindowsNativeDispatcher.*;
893N/Aimport static sun.nio.fs.WindowsConstants.*;
893N/A
893N/A/**
893N/A * A SecurityDescriptor for use when setting a file's ACL or creating a file
893N/A * with an initial ACL.
893N/A */
893N/A
893N/Aclass WindowsSecurityDescriptor {
893N/A private static final Unsafe unsafe = Unsafe.getUnsafe();
893N/A
893N/A /**
893N/A * typedef struct _ACL {
893N/A * BYTE AclRevision;
893N/A * BYTE Sbz1;
893N/A * WORD AclSize;
893N/A * WORD AceCount;
893N/A * WORD Sbz2;
893N/A * } ACL;
893N/A *
893N/A * typedef struct _ACE_HEADER {
893N/A * BYTE AceType;
893N/A * BYTE AceFlags;
893N/A * WORD AceSize;
893N/A * } ACE_HEADER;
893N/A *
893N/A * typedef struct _ACCESS_ALLOWED_ACE {
893N/A * ACE_HEADER Header;
893N/A * ACCESS_MASK Mask;
893N/A * DWORD SidStart;
893N/A * } ACCESS_ALLOWED_ACE;
893N/A *
893N/A * typedef struct _ACCESS_DENIED_ACE {
893N/A * ACE_HEADER Header;
893N/A * ACCESS_MASK Mask;
893N/A * DWORD SidStart;
893N/A * } ACCESS_DENIED_ACE;
893N/A *
893N/A * typedef struct _SECURITY_DESCRIPTOR {
893N/A * BYTE Revision;
893N/A * BYTE Sbz1;
893N/A * SECURITY_DESCRIPTOR_CONTROL Control;
893N/A * PSID Owner;
893N/A * PSID Group;
893N/A * PACL Sacl;
893N/A * PACL Dacl;
893N/A * } SECURITY_DESCRIPTOR;
893N/A */
893N/A private static final short SIZEOF_ACL = 8;
893N/A private static final short SIZEOF_ACCESS_ALLOWED_ACE = 12;
893N/A private static final short SIZEOF_ACCESS_DENIED_ACE = 12;
893N/A private static final short SIZEOF_SECURITY_DESCRIPTOR = 20;
893N/A
893N/A private static final short OFFSETOF_TYPE = 0;
893N/A private static final short OFFSETOF_FLAGS = 1;
893N/A private static final short OFFSETOF_ACCESS_MASK = 4;
893N/A private static final short OFFSETOF_SID = 8;
893N/A
893N/A // null security descriptor
893N/A private static final WindowsSecurityDescriptor NULL_DESCRIPTOR =
893N/A new WindowsSecurityDescriptor();
893N/A
893N/A // native resources
893N/A private final List<Long> sidList;
893N/A private final NativeBuffer aclBuffer, sdBuffer;
893N/A
893N/A /**
893N/A * Creates the "null" SecurityDescriptor
893N/A */
893N/A private WindowsSecurityDescriptor() {
893N/A this.sidList = null;
893N/A this.aclBuffer = null;
893N/A this.sdBuffer = null;
893N/A }
893N/A
893N/A /**
893N/A * Creates a SecurityDescriptor from the given ACL
893N/A */
893N/A private WindowsSecurityDescriptor(List<AclEntry> acl) throws IOException {
893N/A boolean initialized = false;
893N/A
893N/A // SECURITY: need to copy list in case size changes during processing
893N/A acl = new ArrayList<AclEntry>(acl);
893N/A
893N/A // list of SIDs
893N/A sidList = new ArrayList<Long>(acl.size());
893N/A try {
893N/A // initial size of ACL
893N/A int size = SIZEOF_ACL;
893N/A
893N/A // get the SID for each entry
893N/A for (AclEntry entry: acl) {
893N/A UserPrincipal user = entry.principal();
893N/A if (!(user instanceof WindowsUserPrincipals.User))
893N/A throw new ProviderMismatchException();
893N/A String sidString = ((WindowsUserPrincipals.User)user).sidString();
893N/A try {
893N/A long pSid = ConvertStringSidToSid(sidString);
893N/A sidList.add(pSid);
893N/A
893N/A // increase size to allow for entry
893N/A size += GetLengthSid(pSid) +
893N/A Math.max(SIZEOF_ACCESS_ALLOWED_ACE, SIZEOF_ACCESS_DENIED_ACE);
893N/A
893N/A } catch (WindowsException x) {
893N/A throw new IOException("Failed to get SID for " + user.getName()
893N/A + ": " + x.errorString());
893N/A }
893N/A }
893N/A
893N/A // allocate memory for the ACL
893N/A aclBuffer = NativeBuffers.getNativeBuffer(size);
893N/A sdBuffer = NativeBuffers.getNativeBuffer(SIZEOF_SECURITY_DESCRIPTOR);
893N/A
893N/A InitializeAcl(aclBuffer.address(), size);
893N/A
893N/A // Add entry ACE to the ACL
893N/A int i = 0;
893N/A while (i < acl.size()) {
893N/A AclEntry entry = acl.get(i);
893N/A long pSid = sidList.get(i);
893N/A try {
893N/A encode(entry, pSid, aclBuffer.address());
893N/A } catch (WindowsException x) {
893N/A throw new IOException("Failed to encode ACE: " +
893N/A x.errorString());
893N/A }
893N/A i++;
893N/A }
893N/A
893N/A // initialize security descriptor and set DACL
893N/A InitializeSecurityDescriptor(sdBuffer.address());
893N/A SetSecurityDescriptorDacl(sdBuffer.address(), aclBuffer.address());
893N/A initialized = true;
893N/A } catch (WindowsException x) {
893N/A throw new IOException(x.getMessage());
893N/A } finally {
893N/A // release resources if not completely initialized
893N/A if (!initialized)
893N/A release();
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Releases memory associated with SecurityDescriptor
893N/A */
893N/A void release() {
893N/A if (sdBuffer != null)
893N/A sdBuffer.release();
893N/A if (aclBuffer != null)
893N/A aclBuffer.release();
893N/A if (sidList != null) {
893N/A // release memory for SIDs
893N/A for (Long sid: sidList) {
893N/A LocalFree(sid);
893N/A }
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Returns address of SecurityDescriptor
893N/A */
893N/A long address() {
893N/A return (sdBuffer == null) ? 0L : sdBuffer.address();
893N/A }
893N/A
893N/A // decode Windows ACE to NFSv4 AclEntry
893N/A private static AclEntry decode(long aceAddress)
893N/A throws IOException
893N/A {
893N/A // map type
893N/A byte aceType = unsafe.getByte(aceAddress + OFFSETOF_TYPE);
893N/A if (aceType != ACCESS_ALLOWED_ACE_TYPE && aceType != ACCESS_DENIED_ACE_TYPE)
893N/A return null;
893N/A AclEntryType type;
893N/A if (aceType == ACCESS_ALLOWED_ACE_TYPE) {
893N/A type = AclEntryType.ALLOW;
893N/A } else {
893N/A type = AclEntryType.DENY;
893N/A }
893N/A
893N/A // map flags
893N/A byte aceFlags = unsafe.getByte(aceAddress + OFFSETOF_FLAGS);
3471N/A Set<AclEntryFlag> flags = EnumSet.noneOf(AclEntryFlag.class);
893N/A if ((aceFlags & OBJECT_INHERIT_ACE) != 0)
893N/A flags.add(AclEntryFlag.FILE_INHERIT);
893N/A if ((aceFlags & CONTAINER_INHERIT_ACE) != 0)
893N/A flags.add(AclEntryFlag.DIRECTORY_INHERIT);
893N/A if ((aceFlags & NO_PROPAGATE_INHERIT_ACE) != 0)
893N/A flags.add(AclEntryFlag.NO_PROPAGATE_INHERIT);
893N/A if ((aceFlags & INHERIT_ONLY_ACE) != 0)
893N/A flags.add(AclEntryFlag.INHERIT_ONLY);
893N/A
893N/A // map access mask
893N/A int mask = unsafe.getInt(aceAddress + OFFSETOF_ACCESS_MASK);
3471N/A Set<AclEntryPermission> perms = EnumSet.noneOf(AclEntryPermission.class);
893N/A if ((mask & FILE_READ_DATA) > 0)
893N/A perms.add(AclEntryPermission.READ_DATA);
893N/A if ((mask & FILE_WRITE_DATA) > 0)
893N/A perms.add(AclEntryPermission.WRITE_DATA);
893N/A if ((mask & FILE_APPEND_DATA ) > 0)
893N/A perms.add(AclEntryPermission.APPEND_DATA);
893N/A if ((mask & FILE_READ_EA) > 0)
893N/A perms.add(AclEntryPermission.READ_NAMED_ATTRS);
893N/A if ((mask & FILE_WRITE_EA) > 0)
893N/A perms.add(AclEntryPermission.WRITE_NAMED_ATTRS);
893N/A if ((mask & FILE_EXECUTE) > 0)
893N/A perms.add(AclEntryPermission.EXECUTE);
893N/A if ((mask & FILE_DELETE_CHILD ) > 0)
893N/A perms.add(AclEntryPermission.DELETE_CHILD);
893N/A if ((mask & FILE_READ_ATTRIBUTES) > 0)
893N/A perms.add(AclEntryPermission.READ_ATTRIBUTES);
893N/A if ((mask & FILE_WRITE_ATTRIBUTES) > 0)
893N/A perms.add(AclEntryPermission.WRITE_ATTRIBUTES);
893N/A if ((mask & DELETE) > 0)
893N/A perms.add(AclEntryPermission.DELETE);
893N/A if ((mask & READ_CONTROL) > 0)
893N/A perms.add(AclEntryPermission.READ_ACL);
893N/A if ((mask & WRITE_DAC) > 0)
893N/A perms.add(AclEntryPermission.WRITE_ACL);
893N/A if ((mask & WRITE_OWNER) > 0)
893N/A perms.add(AclEntryPermission.WRITE_OWNER);
893N/A if ((mask & SYNCHRONIZE) > 0)
893N/A perms.add(AclEntryPermission.SYNCHRONIZE);
893N/A
893N/A // lookup SID to create UserPrincipal
893N/A long sidAddress = aceAddress + OFFSETOF_SID;
893N/A UserPrincipal user = WindowsUserPrincipals.fromSid(sidAddress);
893N/A
893N/A return AclEntry.newBuilder()
893N/A .setType(type)
893N/A .setPrincipal(user)
893N/A .setFlags(flags).setPermissions(perms).build();
893N/A }
893N/A
893N/A // encode NFSv4 AclEntry as Windows ACE to given ACL
893N/A private static void encode(AclEntry ace, long sidAddress, long aclAddress)
893N/A throws WindowsException
893N/A {
893N/A // ignore non-allow/deny entries for now
893N/A if (ace.type() != AclEntryType.ALLOW && ace.type() != AclEntryType.DENY)
893N/A return;
893N/A boolean allow = (ace.type() == AclEntryType.ALLOW);
893N/A
893N/A // map access mask
893N/A Set<AclEntryPermission> aceMask = ace.permissions();
893N/A int mask = 0;
893N/A if (aceMask.contains(AclEntryPermission.READ_DATA))
893N/A mask |= FILE_READ_DATA;
893N/A if (aceMask.contains(AclEntryPermission.WRITE_DATA))
893N/A mask |= FILE_WRITE_DATA;
893N/A if (aceMask.contains(AclEntryPermission.APPEND_DATA))
893N/A mask |= FILE_APPEND_DATA;
893N/A if (aceMask.contains(AclEntryPermission.READ_NAMED_ATTRS))
893N/A mask |= FILE_READ_EA;
893N/A if (aceMask.contains(AclEntryPermission.WRITE_NAMED_ATTRS))
893N/A mask |= FILE_WRITE_EA;
893N/A if (aceMask.contains(AclEntryPermission.EXECUTE))
893N/A mask |= FILE_EXECUTE;
893N/A if (aceMask.contains(AclEntryPermission.DELETE_CHILD))
893N/A mask |= FILE_DELETE_CHILD;
893N/A if (aceMask.contains(AclEntryPermission.READ_ATTRIBUTES))
893N/A mask |= FILE_READ_ATTRIBUTES;
893N/A if (aceMask.contains(AclEntryPermission.WRITE_ATTRIBUTES))
893N/A mask |= FILE_WRITE_ATTRIBUTES;
893N/A if (aceMask.contains(AclEntryPermission.DELETE))
893N/A mask |= DELETE;
893N/A if (aceMask.contains(AclEntryPermission.READ_ACL))
893N/A mask |= READ_CONTROL;
893N/A if (aceMask.contains(AclEntryPermission.WRITE_ACL))
893N/A mask |= WRITE_DAC;
893N/A if (aceMask.contains(AclEntryPermission.WRITE_OWNER))
893N/A mask |= WRITE_OWNER;
893N/A if (aceMask.contains(AclEntryPermission.SYNCHRONIZE))
893N/A mask |= SYNCHRONIZE;
893N/A
893N/A // map flags
893N/A Set<AclEntryFlag> aceFlags = ace.flags();
893N/A byte flags = 0;
893N/A if (aceFlags.contains(AclEntryFlag.FILE_INHERIT))
893N/A flags |= OBJECT_INHERIT_ACE;
893N/A if (aceFlags.contains(AclEntryFlag.DIRECTORY_INHERIT))
893N/A flags |= CONTAINER_INHERIT_ACE;
893N/A if (aceFlags.contains(AclEntryFlag.NO_PROPAGATE_INHERIT))
893N/A flags |= NO_PROPAGATE_INHERIT_ACE;
893N/A if (aceFlags.contains(AclEntryFlag.INHERIT_ONLY))
893N/A flags |= INHERIT_ONLY_ACE;
893N/A
893N/A if (allow) {
893N/A AddAccessAllowedAceEx(aclAddress, flags, mask, sidAddress);
893N/A } else {
893N/A AddAccessDeniedAceEx(aclAddress, flags, mask, sidAddress);
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Creates a security descriptor with a DACL representing the given ACL.
893N/A */
893N/A static WindowsSecurityDescriptor create(List<AclEntry> acl)
893N/A throws IOException
893N/A {
893N/A return new WindowsSecurityDescriptor(acl);
893N/A }
893N/A
893N/A /**
893N/A * Processes the array of attributes looking for the attribute "acl:acl".
893N/A * Returns security descriptor representing the ACL or the "null" security
893N/A * descriptor if the attribute is not in the array.
893N/A */
893N/A @SuppressWarnings("unchecked")
893N/A static WindowsSecurityDescriptor fromAttribute(FileAttribute<?>... attrs)
893N/A throws IOException
893N/A {
893N/A WindowsSecurityDescriptor sd = NULL_DESCRIPTOR;
893N/A for (FileAttribute<?> attr: attrs) {
893N/A // if more than one ACL specified then last one wins
893N/A if (sd != NULL_DESCRIPTOR)
893N/A sd.release();
893N/A if (attr == null)
893N/A throw new NullPointerException();
893N/A if (attr.name().equals("acl:acl")) {
893N/A List<AclEntry> acl = (List<AclEntry>)attr.value();
893N/A sd = new WindowsSecurityDescriptor(acl);
893N/A } else {
893N/A throw new UnsupportedOperationException("'" + attr.name() +
893N/A "' not supported as initial attribute");
893N/A }
893N/A }
893N/A return sd;
893N/A }
893N/A
893N/A /**
893N/A * Extracts DACL from security descriptor.
893N/A */
893N/A static List<AclEntry> getAcl(long pSecurityDescriptor) throws IOException {
893N/A // get address of DACL
893N/A long aclAddress = GetSecurityDescriptorDacl(pSecurityDescriptor);
893N/A
893N/A // get ACE count
893N/A int aceCount = 0;
893N/A if (aclAddress == 0L) {
893N/A // no ACEs
893N/A aceCount = 0;
893N/A } else {
893N/A AclInformation aclInfo = GetAclInformation(aclAddress);
893N/A aceCount = aclInfo.aceCount();
893N/A }
3471N/A ArrayList<AclEntry> result = new ArrayList<>(aceCount);
893N/A
893N/A // decode each of the ACEs to AclEntry objects
893N/A for (int i=0; i<aceCount; i++) {
893N/A long aceAddress = GetAce(aclAddress, i);
893N/A AclEntry entry = decode(aceAddress);
893N/A if (entry != null)
893N/A result.add(entry);
893N/A }
893N/A return result;
893N/A }
893N/A}