Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Advapi32Util#isCurrentProcessElevated and associated types #1471

Merged
merged 1 commit into from Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -9,6 +9,7 @@ Features
--------
* [#1454](https://github.com/java-native-access/jna/pull/1454): Add `c.s.j.p.win32.Psapi.QueryWorkingSetEx` and associated Types - [@crain-32](https://github.com/Crain-32).
* [#1459](https://github.com/java-native-access/jna/pull/1459): Add `VirtualLock` and `VirtualUnlock` in `c.s.j.p.win32.Kernel32` - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1471](https://github.com/java-native-access/jna/pull/1471): Add `c.s.j.p.win32.Advapi32Util#isCurrentProcessElevated` and associated Types - [@dbwiddis](https://github.com/dbwiddis).

Bug Fixes
---------
Expand Down
24 changes: 24 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java
Expand Up @@ -86,6 +86,7 @@
import com.sun.jna.platform.win32.WinNT.SECURITY_IMPERSONATION_LEVEL;
import com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES;
import com.sun.jna.platform.win32.WinNT.SID_NAME_USE;
import com.sun.jna.platform.win32.WinNT.TOKEN_ELEVATION;
import com.sun.jna.platform.win32.WinNT.TOKEN_TYPE;
import com.sun.jna.platform.win32.WinReg.HKEY;
import com.sun.jna.platform.win32.WinReg.HKEYByReference;
Expand Down Expand Up @@ -3414,4 +3415,27 @@ private HANDLE getThreadToken() throws Win32Exception {
return phThreadToken.getValue();
}
}

/**
* Reports whether the current process is running with elevated permissions.
*
* @return true if the current process has elevated perissions, false otherwise.
*/
public static boolean isCurrentProcessElevated() {
HANDLEByReference hToken = new HANDLEByReference();
IntByReference returnLength = new IntByReference();
if (Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), WinNT.TOKEN_QUERY,
hToken)) {
try {
TOKEN_ELEVATION elevation = new TOKEN_ELEVATION();
if (Advapi32.INSTANCE.GetTokenInformation(hToken.getValue(),
WinNT.TOKEN_INFORMATION_CLASS.TokenElevation, elevation, elevation.size(), returnLength)) {
return elevation.TokenIsElevated > 0;
}
} finally {
Kernel32.INSTANCE.CloseHandle(hToken.getValue());
}
}
return false;
}
}
8 changes: 8 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/WinNT.java
Expand Up @@ -4415,4 +4415,12 @@ public IO_COUNTERS(Pointer memory) {

public int EVENT_MODIFY_STATE = 0x0002;
public int EVENT_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3);

/**
* The TOKEN_ELEVATION structure indicates whether a token has elevated privileges.
*/
@FieldOrder({ "TokenIsElevated" })
class TOKEN_ELEVATION extends Structure {
public int TokenIsElevated;
}
}
Expand Up @@ -915,6 +915,18 @@ public void testPrivilege() {
}
}

/**
* Test TOKEN_ELEVATION structure
*/
public void testIsCurrentProcessElevated() {
// This is either true if we're elevated or false otherwise. Just exercising the function.
try {
Advapi32Util.isCurrentProcessElevated();
} catch (Exception ex) {
fail("Encountered unknown exception - " + ex.getMessage());
}
}

private File createTempFile() throws Exception{
String filePath = System.getProperty("java.io.tmpdir") + System.nanoTime()
+ ".text";
Expand Down