home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3343 / valid.c < prev   
Encoding:
C/C++ Source or Header  |  1991-05-17  |  2.3 KB  |  91 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include "pwd.h"
  14. #ifdef    BSD
  15. #include <strings.h>
  16. #define    strchr    index
  17. #define    strrchr    rindex
  18. #else
  19. #include <string.h>
  20. #include <memory.h>
  21. #endif
  22. #include "config.h"
  23.  
  24. #ifndef    lint
  25. static    char    _sccsid[] = "@(#)valid.c    3.3    08:00:20    2/6/91";
  26. #endif
  27.  
  28. /*
  29.  * valid - compare encrypted passwords
  30.  *
  31.  *    Valid() compares the DES encrypted password from the password file
  32.  *    against the password which the user has entered after it has been
  33.  *    encrypted using the same salt as the original.  Entries which do
  34.  *    not have a password file entry have a NULL pw_name field and this
  35.  *    is used to indicate that a dummy salt must be used to encrypt the
  36.  *    password anyway.
  37.  */
  38.  
  39. int    valid (password, entry)
  40. char    *password;
  41. struct    passwd    *entry;
  42. {
  43.     char    *encrypt;
  44.     char    *salt;
  45.     char    *pw_encrypt ();
  46.     char    *shell;
  47.  
  48.     /*
  49.      * Start with blank or empty password entries.  Always encrypt
  50.      * a password if no such user exists.  Only if the ID exists and
  51.      * the password is really empty do you return quickly.  This
  52.      * routine is meant to waste CPU time.
  53.      */
  54.  
  55.     if (entry->pw_name && ! entry->pw_passwd[0]) {
  56.         if (! password[0])
  57.             return (1);    /* user entered nothing */
  58.         else
  59.             return (0);    /* user entered something! */
  60.     }
  61.  
  62.     /*
  63.      * If there is no entry then we need a salt to use.
  64.      */
  65.  
  66.     if (entry->pw_name == (char *) 0 || entry->pw_passwd[0] == '\0')
  67.         salt = "xx";
  68.     else
  69.         salt = entry->pw_passwd;
  70.  
  71.     /*
  72.      * Now, perform the encryption using the salt from before on
  73.      * the users input.  Since we always encrypt the string, it
  74.      * should be very difficult to determine if the user exists by
  75.      * looking at execution time.
  76.      */
  77.  
  78.     encrypt = pw_encrypt (password, salt);
  79.  
  80.     /*
  81.      * One last time we must deal with there being no password file
  82.      * entry for the user.  We use the pw_passwd == NULL idiom to
  83.      * cause non-existent users to not be validated.
  84.      */
  85.  
  86.     if (entry->pw_name && strcmp (encrypt, entry->pw_passwd) == 0)
  87.         return (1);
  88.     else
  89.         return (0);
  90. }
  91.