[ILUG] finding user name from inside a C program

Joseph Manning manning at cs.ucc.ie
Tue Sep 20 13:23:21 IST 2005


Hi,

   Does anyone know how to safely and easily determine the user name
   of someone running a C program?

   I tried doing:

      getenv( "USER" )

   and this works, but it can easily be tricked by a malicious user
   simply setting  USER=someone-else  before running the program.

   The "whoami" command seems to be independent of any tampering
   with the  USER  environment variable, but doing:

      system( "whoami" )

   will output the user name, rather than returning it.  (Yes, it's
   possible to send its output to a file, and then read in that file,
   but this is an awful hack, there must be a neater solution).

Thanks! -- Joseph

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joseph Manning / Computer Science / UCC Cork Ireland / manning at cs.ucc.ie
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the ILUG mailing list