I'm having issues retrieving current user information of Red Hat Enterprise 6 where the user is an LDAP user?
I have some code (actually part of an installation tool) that needs to retrieve the user name, home directory and other details. It is using the getpwuid() call to do this based on the user id. A simplified breakdown:
uid_t uid = getuid();
printf("UID = %d\n", uid);
errno = 0;
struct passwd* udetails = getpwuid(uid);
if (udetails != NULL)
{
printf("User name = %s\n", udetails->pw_name);
}
else
{
printf("getpwuid returns NULL, errno=%d\n", errno);
}
This works without problems where the user is a local user (in that system's /etc/passwd).
When the user is an LDAP-authenticated user, the call the getuid returns the user ID or the current user, but the call to getpwuid returns 0, with no error code set in errno. According to the documentation, this means that the user doesn't exist.
Should this work? According to the getpwuid manpage:
The getpwnam() function returns a pointer to a structure containing the broken-out fields of the record in the password database (e.g., the local password file /etc/passwd, NIS, and LDAP) that matches the username name.
The getpwuid() function returns a pointer to a structure containing the broken-out fields of the record in the password database that matches the user ID uid.
Is an alternative call required to get the details if the current user was authenticated by LDAP? Is it necessary to open the LDAP database in an application, or should the system call handle that?
Additional: I have also now tried this on a RHEL 5 box authenticating against the same LDAP directory. Could this just be a configuration issue on the RHEL 6 box? Or a wider RHEL 6 issue?
Additional: /etc/nsswitch.conf as requested by Basile Starynkevitch (commented lines removed):
passwd: files sss
shadow: files sss
group: files sss
hosts: files dns
bootparams: nisplus [NOTFOUND=return] files
ethers: files
netmasks: files
networks: files
protocols: files
rpc: files
services: files sss
netgroup: files sss
publickey: nisplus
automount: files ldap
aliases: files nisplus
I'm guessing that some of these should mention ldap at some point? In fact this suggests that it's not using LDAP at all....
I had exactly the same problem on CentOS 8. The answer by @kriko helped me solve it, but I had to
yum install nss_nis.i686
in order to fix it. Notsssd-client.i686