You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.6 KiB

24 years ago
24 years ago
27 years ago
27 years ago
27 years ago
24 years ago
25 years ago
25 years ago
27 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2002 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Stig Bakken <ssb@gaurdian.no> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. | Rasmus Lerdorf <rasmus@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #include <stdlib.h>
  22. #include "php.h"
  23. #if HAVE_CRYPT
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #if HAVE_CRYPT_H
  28. #include <crypt.h>
  29. #endif
  30. #if TM_IN_SYS_TIME
  31. #include <sys/time.h>
  32. #else
  33. #include <time.h>
  34. #endif
  35. #if HAVE_STRING_H
  36. #include <string.h>
  37. #else
  38. #include <strings.h>
  39. #endif
  40. #ifdef PHP_WIN32
  41. #include <process.h>
  42. extern char *crypt(char *__key, char *__salt);
  43. #endif
  44. #include "php_lcg.h"
  45. #include "php_crypt.h"
  46. #include "php_rand.h"
  47. /*
  48. The capabilities of the crypt() function is determined by the test programs
  49. run by configure from aclocal.m4. They will set PHP_STD_DES_CRYPT,
  50. PHP_EXT_DES_CRYPT, PHP_MD5_CRYPT and PHP_BLOWFISH_CRYPT as appropriate
  51. for the target platform
  52. */
  53. #if PHP_STD_DES_CRYPT
  54. #define PHP_MAX_SALT_LEN 2
  55. #endif
  56. #if PHP_EXT_DES_CRYPT
  57. #undef PHP_MAX_SALT_LEN
  58. #define PHP_MAX_SALT_LEN 9
  59. #endif
  60. #if PHP_MD5_CRYPT
  61. #undef PHP_MAX_SALT_LEN
  62. #define PHP_MAX_SALT_LEN 12
  63. #endif
  64. #if PHP_BLOWFISH_CRYPT
  65. #undef PHP_MAX_SALT_LEN
  66. #define PHP_MAX_SALT_LEN 60
  67. #endif
  68. /*
  69. * If the configure-time checks fail, we provide DES.
  70. * XXX: This is a hack. Fix the real problem
  71. */
  72. #ifndef PHP_MAX_SALT_LEN
  73. #define PHP_MAX_SALT_LEN 2
  74. #undef PHP_STD_DES_CRYPT
  75. #define PHP_STD_DES_CRYPT 1
  76. #endif
  77. #define PHP_CRYPT_RAND php_rand(TSRMLS_C)
  78. static int php_crypt_rand_seeded=0;
  79. PHP_MINIT_FUNCTION(crypt)
  80. {
  81. REGISTER_LONG_CONSTANT("CRYPT_SALT_LENGTH", PHP_MAX_SALT_LEN, CONST_CS | CONST_PERSISTENT);
  82. REGISTER_LONG_CONSTANT("CRYPT_STD_DES", PHP_STD_DES_CRYPT, CONST_CS | CONST_PERSISTENT);
  83. REGISTER_LONG_CONSTANT("CRYPT_EXT_DES", PHP_EXT_DES_CRYPT, CONST_CS | CONST_PERSISTENT);
  84. REGISTER_LONG_CONSTANT("CRYPT_MD5", PHP_MD5_CRYPT, CONST_CS | CONST_PERSISTENT);
  85. REGISTER_LONG_CONSTANT("CRYPT_BLOWFISH", PHP_BLOWFISH_CRYPT, CONST_CS | CONST_PERSISTENT);
  86. return SUCCESS;
  87. }
  88. PHP_RINIT_FUNCTION(crypt)
  89. {
  90. if(!php_crypt_rand_seeded) {
  91. php_srand(time(0) * getpid() * (unsigned long) (php_combined_lcg(TSRMLS_C) * 10000.0) TSRMLS_CC);
  92. php_crypt_rand_seeded=1;
  93. }
  94. return SUCCESS;
  95. }
  96. static unsigned char itoa64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  97. static void php_to64(char *s, long v, int n)
  98. {
  99. while (--n >= 0) {
  100. *s++ = itoa64[v&0x3f];
  101. v >>= 6;
  102. }
  103. }
  104. /* {{{ proto string crypt(string str [, string salt])
  105. Encrypt a string */
  106. PHP_FUNCTION(crypt)
  107. {
  108. char salt[PHP_MAX_SALT_LEN+1];
  109. char *str, *salt_in = NULL;
  110. int str_len, salt_in_len;
  111. salt[0]=salt[PHP_MAX_SALT_LEN]='\0';
  112. /* This will produce suitable results if people depend on DES-encryption
  113. available (passing always 2-character salt). At least for glibc6.1 */
  114. memset(&salt[1], '$', PHP_MAX_SALT_LEN-1);
  115. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len,
  116. &salt_in, &salt_in_len) == FAILURE) {
  117. return;
  118. }
  119. if (salt_in) {
  120. memcpy(salt, salt_in, MIN(PHP_MAX_SALT_LEN, salt_in_len));
  121. }
  122. /* The automatic salt generation only covers standard DES and md5-crypt */
  123. if(!*salt) {
  124. #if PHP_MD5_CRYPT
  125. strcpy(salt, "$1$");
  126. php_to64(&salt[3], PHP_CRYPT_RAND, 4);
  127. php_to64(&salt[7], PHP_CRYPT_RAND, 4);
  128. strcpy(&salt[11], "$");
  129. #elif PHP_STD_DES_CRYPT
  130. php_to64(&salt[0], PHP_CRYPT_RAND, 2);
  131. salt[2] = '\0';
  132. #endif
  133. }
  134. RETVAL_STRING(crypt(str, salt), 1);
  135. }
  136. /* }}} */
  137. #endif
  138. /*
  139. * Local variables:
  140. * tab-width: 4
  141. * c-basic-offset: 4
  142. * End:
  143. * vim600: sw=4 ts=4 fdm=marker
  144. * vim<600: sw=4 ts=4
  145. */