summaryrefslogtreecommitdiff
path: root/ext/crack/crack.c
blob: 268e4b048f45105f52d01e0e696a8efb0c6bcc61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2001 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors:                                                             |
   +----------------------------------------------------------------------+
 */
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_crack.h"

#if HAVE_CRACK

#include <packer.h>

ZEND_DECLARE_MODULE_GLOBALS(crack)

/* True global resources - no need for thread safety here */
static int le_crack;

function_entry crack_functions[] = {
	ZEND_FE(crack_opendict, NULL)
	ZEND_FE(crack_closedict, NULL)
	ZEND_FE(crack_check,	NULL)
	ZEND_FE(crack_getlastmessage, NULL)
	{NULL, NULL, NULL}
};

zend_module_entry crack_module_entry = {
	"crack",
	crack_functions,
	ZEND_MODULE_STARTUP_N(crack),
	ZEND_MODULE_SHUTDOWN_N(crack),
	ZEND_MODULE_ACTIVATE_N(crack),
	ZEND_MODULE_DEACTIVATE_N(crack),
	ZEND_MODULE_INFO_N(crack),
	STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_CRACK
ZEND_GET_MODULE(crack)
#endif

PHP_INI_BEGIN()
	STD_PHP_INI_ENTRY("crack.default_dictionary", NULL, PHP_INI_SYSTEM, OnUpdateString, default_dictionary, zend_crack_globals, crack_globals)
PHP_INI_END()

long _crack_open_dict(char *dictpath TSRMLS_DC)
{
	PWDICT *pwdict;
	long resource;

	if (CRACKG(current_id) != -1) {
		zend_error(E_WARNING, "Can not use more than one open dictionary with this implementation of libcrack");
		return -1;
	}
	if (NULL == (pwdict = PWOpen(dictpath, "r"))) {
		zend_error(E_WARNING, "Unable to open a crack dictionary");
		return -1;
	}

	resource = zend_list_insert(pwdict, le_crack);

/*	if (CRACKG(current_id) != -1) {
		zend_list_delete(CRACKG(current_id));
	}
*/
	CRACKG(current_id) = resource;
	
	return resource;
}

void _close_crack_dict(PWDICT *pwdict)
{
	PWClose(pwdict);
}

ZEND_MODULE_STARTUP_D(crack)
{
	REGISTER_INI_ENTRIES();

	le_crack = register_list_destructors(_close_crack_dict, NULL);

	return SUCCESS;
}

ZEND_MODULE_SHUTDOWN_D(crack)
{
	UNREGISTER_INI_ENTRIES();
	return SUCCESS;
}

ZEND_MODULE_ACTIVATE_D(crack)
{
	CRACKG(last_message) = NULL;
	CRACKG(current_id) = -1;

	return SUCCESS;
}

ZEND_MODULE_DEACTIVATE_D(crack)
{
	if (NULL != CRACKG(last_message)) {
		efree(CRACKG(last_message));
	}
	return SUCCESS;
}

ZEND_MODULE_INFO_D(crack)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "crack support", "enabled");
	php_info_print_table_end();

	DISPLAY_INI_ENTRIES();
}

/* {{{ proto string crack_opendict(string dictionary)
   Opens a new cracklib dictionary */
ZEND_FUNCTION(crack_opendict)
{
	zval **dictpath;
	long resource;

	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &dictpath) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_string_ex(dictpath);

	if (-1 == (resource = _crack_open_dict(Z_STRVAL_PP(dictpath) TSRMLS_CC))) {
		RETURN_FALSE;
	}
	
	RETURN_RESOURCE(resource);
}
/* }}} */

/* {{{ proto string crack_closedict([int link_identifier])
   Closes an open cracklib dictionary. */
ZEND_FUNCTION(crack_closedict)
{
	PWDICT *pwdict;
	zval **dictionary;
	long id;

	switch (ZEND_NUM_ARGS()) {
		case 0:
			id = CRACKG(current_id);
			break;
		case 1:
			if (zend_get_parameters_ex(1, &dictionary) == FAILURE) {
				WRONG_PARAM_COUNT;
			}
			id = Z_LVAL_PP(dictionary);
			break;
		default:
			WRONG_PARAM_COUNT;
			break;
	}

	ZEND_FETCH_RESOURCE(pwdict, PWDICT *, dictionary, id, "cracklib dictionary", le_crack);
	if (CRACKG(current_id) == id) {
		CRACKG(current_id) = -1;
	}
	zend_list_delete(id);

	RETURN_TRUE;
}
/* }}} */

/* {{{ proto string crack_check([int dictionary, ] string password)
   Performs an obscure check with the given password */
ZEND_FUNCTION(crack_check)
{
	zval **dictionary = NULL, **password;
	char pwtrunced[STRINGSIZE];
	char *message;
	PWDICT *pwdict;
	long id;

	switch (ZEND_NUM_ARGS()) {
		case 1:
			if (zend_get_parameters_ex(1, &password) == FAILURE) {
				RETURN_FALSE;
			}
			if (NULL != CRACKG(default_dictionary) && CRACKG(current_id) == -1) {
				_crack_open_dict(CRACKG(default_dictionary) TSRMLS_CC);
			}
			id = CRACKG(current_id);
			break;
		case 2:
			if (zend_get_parameters_ex(2, &dictionary, &password) == FAILURE) {
				RETURN_FALSE;
			}
			id = -1;
			break;
		default:
			WRONG_PARAM_COUNT;
		break;
	}

	ZEND_FETCH_RESOURCE(pwdict, PWDICT *, dictionary, id, "cracklib dictionary", le_crack);
	convert_to_string_ex(password);

	/* Prevent buffer overflow attacks. */
	strlcpy(pwtrunced, Z_STRVAL_PP(password), sizeof(pwtrunced));

	message = (char *)FascistLook(pwdict, pwtrunced);

	if (NULL != CRACKG(last_message)) {
		efree(CRACKG(last_message));
	}

	if (NULL == message) {
		CRACKG(last_message) = estrdup("strong password");
		RETURN_TRUE;
	}
	
	CRACKG(last_message) = estrdup(message);
	RETURN_FALSE;
}
/* }}} */

/* {{{ proto string crack_getlastmessage()
   Returns the message from the last obscure check. */
ZEND_FUNCTION(crack_getlastmessage)
{
	if (ZEND_NUM_ARGS() != 0) {
		WRONG_PARAM_COUNT;
	}
	
	if (NULL == CRACKG(last_message)) {
		zend_error(E_WARNING, "No obscure checks in this session");
		RETURN_FALSE;
	}

	RETURN_STRING(CRACKG(last_message), 1);
}
/* }}} */

#endif	/* HAVE_CRACK */


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: sw=4 ts=4 fdm=marker
 * vim<600: sw=4 ts=4
 */