~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/standard/crypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

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