Search in sources :

Example 41 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class Advapi32Util method getTokenGroups.

/**
	 * This function returns the groups associated with a security token, such
	 * as a user token.
	 *
	 * @param hToken
	 *            Token.
	 * @return Token groups.
	 */
public static Account[] getTokenGroups(HANDLE hToken) {
    // get token group information size
    IntByReference tokenInformationLength = new IntByReference();
    if (Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, null, 0, tokenInformationLength)) {
        throw new RuntimeException("Expected GetTokenInformation to fail with ERROR_INSUFFICIENT_BUFFER");
    }
    int rc = Kernel32.INSTANCE.GetLastError();
    if (rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
        throw new Win32Exception(rc);
    }
    // get token group information
    WinNT.TOKEN_GROUPS groups = new WinNT.TOKEN_GROUPS(tokenInformationLength.getValue());
    if (!Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, groups, tokenInformationLength.getValue(), tokenInformationLength)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    ArrayList<Account> userGroups = new ArrayList<Account>();
    // make array of names
    for (SID_AND_ATTRIBUTES sidAndAttribute : groups.getGroups()) {
        Account group = null;
        try {
            group = Advapi32Util.getAccountBySid(sidAndAttribute.Sid);
        } catch (Exception e) {
            group = new Account();
            group.sid = sidAndAttribute.Sid.getBytes();
            group.sidString = Advapi32Util.convertSidToStringSid(sidAndAttribute.Sid);
            group.name = group.sidString;
            group.fqn = group.sidString;
            group.accountType = SID_NAME_USE.SidTypeGroup;
        }
        userGroups.add(group);
    }
    return userGroups.toArray(new Account[0]);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ArrayList(java.util.ArrayList) SID_AND_ATTRIBUTES(com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES) IOException(java.io.IOException)

Example 42 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class Advapi32Util method fileEncryptionStatus.

/**
     * Checks the encryption status of a file.
     *
     * @param file
     *         The file to check the status for.
     * @return The status of the file.
     */
public static int fileEncryptionStatus(File file) {
    DWORDByReference status = new DWORDByReference();
    String lpFileName = file.getAbsolutePath();
    if (!Advapi32.INSTANCE.FileEncryptionStatus(lpFileName, status)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    return status.getValue().intValue();
}
Also used : DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference)

Example 43 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class Advapi32Util method setSecurityDescriptorForObject.

/**
     * Set a self relative security descriptor for the given object type.
     *
     * @param absoluteObjectPath
     *         A pointer to a null-terminated string that specifies the name of the object
     *         from which to retrieve security information. For descriptions of the string
     *         formats for the different object types, see {@link AccCtrl.SE_OBJECT_TYPE}.
     * @param objectType
     *         Object type referred to by the path. See  {@link AccCtrl.SE_OBJECT_TYPE} for valid definitions.
     * @param securityDescriptor
     *         A security descriptor to set.
     * @param setOwner
     *         Set the owner. The owner is extracted from securityDescriptor and must be valid,
     *         otherwise IllegalArgumentException is throw.
     *         See {@link Advapi32#SetNamedSecurityInfo} for process privilege requirements in getting the OWNER.
     * @param setGroup
     *         Set the group. The group is extracted from securityDescriptor and must be valid,
     *         otherwise IllegalArgumentException is throw.
     * @param setDACL
     *         Set the DACL. The DACL is extracted from securityDescriptor and must be valid,
     *         otherwise IllegalArgumentException is throw.
     * @param setSACL
     *         Set the SACL. The SACL is extracted from securityDescriptor and must be valid,
     *         otherwise IllegalArgumentException is throw.
     *          See {@link Advapi32#SetNamedSecurityInfo} for process privilege requirements in getting the SACL.
     * @param setDACLProtectedStatus
     *         Set DACL protected status as contained within securityDescriptor.control.
     * @param setSACLProtectedStatus
     *         Set SACL protected status as contained within securityDescriptor.control.
     */
public static void setSecurityDescriptorForObject(final String absoluteObjectPath, int objectType, SECURITY_DESCRIPTOR_RELATIVE securityDescriptor, boolean setOwner, boolean setGroup, boolean setDACL, boolean setSACL, boolean setDACLProtectedStatus, boolean setSACLProtectedStatus) {
    final PSID psidOwner = securityDescriptor.getOwner();
    final PSID psidGroup = securityDescriptor.getGroup();
    final ACL dacl = securityDescriptor.getDiscretionaryACL();
    final ACL sacl = securityDescriptor.getSystemACL();
    int infoType = 0;
    // Parameter validation and infoType flag setting.
    if (setOwner) {
        if (psidOwner == null)
            throw new IllegalArgumentException("SECURITY_DESCRIPTOR_RELATIVE does not contain owner");
        if (!Advapi32.INSTANCE.IsValidSid(psidOwner))
            throw new IllegalArgumentException("Owner PSID is invalid");
        infoType |= OWNER_SECURITY_INFORMATION;
    }
    if (setGroup) {
        if (psidGroup == null)
            throw new IllegalArgumentException("SECURITY_DESCRIPTOR_RELATIVE does not contain group");
        if (!Advapi32.INSTANCE.IsValidSid(psidGroup))
            throw new IllegalArgumentException("Group PSID is invalid");
        infoType |= GROUP_SECURITY_INFORMATION;
    }
    if (setDACL) {
        if (dacl == null)
            throw new IllegalArgumentException("SECURITY_DESCRIPTOR_RELATIVE does not contain DACL");
        if (!Advapi32.INSTANCE.IsValidAcl(dacl.getPointer()))
            throw new IllegalArgumentException("DACL is invalid");
        infoType |= DACL_SECURITY_INFORMATION;
    }
    if (setSACL) {
        if (sacl == null)
            throw new IllegalArgumentException("SECURITY_DESCRIPTOR_RELATIVE does not contain SACL");
        if (!Advapi32.INSTANCE.IsValidAcl(sacl.getPointer()))
            throw new IllegalArgumentException("SACL is invalid");
        infoType |= SACL_SECURITY_INFORMATION;
    }
    /*
    	 * Control bits SE_DACL_PROTECTED/SE_SACL_PROTECTED indicate the *ACL is protected. The *ACL_SECURITY_INFORMATION flags
    	 * are meta flags for SetNamedSecurityInfo and are not stored in the SD.  If either *ACLProtectedStatus is set,
    	 * get the current status from the securityDescriptor and apply as such, otherwise the ACL remains at its default.
    	*/
    if (setDACLProtectedStatus) {
        if ((securityDescriptor.Control & SE_DACL_PROTECTED) != 0) {
            infoType |= PROTECTED_DACL_SECURITY_INFORMATION;
        } else if ((securityDescriptor.Control & SE_DACL_PROTECTED) == 0) {
            infoType |= UNPROTECTED_DACL_SECURITY_INFORMATION;
        }
    }
    if (setSACLProtectedStatus) {
        if ((securityDescriptor.Control & SE_SACL_PROTECTED) != 0) {
            infoType |= PROTECTED_SACL_SECURITY_INFORMATION;
        } else if ((securityDescriptor.Control & SE_SACL_PROTECTED) == 0) {
            infoType |= UNPROTECTED_SACL_SECURITY_INFORMATION;
        }
    }
    int lastError = Advapi32.INSTANCE.SetNamedSecurityInfo(absoluteObjectPath, objectType, infoType, setOwner ? psidOwner.getPointer() : null, setGroup ? psidGroup.getPointer() : null, setDACL ? dacl.getPointer() : null, setSACL ? sacl.getPointer() : null);
    if (lastError != 0) {
        throw new Win32Exception(lastError);
    }
}
Also used : ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 44 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class Advapi32Util method registrySetBinaryValue.

/**
	 * Set a binary value in registry.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to an existing registry key.
	 * @param name
	 *            Value name.
	 * @param data
	 *            Data to write to registry.
	 */
public static void registrySetBinaryValue(HKEY root, String keyPath, String name, byte[] data) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
    }
    try {
        registrySetBinaryValue(phkKey.getValue(), name, data);
    } finally {
        rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
        if (rc != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(rc);
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 45 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class Advapi32Util method registrySetLongValue.

/**
	 * Set a long value in registry.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to an existing registry key.
	 * @param name
	 *            Value name.
	 * @param value
	 *            Value to write to registry.
	 */
public static void registrySetLongValue(HKEY root, String keyPath, String name, long value) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
    }
    try {
        registrySetLongValue(phkKey.getValue(), name, value);
    } finally {
        rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
        if (rc != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(rc);
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)35 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)18 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 Memory (com.sun.jna.Memory)15 PointerByReference (com.sun.jna.ptr.PointerByReference)11 ArrayList (java.util.ArrayList)11 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Pointer (com.sun.jna.Pointer)6 File (java.io.File)6 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)5 Test (org.junit.Test)5 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)4 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)4 PSID (com.sun.jna.platform.win32.WinNT.PSID)4 Win32Exception (com.sun.jna.platform.win32.Win32Exception)3 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)3 LOCALGROUP_INFO_1 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_INFO_1)2 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)2 DATA_BLOB (com.sun.jna.platform.win32.WinCrypt.DATA_BLOB)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2