Unix Programming - get current user name?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2005 > get current user name?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author get current user name?
djbitchpimp@snowboard.com

2005-08-18, 5:54 pm

How do I retrieve the username of the currently logged in user? I
basically to check if the user is logged in as root and if not, exit
the program.

I though about using the LOGNAME environment variable, but if a regular
user used su root, the environment variable still has the user's login
name.

Rich Teer

2005-08-18, 5:54 pm

On Thu, 18 Aug 2005 djbitchpimp@snowboard.com wrote:

> How do I retrieve the username of the currently logged in user? I
> basically to check if the user is logged in as root and if not, exit
> the program.


A better way is to check the real user-ID == 0.

But if you insist, see man cuserid.

--
Rich Teer, SCNA, SCSA, OpenSolaris CAB member

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
Måns Rullgård

2005-08-18, 5:54 pm

djbitchpimp@snowboard.com writes:

> How do I retrieve the username of the currently logged in user? I
> basically to check if the user is logged in as root and if not, exit
> the program.


This is generally a bad idea. It's better to attempt whatever you're
about to do, and only complain if it fails. On some system, it may be
perfectly safe and legitimate for (perhaps special) a non-root to do
it.

If you still insist, use getuid() and/or geteuid(), and check for a
UID of zero.

--
Måns Rullgård
mru@inprovide.com
Pascal Bourguignon

2005-08-18, 5:54 pm

djbitchpimp@snowboard.com writes:

> How do I retrieve the username of the currently logged in user?


> I basically to check if the user is logged in as root and if not, exit
> the program.


That's two questions. Which one do you want to get an answer to?

root is defined by: getuid()==0

man getpw

> I though about using the LOGNAME environment variable, but if a regular
> user used su root, the environment variable still has the user's login
> name.


man getuid
man geteuid


--
__Pascal Bourguignon__ http://www.informatimago.com/
Small brave carnivores
Kill pine cones and mosquitoes
Fear vacuum cleaner
Måns Rullgård

2005-08-18, 5:54 pm

Rich Teer <rich.teer@rite-group.com> writes:

> On Thu, 18 Aug 2005 djbitchpimp@snowboard.com wrote:
>
>
> A better way is to check the real user-ID == 0.


Or possibly effective user ID. Which is appropriate depends on the
reason for checking in the first place.

> But if you insist, see man cuserid.


Or not. My man page contains this paragraph:

Nobody knows precisely what cuserid() does - avoid it in portable pro-
grams - avoid it altogether - use getpwuid(geteuid()) instead, if that
is what you meant. DO NOT USE cuserid().

--
Måns Rullgård
mru@inprovide.com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com