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

[Feature Request] Add Sudo Support on Windows #3541

Open
lunatic-gh opened this issue Nov 21, 2024 · 6 comments
Open

[Feature Request] Add Sudo Support on Windows #3541

lunatic-gh opened this issue Nov 21, 2024 · 6 comments

Comments

@lunatic-gh
Copy link

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:
image

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 run my-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: /

@niten94
Copy link
Contributor

niten94 commented Nov 21, 2024

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.

@lunatic-gh
Copy link
Author

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.

@niten94
Copy link
Contributor

niten94 commented Nov 21, 2024

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 echo "new file content" | sudo dd bs=4k of=file.txt, but micro uses functions to write input that sudo passes to dd instead of echo "new file content" |.

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 dd ... in Windows to add support, but I don't know what command can be used.

@JoeKar
Copy link
Collaborator

JoeKar commented Nov 21, 2024

As @niten94 already wrote, it's unfortunately not done by allowing/enabling sudo on Windows, because it would require additionally dd (coreutils) for Windows. The latter one isn't impossible, because they already have been ported (e.g. gnuwin32 coreutils, rust coreutils), but need to be installed too.
Or it requires a fully different approach for storing files with extended privileges like ShellExecute (see here: https://blog.hadenes.io/post/how-to-request-admin-permissions-in-windows-through-uac-with-golang/).

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 sudo support and give hints that additional tools are required.

@lunatic-gh
Copy link
Author

Micro may just use a different command instead of dd ... in Windows to add support, but I don't know what command can be used.

If all that is needed is an alternative native command, can't we just use powershell for that?
I googled a bit, and tested a bit of powershell script in a freshly installed windows-10 vm (only gsudo installed), and could observe the following:
image

Maybe this could be used on windows?

@JoeKar
Copy link
Collaborator

JoeKar commented Nov 21, 2024

Maybe this could be used on windows?

That's like running micro itself with super user rights and using Go's standard functionalities to open and write, but this isn't what we want...at least not in this form. The extended privileges shall only be used for the file save process, not for the rest of micro.
This is the current approach to store the file:

cmd = exec.Command(config.GlobalSettings["sucmd"].(string), "dd", "bs=4k", "of="+name)

Currently I don't know if there is a different alternative other than dd under Windows to use the stdin as input and write to a file.

BTW:

Or it requires a fully different approach for storing files with extended privileges like ShellExecute (see here: https://blog.hadenes.io/post/how-to-request-admin-permissions-in-windows-through-uac-with-golang/).

Currently realized, that we don't even need sudo, because Windows already offers runas, which can be used via the native exec.Command() without the need of ShellExecute. And the runas can be stored into sucmd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants