Note that that code will only work if your passwords are stored in /etc/passwd (or possibly in /etc/shadow, been too long since I've used that). It will
work if you're using PAM or something fancy like that to do your authentication. Your first step should probably be to check whether getpwnam is returning an encrypted password. If it's not, you may be out of luck (unless you're willing to delve pretty deeply into the magic).