detecting elevation of a process running in bash in vista

Brian Dessent brian@dessent.net
Sat Nov 3 07:27:00 GMT 2007


Kurt Franke wrote:

> does anyone found a methode to detect if a bash process
> is running so-called "elevated" with cygwin on windows vista ?
> it would be a nice feature setting the prompt to a value
> reflecting this like done when running as root under an unix os.
> or to modify the windows title to reflect the fact that
> the shell is running with special privileges.

You can use something like the attached program.  It will simply return
an exit status code so that in your shell startup you can e.g.:

if isadmin; then
	title="[Admin]"
else
	title=""
fi
PS1='\[\e]0;$title\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '

Note that w32api currently lacks the CheckTokenMembership() prototype so
you'll need to apply
<http://sourceforge.net/tracker/index.php?func=detail&aid=1825044&group_id=2435&atid=302435>
first in order to build this.

Brian
-------------- next part --------------
#include <stdio.h>
#define _WIN32_WINNT 0x0500
#include <windows.h>

int main (int argc, char **argv)
{
  SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
  PSID AdministratorsGroup;
  BOOL isAdmin;

  if (AllocateAndInitializeSid (&NtAuthority, 2,
          SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
          0, 0, 0, 0, 0, 0, &AdministratorsGroup) == 0 ||
      CheckTokenMembership (NULL, AdministratorsGroup, &isAdmin) == 0)
    {
      printf ("failed with win32 error %lu\n", GetLastError ());
      exit (2);
    }

  FreeSid (AdministratorsGroup);
  exit (!isAdmin);
}

-------------- next part --------------
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


More information about the Cygwin mailing list