PDA

View Full Version : Identifying Who Logged In & getlogin()



Crashgr
12-08-2004, 02:25 PM
Hello!!

What is the best way to get the username of the logged in user? I used getlogin() and it worked fine with these distros :

Solaris 8
Suse 9

It also worked perfectly at cygwin. However, Mandrake 10, returns a ENOENT error message. What's more, I found this page :

http://jamesthornton.com/linux/man/getlogin.3.html

which says :


Unfortunately, it is often rather easy to fool getlogin(). Sometimes it does not work at all, because some program messed up the utmp file. Often, it gives only the first 8 characters of the login name. The user currently logged in on the controlling tty of our program need not be the user who started it. Avoid getlogin() for security-related purposes.

and this page :

http://linux-documentation.com/en/package/glibc/Who-Logged-In.html

says:


These functions let your program identify positively the user who is running or the user who logged in this session. (These can differ when setuid programs are involved; see Process Persona.) The user cannot do anything to fool these functions.

Can anyone inform me about the best way (portable and secure) to find the login name of the user?

Thank you!

Perspective
12-08-2004, 02:29 PM
try this...


echo $USER ; man getenv

Crashgr
12-08-2004, 02:33 PM
Perspective, thank you for your answer, however I believe that $USER is not safe at all. any user can change the value of this variable.

As I said, I'm looking for a portable and secure way to find the login name of the user!

iain
12-08-2004, 02:48 PM
there is 'whoami', though im unsure if there is a function for that, i think it uses the $USER anyway (?)

Perspective
12-08-2004, 04:07 PM
Perspective, thank you for your answer, however I believe that $USER is not safe at all. any user can change the value of this variable.

As I said, I'm looking for a portable and secure way to find the login name of the user!

hmmm, how bout


cout << "Please give me your real user name... pretty please?" << endl;
cin >> uname;

;)

yeah, im out of ideas. sorry.

chrismiceli
12-08-2004, 06:05 PM
I don't know, mabey if you wanted, you could look at the process tree and find the first login above you in your programs family tree. Processes in kernel space have fields describing their family, don't know if you want to go that in depth, but look here: http://www.iamexwi.unibe.ch/studenten/schlpbch/linuxScheduling/LinuxScheduling-2.html

Crashgr
12-10-2004, 07:51 AM
There must be an easier way.... What bash shell does to retrieve my login name? It surely doesn't use the $USER variable.

Codeplug
12-11-2004, 01:29 AM
The most standard alternative I know of is getpwuid(geteuid() or getuid() etc...)

http://www.opengroup.org/onlinepubs/000095399/functions/getpwuid.html

gg

dagdarian
12-21-2004, 09:51 AM
echo ~

Perspective
12-21-2004, 08:37 PM
echo ~

~ does not always resolve to a directory containing the users name. Not all users have a /home directory.

Kleid-0
12-21-2004, 09:15 PM
Would this work?

$theuser = system("whoami");
It works in PHP! :)

Codeplug
12-21-2004, 09:49 PM
The most standard alternative I know of is getpwuid(geteuid() or getuid() etc...)

http://www.opengroup.org/onlinepubs/000095399/functions/getpwuid.html

ggWhich is what GNU's whoami does.



uid = geteuid ();
pw = getpwuid (uid);
if (pw)
{
puts (pw->pw_name);
exit (EXIT_SUCCESS);
}gg

Crashgr
12-25-2004, 09:01 PM
Thank you all for your replies!! ;)