Re: Getting the user id
From: Nick Landsberg (SPAMhukolauTRAP_at_SPAMworldnetTRAP.att.net)
Date: 10/04/04
- Next message: Michael Fuhr: "Re: libpcap issues"
- Previous message: joe_at_invalid.address: "Re: Getting the user id"
- In reply to: joe_at_invalid.address: "Re: Getting the user id"
- Next in thread: joe_at_invalid.address: "Re: Getting the user id"
- Reply: joe_at_invalid.address: "Re: Getting the user id"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Michael Fuhr: "Re: libpcap issues"
- Previous message: joe_at_invalid.address: "Re: Getting the user id"
- In reply to: joe_at_invalid.address: "Re: Getting the user id"
- Next in thread: joe_at_invalid.address: "Re: Getting the user id"
- Reply: joe_at_invalid.address: "Re: Getting the user id"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|