-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
stdlib/os: add isAdmin #17012
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
Merged
Merged
stdlib/os: add isAdmin #17012
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4b0a550
stdlib/os: add isAdmin
rominf 244eb6e
uint8 -> cuchar, assert isAdmin on Azure
rominf 915cc4f
Update lib/pure/os.nim docs
rominf 2d82330
Address comments on #17012
rominf 6f32fd4
Raise on errors in #17012
rominf ecf54d1
Check the result of FreeSid in #17012
rominf 789afed
Change case in #17012
rominf 2eb6ca4
Fix memory leak in #17012
rominf 24f75eb
Address comments in #17012
rominf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ when useWinUnicode: | |
else: | ||
type WinChar* = char | ||
|
||
# See https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types | ||
type | ||
Handle* = int | ||
LONG* = int32 | ||
|
@@ -33,13 +34,15 @@ type | |
WINBOOL* = int32 | ||
## `WINBOOL` uses opposite convention as posix, !=0 meaning success. | ||
# xxx this should be distinct int32, distinct would make code less error prone | ||
PBOOL* = ptr WINBOOL | ||
rominf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
DWORD* = int32 | ||
PDWORD* = ptr DWORD | ||
LPINT* = ptr int32 | ||
ULONG_PTR* = uint | ||
PULONG_PTR* = ptr uint | ||
HDC* = Handle | ||
HGLRC* = Handle | ||
BYTE* = cuchar | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok for now, but we should address nim-lang/RFCs#344 before next release and move it to |
||
|
||
SECURITY_ATTRIBUTES* {.final, pure.} = object | ||
nLength*: int32 | ||
|
@@ -136,6 +139,10 @@ const | |
|
||
HANDLE_FLAG_INHERIT* = 0x00000001'i32 | ||
|
||
proc isSuccess*(a: WINBOOL): bool {.inline.} = | ||
## Returns true if `a != 0`. Windows uses a different convention than POSIX, | ||
## where `a == 0` is commonly used on success. | ||
a != 0 | ||
proc getVersionExW*(lpVersionInfo: ptr OSVERSIONINFO): WINBOOL {. | ||
stdcall, dynlib: "kernel32", importc: "GetVersionExW", sideEffect.} | ||
proc getVersionExA*(lpVersionInfo: ptr OSVERSIONINFO): WINBOOL {. | ||
|
@@ -1129,5 +1136,42 @@ proc setFileTime*(hFile: Handle, lpCreationTime: LPFILETIME, | |
lpLastAccessTime: LPFILETIME, lpLastWriteTime: LPFILETIME): WINBOOL | ||
{.stdcall, dynlib: "kernel32", importc: "SetFileTime".} | ||
|
||
type | ||
# https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-sid_identifier_authority | ||
SID_IDENTIFIER_AUTHORITY* {.importc, header: "<windows.h>".} = object | ||
value* {.importc: "Value"}: array[6, BYTE] | ||
# https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-sid | ||
SID* {.importc, header: "<windows.h>".} = object | ||
Revision: BYTE | ||
rominf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SubAuthorityCount: BYTE | ||
IdentifierAuthority: SID_IDENTIFIER_AUTHORITY | ||
SubAuthority: ptr ptr DWORD | ||
PSID* = ptr SID | ||
|
||
const | ||
# https://docs.microsoft.com/en-us/windows/win32/secauthz/sid-components | ||
# https://github.com/mirror/mingw-w64/blob/84c950bdab7c999ace49fe8383856be77f88c4a8/mingw-w64-headers/include/winnt.h#L2994 | ||
SECURITY_NT_AUTHORITY* = [BYTE(0), BYTE(0), BYTE(0), BYTE(0), BYTE(0), BYTE(5)] | ||
SECURITY_BUILTIN_DOMAIN_RID* = 32 | ||
DOMAIN_ALIAS_RID_ADMINS* = 544 | ||
|
||
proc allocateAndInitializeSid*(pIdentifierAuthority: ptr SID_IDENTIFIER_AUTHORITY, | ||
nSubAuthorityCount: BYTE, | ||
nSubAuthority0: DWORD, | ||
nSubAuthority1: DWORD, | ||
nSubAuthority2: DWORD, | ||
nSubAuthority3: DWORD, | ||
nSubAuthority4: DWORD, | ||
nSubAuthority5: DWORD, | ||
nSubAuthority6: DWORD, | ||
nSubAuthority7: DWORD, | ||
pSid: ptr PSID): WINBOOL | ||
{.stdcall, dynlib: "Advapi32", importc: "AllocateAndInitializeSid".} | ||
proc checkTokenMembership*(tokenHandle: Handle, sidToCheck: PSID, | ||
isMember: PBOOL): WINBOOL | ||
{.stdcall, dynlib: "Advapi32", importc: "CheckTokenMembership".} | ||
proc freeSid*(pSid: PSID): PSID | ||
{.stdcall, dynlib: "Advapi32", importc: "FreeSid".} | ||
|
||
when defined(nimHasStyleChecks): | ||
{.pop.} # {.push styleChecks: off.} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.