Search in sources :

Example 11 with Win32Exception

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

the class Advapi32Util method registryGetKey.

/**
	 * Get a registry key, the caller is responsible to close the key after use.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to a registry key.
	 *
	 * @param samDesired
	 *            Access level (e.g. WinNT.KEY_READ)
	 *
	 * @return HKEYByReference.
	 */
public static HKEYByReference registryGetKey(HKEY root, String keyPath, int samDesired) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, samDesired, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
    }
    return phkKey;
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 12 with Win32Exception

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

the class Advapi32Util method getFileSecurity.

public static ACCESS_ACEStructure[] getFileSecurity(String fileName, boolean compact) {
    int infoType = WinNT.DACL_SECURITY_INFORMATION;
    int nLength = 1024;
    boolean repeat = false;
    Memory memory = null;
    do {
        repeat = false;
        memory = new Memory(nLength);
        IntByReference lpnSize = new IntByReference();
        boolean succeded = Advapi32.INSTANCE.GetFileSecurity(fileName, infoType, memory, nLength, lpnSize);
        if (!succeded) {
            int lastError = Kernel32.INSTANCE.GetLastError();
            memory.clear();
            if (W32Errors.ERROR_INSUFFICIENT_BUFFER != lastError) {
                throw new Win32Exception(lastError);
            }
        }
        int lengthNeeded = lpnSize.getValue();
        if (nLength < lengthNeeded) {
            repeat = true;
            nLength = lengthNeeded;
            memory.clear();
        }
    } while (repeat);
    SECURITY_DESCRIPTOR_RELATIVE sdr = new WinNT.SECURITY_DESCRIPTOR_RELATIVE(memory);
    memory.clear();
    ACL dacl = sdr.getDiscretionaryACL();
    ACCESS_ACEStructure[] aceStructures = dacl.getACEStructures();
    if (compact) {
        Map<String, ACCESS_ACEStructure> aceMap = new HashMap<String, ACCESS_ACEStructure>();
        for (ACCESS_ACEStructure aceStructure : aceStructures) {
            boolean inherted = ((aceStructure.AceFlags & WinNT.VALID_INHERIT_FLAGS) != 0);
            String key = aceStructure.getSidString() + "/" + inherted + "/" + aceStructure.getClass().getName();
            ACCESS_ACEStructure aceStructure2 = aceMap.get(key);
            if (aceStructure2 != null) {
                int accessMask = aceStructure2.Mask;
                accessMask = accessMask | aceStructure.Mask;
                aceStructure2.Mask = accessMask;
            } else {
                aceMap.put(key, aceStructure);
            }
        }
        return aceMap.values().toArray(new ACCESS_ACEStructure[aceMap.size()]);
    }
    return aceStructures;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HashMap(java.util.HashMap) Memory(com.sun.jna.Memory) SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE) ACCESS_ACEStructure(com.sun.jna.platform.win32.WinNT.ACCESS_ACEStructure) ACL(com.sun.jna.platform.win32.WinNT.ACL)

Example 13 with Win32Exception

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

the class Advapi32Util method registryDeleteValue.

/**
	 * Delete a registry value.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to an existing registry key.
	 * @param valueName
	 *            Name of the value to delete.
	 */
public static void registryDeleteValue(HKEY root, String keyPath, String valueName) {
    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 {
        registryDeleteValue(phkKey.getValue(), valueName);
    } 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 14 with Win32Exception

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

the class Advapi32Util method convertStringSidToSid.

/**
	 * Convert a string representation of a security identifier (SID) to a
	 * binary format.
	 *
	 * @param sidString
	 *            String SID.
	 * @return SID bytes.
	 */
public static byte[] convertStringSidToSid(String sidString) {
    PSIDByReference pSID = new PSIDByReference();
    if (!Advapi32.INSTANCE.ConvertStringSidToSid(sidString, pSID)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    PSID value = pSID.getValue();
    try {
        return value.getBytes();
    } finally {
        Kernel32Util.freeLocalMemory(value.getPointer());
    }
}
Also used : PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 15 with Win32Exception

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

the class Advapi32Util method accessCheck.

/**
     * Checks if the current process has the given permission for the file.
     * @param file the file to check
     * @param permissionToCheck the permission to check for the file
     * @return true if has access, otherwise false
     */
public static boolean accessCheck(File file, AccessCheckPermission permissionToCheck) {
    Memory securityDescriptorMemoryPointer = getSecurityDescriptorForFile(file.getAbsolutePath().replace('/', '\\'));
    HANDLEByReference openedAccessToken = new HANDLEByReference();
    HANDLEByReference duplicatedToken = new HANDLEByReference();
    Win32Exception err = null;
    try {
        int desireAccess = TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ;
        HANDLE hProcess = Kernel32.INSTANCE.GetCurrentProcess();
        if (!Advapi32.INSTANCE.OpenProcessToken(hProcess, desireAccess, openedAccessToken)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        if (!Advapi32.INSTANCE.DuplicateToken(openedAccessToken.getValue(), SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, duplicatedToken)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        GENERIC_MAPPING mapping = new GENERIC_MAPPING();
        mapping.genericRead = new DWORD(FILE_GENERIC_READ);
        mapping.genericWrite = new DWORD(FILE_GENERIC_WRITE);
        mapping.genericExecute = new DWORD(FILE_GENERIC_EXECUTE);
        mapping.genericAll = new DWORD(FILE_ALL_ACCESS);
        DWORDByReference rights = new DWORDByReference(new DWORD(permissionToCheck.getCode()));
        Advapi32.INSTANCE.MapGenericMask(rights, mapping);
        PRIVILEGE_SET privileges = new PRIVILEGE_SET(1);
        privileges.PrivilegeCount = new DWORD(0);
        DWORDByReference privilegeLength = new DWORDByReference(new DWORD(privileges.size()));
        DWORDByReference grantedAccess = new DWORDByReference();
        BOOLByReference result = new BOOLByReference();
        if (!Advapi32.INSTANCE.AccessCheck(securityDescriptorMemoryPointer, duplicatedToken.getValue(), rights.getValue(), mapping, privileges, privilegeLength, grantedAccess, result)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        return result.getValue().booleanValue();
    } catch (Win32Exception e) {
        err = e;
        // re-throw so finally block executed
        throw err;
    } finally {
        try {
            Kernel32Util.closeHandleRefs(openedAccessToken, duplicatedToken);
        } catch (Win32Exception e) {
            if (err == null) {
                err = e;
            } else {
                err.addSuppressed(e);
            }
        }
        if (securityDescriptorMemoryPointer != null) {
            securityDescriptorMemoryPointer.clear();
        }
        if (err != null) {
            throw err;
        }
    }
}
Also used : BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) PRIVILEGE_SET(com.sun.jna.platform.win32.WinNT.PRIVILEGE_SET) GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) Memory(com.sun.jna.Memory) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

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