Esempio n. 1
0
  static UserPrincipal lookup(String name) throws IOException {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
      sm.checkPermission(new RuntimePermission("lookupUserInformation"));
    }

    // invoke LookupAccountName to get buffer size needed for SID
    int size = 0;
    try {
      size = LookupAccountName(name, 0L, 0);
    } catch (WindowsException x) {
      if (x.lastError() == ERROR_NONE_MAPPED) throw new UserPrincipalNotFoundException(name);
      throw new IOException(name + ": " + x.errorString());
    }
    assert size > 0;

    // allocate buffer and re-invoke LookupAccountName get SID
    NativeBuffer sidBuffer = NativeBuffers.getNativeBuffer(size);
    try {
      int newSize = LookupAccountName(name, sidBuffer.address(), size);
      if (newSize != size) {
        // can this happen?
        throw new AssertionError("SID change during lookup");
      }

      // return user principal
      return fromSid(sidBuffer.address());
    } catch (WindowsException x) {
      throw new IOException(name + ": " + x.errorString());
    } finally {
      sidBuffer.release();
    }
  }
Esempio n. 2
0
  static UserPrincipal fromSid(long sidAddress) throws IOException {
    String sidString;
    try {
      sidString = ConvertSidToStringSid(sidAddress);
      if (sidString == null) {
        // pre-Windows XP system?
        throw new AssertionError();
      }
    } catch (WindowsException x) {
      throw new IOException("Unable to convert SID to String: " + x.errorString());
    }

    // lookup account; if not available then use the SID as the name
    Account account = null;
    String name;
    try {
      account = LookupAccountSid(sidAddress);
      name = account.domain() + "\\" + account.name();
    } catch (WindowsException x) {
      name = sidString;
    }

    int sidType = (account == null) ? SidTypeUnknown : account.use();
    if ((sidType == SidTypeGroup)
        || (sidType == SidTypeWellKnownGroup)
        || (sidType == SidTypeAlias)) // alias for local group
    {
      return new Group(sidString, sidType, name);
    } else {
      return new User(sidString, sidType, name);
    }
  }