Search in sources :

Example 1 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Test method testGetAce.

public void testGetAce() throws IOException {
    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));
    PointerByReference pAce = new PointerByReference(new Memory(16));
    assertTrue(Advapi32.INSTANCE.GetAce(pAcl, 0, pAce));
    ACCESS_ALLOWED_ACE pAceGet = new ACCESS_ALLOWED_ACE(pAce.getValue());
    assertTrue(pAceGet.Mask == WinNT.STANDARD_RIGHTS_ALL);
    assertTrue(Advapi32.INSTANCE.EqualSid(pAceGet.psid, pSid));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ACCESS_ALLOWED_ACE(com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE) Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 2 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Test method testCreateWellKnownSid.

public void testCreateWellKnownSid() {
    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.WinWorldSid, null, pSid, cbSid));
    assertTrue("Not recognized as well-known SID", Advapi32.INSTANCE.IsWellKnownSid(pSid, WELL_KNOWN_SID_TYPE.WinWorldSid));
    assertTrue("Invalid SID size", cbSid.getValue() <= WinNT.SECURITY_MAX_SID_SIZE);
    PointerByReference convertedSidStringPtr = new PointerByReference();
    assertTrue("Failed to convert SID", Advapi32.INSTANCE.ConvertSidToStringSid(pSid, convertedSidStringPtr));
    Pointer conv = convertedSidStringPtr.getValue();
    try {
        String convertedSidString = conv.getWideString(0);
        assertEquals("Mismatched SID string", EVERYONE, convertedSidString);
    } finally {
        Kernel32Util.freeLocalMemory(conv);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 3 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Test method testAddAccessAllowedAce.

public void testAddAccessAllowedAce() throws IOException {
    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 += Advapi32Util.getAceSize(sidLength);
    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));
    PointerByReference pAce = new PointerByReference(new Memory(16));
    assertTrue(Advapi32.INSTANCE.GetAce(pAcl, 0, pAce));
    ACCESS_ALLOWED_ACE pAceGet = new ACCESS_ALLOWED_ACE(pAce.getValue());
    assertTrue(pAceGet.Mask == WinNT.STANDARD_RIGHTS_ALL);
    assertTrue(Advapi32.INSTANCE.EqualSid(pAceGet.psid, pSid));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ACCESS_ALLOWED_ACE(com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE) Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 4 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Test method testReadEncryptedFileRaw.

public void testReadEncryptedFileRaw() throws Exception {
    // create an encrypted file
    File file = createTempFile();
    String lpFileName = file.getAbsolutePath();
    assertTrue("EncryptFile(" + lpFileName + ")", Advapi32.INSTANCE.EncryptFile(lpFileName));
    // open file for export
    ULONG ulFlags = new ULONG(0);
    PointerByReference pvContext = new PointerByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lpFileName, ulFlags, pvContext));
    // read encrypted file
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    FE_EXPORT_FUNC pfExportCallback = new FE_EXPORT_FUNC() {

        @Override
        public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONG ulLength) {
            if (pbData == null) {
                throw new NullPointerException("Callback data unexpectedly missing");
            }
            byte[] arr = pbData.getByteArray(0, ulLength.intValue());
            try {
                outputStream.write(arr);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return new DWORD(W32Errors.ERROR_SUCCESS);
        }
    };
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.ReadEncryptedFileRaw(pfExportCallback, null, pvContext.getValue()));
    outputStream.close();
    Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
    file.delete();
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) PointerByReference(com.sun.jna.ptr.PointerByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) FE_EXPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC) Pointer(com.sun.jna.Pointer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 5 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Test method testMakeAbsoluteSD.

public void testMakeAbsoluteSD() throws Exception {
    SECURITY_DESCRIPTOR absolute = new SECURITY_DESCRIPTOR(64 * 1024);
    // Get a SD in self relative form
    int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
    PointerByReference relativeByReference = new PointerByReference();
    File file = createTempFile();
    try {
        try {
            assertEquals("GetNamedSecurityInfo(" + file + ")", Advapi32.INSTANCE.GetNamedSecurityInfo(file.getAbsolutePath(), AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, null, null, null, null, relativeByReference), 0);
            SECURITY_DESCRIPTOR_RELATIVE relative = new SECURITY_DESCRIPTOR_RELATIVE(relativeByReference.getValue());
            PSID pOwner = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
            PSID pGroup = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
            ACL pDacl = new ACL(ACL.MAX_ACL_SIZE);
            ACL pSacl = new ACL(ACL.MAX_ACL_SIZE);
            IntByReference lpdwBufferLength = new IntByReference(absolute.size());
            IntByReference lpdwDaclSize = new IntByReference(ACL.MAX_ACL_SIZE);
            IntByReference lpdwSaclSize = new IntByReference(ACL.MAX_ACL_SIZE);
            IntByReference lpdwOwnerSize = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
            IntByReference lpdwPrimaryGroupSize = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
            assertTrue(Advapi32.INSTANCE.MakeAbsoluteSD(relative, absolute, lpdwBufferLength, pDacl, lpdwDaclSize, pSacl, lpdwSaclSize, pOwner, lpdwOwnerSize, pGroup, lpdwPrimaryGroupSize));
        } finally {
            file.delete();
        }
    } finally {
        Kernel32Util.freeLocalMemory(relativeByReference.getValue());
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE) SECURITY_DESCRIPTOR(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID) File(java.io.File)

Aggregations

PointerByReference (com.sun.jna.ptr.PointerByReference)127 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)54 IntByReference (com.sun.jna.ptr.IntByReference)35 Pointer (com.sun.jna.Pointer)20 Test (org.junit.Test)19 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)18 REFIID (com.sun.jna.platform.win32.Guid.REFIID)13 File (java.io.File)13 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)10 PSID (com.sun.jna.platform.win32.WinNT.PSID)10 Memory (com.sun.jna.Memory)8 WString (com.sun.jna.WString)8 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)7 ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)7 WinNT (com.sun.jna.platform.win32.WinNT)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 IID (com.sun.jna.platform.win32.Guid.IID)6 ArrayList (java.util.ArrayList)6 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)5 UINT (com.sun.jna.platform.win32.WinDef.UINT)5