xcrypt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2003 Ben Lindstrom. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "includes.h"
  25. #include <sys/types.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <pwd.h>
  29. # if defined(HAVE_CRYPT_H) && !defined(HAVE_SECUREWARE)
  30. # include <crypt.h>
  31. # endif
  32. # ifdef __hpux
  33. # include <hpsecurity.h>
  34. # include <prot.h>
  35. # endif
  36. # ifdef HAVE_SECUREWARE
  37. # include <sys/security.h>
  38. # include <sys/audit.h>
  39. # include <prot.h>
  40. # endif
  41. # if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
  42. # include <shadow.h>
  43. # endif
  44. # if defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW)
  45. # include <sys/label.h>
  46. # include <sys/audit.h>
  47. # include <pwdadj.h>
  48. # endif
  49. # if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
  50. # include "md5crypt.h"
  51. # endif
  52. # if defined(WITH_OPENSSL) && !defined(HAVE_CRYPT) && defined(HAVE_DES_CRYPT)
  53. # include <openssl/des.h>
  54. # define crypt DES_crypt
  55. # endif
  56. /*
  57. * Pick an appropriate password encryption type and salt for the running
  58. * system by searching through accounts until we find one that has a valid
  59. * salt. Usually this will be root unless the root account is locked out.
  60. * If we don't find one we return a traditional DES-based salt.
  61. */
  62. static const char *
  63. pick_salt(void)
  64. {
  65. struct passwd *pw;
  66. char *passwd, *p;
  67. size_t typelen;
  68. static char salt[32];
  69. if (salt[0] != '\0')
  70. return salt;
  71. strlcpy(salt, "xx", sizeof(salt));
  72. setpwent();
  73. while ((pw = getpwent()) != NULL) {
  74. if ((passwd = shadow_pw(pw)) == NULL)
  75. continue;
  76. if (passwd[0] == '$' && (p = strrchr(passwd+1, '$')) != NULL) {
  77. typelen = p - passwd + 1;
  78. strlcpy(salt, passwd, MIN(typelen, sizeof(salt)));
  79. explicit_bzero(passwd, strlen(passwd));
  80. goto out;
  81. }
  82. }
  83. out:
  84. endpwent();
  85. return salt;
  86. }
  87. char *
  88. xcrypt(const char *password, const char *salt)
  89. {
  90. char *crypted;
  91. /*
  92. * If we don't have a salt we are encrypting a fake password for
  93. * for timing purposes. Pick an appropriate salt.
  94. */
  95. if (salt == NULL)
  96. salt = pick_salt();
  97. # ifdef HAVE_MD5_PASSWORDS
  98. if (is_md5_salt(salt))
  99. crypted = md5_crypt(password, salt);
  100. else
  101. crypted = crypt(password, salt);
  102. # elif defined(__hpux) && !defined(HAVE_SECUREWARE)
  103. if (iscomsec())
  104. crypted = bigcrypt(password, salt);
  105. else
  106. crypted = crypt(password, salt);
  107. # elif defined(HAVE_SECUREWARE)
  108. crypted = bigcrypt(password, salt);
  109. # else
  110. crypted = crypt(password, salt);
  111. # endif
  112. return crypted;
  113. }
  114. /*
  115. * Handle shadowed password systems in a cleaner way for portable
  116. * version.
  117. */
  118. char *
  119. shadow_pw(struct passwd *pw)
  120. {
  121. char *pw_password = pw->pw_passwd;
  122. # if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
  123. struct spwd *spw = getspnam(pw->pw_name);
  124. if (spw != NULL)
  125. pw_password = spw->sp_pwdp;
  126. # endif
  127. #ifdef USE_LIBIAF
  128. return(get_iaf_password(pw));
  129. #endif
  130. # if defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW)
  131. struct passwd_adjunct *spw;
  132. if (issecure() && (spw = getpwanam(pw->pw_name)) != NULL)
  133. pw_password = spw->pwa_passwd;
  134. # elif defined(HAVE_SECUREWARE)
  135. struct pr_passwd *spw = getprpwnam(pw->pw_name);
  136. if (spw != NULL)
  137. pw_password = spw->ufld.fd_encrypt;
  138. # endif
  139. return pw_password;
  140. }