Re: Getting the user id

From: Nick Landsberg (SPAMhukolauTRAP_at_SPAMworldnetTRAP.att.net)
Date: 10/04/04


Date: Sun, 03 Oct 2004 22:20:59 GMT

joe@invalid.address wrote:
> "Paul F. Johnson" <paul@all-the-johnsons.co.uk> writes:
>
>
>>I'm trying to determine if the user running a program I'm writing is
>>in as su or as not su. Currently, the code I have is this
>>
>>#include <stdio.h>
>>#include <pwd.h>
>>#include <sys/stat.h>
>>
>>struct stat statp;
>>
>>int main()
>>{
>> struct passwd *pwd = getpwuid(statp.pw_uid);
>
>
> statp hasn't been initialized (and I'm not sure why you're using
> struct stat for this anyway). Since it's declared at file scope its
> members will all be set to 0, which is why getpwuid() is telling you
> you're root.
>
> Use getuid() or geteuid() to find out the real or effective user id
> running the program, then use that to call getpwuid().
>
> #include <stdio.h>
> #include <pwd.h>
> #include <sys/types.h>
> #include <unistd.h>
>
> int main()
> {
> uid_t myuid = getuid();
> struct passwd *pwd = getpwuid(myuid);
> printf("%d\n",pwd->pw_uid);
> printf("username : %s\n", pwd->pw_name);
> if (strcmp("root", pwd->pw_name) != 0)
> printf("Not root\n");
> else
> printf("root\n");
> }
>
> Joe

Further refinement. The uid for root is
always 0 (zero) is it not? (Unless you
have a very wierd system)

The name hardly matters, but what matters is
if you are running as the specific privileged
user with uid=0. (Being a professional paranoid,
I can envision some sysadm refugee from the
evil empire adding an "administrator" login
with a uid of zero and deleting the "root"
entry.)

Just the getuid() call should be sufficient
to check for uid == 0, without the other stuff.

NPL

P.S. - if you do use strcmp(), you should
#include <string.h>

-- 
"It is impossible to make anything foolproof
because fools are so ingenious"
  - A. Bloch


Relevant Pages

  • Re: theoretical question - can roots username be changed?
    ... Any password file entry with a uid of zero is root, so yes, but there really isn't any point in changing it. ...
    (Fedora)
  • Re: Rename root to avoid hacking?
    ... Those are remote attacks, ... root user by name, but I am absolutely certain that no system-local ones ... By using the UID instead of the username, ... ...reach exactly the same SMTP daemon welcome banner. ...
    (comp.os.linux.security)
  • Re: allocate array
    ... I'm afraid you have bigger problems than that. ... function results to be exactly zero. ... If the function might have a root at a point where it ... of having an allocation failure on something this small are pretty low. ...
    (comp.lang.fortran)
  • Re[2]: accounting with ipfw (gid, uid riles)
    ... MS> The uid associated with a socket is the uid of the process which created ... it's still accounted to root. ... far, is adding alias interface, bind squid to this interface and count ...
    (FreeBSD-Security)
  • Re: Root is root no more
    ... > they required root access. ... > cchsu etc, cchsu being the first root uid account. ... > pwconv'd the file, added the passwd for these accounts, changed the $HOME ...
    (comp.unix.solaris)