Search in sources :

Example 1 with BOOLByReference

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

the class Advapi32Test method testSetGetSecurityDescriptorDacl.

public void testSetGetSecurityDescriptorDacl() throws IOException {
    SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024);
    assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION));
    ACL pAcl;
    int cbAcl = 0;
    PSID pSid = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
    IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
    assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSid, cbSid));
    int sidLength = Advapi32.INSTANCE.GetLengthSid(pSid);
    cbAcl = Native.getNativeSize(ACL.class, null);
    cbAcl += Native.getNativeSize(ACCESS_ALLOWED_ACE.class, null);
    cbAcl += (sidLength - DWORD.SIZE);
    cbAcl = Advapi32Util.alignOnDWORD(cbAcl);
    pAcl = new ACL(cbAcl);
    assertTrue(Advapi32.INSTANCE.InitializeAcl(pAcl, cbAcl, WinNT.ACL_REVISION));
    assertTrue(Advapi32.INSTANCE.AddAccessAllowedAce(pAcl, WinNT.ACL_REVISION, WinNT.STANDARD_RIGHTS_ALL, pSid));
    assertTrue(Advapi32.INSTANCE.SetSecurityDescriptorDacl(sd, true, pAcl, false));
    BOOLByReference lpbDaclPresent = new BOOLByReference();
    BOOLByReference lpbDaclDefaulted = new BOOLByReference();
    PACLByReference pDacl = new PACLByReference();
    assertTrue(Advapi32.INSTANCE.GetSecurityDescriptorDacl(sd, lpbDaclPresent, pDacl, lpbDaclDefaulted));
    ACL pAclGet = pDacl.getValue();
    assertEquals(new BOOL(true), lpbDaclPresent.getValue());
    assertEquals(new BOOL(false), lpbDaclDefaulted.getValue());
    assertEquals(1, pAclGet.AceCount);
    assertEquals(WinNT.ACL_REVISION, pAclGet.AclRevision);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ACCESS_ALLOWED_ACE(com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE) BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) BOOL(com.sun.jna.platform.win32.WinDef.BOOL) SECURITY_DESCRIPTOR(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR) PACLByReference(com.sun.jna.platform.win32.WinNT.PACLByReference) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 2 with BOOLByReference

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

the class Advapi32Test method testSetGetSecurityDescriptorOwner.

public void testSetGetSecurityDescriptorOwner() {
    SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024);
    assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION));
    PSID pSidPut = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
    IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
    assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSidPut, cbSid));
    assertTrue(Advapi32.INSTANCE.SetSecurityDescriptorOwner(sd, pSidPut, true));
    BOOLByReference lpbOwnerDefaulted = new BOOLByReference();
    PSIDByReference prSd = new PSIDByReference();
    assertTrue(Advapi32.INSTANCE.GetSecurityDescriptorOwner(sd, prSd, lpbOwnerDefaulted));
    PSID pSidGet = prSd.getValue();
    assertTrue(Advapi32.INSTANCE.EqualSid(pSidPut, pSidGet));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) SECURITY_DESCRIPTOR(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 3 with BOOLByReference

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

the class Advapi32Test method testSetGetSecurityDescriptorGroup.

public void testSetGetSecurityDescriptorGroup() {
    SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024);
    assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION));
    PSID pSidPut = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
    IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
    assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSidPut, cbSid));
    assertTrue(Advapi32.INSTANCE.SetSecurityDescriptorGroup(sd, pSidPut, true));
    BOOLByReference lpbOwnerDefaulted = new BOOLByReference();
    PSIDByReference prSd = new PSIDByReference();
    assertTrue(Advapi32.INSTANCE.GetSecurityDescriptorGroup(sd, prSd, lpbOwnerDefaulted));
    PSID pSidGet = prSd.getValue();
    assertTrue(Advapi32.INSTANCE.EqualSid(pSidPut, pSidGet));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) SECURITY_DESCRIPTOR(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 4 with BOOLByReference

use of com.sun.jna.platform.win32.WinDef.BOOLByReference 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)

Example 5 with BOOLByReference

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

the class Rasapi32Test method testRasGetEntryDialParams.

public void testRasGetEntryDialParams() {
    RASDIALPARAMS.ByReference rasDialParams = new RASDIALPARAMS.ByReference();
    System.arraycopy(rasDialParams.szEntryName, 0, "TEST".toCharArray(), 0, "TEST".length());
    BOOLByReference lpfPassword = new BOOLByReference();
    int err = Rasapi32.INSTANCE.RasGetEntryDialParams(null, rasDialParams, lpfPassword);
    assertEquals(623, err);
}
Also used : BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) RASDIALPARAMS(com.sun.jna.platform.win32.WinRas.RASDIALPARAMS) BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) IntByReference(com.sun.jna.ptr.IntByReference)

Aggregations

BOOLByReference (com.sun.jna.platform.win32.WinDef.BOOLByReference)8 IntByReference (com.sun.jna.ptr.IntByReference)5 PSID (com.sun.jna.platform.win32.WinNT.PSID)3 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)3 Memory (com.sun.jna.Memory)2 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2 GENERIC_MAPPING (com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING)2 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)2 PRIVILEGE_SET (com.sun.jna.platform.win32.WinNT.PRIVILEGE_SET)2 PSIDByReference (com.sun.jna.platform.win32.WinNT.PSIDByReference)2 RASDIALPARAMS (com.sun.jna.platform.win32.WinRas.RASDIALPARAMS)2 LPOLESTR (com.sun.jna.platform.win32.WTypes.LPOLESTR)1 BOOL (com.sun.jna.platform.win32.WinDef.BOOL)1 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)1 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)1 ACL (com.sun.jna.platform.win32.WinNT.ACL)1 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)1 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)1 PACLByReference (com.sun.jna.platform.win32.WinNT.PACLByReference)1