ptg10805159
918 Solutions to Selected Exercises Appendix C
Chapter 6
6.1 The functions to access the shadow passwordfile on Linux and Solaris are
discussed in Section 6.3.We can’t use the value returned in thepw_passwdfield
by the functions described in Section 6.2 to compare an encrypted password, since
that field is not the encrypted password. Instead, we need to find the user’s entry
in the shadow file and use its encrypted passwordfield.
On FreeBSD and Mac OS X, the passwordfile is shadowed automatically.Inthe
passwdstructurereturned bygetpwnamandgetpwuidon FreeBSD 8.0, the
pw_passwd field contains the encrypted password, but only if the caller’s
effective user ID is 0. On Mac OS X 10.6.8, the encrypted password is not
accessible using these interfaces.
6.2 The program in FigureC.5 prints the encrypted password on Linux 3.2.0 and
Solaris 10. Unless this program is run with superuser permissions, the call to
getspnamfails with an error ofEACCES.
#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);
}
Figure C.5 Print encrypted passwordunder Linux and Solaris
Under FreeBSD 8.0, the program in FigureC.6 prints the encrypted passwordif
the program is run with superuser permissions. Otherwise, the value returned in
pw_passwdis an asterisk. On Mac OS X 10.6.8, asterisks areprinted regardless of
the permissions with which it is run.
#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");