users_groups/check_password.cThis is users_groups/check_password.c (Listing 8-2, page 164), an example from the book, The Linux Programming Interface. The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3. This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below. In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions. |
+/* check_password.c + + Read a user name and password and check if they are valid. + + This program uses the shadow password file. Some UNIX implementations + don't support this feature. +*/ +/* Compile with -lcrypt */ +#if ! defined(__sun) #define _BSD_SOURCE /* Get getpass() declaration from <unistd.h> */ +#ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE /* Get crypt() declaration from <unistd.h> */ +#endif +#endif #include <unistd.h> #include <limits.h> #include <pwd.h> #include <shadow.h> +#include <crypt.h> #include "tlpi_hdr.h" int main(int argc, char *argv[]) { char *username, *password, *encrypted, *p; struct passwd *pwd; struct spwd *spwd; Boolean authOk; size_t len; long lnmax; + /* Determine size of buffer required for a username, and allocate it */ + lnmax = sysconf(_SC_LOGIN_NAME_MAX); if (lnmax == -1) /* If limit is indeterminate */ lnmax = 256; /* make a guess */ username = malloc(lnmax); if (username == NULL) errExit("malloc"); printf("Username: "); fflush(stdout); if (fgets(username, lnmax, stdin) == NULL) exit(EXIT_FAILURE); /* Exit on EOF */ len = strlen(username); if (username[len - 1] == '\n') username[len - 1] = '\0'; /* Remove trailing '\n' */ + /* Look up password and shadow password records for username */ + pwd = getpwnam(username); if (pwd == NULL) fatal("couldn't get password record"); spwd = getspnam(username); if (spwd == NULL && errno == EACCES) fatal("no permission to read shadow password file"); if (spwd != NULL) /* If there is a shadow password record */ pwd->pw_passwd = spwd->sp_pwdp; /* Use the shadow password */ password = getpass("Password: "); /* Encrypt password and erase cleartext version immediately */ encrypted = crypt(password, pwd->pw_passwd); for (p = password; *p != '\0'; ) *p++ = '\0'; if (encrypted == NULL) errExit("crypt"); authOk = strcmp(encrypted, pwd->pw_passwd) == 0; if (!authOk) { printf("Incorrect password\n"); exit(EXIT_FAILURE); } printf("Successfully authenticated: UID=%ld\n", (long) pwd->pw_uid); /* Now do authenticated work... */ exit(EXIT_SUCCESS); }
Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.