Search in sources :

Example 26 with HANDLEByReference

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

the class Advapi32Test method testSetThreadTokenThisThread.

public void testSetThreadTokenThisThread() {
    HANDLEByReference phToken = new HANDLEByReference();
    HANDLEByReference phTokenDup = new HANDLEByReference();
    try {
        HANDLEByReference pthreadHandle = new HANDLEByReference();
        pthreadHandle.setValue(Kernel32.INSTANCE.GetCurrentThread());
        // See if thread has a token. If not, must duplicate process token and set thread token using that.
        if (!Advapi32.INSTANCE.OpenThreadToken(pthreadHandle.getValue(), WinNT.TOKEN_IMPERSONATE | WinNT.TOKEN_QUERY, false, phToken)) {
            assertEquals(W32Errors.ERROR_NO_TOKEN, Kernel32.INSTANCE.GetLastError());
            HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
            assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE, phToken));
            assertTrue(Advapi32.INSTANCE.DuplicateTokenEx(phToken.getValue(), WinNT.TOKEN_IMPERSONATE, null, WinNT.SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, WinNT.TOKEN_TYPE.TokenImpersonation, phTokenDup));
            // Use HANDLEByReference on this thread to test, should be good enough for API compatibility.
            assertTrue(Advapi32.INSTANCE.SetThreadToken(pthreadHandle, phTokenDup.getValue()));
        } else {
            // Use HANDLEByReference on this thread to test, should be good enough for API compatibility.
            assertTrue(Advapi32.INSTANCE.SetThreadToken(pthreadHandle, phToken.getValue()));
        }
        // Revert and cleanup
        assertTrue(Advapi32.INSTANCE.SetThreadToken(null, null));
    } finally {
        Kernel32Util.closeHandleRefs(phToken, phTokenDup);
    }
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 27 with HANDLEByReference

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

the class Advapi32UtilTest method testGetUserGroups.

public void testGetUserGroups() {
    USER_INFO_1 userInfo = new USER_INFO_1();
    userInfo.usri1_name = "JNANetapi32TestUser";
    userInfo.usri1_password = "!JNAP$$Wrd0";
    userInfo.usri1_priv = LMAccess.USER_PRIV_USER;
    // ignore test if not able to add user (need to be administrator to do this).
    if (LMErr.NERR_Success != Netapi32.INSTANCE.NetUserAdd(null, 1, userInfo, null)) {
        return;
    }
    try {
        HANDLEByReference phUser = new HANDLEByReference();
        try {
            assertTrue(Advapi32.INSTANCE.LogonUser(userInfo.usri1_name.toString(), null, userInfo.usri1_password.toString(), WinBase.LOGON32_LOGON_NETWORK, WinBase.LOGON32_PROVIDER_DEFAULT, phUser));
            Account[] groups = Advapi32Util.getTokenGroups(phUser.getValue());
            assertTrue(groups.length > 0);
            for (Account group : groups) {
                assertTrue(group.name.length() > 0);
                assertTrue(group.sidString.length() > 0);
                assertTrue(group.sid.length > 0);
            }
        } finally {
            HANDLE hUser = phUser.getValue();
            if (!WinBase.INVALID_HANDLE_VALUE.equals(hUser)) {
                Kernel32Util.closeHandle(hUser);
            }
        }
    } finally {
        assertEquals("Error in NetUserDel", LMErr.NERR_Success, Netapi32.INSTANCE.NetUserDel(null, userInfo.usri1_name.toString()));
    }
}
Also used : Account(com.sun.jna.platform.win32.Advapi32Util.Account) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) USER_INFO_1(com.sun.jna.platform.win32.LMAccess.USER_INFO_1) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 28 with HANDLEByReference

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

the class Advapi32Test method testGetSetSecurityInfoForFileWithSACL.

