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()));
}
}
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);
}
}
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()));
}
}
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));
}
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;
}
}
}
Aggregations