Search in sources :

Example 6 with UserInfo

use of com.sun.jna.platform.win32.Netapi32Util.UserInfo in project jna by java-native-access.

the class Netapi32Util method getUserInfo.

public static UserInfo getUserInfo(String accountName, String domainName) {
    PointerByReference bufptr = new PointerByReference();
    int rc = -1;
    try {
        rc = Netapi32.INSTANCE.NetUserGetInfo(domainName, accountName, (short) 23, bufptr);
        if (rc == LMErr.NERR_Success) {
            USER_INFO_23 info_23 = new USER_INFO_23(bufptr.getValue());
            UserInfo userInfo = new UserInfo();
            if (info_23.usri23_comment != null) {
                userInfo.comment = info_23.usri23_comment.toString();
            }
            userInfo.flags = info_23.usri23_flags;
            if (info_23.usri23_full_name != null) {
                userInfo.fullName = info_23.usri23_full_name.toString();
            }
            if (info_23.usri23_name != null) {
                userInfo.name = info_23.usri23_name.toString();
            }
            if (info_23.usri23_user_sid != null) {
                userInfo.sidString = Advapi32Util.convertSidToStringSid(info_23.usri23_user_sid);
            }
            userInfo.sid = info_23.usri23_user_sid;
            return userInfo;
        } else {
            throw new Win32Exception(rc);
        }
    } finally {
        if (bufptr.getValue() != Pointer.NULL) {
            Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue());
        }
    }
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) USER_INFO_23(com.sun.jna.platform.win32.LMAccess.USER_INFO_23)

Example 7 with UserInfo

use of com.sun.jna.platform.win32.Netapi32Util.UserInfo 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 8 with UserInfo

use of com.sun.jna.platform.win32.Netapi32Util.UserInfo in project jna by java-native-access.

the class Netapi32Test method testNetUserAdd.

public void testNetUserAdd() {
    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;
    }
    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)

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 PointerByReference (com.sun.jna.ptr.PointerByReference)2 USER_INFO_23 (com.sun.jna.platform.win32.LMAccess.USER_INFO_23)1 UserInfo (com.sun.jna.platform.win32.Netapi32Util.UserInfo)1 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)1 IntByReference (com.sun.jna.ptr.IntByReference)1