Thread: Identifying Who Logged In & getlogin()

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    10

    Identifying Who Logged In & getlogin()

    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/pa...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!

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    try this...
    Code:
    echo $USER ; man getenv

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    10
    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!

  4. #4
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    there is 'whoami', though im unsure if there is a function for that, i think it uses the $USER anyway (?)
    Monday - what a way to spend a seventh of your life

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Crashgr
    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
    Code:
    cout << "Please give me your real user name... pretty please?" << endl;
    cin >> uname;


    yeah, im out of ideas. sorry.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    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/studente...eduling-2.html
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    10
    There must be an easier way.... What bash shell does to retrieve my login name? It surely doesn't use the $USER variable.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The most standard alternative I know of is getpwuid(geteuid() or getuid() etc...)

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

    gg

  9. #9
    </life>
    Join Date
    Oct 2004
    Posts
    83
    echo ~
    Microsoft is merely an illusion, albeit a very persistant one.

  10. #10
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by dagdarian
    echo ~
    ~ does not always resolve to a directory containing the users name. Not all users have a /home directory.

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Would this work?
    Code:
    $theuser = system("whoami");
    It works in PHP!

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Quote Originally Posted by Codeplug
    The most standard alternative I know of is getpwuid(geteuid() or getuid() etc...)

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

    gg
    Which is what GNU's whoami does.
    Quote Originally Posted by GNU's whoami.c
    Code:
    uid = geteuid ();
    pw = getpwuid (uid);
    if (pw)
      {
        puts (pw->pw_name);
        exit (EXIT_SUCCESS);
      }
    gg

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    10
    Thank you all for your replies!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems accessing logged on user via System.Security
    By conor20_ie in forum C# Programming
    Replies: 5
    Last Post: 11-16-2007, 07:52 AM
  2. Determining Logged On Users
    By mikeman118 in forum Windows Programming
    Replies: 4
    Last Post: 10-17-2007, 09:43 AM
  3. Searching User Currently Logged On
    By cfrost in forum Networking/Device Communication
    Replies: 5
    Last Post: 03-18-2005, 07:21 AM
  4. [C] Get the current logged user (win2k)
    By BianConiglio in forum Windows Programming
    Replies: 10
    Last Post: 03-09-2005, 12:39 AM
  5. ``You are not logged in''
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-24-2002, 08:50 AM