The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do i change to super user then revert back to ordinary user ,using shell script? wrapster Shell Programming and Scripting 3 06-04-2008 08:11 AM
Displaying current user process felixwhoals Shell Programming and Scripting 14 12-16-2007 07:00 PM
How to check the status of the processes running for the current user? ravind27 UNIX for Dummies Questions & Answers 1 07-05-2007 04:10 AM
To look for occurrences of the current user bobby36 Shell Programming and Scripting 1 04-12-2007 12:19 AM
how to find password for current user in unix box balireddy_77 Shell Programming and Scripting 1 01-30-2007 03:42 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-28-2003
szzz szzz is offline
Registered User
  
 

Join Date: Oct 2003
Location: Jerusalem
Posts: 12
Who is the current user

How could I get the username and password of user, who started this programm?
  #2 (permalink)  
Old 10-28-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
The user name will be in the environment and you can get it with getenv(). The environment variable will be called either USER or LOGNAME. (Posix requires LOGNAME) Whichever variable your system uses, it is set by the login program. And a decent cron will also set it for cron and "at" jobs. It won't get set if the user invokes your program via a remote service that did not properly log him in. But a clever user can set the env variable himself in that case. Which bring up a problem....a user could also clobber the variable, intentionally or otherwise.

Another approach is the inspect fd's 0, 1, and 2. They may be connected to the user's tty. isatty() can tell you if they are tty's or not. And ttyname() can provide the name of the tty. Once you have that, you can search the utmp file via getutmp() to get the user name as recorded by the login (or similiar) program. Again, not all remote services will set a utmp entry. And this will fail for cron or "at" jobs. This is how "who am i" works. And it is why
who am i < /dev/null 2 > /dev/null | cat
fails.

Another approach is to get the uid of the process with getuid() and then scan the passwd file via getpwnam(). This is how the program whoami works. But if user A signs on and su's to user B and then runs your program, you get user B.

As for the password, you can't get that. And I wonder why you want it.
  #3 (permalink)  
Old 11-05-2003
szzz szzz is offline
Registered User
  
 

Join Date: Oct 2003
Location: Jerusalem
Posts: 12
No, this
getenv("USER");
getenv("LOGNAME");
return NULL. It does not work ...
(I use LinuxRH7.2)
  #4 (permalink)  
Old 11-05-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Sign on to your system and type the command "env". I believe that you will find both variables set. At least I see them both on redhat.

And type "man environ". I see them as the first two ducumented environment settings. What do you see?

Maybe your login scripts unset both environment variables?

But looking at this thread and your other thread involving mtime, the more probable answer is that you are having some difficulties with programs that you write.
  #5 (permalink)  
Old 11-10-2003
S.P.Prasad S.P.Prasad is offline
Registered User
  
 

Join Date: Jan 2002
Location: India
Posts: 111
I don't think one can get password of a logged user but there are many a ways by which one can know the user who has logged in.

Perderabo has already defined multiple ways and all that I want to add is standard C library routine to find a username / terminal associated with the current process:

#include <stdio.h>

char *cuserid(char *s);
char *ctermid(char *s);

See man pages for more details.
  #6 (permalink)  
Old 11-14-2003
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
how about
getuid() function

And really you cannot get the password of the user logged in, however you can get it in the way it is stored, but it wont make much sense. you can use the getpwnam() function
  #7 (permalink)  
Old 11-15-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Read paragraph 3 of my first post for comments on getuid(). Hmmm, I see that I said to use getpwnam(). Actually, since we have the uid, we should use getpwuid().

ctermid() is a silly little function that always returns the string "/dev/tty". I will never understand why people write functions to return a constant.

cuserid() was dropped from posix in 1990. It is still available on many systems, but I recommend that it not be used for new code. It first tries the technique I mentioned in the second paragraph of my first post. If that fails, it then uses the getuid() technique that I mentioned in my third paragraph. No information is available to the caller about which technique worked. And it never tries the technique mentioned in my first paragraph which posix seems to promote as the preferred method.

Also there is getlogin() which automates looking up the controlling terminal in utmp.

I have the uneasy feeling that there are more functions that automate some conbination of the 3 techniques that I mentioned. It's dangerous to be sure of a negative, but I'm rather sure that there isn't a 4th technique lurking around.
Closed Thread

Bookmarks

Tags
mtime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0