There are always three steps:
We will not discuss authenticating through PAM here. See elsewhere.
#include <pwd.h> : struct passwd *user; : if ((user= getpwnam(login)) == NULL) printf("No such user\n"); else if (!strcmp(user->pw_passwd, crypt(password, user->pw_passwd)) printf("Password correct\n"); else printf("Password incorrect\n");On some systems, you will need to link with -lcrypt to get the crypt() function.
Most BSD systems, including FreeBSD, NetBSD, OpenBSD, and BSDI implement this very transparently. You still call getpwnam(), but if you aren't root, it doesn't return the encrypted password. Thus the password checking code looks exactly like the traditional code, except you have to be root for it to work.
#include <shadow.h> : struct spwd *user; : if ((user= getspnam(login)) == NULL) printf("No such user\n"); else if (!strcmp(user->sp_pwdp, crypt(password, user->sp_pwdp)) printf("Password correct\n"); else printf("Password incorrect\n");On some systems, you will need to link with -lcrypt to get the crypt() function.
The open source Linux version of this was developed by Julianne F. Haugh. Some installations that use that may need to link with -lshadow to get the getspnam() function (and faster versions of getpwnam()).
Ancient versions of the Julianne F. Haugh shadow package replaced the crypt() function with one named pw_encrypt() which was supposed to do a better job encrypting long passwords. It didn't really, and has fallen into disfavor, and is rarely seen now. It's arguments were the same as those to crypt().
#include <userpw.h> : struct userpw *user; : if ((user= getuserpw(login)) == NULL) printf("No such user\n"); else if (!strcmp(user->upw_passwd, crypt(password, user->upw_passwd)) printf("Password correct\n"); else printf("Password incorrect\n");I haven't actually tried this.
#include <sys/type.h> #include <hpsecurity.h> #include <prot.h> : struct pr_passwd *user; : if ((user= getspwnam(login)) == NULL) printf("No such user\n"); else if (!strcmp(user->ufld.fd_encrypt, crypt(password, user->ufld.fd_encrypt)) printf("Password correct\n"); else printf("Password incorrect\n");I haven't actually tried this. It's possible that you have to call bigcrypt() instead of crypt().
HP-UX also has a function called getspwnam() which returns the encrypted password with less other baggage than getprpwnam() does, but their documentation suggests it is depreciated.