home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / shadow / part12 / valid.c < prev   
C/C++ Source or Header  |  1993-08-14  |  2KB  |  95 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, 1993, 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.  * This software is provided on an AS-IS basis and the author makes
  12.  * no warrantee of any kind.
  13.  */
  14.  
  15. #include <sys/types.h>
  16. #include <stdio.h>
  17. #ifdef    BSD
  18. #include <strings.h>
  19. #define    strchr    index
  20. #define    strrchr    rindex
  21. #else
  22. #include <string.h>
  23. #include <memory.h>
  24. #endif
  25. #include "config.h"
  26. #include "pwd.h"
  27.  
  28. #ifndef    lint
  29. static    char    _sccsid[] = "@(#)valid.c    3.5    22:04:54    02 Jun 1993";
  30. #endif
  31.  
  32. /*
  33.  * valid - compare encrypted passwords
  34.  *
  35.  *    Valid() compares the DES encrypted password from the password file
  36.  *    against the password which the user has entered after it has been
  37.  *    encrypted using the same salt as the original.  Entries which do
  38.  *    not have a password file entry have a NULL pw_name field and this
  39.  *    is used to indicate that a dummy salt must be used to encrypt the
  40.  *    password anyway.
  41.  */
  42.  
  43. int
  44. valid (password, entry)
  45. char    *password;
  46. struct    passwd    *entry;
  47. {
  48.     char    *encrypt;
  49.     char    *salt;
  50.     char    *pw_encrypt ();
  51.  
  52.     /*
  53.      * Start with blank or empty password entries.  Always encrypt
  54.      * a password if no such user exists.  Only if the ID exists and
  55.      * the password is really empty do you return quickly.  This
  56.      * routine is meant to waste CPU time.
  57.      */
  58.  
  59.     if (entry->pw_name && ! entry->pw_passwd[0]) {
  60.         if (! password[0])
  61.             return (1);    /* user entered nothing */
  62.         else
  63.             return (0);    /* user entered something! */
  64.     }
  65.  
  66.     /*
  67.      * If there is no entry then we need a salt to use.
  68.      */
  69.  
  70.     if (entry->pw_name == (char *) 0 || entry->pw_passwd[0] == '\0')
  71.         salt = "xx";
  72.     else
  73.         salt = entry->pw_passwd;
  74.  
  75.     /*
  76.      * Now, perform the encryption using the salt from before on
  77.      * the users input.  Since we always encrypt the string, it
  78.      * should be very difficult to determine if the user exists by
  79.      * looking at execution time.
  80.      */
  81.  
  82.     encrypt = pw_encrypt (password, salt);
  83.  
  84.     /*
  85.      * One last time we must deal with there being no password file
  86.      * entry for the user.  We use the pw_passwd == NULL idiom to
  87.      * cause non-existent users to not be validated.
  88.      */
  89.  
  90.     if (entry->pw_name && strcmp (encrypt, entry->pw_passwd) == 0)
  91.         return (1);
  92.     else
  93.         return (0);
  94. }
  95.