Search in sources :

Example 6 with HANDLEByReference

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

the class Advapi32Test method testImpersonateLoggedOnUser.

public void testImpersonateLoggedOnUser() {
    USER_INFO_1 userInfo = new USER_INFO_1();
    userInfo.usri1_name = "JNAAdvapi32TestImp";
    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));
            assertTrue(Advapi32.INSTANCE.ImpersonateLoggedOnUser(phUser.getValue()));
            assertTrue(Advapi32.INSTANCE.RevertToSelf());
        } finally {
            HANDLE hUser = phUser.getValue();
            if (!WinBase.INVALID_HANDLE_VALUE.equals(hUser)) {
                Kernel32Util.closeHandle(hUser);
            }
        }
    } finally {
        assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserDel(null, userInfo.usri1_name.toString()));
    }
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) USER_INFO_1(com.sun.jna.platform.win32.LMAccess.USER_INFO_1) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 7 with HANDLEByReference

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

the class Advapi32Test method testCreateProcessAsUser.

public void testCreateProcessAsUser() {
    HANDLEByReference hToken = new HANDLEByReference();
    HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
    assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, hToken));
    try {
        assertFalse(Advapi32.INSTANCE.CreateProcessAsUser(hToken.getValue(), null, "InvalidCmdLine.jna", null, null, false, 0, null, null, new WinBase.STARTUPINFO(), new WinBase.PROCESS_INFORMATION()));
        assertEquals(W32Errors.ERROR_FILE_NOT_FOUND, Kernel32.INSTANCE.GetLastError());
    } finally {
        Kernel32Util.closeHandleRef(hToken);
    }
}
Also used : PROCESS_INFORMATION(com.sun.jna.platform.win32.WinBase.PROCESS_INFORMATION) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) STARTUPINFO(com.sun.jna.platform.win32.WinBase.STARTUPINFO) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 8 with HANDLEByReference

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

the class Advapi32UtilTest method testGetUserAccount.

public void testGetUserAccount() {
    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));
            Advapi32Util.Account account = Advapi32Util.getTokenAccount(phUser.getValue());
            assertTrue(account.name.length() > 0);
            assertEquals(userInfo.usri1_name.toString(), account.name);
        } finally {
            HANDLE hUser = phUser.getValue();
            if (!WinBase.INVALID_HANDLE_VALUE.equals(hUser)) {
                Kernel32Util.closeHandle(hUser);
            }
        }
    } finally {
        assertEquals(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 9 with HANDLEByReference

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

the class Secur32Test method testQuerySecurityContextToken.

public void testQuerySecurityContextToken() {
    // client ----------- acquire outbound credential handle
    CredHandle phClientCredential = new CredHandle();
    TimeStamp ptsClientExpiry = new TimeStamp();
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.AcquireCredentialsHandle(null, "Negotiate", Sspi.SECPKG_CRED_OUTBOUND, null, null, null, null, phClientCredential, ptsClientExpiry));
    // client ----------- security context
    CtxtHandle phClientContext = new CtxtHandle();
    IntByReference pfClientContextAttr = new IntByReference();
    // server ----------- acquire inbound credential handle
    CredHandle phServerCredential = new CredHandle();
    TimeStamp ptsServerExpiry = new TimeStamp();
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.AcquireCredentialsHandle(null, "Negotiate", Sspi.SECPKG_CRED_INBOUND, null, null, null, null, phServerCredential, ptsServerExpiry));
    // server ----------- security context
    CtxtHandle phServerContext = new CtxtHandle();
    SecBufferDesc pbServerToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE);
    IntByReference pfServerContextAttr = new IntByReference();
    int clientRc = W32Errors.SEC_I_CONTINUE_NEEDED;
    int serverRc = W32Errors.SEC_I_CONTINUE_NEEDED;
    do {
        // client token returned is always new
        SecBufferDesc pbClientToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE);
        // client ----------- initialize security context, produce a client token
        if (clientRc == W32Errors.SEC_I_CONTINUE_NEEDED) {
            // server token is empty the first time
            clientRc = Secur32.INSTANCE.InitializeSecurityContext(phClientCredential, phClientContext.isNull() ? null : phClientContext, Advapi32Util.getUserName(), Sspi.ISC_REQ_CONNECTION, 0, Sspi.SECURITY_NATIVE_DREP, pbServerToken, 0, phClientContext, pbClientToken, pfClientContextAttr, null);
            assertTrue(clientRc == W32Errors.SEC_I_CONTINUE_NEEDED || clientRc == W32Errors.SEC_E_OK);
        }
        // server ----------- accept security context, produce a server token
        if (serverRc == W32Errors.SEC_I_CONTINUE_NEEDED) {
            serverRc = Secur32.INSTANCE.AcceptSecurityContext(phServerCredential, phServerContext.isNull() ? null : phServerContext, pbClientToken, Sspi.ISC_REQ_CONNECTION, Sspi.SECURITY_NATIVE_DREP, phServerContext, pbServerToken, pfServerContextAttr, ptsServerExpiry);
            assertTrue(serverRc == W32Errors.SEC_I_CONTINUE_NEEDED || serverRc == W32Errors.SEC_E_OK);
        }
    } while (serverRc != W32Errors.SEC_E_OK || clientRc != W32Errors.SEC_E_OK);
    // query security context token
    HANDLEByReference phContextToken = new HANDLEByReference();
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.QuerySecurityContextToken(phServerContext, phContextToken));
    // release security context token
    Kernel32Util.closeHandleRef(phContextToken);
    // release server context
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext(phServerContext));
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle(phServerCredential));
    // release client context
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext(phClientContext));
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle(phClientCredential));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) CtxtHandle(com.sun.jna.platform.win32.Sspi.CtxtHandle) CredHandle(com.sun.jna.platform.win32.Sspi.CredHandle) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) TimeStamp(com.sun.jna.platform.win32.Sspi.TimeStamp) SecBufferDesc(com.sun.jna.platform.win32.Sspi.SecBufferDesc)

Example 10 with HANDLEByReference

use of com.sun.jna.platform.win32.WinNT.HANDLEByReference 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

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