-
Notifications
You must be signed in to change notification settings - Fork 10
/
session.c
311 lines (252 loc) · 8.93 KB
/
session.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
+----------------------------------------------------------------------+
| PHP Version 5 / Tokyo tyrant |
+----------------------------------------------------------------------+
| Copyright (c) 2009-2010 Mikko Koppanen |
+----------------------------------------------------------------------+
| This source file is dual-licensed. |
| It is available under the terms of New BSD License that is bundled |
| with this package in the file LICENSE and available under the terms |
| of PHP license version 3.01. PHP license is bundled with this |
| package in the file LICENSE and can be obtained through the |
| world-wide-web at the following url: |
| http://www.php.net/license/3_01.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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Mikko Kopppanen <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_tokyo_tyrant_session.h"
#include "php_tokyo_tyrant_session_funcs.h"
ps_module ps_mod_tokyo_tyrant = {
PS_MOD_SID(tokyo_tyrant)
};
#define TT_SESS_DATA php_tt_session *session = PS_GET_MOD_DATA();
/* {{{ Create new session id */
PS_CREATE_SID_FUNC(tokyo_tyrant)
{
php_tt_conn *conn;
php_tt_server *server;
php_tt_server_pool *pool;
char *current_rand = NULL;
char *sess_rand, *sid, *pk = NULL;
int idx = -1, pk_len;
zend_bool is_regenerated = 0;
if (!TOKYO_G(salt)) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "tokyo_tyrant.session_salt needs to be set in order to use the session handler");
}
/* Session id is being regenerated. Need to copy some data */
if (PS(session_status) == php_session_active) {
TT_SESS_DATA;
if (!session) {
/* This is a situation where session_regenerate_id(TRUE) is called */
is_regenerated = 1;
} else {
/* Use old values unless regeneration is forced */
if (session->remap == 0) {
idx = session->idx;
pk = estrdup(session->pk);
current_rand = estrdup(session->sess_rand);
} else {
session->remap = 0;
}
}
}
/* Create the random part of the session id */
sess_rand = php_session_create_id(PS(mod_data), NULL TSRMLS_CC);
if (!sess_rand) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to generate session id");
}
/* Init the server pool */
pool = php_tt_pool_init2(PS(save_path) TSRMLS_CC);
if (!pool) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to parse session.save_path");
}
/* Create idx if there isn't one already */
if (idx == -1) {
idx = php_tt_pool_map(pool, sess_rand TSRMLS_CC);
if (idx < 0) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to map the session to a server");
}
}
/* Get the server for the node */
server = php_tt_pool_get_server(pool, idx TSRMLS_CC);
if (!server) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Internal error: idx does not map to a server (should not happen)");
}
/* Create connection to the server */
conn = php_tt_conn_init(TSRMLS_C);
if (!php_tt_connect_ex(conn, server->host, server->port, TOKYO_G(default_timeout), 1 TSRMLS_CC)) {
php_tt_server_fail_incr(server->host, server->port TSRMLS_CC);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to the session server");
}
if (!pk) {
pk = php_tt_create_pk(conn, &pk_len TSRMLS_CC);
if (!pk) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to create a primary key. Not connected to a table database?");
}
} else {
if (!php_tt_sess_touch(conn, current_rand, sess_rand, pk, strlen(pk) TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to update the session");
}
efree(current_rand);
}
sid = php_tt_create_sid(sess_rand, idx, pk, TOKYO_G(salt) TSRMLS_CC);
efree(sess_rand);
efree(pk);
php_tt_conn_deinit(conn TSRMLS_CC);
php_tt_pool_deinit(pool TSRMLS_CC);
/* This is a situation where session_regenerate_id(TRUE) is called */
if (is_regenerated) {
int ret = ps_open_tokyo_tyrant(mod_data, PS(save_path), PS(session_name) TSRMLS_CC);
if (ret == SUCCESS) {
char *dummy_val = NULL;
int val_len = 0;
ret = ps_read_tokyo_tyrant(mod_data, sid, &dummy_val, &val_len TSRMLS_CC);
if (ret != SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read session data during regeneration");
}
if (dummy_val) {
efree(dummy_val);
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open the session during regeneration");
}
}
return sid;
}
/* }}} */
/* {{{ PS_OPEN */
PS_OPEN_FUNC(tokyo_tyrant)
{
php_tt_session *session = php_tt_session_init(TSRMLS_C);
if (!session) {
PS_SET_MOD_DATA(NULL);
return FAILURE;
}
session->pool = php_tt_pool_init2(PS(save_path) TSRMLS_CC);
if (!session->pool) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to parse session.save_path");
}
PS_SET_MOD_DATA(session);
return SUCCESS;
}
/* }}} */
PS_READ_FUNC(tokyo_tyrant)
{
TT_SESS_DATA;
php_tt_server *server;
zend_bool mismatch;
/* Try to tokenize session id */
if (!php_tt_tokenize((char *)key, &(session->sess_rand), &(session->checksum), &(session->idx), &(session->pk) TSRMLS_CC)) {
session->remap = 1;
PS(invalid_session_id) = 1;
return FAILURE;
}
/* Set additional data */
session->sess_rand_len = strlen(session->sess_rand);
session->checksum_len = strlen(session->checksum);
session->pk_len = strlen(session->pk);
/* Validate the session id */
if (!php_tt_validate(session->sess_rand, session->checksum, session->idx, session->pk, TOKYO_G(salt) TSRMLS_CC)) {
session->remap = 1;
PS(invalid_session_id) = 1;
return FAILURE;
}
server = php_tt_pool_get_server(session->pool, session->idx TSRMLS_CC);
if (!server) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Internal error: idx does not map to a server");
session->remap = 1;
PS(invalid_session_id) = 1;
return FAILURE;
}
session->conn = php_tt_conn_init(TSRMLS_C);
if (!php_tt_connect_ex(session->conn, server->host, server->port, TOKYO_G(default_timeout), 1 TSRMLS_CC)) {
php_tt_server_fail_incr(server->host, server->port TSRMLS_CC);
/* Remap if the server has been failed */
if (!php_tt_server_ok(server->host, server->port TSRMLS_CC)) {
session->remap = 1;
PS(invalid_session_id) = 1;
return FAILURE;
}
}
*val = php_tt_get_sess_data(session->conn, session->sess_rand, session->pk, session->pk_len, vallen, &mismatch TSRMLS_CC);
if (*val == NULL) {
/* Session got mapped to wrong server */
if (mismatch) {
session->remap = 1;
PS(invalid_session_id) = 1;
return FAILURE;
}
/* Return empty data */
*val = estrdup("");
}
return SUCCESS;
}
PS_WRITE_FUNC(tokyo_tyrant)
{
TT_SESS_DATA;
php_tt_server *server;
/* Try to tokenize session id */
efree(session->sess_rand);
efree(session->checksum);
efree(session->pk);
if (!php_tt_tokenize((char *)key, &(session->sess_rand), &(session->checksum), &(session->idx), &(session->pk) TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse the session id");
session->remap = 1;
PS(invalid_session_id) = 1;
return FAILURE;
}
/* Set additional data */
session->sess_rand_len = strlen(session->sess_rand);
session->checksum_len = strlen(session->checksum);
session->pk_len = strlen(session->pk);
if (!php_tt_validate(session->sess_rand, session->checksum, session->idx, session->pk, TOKYO_G(salt) TSRMLS_CC)) {
return FAILURE;
}
if (!php_tt_save_sess_data(session->conn, session->sess_rand, session->pk, strlen(session->pk), val, vallen TSRMLS_CC)) {
server = php_tt_pool_get_server(session->pool, session->idx TSRMLS_CC);
php_tt_server_fail_incr(server->host, server->port TSRMLS_CC);
/* Remap if the server has been marked as failed */
if (!php_tt_server_ok(server->host, server->port TSRMLS_CC)) {
session->remap = 1;
PS(invalid_session_id) = 1;
return FAILURE;
}
return FAILURE;
}
return SUCCESS;
}
PS_DESTROY_FUNC(tokyo_tyrant)
{
TT_SESS_DATA;
zend_bool res;
res = php_tt_sess_destroy(session->conn, session->pk, session->pk_len TSRMLS_CC);
php_tt_session_deinit(session TSRMLS_CC);
session = NULL;
PS_SET_MOD_DATA(NULL);
if (!res) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to destroy the session");
return FAILURE;
}
return SUCCESS;
}
PS_GC_FUNC(tokyo_tyrant)
{
/* Handle expiration on PHP side? */
if (TOKYO_G(php_expiration)) {
TT_SESS_DATA;
return php_tt_gc(session->pool TSRMLS_CC);
}
return SUCCESS;
}
PS_CLOSE_FUNC(tokyo_tyrant)
{
TT_SESS_DATA;
php_tt_session_deinit(session TSRMLS_CC);
session = NULL;
PS_SET_MOD_DATA(NULL);
return SUCCESS;
}