ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.