-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[Feature Request] Add Sudo Support on Windows #3541
Comments
I am not available to look much into adding support, but I tried checking what had to be specifically done to add support months ago. Micro runs dd when saving files using sudo but dd is not preinstalled. I wrote about it in a comment in a different issue: #2660 (comment) Edit: I was thinking about running a PowerShell command instead of dd sometime after I posted the comment, but I have not checked anything about it. |
What exactly is micro internally using on windows to save files? Couldn't it just do the exact same it is already doing, but instead with this sudo/gsudo command as a prefix? Sadly i don't know anything about the go language or windows internals itself, otherwise i'd gladly help however i can. |
Micro writes to files using Go standard library functions in all platforms when saving without sudo, so the same thing cannot be done as a command using sudo. The actual command run with sudo in all platforms except Windows is like dd is a program used like a command that is usually preinstalled in Linux distributions. It's not available by default in Windows so it may be inconvenient if micro uses dd in Windows. Micro may just use a different command instead of |
As @niten94 already wrote, it's unfortunately not done by allowing/enabling The easiest way for us would be option one, since the mechanism doesn't need to be rewritten or extended and we just need to enable the |
That's like running Line 37 in 56c1f75
Currently I don't know if there is a different alternative other than BTW:
Currently realized, that we don't even need |
in this case, my basic "idea" wasn't to run the entirety of micro as sudo, but rather something like cmd = exec.Command(config.GlobalSettings["sucmd"].(string), "powershell -command", "& {...the-command...}") (assuming that syntax is correct) But if the runas method is more convenient that is probably better than this awfully long powershell-script. |
But the PowerShell isn't available by default either or am I wrong? |
both windows 10 and 11 come with powershell 5.1 pre-installed. it should therefore be available on every pc that isn't stuck 200 years ago. I think it might even be available out of the box since windows 7, but i am not 100% sure on that. the screenshot i gave above was indeed done on a vm with windows 10, where i really only installed gsudo (well and updated the app installer through ms-store, so i can actually install gsudo) |
Ok, if this is the case then...
...could do the trick, right? |
That command does not seem to work from what i can see. Dunno what the Does the 4k block size matter? If not, the simplest way would probably be the command: powershell -command "string-content" | Out-File -FilePath "path/to/file" or in go: cmd = exec.Command(config.GlobalSettings["sucmd"].(string), "powershell", "-command", content-string, "| Out-File -FilePath", name) If i got the syntax right. This would create the given file (if it does not exist), and afterwards write the given content to it. |
I think @JoeKar included Lines 39 to 64 in 56c1f75
I do not know much about .NET and PowerShell but I tried reading documentation and I do not think cmdlets can be used. The output encoding is handled in line 63 but encoding seems to be converted when using pipes and I did not check anything with error handling but I think this can be done. The path would be passed as a different argument like using namespace System.IO
$f = [File]::Open($args[0],
[FileMode]::OpenOrCreate -bor [FileMode]::Truncate, [FileAccess]::Write)
$i = [System.Console]::OpenStandardInput()
$i.CopyTo($f)
$i.Dispose()
$f.Dispose() Footnotes |
Yep, the AI fooled me, since "-" can't be used as placeholder for the Maybe then something like this: But to be honest, I never used PS before. |
Honestly, i think i totally lack the options to test something like this. I have no idea what If someone else with go & ps knowledge and the ability to build micro could test this all, it would probably be way easier than me trying to fiddle my way into this without knowing anything about... anything really. |
It has nothing to do with |
Sorry, I think my previous comment had unnecessary information and was confusing. I have Windows 10 installed on my personal machine so I can build micro and test, but replacing
I do not think there is anything else available on Windows that can be used by default. I tried to somewhat search and learn about .NET and PowerShell, but I do not think there is a short command that can be used. I used I have only tested the code with trap { break }
$f = [IO.File]::Open($env:OUTFILE, [IO.FileMode]::OpenOrCreate, [IO.FileAccess]::Write)
$i = [Console]::OpenStandardInput()
$i.CopyTo($f)
$i.Dispose()
$f.SetLength($f.Position)
$f.Dispose() trap { break }
$i = [Console]::OpenStandardInput()
& {
$b = [byte[]]::new(4096)
while(($c = $i.Read($b, 0, $b.Length)) -gt 0) {
[ArraySegment[byte]]::new($b, 0, $c)
}
} | Set-Content -Encoding byte -LiteralPath $env:OUTFILE
$i.Dispose() trap { break }
$i = [Console]::OpenStandardInput()
$ms = [IO.MemoryStream]::new()
$i.CopyTo($ms)
Set-Content -Encoding byte -Value $ms.ToArray() -LiteralPath $env:OUTFILE
$ms.Dispose()
$i.Dispose() I have no experience with measuring performance so something wrong is done, but these results came out when I ran this script using busybox-w32: set -u -e
if [ $# -ne 2 ]; then
prog=`basename -- "$0"`
printf '%s\n' "usage: $prog dest src" >&2
exit 1
fi
d=`dirname -- "$0"`
export OUTFILE="$1"
for s in "$d"/*.ps1; do
printf '%s\n' "${s#$d/}"
cmd=`cat "$s"`
time powershell -command "$cmd" < "$2"
cmp -- "$2" "$1"
read _
done
|
I realized this when testing but I think additional parameters have to provided when using |
I've the same feeling, since the |
Sorry, I am probably the one who has searched the most in the thread, but I did not update on details I have found and what I was going to do. I think I should have at least said something even if short but I tried creating a pull request and adding support. |
I do not know why I just realized this now, but is there any reason to not run micro itself with sudo and pass an internal flag like this? sucmd := config.GlobalSettings["sucmd"].(string)
if runtime.GOOS == "windows" {
exe, err := os.Executable()
if err == nil {
cmd = exec.Command(sucmd, exe, "-write", name)
} else {
return err
}
} Using Edit: Sorry for the noise. I tried reading the thread again but I do not think using a PowerShell script would go well or be accepted, so I went ahead and changed the pull request to just running micro itself with an internal flag on Windows. I think there was a bug when I was testing with gsudo so I do not think |
Description of the problem or steps to reproduce
Since Sudo wasn't really a thing on Windows, micro has no support for this, and will happily tell this to the user when trying to use:
However, since 24H2, Windows added sudo as an option. It behaves pretty similar, eg by running
sudo <my-command>
it would show a UAC prompt, and on success runmy-command
in an administrator instance of that commandline.Additionally, even if 24H2 is not being used, GSudo is basically the exact same.
It would be a great addition to support sudo when either 24H2's Sudo or Gsudo is present.
Specifications
Commit hash: /
OS: Windows 11
Terminal: /
The text was updated successfully, but these errors were encountered: