home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / shadow-2.pt3 / valid.c < prev   
C/C++ Source or Header  |  1989-02-03  |  2KB  |  67 lines

  1. #include <pwd.h>
  2.  
  3. /*
  4.  * valid - compare encrypted passwords
  5.  *
  6.  *    Valid() compares the DES encrypted password from the password file
  7.  *    against the password which the user has entered after it has been
  8.  *    encrypted using the same salt as the original.
  9.  */
  10.  
  11. int    valid (password, entry)
  12. char    *password;
  13. struct    passwd    *entry;
  14. {
  15.     char    *encrypt;
  16.     char    *salt;
  17.     char    *crypt ();
  18.  
  19.     /*
  20.      * Start with blank or empty password entries.  Always encrypt
  21.      * a password if no such user exists.  Only if the ID exists and
  22.      * the password is really empty do you return quickly.  This
  23.      * routine is meant to waste CPU time.
  24.      */
  25.  
  26.     if (entry->pw_name &&
  27.             (entry->pw_passwd == (char *) 0 ||
  28.              strlen (entry->pw_passwd) == 0)) {
  29.         if (strlen (password) == 0)
  30.             return (1);    /* user entered nothing */
  31.         else
  32.             return (0);    /* user entered something! */
  33.     }
  34.  
  35.     /*
  36.      * If there is no entry then we need a salt to use.
  37.      */
  38.  
  39.     if (entry->pw_passwd == (char *) 0 || entry->pw_passwd[0] == '\0')
  40.         salt = "xx";
  41.     else
  42.         salt = entry->pw_passwd;
  43.  
  44.     /*
  45.      * Now, perform the encryption using the salt from before on
  46.      * the users input.  Since we always encrypt the string, it
  47.      * should be very difficult to determine if the user exists by
  48.      * looking at execution time.
  49.      */
  50.  
  51.     encrypt = crypt (password, salt);
  52.  
  53.     /*
  54.      * One last time we must deal with there being no password file
  55.      * entry for the user.  We use the pw_passwd == NULL idiom to
  56.      * cause non-existent users to not be validated.  Even still,
  57.      * we are safe because if the string were == "", any encrypted
  58.      * string is not going to match - the output of crypt() begins
  59.      * with the salt, which is "xx", not "".
  60.      */
  61.  
  62.     if (entry->pw_passwd && strcmp (encrypt, entry->pw_passwd) == 0)
  63.         return (1);
  64.     else
  65.         return (0);
  66. }
  67.