-
Notifications
You must be signed in to change notification settings - Fork 66
feat: Add newgrp command (#94) #117
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
Open
Niloyyy
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
Niloyyy:feature/add-newgrp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+237
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [package] | ||
| name = "uu_newgrp" | ||
| version = "0.0.0" | ||
| edition = "2024" | ||
| license = "MIT" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| clap = { version = "4.5", features = ["wrap_help"] } | ||
| uucore = { path = "../coreutils/src/uucore" } | ||
|
|
||
| [dependencies.windows-sys] | ||
| version = "*" | ||
| features = [ | ||
| "Win32_Security", | ||
| "Win32_System_Threading", | ||
| "Win32_Foundation", | ||
| "Win32_System_Environment", | ||
| ] |
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 |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use clap::{Arg, ArgAction, Command}; | ||
| use std::io; | ||
| use std::ptr; | ||
| use uucore::error::{UResult, USimpleError}; | ||
|
|
||
| use windows_sys::Win32::Foundation::{ | ||
| CloseHandle, GetLastError, ERROR_INSUFFICIENT_BUFFER, HANDLE, | ||
| }; | ||
| use windows_sys::Win32::Security::{ | ||
| DuplicateTokenEx, LookupAccountNameW, SecurityImpersonation, SetTokenInformation, TokenPrimary, | ||
| TokenPrimaryGroup, TOKEN_ADJUST_DEFAULT, TOKEN_ASSIGN_PRIMARY, TOKEN_DUPLICATE, | ||
| TOKEN_PRIMARY_GROUP, TOKEN_QUERY, | ||
| }; | ||
| use windows_sys::Win32::System::Threading::{ | ||
| CreateProcessAsUserW, GetCurrentProcess, GetExitCodeProcess, OpenProcessToken, | ||
| WaitForSingleObject, INFINITE, PROCESS_INFORMATION, STARTUPINFOW, | ||
| }; | ||
|
|
||
| pub fn uu_app() -> Command { | ||
| Command::new("newgrp") | ||
| .about("Log in to a new group") | ||
| .arg( | ||
| Arg::new("group") | ||
| .help("The group to log into") | ||
| .index(1) | ||
| .required(false), | ||
| ) | ||
| .arg( | ||
| Arg::new("command") | ||
| .short('c') | ||
| .long("command") | ||
| .help("Command to execute") | ||
| .action(ArgAction::Set), | ||
| ) | ||
| } | ||
|
|
||
| #[uucore::main(no_signals)] | ||
| pub fn uumain(args: impl uucore::Args) -> UResult<()> { | ||
| let matches = uu_app().get_matches_from(args); | ||
| let group = matches.get_one::<String>("group"); | ||
| let command = matches.get_one::<String>("command"); | ||
|
|
||
| let mut token: HANDLE = std::ptr::null_mut(); | ||
| unsafe { | ||
| if OpenProcessToken( | ||
| GetCurrentProcess(), | ||
| TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY | TOKEN_ADJUST_DEFAULT, | ||
| &mut token, | ||
| ) == 0 | ||
| { | ||
| return Err(USimpleError::new( | ||
| 1, | ||
| format!( | ||
| "OpenProcessToken failed: {}", | ||
| io::Error::from_raw_os_error(GetLastError() as i32) | ||
| ), | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| let mut new_token: HANDLE = std::ptr::null_mut(); | ||
| unsafe { | ||
| if DuplicateTokenEx( | ||
| token, | ||
| TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY | TOKEN_ADJUST_DEFAULT, | ||
| ptr::null(), | ||
| SecurityImpersonation, | ||
| TokenPrimary, | ||
| &mut new_token, | ||
| ) == 0 | ||
| { | ||
| CloseHandle(token); | ||
| return Err(USimpleError::new( | ||
| 1, | ||
| format!( | ||
| "DuplicateTokenEx failed: {}", | ||
| io::Error::from_raw_os_error(GetLastError() as i32) | ||
| ), | ||
| )); | ||
| } | ||
| CloseHandle(token); | ||
| } | ||
|
|
||
| if let Some(g) = group { | ||
| if let Err(e) = change_primary_group(new_token, g) { | ||
| unsafe { | ||
| CloseHandle(new_token); | ||
| } | ||
| return Err(USimpleError::new( | ||
| 1, | ||
| format!("failed to change primary group: {}", e), | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| // Spawn the shell or command | ||
| let cmd = if let Some(c) = command { | ||
| format!("cmd.exe /c {}", c) | ||
| } else { | ||
| "cmd.exe".to_string() | ||
| }; | ||
|
|
||
| let mut cmd_w: Vec<u16> = cmd.encode_utf16().chain(std::iter::once(0)).collect(); | ||
|
|
||
| unsafe { | ||
| let mut startup_info: STARTUPINFOW = std::mem::zeroed(); | ||
| startup_info.cb = std::mem::size_of::<STARTUPINFOW>() as u32; | ||
| let mut process_info: PROCESS_INFORMATION = std::mem::zeroed(); | ||
|
|
||
| if CreateProcessAsUserW( | ||
| new_token, | ||
| ptr::null(), | ||
| cmd_w.as_mut_ptr(), | ||
| ptr::null(), | ||
| ptr::null(), | ||
| 0, | ||
| 0, | ||
| ptr::null(), | ||
| ptr::null(), | ||
| &startup_info, | ||
| &mut process_info, | ||
| ) == 0 | ||
| { | ||
| let err = GetLastError(); | ||
| CloseHandle(new_token); | ||
| return Err(USimpleError::new( | ||
| 1, | ||
| format!( | ||
| "CreateProcessAsUserW failed: {}", | ||
| io::Error::from_raw_os_error(err as i32) | ||
| ), | ||
| )); | ||
| } | ||
|
|
||
| CloseHandle(new_token); | ||
| CloseHandle(process_info.hThread); | ||
|
|
||
| WaitForSingleObject(process_info.hProcess, INFINITE); | ||
|
|
||
| let mut exit_code = 0; | ||
| GetExitCodeProcess(process_info.hProcess, &mut exit_code); | ||
| CloseHandle(process_info.hProcess); | ||
|
|
||
| if exit_code != 0 { | ||
| std::process::exit(exit_code as i32); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn change_primary_group(token: HANDLE, group: &str) -> io::Result<()> { | ||
| unsafe { | ||
| let group_w: Vec<u16> = group.encode_utf16().chain(std::iter::once(0)).collect(); | ||
| let mut sid_size = 0; | ||
| let mut domain_size = 0; | ||
| let mut pe_use = 0; | ||
|
|
||
| // First call to get required sizes | ||
| LookupAccountNameW( | ||
| ptr::null(), | ||
| group_w.as_ptr(), | ||
| ptr::null_mut(), | ||
| &mut sid_size, | ||
| ptr::null_mut(), | ||
| &mut domain_size, | ||
| &mut pe_use, | ||
| ); | ||
|
|
||
| if GetLastError() != ERROR_INSUFFICIENT_BUFFER { | ||
| return Err(io::Error::from_raw_os_error(GetLastError() as i32)); | ||
| } | ||
|
|
||
| let mut sid = vec![0u8; sid_size as usize]; | ||
| let mut domain = vec![0u16; domain_size as usize]; | ||
|
|
||
| if LookupAccountNameW( | ||
| ptr::null(), | ||
| group_w.as_ptr(), | ||
| sid.as_mut_ptr() as _, | ||
| &mut sid_size, | ||
| domain.as_mut_ptr(), | ||
| &mut domain_size, | ||
| &mut pe_use, | ||
| ) == 0 { | ||
| return Err(io::Error::from_raw_os_error(GetLastError() as i32)); | ||
| } | ||
|
|
||
| let mut token_group = TOKEN_PRIMARY_GROUP { | ||
| PrimaryGroup: sid.as_mut_ptr() as _, | ||
| }; | ||
|
|
||
| if SetTokenInformation( | ||
| token, | ||
| TokenPrimaryGroup, | ||
| &mut token_group as *mut _ as *const _, | ||
| std::mem::size_of::<TOKEN_PRIMARY_GROUP>() as u32, | ||
| ) == 0 { | ||
| return Err(io::Error::from_raw_os_error(GetLastError() as i32)); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
-c/--commandarg is missing.