Path: blob/master/src/java.base/unix/classes/sun/nio/fs/UnixUserPrincipals.java
41137 views
/*1* Copyright (c) 2008, 2021, 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.attribute.*;28import java.io.IOException;29import static sun.nio.fs.UnixNativeDispatcher.*;3031/**32* Unix implementation of java.nio.file.attribute.UserPrincipal33*/3435public class UnixUserPrincipals {36private static User createSpecial(String name) { return new User(-1, name); }3738static final User SPECIAL_OWNER = createSpecial("OWNER@");39static final User SPECIAL_GROUP = createSpecial("GROUP@");40static final User SPECIAL_EVERYONE = createSpecial("EVERYONE@");4142static class User implements UserPrincipal {43private final int id; // uid or gid44private final boolean isGroup;45private final String name;4647private User(int id, boolean isGroup, String name) {48this.id = id;49this.isGroup = isGroup;50this.name = name;51}5253User(int id, String name) {54this(id, false, name);55}5657int uid() {58if (isGroup)59throw new AssertionError();60return id;61}6263int gid() {64if (isGroup)65return id;66throw new AssertionError();67}6869@Override70public String getName() {71return name;72}7374@Override75public String toString() {76return name;77}7879@Override80public boolean equals(Object obj) {81if (obj == this)82return true;83if (!(obj instanceof User))84return false;85User other = (User)obj;86if ((this.id != other.id) ||87(this.isGroup != other.isGroup)) {88return false;89}90// specials91if (this.id == -1 && other.id == -1)92return this.name.equals(other.name);9394return true;95}9697@Override98public int hashCode() {99return (id != -1) ? id : name.hashCode();100}101}102103static class Group extends User implements GroupPrincipal {104Group(int id, String name) {105super(id, true, name);106}107}108109// return UserPrincipal representing given uid110public static User fromUid(int uid) {111String name;112try {113name = Util.toString(getpwuid(uid));114} catch (UnixException x) {115name = Integer.toString(uid);116}117return new User(uid, name);118}119120// return GroupPrincipal representing given gid121public static Group fromGid(int gid) {122String name;123try {124name = Util.toString(getgrgid(gid));125} catch (UnixException x) {126name = Integer.toString(gid);127}128return new Group(gid, name);129}130131// lookup user or group name132private static int lookupName(String name, boolean isGroup)133throws IOException134{135@SuppressWarnings("removal")136SecurityManager sm = System.getSecurityManager();137if (sm != null) {138sm.checkPermission(new RuntimePermission("lookupUserInformation"));139}140int id;141try {142id = (isGroup) ? getgrnam(name) : getpwnam(name);143} catch (UnixException x) {144throw new IOException(name + ": " + x.errorString());145}146if (id == -1) {147// lookup failed, allow input to be uid or gid148try {149id = Integer.parseInt(name);150} catch (NumberFormatException ignore) {151throw new UserPrincipalNotFoundException(name);152}153}154return id;155156}157158// lookup user name159static UserPrincipal lookupUser(String name) throws IOException {160if (name.equals(SPECIAL_OWNER.getName()))161return SPECIAL_OWNER;162if (name.equals(SPECIAL_GROUP.getName()))163return SPECIAL_GROUP;164if (name.equals(SPECIAL_EVERYONE.getName()))165return SPECIAL_EVERYONE;166int uid = lookupName(name, false);167return new User(uid, name);168}169170// lookup group name171static GroupPrincipal lookupGroup(String group)172throws IOException173{174int gid = lookupName(group, true);175return new Group(gid, group);176}177}178179180