Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsUserPrincipals.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*/24package sun.nio.fs;2526import java.nio.file.attribute.*;27import java.io.IOException;2829import static sun.nio.fs.WindowsConstants.*;30import static sun.nio.fs.WindowsNativeDispatcher.*;3132class WindowsUserPrincipals {33private WindowsUserPrincipals() { }3435static class User implements UserPrincipal {36// String representation of SID37private final String sidString;3839// SID type40private final int sidType;4142// Account name (if available) or SID43private final String accountName;4445User(String sidString, int sidType, String accountName) {46this.sidString = sidString;47this.sidType = sidType;48this.accountName = accountName;49}5051// package-private52String sidString() {53return sidString;54}5556@Override57public String getName() {58return accountName;59}6061@Override62public String toString() {63String type;64switch (sidType) {65case SidTypeUser : type = "User"; break;66case SidTypeGroup : type = "Group"; break;67case SidTypeDomain : type = "Domain"; break;68case SidTypeAlias : type = "Alias"; break;69case SidTypeWellKnownGroup : type = "Well-known group"; break;70case SidTypeDeletedAccount : type = "Deleted"; break;71case SidTypeInvalid : type = "Invalid"; break;72case SidTypeComputer : type = "Computer"; break;73default: type = "Unknown";74}75return accountName + " (" + type + ")";76}7778@Override79public boolean equals(Object obj) {80if (obj == this)81return true;82if (!(obj instanceof WindowsUserPrincipals.User))83return false;84WindowsUserPrincipals.User other = (WindowsUserPrincipals.User)obj;85return this.sidString.equals(other.sidString);86}8788@Override89public int hashCode() {90return sidString.hashCode();91}92}9394static class Group extends User implements GroupPrincipal {95Group(String sidString, int sidType, String accountName) {96super(sidString, sidType, accountName);97}98}99100static UserPrincipal fromSid(long sidAddress) throws IOException {101String sidString;102try {103sidString = ConvertSidToStringSid(sidAddress);104if (sidString == null) {105// pre-Windows XP system?106throw new AssertionError();107}108} catch (WindowsException x) {109throw new IOException("Unable to convert SID to String: " +110x.errorString());111}112113// lookup account; if not available then use the SID as the name114Account account = null;115String name;116try {117account = LookupAccountSid(sidAddress);118name = account.domain() + "\\" + account.name();119} catch (WindowsException x) {120name = sidString;121}122123int sidType = (account == null) ? SidTypeUnknown : account.use();124if ((sidType == SidTypeGroup) ||125(sidType == SidTypeWellKnownGroup) ||126(sidType == SidTypeAlias)) // alias for local group127{128return new Group(sidString, sidType, name);129} else {130return new User(sidString, sidType, name);131}132}133134static UserPrincipal lookup(String name) throws IOException {135SecurityManager sm = System.getSecurityManager();136if (sm != null) {137sm.checkPermission(new RuntimePermission("lookupUserInformation"));138}139140// invoke LookupAccountName to get buffer size needed for SID141int size = 0;142try {143size = LookupAccountName(name, 0L, 0);144} catch (WindowsException x) {145if (x.lastError() == ERROR_NONE_MAPPED)146throw new UserPrincipalNotFoundException(name);147throw new IOException(name + ": " + x.errorString());148}149assert size > 0;150151// allocate buffer and re-invoke LookupAccountName get SID152NativeBuffer sidBuffer = NativeBuffers.getNativeBuffer(size);153try {154int newSize = LookupAccountName(name, sidBuffer.address(), size);155if (newSize != size) {156// can this happen?157throw new AssertionError("SID change during lookup");158}159160// return user principal161return fromSid(sidBuffer.address());162} catch (WindowsException x) {163throw new IOException(name + ": " + x.errorString());164} finally {165sidBuffer.release();166}167}168}169170171