Skip to content

Commit

Permalink
Add Advapi32Util#isCurrentProcessElevated and associated types (#1471)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <widdis@gmail.com>

Signed-off-by: Daniel Widdis <widdis@gmail.com>
  • Loading branch information
dbwiddis committed Oct 8, 2022
1 parent 88401ae commit a9c8621
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
}
Original file line number Diff line number Diff line change
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

0 comments on commit a9c8621

Please sign in to comment.