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

Check if app is run by sudo/admin on Linux #25118

Closed
Petermarcu opened this issue Feb 21, 2018 · 6 comments
Closed

Check if app is run by sudo/admin on Linux #25118

Petermarcu opened this issue Feb 21, 2018 · 6 comments
Labels
area-System.Runtime os-linux Linux OS (any supported distro)
Milestone

Comments

@Petermarcu
Copy link
Member

@maroallegro commented on Mon Jan 29 2018

I am trying to check if .NetCore code is running as sudo/admin on Linux. It works on Windows but throws exception on Linux.

How do I check if app is run by admin/sudo on Ubuntu Linux by using .NET Core2.0 build-in class?

Here is the code I have tried (however i thought that it will not manage admin rights on Linux):

using System;
using System.Security.Principal;

namespace smallTestsCore
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Program.IsAdministrator);
            Console.ReadLine();
        }
        public static bool IsAdministrator =>
            new WindowsPrincipal(WindowsIdentity.GetCurrent())
                .IsInRole(WindowsBuiltInRole.Administrator);
    }
}

The code works on Windows, but does not work on Linux:

Exception has occurred: CLR/System.PlatformNotSupportedException
An unhandled exception of type 'System.PlatformNotSupportedException' occurred in
System.Security.Principal.Windows.dll: 'Windows Principal functionality is not supported on this platform.'
  at System.Security.Principal.WindowsIdentity.GetCurrent()
   at adminTst.Program.get_IsAdministrator() in /home/user/adminTst/Program.cs:line 15
   at adminTst.Program.Main(String[] args) in /home/user/adminTst/Program.cs:line 11
@danmoseley
Copy link
Member

@danmoseley
Copy link
Member

You will want to decide whether there should be a generic API, or Unix specific, ie., are sudo and the Windows concept of elevated sufficiently common concepts.

@wfurt
Copy link
Member

wfurt commented Feb 21, 2018

one way would be to check effective rights. (SystemNative_GetEUid) However things may be more complicate with POSIX capabilities. With work for dotnet/corefx#26431 we may have enough plumbing to process groups as well.

@danmoseley
Copy link
Member

@eerhardt @wfurt are the concepts of Unix sudo and Windows elevation sufficiently analogous that we could have some single boolean property for both? My guess is not because after this boolean concepts quickly diverge.

Also is there a reasonable type where such API could go? RuntimeInformation is more about static information about the platform. Environment is a grab bag.

@eerhardt
Copy link
Member

To check if you are running as root on Unix:

  1. add a PackageReference to Mono.Posix.NETStandard
  2. Change IsAdminstrator to the following:
        public static bool IsAdministrator =>
            RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
                new WindowsPrincipal(WindowsIdentity.GetCurrent())
                    .IsInRole(WindowsBuiltInRole.Administrator) :
                Mono.Unix.Native.Syscall.geteuid() == 0; 

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@ericstj ericstj removed the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jul 29, 2020
@ericstj
Copy link
Member

ericstj commented Jul 29, 2020

I believe @eerhardt's suggestion addressed the original request.

If you'd like to file an API suggestion, please follow the process using appropriate template.

@ericstj ericstj closed this as completed Jul 29, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime os-linux Linux OS (any supported distro)
Projects
None yet
Development

No branches or pull requests

6 participants