Search in sources :

Example 1 with USER_INFO_1

use of com.sun.jna.platform.win32.LMAccess.USER_INFO_1 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 2 with USER_INFO_1

use of com.sun.jna.platform.win32.LMAccess.USER_INFO_1 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 3 with USER_INFO_1

use of com.sun.jna.platform.win32.LMAccess.USER_INFO_1 in project jna by java-native-access.

the class Netapi32Test method testNetUserEnum.

public void testNetUserEnum() {
    PointerByReference bufptr = new PointerByReference();
    IntByReference entriesread = new IntByReference();
    IntByReference totalentries = new IntByReference();
    assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserEnum(null, 1, 0, bufptr, LMCons.MAX_PREFERRED_LENGTH, entriesread, totalentries, null));
    USER_INFO_1 userinfo = new USER_INFO_1(bufptr.getValue());
    USER_INFO_1[] userinfos = (USER_INFO_1[]) userinfo.toArray(entriesread.getValue());
    for (USER_INFO_1 ui : userinfos) {
        assertTrue(ui.usri1_name.length() > 0);
    }
    assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue()));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) USER_INFO_1(com.sun.jna.platform.win32.LMAccess.USER_INFO_1)

Example 4 with USER_INFO_1

use of com.sun.jna.platform.win32.LMAccess.USER_INFO_1 in project jna by java-native-access.

the class Netapi32Test method testNetUserChangePassword.

public void testNetUserChangePassword() {
    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(Kernel32Util.getComputerName(), 1, userInfo, null)) {
        return;
    }
    try {
        assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserChangePassword(Kernel32Util.getComputerName(), userInfo.usri1_name.toString(), userInfo.usri1_password.toString(), "!JNAP%%Wrd1"));
    } finally {
        assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserDel(Kernel32Util.getComputerName(), userInfo.usri1_name.toString()));
    }
}
Also used : USER_INFO_1(com.sun.jna.platform.win32.LMAccess.USER_INFO_1)

Example 5 with USER_INFO_1

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

Aggregations

USER_INFO_1 (com.sun.jna.platform.win32.LMAccess.USER_INFO_1)6 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)3 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)3 Account (com.sun.jna.platform.win32.Advapi32Util.Account)2 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)1 IntByReference (com.sun.jna.ptr.IntByReference)1 PointerByReference (com.sun.jna.ptr.PointerByReference)1