LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-01-2011, 06:32 PM   #1
cshong
LQ Newbie
 
Registered: Nov 2006
Posts: 24

Rep: Reputation: 0
Getting user account information in C/C++


In Windows, we can use GetUserName() function ( http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx ), NetUserGetInfo() function ( http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx ), to get the username and account information such as account type (administrator, guest, normal user), last logon time, number of seconds since last password change, and a lot more information.

How can we do the same thing in Linux, by using either C or C++?

NetUserGetInfo() function in Windows not only can get user information from local computer, but also remote server. Can we do this in Linux also?
 
Old 12-01-2011, 08:16 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Once you get to system-oriented aspects such as user information and passwords, you won't find direct equivalents between Windows and Linux.

Linux is more 'file' oriented when it comes to storing such information.

User login information is stored in utmp and its variants, and is accessible using calls such as the getutxent family.

Password and group information is stored in /etc/passwd, /etc/shadow, and /etc/groups. This is also accessible through libraries, or directly. Linux doesn't have privileged accounts (except the superuser), but groups are used to provide common access to various files and devices. For installations which also include access control systems (such as SELinux), there are more fine-grained ways to control privilege context, and you would use the functions from that system to report on such context.

Rather than try to find an equivalent to the Windows function, you will have to define what it is you want to do specifically.

When it comes to remote systems, the method of accessing such information depends on the type of directory services being used across the network.
 
1 members found this post helpful.
Old 12-01-2011, 09:10 PM   #3
cshong
LQ Newbie
 
Registered: Nov 2006
Posts: 24

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
Rather than try to find an equivalent to the Windows function, you will have to define what it is you want to do specifically.
Get the username that user used to run the application, then based on the username, find all the account information of that username, including but not limited to user's home directory, user's full name, account expiry date, the period of time since last password change, logon hours, groups that the username belongs to, etc and more, just every user information that stored by linux.

I do these for learning purpose.
 
Old 12-02-2011, 02:30 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
getpwent(3) could be useful
 
Old 12-03-2011, 02:01 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
To find out the user ID, use getuid() (see man 2 getuid for details). If you want to, you can use getgid() (see man 2 getgid) to find out the group ID, and getgroups() (see man 2 getgroups) to find out the supplemental group IDs of the user running the application.

You can then use getpwuid() (see man 2 getpwuid ) to obtain the basic information about the user, including home directory, full name, and preferred shell. You can use the matching getgrgid() (see man 2 getgrgid) to find out the name and members of each group ID.

This covers all users, not just local user accounts. What neonsignal said is only true for cases where the users are locally defined; if using LDAP for example, the local user and group files typically contain just system accounts, no real users at all. Name Service Switch (NSS) and Pluggable Authentication Modules (PAM) are tied into the above functions, so that when a remote user database is used, they automagically query also the remote databases correctly.

Password change information is privileged. In some cases you can find out the last date the user changed the password by executing chage -l username (which by the way will only work if you are superuser, or username is the current user's username), but that information is not always available -- for example, when using LDAP. (The chage utility is privileged, so you do need to call it as an external program.)

Login information can be obtained using getutxent() or getutxid() functions (see man 3 getutxent for details). However, it does not always contain e.g. X sessions. (On Xubuntu 11.10 at least, X sessions do not show up at all -- a bug, I think.)

If I may, I would recommend pausing for a moment, and reconsidering why you need to collect all that information about the user. I personally find it suspicious that you wish to find out everything about the user running the program -- are you perhaps sending that information to your own servers?
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting user account information from server the_transltr Linux - Server 8 09-15-2008 01:13 PM
php myadmin account information alsaaf Linux - General 1 02-05-2007 10:14 AM
Prevent user account from logging in but allow su to account DejaCpp Linux - General 4 07-26-2006 11:44 AM
is it legitimate and allowed and can be done to make another user account set uid and gid to null 0 to make another root account with different name and possibly not damage the debian system creating and using that new account BenJoBoy Linux - Newbie 12 01-29-2006 10:02 AM
How to preserve user account information when replacing Linux distro? farnell_mark Linux - Newbie 4 03-28-2005 09:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration