The program in Figure C.4 prints the encrypted password on Linux and Solaris. Unless this program is run with superuser permissions, the call to getspnam fails with an error of EACCES.
Figure C.4. Print encrypted password under Linux and Solaris
#include "apue.h"
#include <shadow.h>
int
main(void) /* Linux/Solaris version */
{
struct spwd *ptr;
if ((ptr = getspnam("sar")) == NULL)
err_sys("getspnam error");
printf("sp_pwdp = %s\n", ptr->sp_pwdp == NULL ||
ptr->sp_pwdp[0] == 0 ? "(null)" : ptr->sp_pwdp);
exit(0);
}
Under FreeBSD, the program in Figure C.5 prints the encrypted password if the program is run with superuser permissions. Otherwise, the value returned in pw_passwd is an asterisk. On Mac OS X, the encrypted password is printed regardless of the permissions with which it is run.
Figure C.5. Print encrypted password under FreeBSD and Mac OS X
#include "apue.h"
#include <pwd.h>
int
main(void) /* FreeBSD/Mac OS X version */
{
struct passwd *ptr;
if ((ptr = getpwnam("sar")) == NULL)
err_sys("getpwnam error");
printf("pw_passwd = %s\n", ptr->pw_passwd == NULL ||
ptr->pw_passwd[0] == 0 ? "(null)" : ptr->pw_passwd);
exit(0);
}
|