home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2288 / valid.c < prev   
C/C++ Source or Header  |  1990-12-28  |  3KB  |  133 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Use, duplication, and disclosure prohibited without
  6.  * the express written permission of the author.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <pwd.h>
  11. #ifndef    BSD
  12. #include <string.h>
  13. #include <memory.h>
  14. #else
  15. #include <strings.h>
  16. #define    strchr    index
  17. #define    strrchr    rindex
  18. #endif
  19. #include "config.h"
  20.  
  21. #ifndef    lint
  22. static    char    _sccsid[] = "@(#)valid.c    2.4    19:24:27    7/29/90";
  23. #endif
  24.  
  25. /*
  26.  * valid - compare encrypted passwords
  27.  *
  28.  *    Valid() compares the DES encrypted password from the password file
  29.  *    against the password which the user has entered after it has been
  30.  *    encrypted using the same salt as the original.
  31.  */
  32.  
  33. int    valid (password, entry)
  34. char    *password;
  35. struct    passwd    *entry;
  36. {
  37.     char    *encrypt;
  38.     char    *salt;
  39.     char    *crypt ();
  40.     char    *shell;
  41. #ifdef    DOUBLESIZE
  42.     int    firsthalf;
  43.     int    longpass;
  44. #endif
  45.  
  46. #ifdef    NOUSE
  47.     if (entry->pw_shell && strcmp (NOUSE, entry->pw_shell) == 0)
  48.         return (0);
  49. #if defined(SU) && defined (NOLOGIN)
  50.     if (entry->pw_shell && strcmp (NOLOGIN, entry->pw_shell) == 0) {
  51.         if (! (shell = getenv ("SHELL")))
  52.             return 0;
  53.     }
  54. #endif
  55. #if !defined(SU) && defined (NOLOGIN)
  56.     if (entry->pw_shell && strcmp (NOLOGIN, entry->pw_shell) == 0)
  57.         return 0;
  58. #endif
  59. #endif
  60.     /*
  61.      * Start with blank or empty password entries.  Always encrypt
  62.      * a password if no such user exists.  Only if the ID exists and
  63.      * the password is really empty do you return quickly.  This
  64.      * routine is meant to waste CPU time.
  65.      */
  66.  
  67.     if (entry->pw_name &&
  68.             (entry->pw_passwd == (char *) 0 ||
  69.              strlen (entry->pw_passwd) == 0)) {
  70.         if (strlen (password) == 0)
  71.             return (1);    /* user entered nothing */
  72.         else
  73.             return (0);    /* user entered something! */
  74.     }
  75.  
  76. #ifdef    DOUBLESIZE
  77.     longpass = entry->pw_passwd && strlen (entry->pw_passwd) > 13;
  78. #endif
  79.  
  80.     /*
  81.      * If there is no entry then we need a salt to use.
  82.      */
  83.  
  84.     if (entry->pw_passwd == (char *) 0 || entry->pw_passwd[0] == '\0')
  85.         salt = "xx";
  86.     else
  87.         salt = entry->pw_passwd;
  88.  
  89.     /*
  90.      * Now, perform the encryption using the salt from before on
  91.      * the users input.  Since we always encrypt the string, it
  92.      * should be very difficult to determine if the user exists by
  93.      * looking at execution time.
  94.      */
  95.  
  96.     encrypt = crypt (password, salt);
  97. #ifdef    DOUBLESIZE
  98.     firsthalf = entry->pw_passwd
  99.         && strncmp (encrypt + 2, entry->pw_passwd + 2, 11) == 0;
  100.  
  101.     if (strlen (password) > 8)
  102.         encrypt = crypt (password + 8, salt);
  103.     else {
  104.         (void) crypt (password, salt);    /* waste time ... */
  105.         encrypt = "";
  106.     }
  107. #endif
  108.     /*
  109.      * One last time we must deal with there being no password file
  110.      * entry for the user.  We use the pw_passwd == NULL idiom to
  111.      * cause non-existent users to not be validated.  Even still,
  112.      * we are safe because if the string were == "", any encrypted
  113.      * string is not going to match - the output of crypt() begins
  114.      * with the salt, which is "xx", not "".
  115.      */
  116.  
  117. #ifndef    DOUBLESIZE
  118.     if (entry->pw_passwd && strcmp (encrypt, entry->pw_passwd) == 0)
  119.         return (1);
  120.     else
  121.         return (0);
  122. #else
  123.     if (! longpass)
  124.         return (firsthalf);
  125.  
  126.     if (entry->pw_passwd && firsthalf
  127.             && strncmp (encrypt + 2, entry->pw_passwd + 13) == 0)
  128.         return (1);
  129.     else
  130.         return (0);
  131. #endif
  132. }
  133.