Search in sources :

Example 21 with PSID

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

the class Advapi32UtilTest method testConvertSid.

public void testConvertSid() {
    // Everyone
    String sidString = "S-1-1-0";
    byte[] sidBytes = Advapi32Util.convertStringSidToSid(sidString);
    assertTrue(sidBytes.length > 0);
    String convertedSidString = Advapi32Util.convertSidToStringSid(new PSID(sidBytes));
    assertEquals(convertedSidString, sidString);
}
Also used : PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 22 with PSID

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

the class Advapi32Test method testLookupAccountSid.

public void testLookupAccountSid() {
    // get SID bytes
    String sidString = EVERYONE;
    PSIDByReference sid = new PSIDByReference();
    assertTrue("Failed to create sid", Advapi32.INSTANCE.ConvertStringSidToSid(sidString, sid));
    PSID value = sid.getValue();
    try {
        int sidLength = Advapi32.INSTANCE.GetLengthSid(value);
        assertTrue("Non-positive sid length", sidLength > 0);
        // lookup account
        IntByReference cchName = new IntByReference();
        IntByReference cchReferencedDomainName = new IntByReference();
        PointerByReference peUse = new PointerByReference();
        assertFalse(Advapi32.INSTANCE.LookupAccountSid(null, value, null, cchName, null, cchReferencedDomainName, peUse));
        assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
        assertTrue(cchName.getValue() > 0);
        assertTrue(cchReferencedDomainName.getValue() > 0);
        char[] referencedDomainName = new char[cchReferencedDomainName.getValue()];
        char[] name = new char[cchName.getValue()];
        assertTrue(Advapi32.INSTANCE.LookupAccountSid(null, value, name, cchName, referencedDomainName, cchReferencedDomainName, peUse));
        // SidTypeWellKnownGroup
        assertEquals(5, peUse.getPointer().getInt(0));
        String nameString = Native.toString(name);
        String referencedDomainNameString = Native.toString(referencedDomainName);
        assertTrue(nameString.length() > 0);
        if (AbstractWin32TestSupport.isEnglishLocale) {
            assertEquals("Everyone", nameString);
        }
        assertTrue(referencedDomainNameString.length() == 0);
    } finally {
        Kernel32Util.freeLocalMemory(value.getPointer());
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 23 with PSID

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

the class Advapi32Test method testAddAccessAllowedAceEx.

public void testAddAccessAllowedAceEx() 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.AddAccessAllowedAceEx(pAcl, WinNT.ACL_REVISION, WinNT.INHERIT_ONLY_ACE, 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)

Aggregations

PSID (com.sun.jna.platform.win32.WinNT.PSID)23 IntByReference (com.sun.jna.ptr.IntByReference)13 PointerByReference (com.sun.jna.ptr.PointerByReference)10 PSIDByReference (com.sun.jna.platform.win32.WinNT.PSIDByReference)9 ACL (com.sun.jna.platform.win32.WinNT.ACL)8 Memory (com.sun.jna.Memory)6 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)6 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)4 BOOLByReference (com.sun.jna.platform.win32.WinDef.BOOLByReference)3 Pointer (com.sun.jna.Pointer)2 Account (com.sun.jna.platform.win32.Advapi32Util.Account)2 BOOL (com.sun.jna.platform.win32.WinDef.BOOL)1 PACLByReference (com.sun.jna.platform.win32.WinNT.PACLByReference)1 SECURITY_DESCRIPTOR_RELATIVE (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE)1 File (java.io.File)1