jdk/src/windows/classes/sun/nio/fs/WindowsUserPrincipals.java
changeset 2057 3acf8e5e2ca0
child 5506 202f599c92aa
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 package sun.nio.fs;
       
    26 
       
    27 import java.nio.file.attribute.*;
       
    28 import java.io.IOException;
       
    29 
       
    30 import static sun.nio.fs.WindowsConstants.*;
       
    31 import static sun.nio.fs.WindowsNativeDispatcher.*;
       
    32 
       
    33 class WindowsUserPrincipals {
       
    34     private WindowsUserPrincipals() { }
       
    35 
       
    36     static class User implements UserPrincipal {
       
    37         // String representation of SID
       
    38         private final String sidString;
       
    39 
       
    40         // SID type
       
    41         private final int sidType;
       
    42 
       
    43         // Account name (if available) or SID
       
    44         private final String accountName;
       
    45 
       
    46         User(String sidString, int sidType, String accountName) {
       
    47             this.sidString = sidString;
       
    48             this.sidType = sidType;
       
    49             this.accountName = accountName;
       
    50         }
       
    51 
       
    52         // package-private
       
    53         String sidString() {
       
    54             return sidString;
       
    55         }
       
    56 
       
    57         @Override
       
    58         public String getName() {
       
    59             return accountName;
       
    60         }
       
    61 
       
    62         @Override
       
    63         public String toString() {
       
    64             String type;
       
    65             switch (sidType) {
       
    66                 case SidTypeUser : type = "User"; break;
       
    67                 case SidTypeGroup : type = "Group"; break;
       
    68                 case SidTypeDomain : type = "Domain"; break;
       
    69                 case SidTypeAlias : type = "Alias"; break;
       
    70                 case SidTypeWellKnownGroup : type = "Well-known group"; break;
       
    71                 case SidTypeDeletedAccount : type = "Deleted"; break;
       
    72                 case SidTypeInvalid : type = "Invalid"; break;
       
    73                 case SidTypeComputer : type = "Computer"; break;
       
    74                 default: type = "Unknown";
       
    75             }
       
    76             return accountName + " (" + type + ")";
       
    77         }
       
    78 
       
    79         @Override
       
    80         public boolean equals(Object obj) {
       
    81             if (obj == this)
       
    82                 return true;
       
    83             if (!(obj instanceof WindowsUserPrincipals.User))
       
    84                 return false;
       
    85             WindowsUserPrincipals.User other = (WindowsUserPrincipals.User)obj;
       
    86             return this.sidString.equals(other.sidString);
       
    87         }
       
    88 
       
    89         @Override
       
    90         public int hashCode() {
       
    91             return sidString.hashCode();
       
    92         }
       
    93     }
       
    94 
       
    95     static class Group extends User implements GroupPrincipal {
       
    96         Group(String sidString, int sidType, String accountName) {
       
    97             super(sidString, sidType, accountName);
       
    98         }
       
    99     }
       
   100 
       
   101     static UserPrincipal fromSid(long sidAddress) throws IOException {
       
   102         String sidString;
       
   103         try {
       
   104             sidString = ConvertSidToStringSid(sidAddress);
       
   105             if (sidString == null) {
       
   106                 // pre-Windows XP system?
       
   107                 throw new AssertionError();
       
   108             }
       
   109         } catch (WindowsException x) {
       
   110             throw new IOException("Unable to convert SID to String: " +
       
   111                 x.errorString());
       
   112         }
       
   113 
       
   114         // lookup account; if not available then use the SID as the name
       
   115         Account account = null;
       
   116         String name;
       
   117         try {
       
   118             account = LookupAccountSid(sidAddress);
       
   119             name = account.domain() + "\\" + account.name();
       
   120         } catch (WindowsException x) {
       
   121             name = sidString;
       
   122         }
       
   123 
       
   124         int sidType = (account == null) ? SidTypeUnknown : account.use();
       
   125         if ((sidType == SidTypeGroup) ||
       
   126             (sidType == SidTypeWellKnownGroup) ||
       
   127             (sidType == SidTypeAlias)) // alias for local group
       
   128         {
       
   129             return new Group(sidString, sidType, name);
       
   130         } else {
       
   131             return new User(sidString, sidType, name);
       
   132         }
       
   133     }
       
   134 
       
   135     static UserPrincipal lookup(String name) throws IOException {
       
   136         SecurityManager sm = System.getSecurityManager();
       
   137         if (sm != null) {
       
   138             sm.checkPermission(new RuntimePermission("lookupUserInformation"));
       
   139         }
       
   140 
       
   141         // invoke LookupAccountName to get buffer size needed for SID
       
   142         int size = 0;
       
   143         try {
       
   144             size = LookupAccountName(name, 0L, 0);
       
   145         } catch (WindowsException x) {
       
   146             if (x.lastError() == ERROR_NONE_MAPPED)
       
   147                 throw new UserPrincipalNotFoundException(name);
       
   148             throw new IOException(name + ": " + x.errorString());
       
   149         }
       
   150         assert size > 0;
       
   151 
       
   152         // allocate buffer and re-invoke LookupAccountName get SID
       
   153         NativeBuffer sidBuffer = NativeBuffers.getNativeBuffer(size);
       
   154         try {
       
   155             int newSize = LookupAccountName(name, sidBuffer.address(), size);
       
   156             if (newSize != size) {
       
   157                 // can this happen?
       
   158                 throw new AssertionError("SID change during lookup");
       
   159             }
       
   160 
       
   161             // return user principal
       
   162             return fromSid(sidBuffer.address());
       
   163         } catch (WindowsException x) {
       
   164             throw new IOException(name + ": " + x.errorString());
       
   165         } finally {
       
   166             sidBuffer.release();
       
   167         }
       
   168     }
       
   169 }