9

So, I want to know if a user has run the application with administrator privileges -- irrespective of the OS the person is on.

I found the solution for Windows (from a website):

public static boolean isAdmin() {
    String groups[] = (new com.sun.security.auth.module.NTSystem()).getGroupIDs();
    for (String group : groups) {
        if (group.equals("S-1-5-32-544"))
            return true;
    }
    return false;
}  

What about Mac and Ubuntu ?

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • 4
    I am deeply suspicious that your above solution will run reliably on different versions of Windows, especially with the verboten `com.sun.*` import. Perhaps you should describe *why* you need to detect this -- there is probably a better solution. – Thorn G Jun 14 '13 at 17:12
  • @TomG I am not a Java expert; just a novice. Why won't it work ?? – An SO User Jun 14 '13 at 17:13
  • 1
    On Ubuntu you probably can check if the `whoami` command returns `root`. – Piovezan Jun 14 '13 at 17:13
  • @Piovezan How do I do that in Java ? I mean you can do that in the shell but in a `.jar` file ? – An SO User Jun 14 '13 at 17:13
  • You can use the `ProcessBuilder` class to run shell commands. – Sotirios Delimanolis Jun 14 '13 at 17:14
  • 2
    Java has no portable way to know that -- heck, no programming language has, I believe – fge Jun 14 '13 at 17:14
  • 1
    @LittleChild Classes from the Sun namespace are not included in alternative JREs. – Thorn G Jun 14 '13 at 17:14
  • 1
    @Piovezan even this is not reliable, you can be a user with uid 0 but whose name is not root... – fge Jun 14 '13 at 17:16
  • @fge: And a user with UID 0 is not superuser whatever the name it has? – Igor Rodriguez Jun 14 '13 at 17:19
  • @IgorRodriguez _is_ superuser, you mean -- yup, pretty much so... Unless some advanced security modules can thwart that, but I don't know of one – fge Jun 14 '13 at 17:23
  • 2
    @LittleChild you are attempting to get platform-specific information in a language for which one stated goal is to remain independent of platform. It seems conceptually dodgy to me since a user having an "admin" role or privilege does not mean the same thing on different systems. – arcy Jun 14 '13 at 18:00
  • 1
    maybe try to write to some restricted directories to check if you have admin rights? For example to C:programFiles for windows and / for unix based Os's – Osama Javed Jun 14 '13 at 18:37
  • 3
    There is no way because not all OSes have a concept of an administrator or super-user. Java can be run on Windows 95 and Windows 95 does not have any such maximally privileged principle. – Mike Samuel Jun 14 '13 at 18:40
  • 1
    Why do you think to need to know this? Shouldn't you rather try to see if you can actually do some specific action? Your user might not have all privileges, but might have enough for what you want to do. – Paŭlo Ebermann Jun 14 '13 at 19:08
  • @PaŭloEbermann you might want the program to refuse to run as the administrator. – Raedwald Jun 14 '13 at 19:40
  • I don't understand why this question is marked as duplicate? I've found a different solution, that might be more platform-independent: http://stackoverflow.com/a/23538961/2424073 – MyPasswordIsLasercats May 08 '14 at 10:19

1 Answers1

7

I don't think is possible to be totally OS independent, but a few months ago I had to check IzPack source code and it does exactly what you need.

In the PrivilegedRunner class it has to check if it has admin privileges, check the method isElevationNeeded

Here is the source code

Federico Nafria
  • 1,397
  • 14
  • 39