public void testGetSetSecurityInfoForFileWithSACL() throws Exception {
    boolean impersontating = false;
    HANDLEByReference phToken = new HANDLEByReference();
    HANDLEByReference phTokenDuplicate = new HANDLEByReference();
    try {
        // open thread or process token, elevate
        if (!Advapi32.INSTANCE.OpenThreadToken(Kernel32.INSTANCE.GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, false, phToken)) {
            assertEquals(W32Errors.ERROR_NO_TOKEN, Kernel32.INSTANCE.GetLastError());
            // OpenThreadToken may fail with W32Errors.ERROR_NO_TOKEN if current thread is anonymous.  When this happens,
            // we need to open the process token to duplicate it, then set our thread token.
            assertTrue(Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), TOKEN_DUPLICATE, phToken));
            // Process token opened, now duplicate
            assertTrue(Advapi32.INSTANCE.DuplicateTokenEx(phToken.getValue(), TOKEN_ADJUST_PRIVILEGES | TOKEN_IMPERSONATE, null, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenImpersonation, phTokenDuplicate));
            // And set thread token.
            assertTrue(Advapi32.INSTANCE.SetThreadToken(null, phTokenDuplicate.getValue()));
            impersontating = true;
        }
        // Which token to adjust depends on whether we had to impersonate or not.
        HANDLE tokenAdjust = impersontating ? phTokenDuplicate.getValue() : phToken.getValue();
        WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(1);
        WinNT.LUID pLuid = new WinNT.LUID();
        assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_SECURITY_NAME, pLuid));
        tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
        assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
        assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_RESTORE_NAME, pLuid));
        tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
        assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
        // create a temp file
        File file = createTempFile();
        int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION;
        PointerByReference ppsidOwner = new PointerByReference();
        PointerByReference ppsidGroup = new PointerByReference();
        PointerByReference ppDacl = new PointerByReference();
        PointerByReference ppSacl = new PointerByReference();
        PointerByReference ppSecurityDescriptor = new PointerByReference();
        String filePath = file.getAbsolutePath();
        HANDLE hFile = WinBase.INVALID_HANDLE_VALUE;
        try {
            try {
                hFile = Kernel32.INSTANCE.CreateFile(filePath, WinNT.ACCESS_SYSTEM_SECURITY | WinNT.GENERIC_WRITE | WinNT.WRITE_OWNER | WinNT.WRITE_DAC, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
                assertEquals("GetSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.GetSecurityInfo(hFile, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor));
                assertEquals("SetSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.SetSecurityInfo(hFile, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner.getValue(), ppsidGroup.getValue(), ppDacl.getValue(), ppSacl.getValue()));
            } finally {
                if (hFile != WinBase.INVALID_HANDLE_VALUE)
                    Kernel32.INSTANCE.CloseHandle(hFile);
                file.delete();
            }
        } finally {
            Kernel32Util.freeLocalMemory(ppSecurityDescriptor.getValue());
        }
        if (impersontating) {
            assertTrue("SetThreadToken", Advapi32.INSTANCE.SetThreadToken(null, null));
        } else {
            tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(0));
            assertTrue("AdjustTokenPrivileges", Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
        }
    } finally {
        Kernel32Util.closeHandleRefs(phToken, phTokenDuplicate);
    }
}
Also used : TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) PointerByReference(com.sun.jna.ptr.PointerByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) File(java.io.File)

Example 29 with HANDLEByReference

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

the class MprTest method testWNetOpenCloseConnection.

public void testWNetOpenCloseConnection() throws Exception {
    HANDLEByReference lphEnum = new HANDLEByReference();
    assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetOpenEnum(RESOURCESCOPE.RESOURCE_CONNECTED, RESOURCETYPE.RESOURCETYPE_DISK, RESOURCEUSAGE.RESOURCEUSAGE_ALL, null, lphEnum));
    assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetCloseEnum(lphEnum.getValue()));
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Example 30 with HANDLEByReference

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

the class Advapi32Test method testGetTokenUserInformation.

public void testGetTokenUserInformation() {
    HANDLEByReference phToken = new HANDLEByReference();
    try {
        HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
        assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, phToken));
        IntByReference tokenInformationLength = new IntByReference();
        assertFalse(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenUser, null, 0, tokenInformationLength));
        assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
        WinNT.TOKEN_USER user = new WinNT.TOKEN_USER(tokenInformationLength.getValue());
        assertTrue(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenUser, user, tokenInformationLength.getValue(), tokenInformationLength));
        assertTrue(tokenInformationLength.getValue() > 0);
        assertTrue(Advapi32.INSTANCE.IsValidSid(user.User.Sid));
        int sidLength = Advapi32.INSTANCE.GetLengthSid(user.User.Sid);
        assertTrue(sidLength > 0);
        assertTrue(sidLength < tokenInformationLength.getValue());
    // System.out.println(Advapi32Util.convertSidToStringSid(user.User.Sid));
    } finally {
        Kernel32Util.closeHandleRef(phToken);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Aggregations

HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)22 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)15 IntByReference (com.sun.jna.ptr.IntByReference)11 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)7 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 File (java.io.File)5 USER_INFO_1 (com.sun.jna.platform.win32.LMAccess.USER_INFO_1)3 BOOLByReference (com.sun.jna.platform.win32.WinDef.BOOLByReference)3 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)3 PointerByReference (com.sun.jna.ptr.PointerByReference)3 Memory (com.sun.jna.Memory)2 Account (com.sun.jna.platform.win32.Advapi32Util.Account)2 PDH_COUNTER_PATH_ELEMENTS (com.sun.jna.platform.win32.Pdh.PDH_COUNTER_PATH_ELEMENTS)2 PDH_RAW_COUNTER (com.sun.jna.platform.win32.Pdh.PDH_RAW_COUNTER)2 RASCREDENTIALS (com.sun.jna.platform.win32.WinRas.RASCREDENTIALS)2 RASDIALPARAMS (com.sun.jna.platform.win32.WinRas.RASDIALPARAMS)2 Test (org.junit.Test)2 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)1 CtxtHandle (com.sun.jna.platform.win32.Sspi.CtxtHandle)1