Although sauoq has alredy answered your question, I'd like to point out few things that you might find useful:
Rarely this days passwords are stored in /etc/passwd. Most probably you'll need to make use of /etc/shadow.
Rarely this days system passwords are stored plainly crypt()-ed. Most probably you'll need to work with MD5 or some other stronger cryptography.
If your system is using PAM, then you should do so too. Cpan has modules for you. With PAM you will not have to care about crypt/md5, /etc/passwd or /etc/shador, or even LDAP authentication for that matter.