forked from mongodb/mongo-php-driver-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_stream.c
427 lines (356 loc) · 13.6 KB
/
io_stream.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
* Copyright 2009-2013 10gen, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "io_stream.h"
#include "mcon/types.h"
#include "mcon/utils.h"
#include "mcon/manager.h"
#include "mcon/connections.h"
#include "php_mongo.h"
#include <php.h>
#include <main/php_streams.h>
#include <main/php_network.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#else
# if WIN32
# include "config-w32.h"
# endif
#endif
#if HAVE_MONGO_SASL
#include <sasl/sasl.h>
#include <sasl/saslutil.h>
#endif
void* php_mongo_io_stream_connect(mongo_con_manager *manager, mongo_server_def *server, mongo_server_options *options, char **error_message)
{
char *errmsg;
int errcode;
php_stream *stream;
char *hash = mongo_server_create_hash(server);
struct timeval ctimeout = {0};
char *dsn;
int dsn_len;
TSRMLS_FETCH();
if (server->host[0] == '/') {
dsn_len = spprintf(&dsn, 0, "unix://%s", server->host);
} else {
dsn_len = spprintf(&dsn, 0, "tcp://%s:%d", server->host, server->port);
}
if (options->connectTimeoutMS) {
ctimeout.tv_sec = options->connectTimeoutMS / 1000;
ctimeout.tv_usec = (options->connectTimeoutMS % 1000) * 1000;
}
stream = php_stream_xport_create(dsn, dsn_len, 0, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hash, options->connectTimeoutMS ? &ctimeout : NULL, (php_stream_context *)options->ctx, &errmsg, &errcode);
efree(dsn);
free(hash);
if (!stream) {
/* error_message will be free()d, but errmsg was allocated by PHP and needs efree() */
*error_message = strdup(errmsg);
efree(errmsg);
return NULL;
}
if (options->ssl) {
if (php_stream_xport_crypto_setup(stream, STREAM_CRYPTO_METHOD_TLS_CLIENT, NULL TSRMLS_CC) < 0) {
*error_message = strdup("Cannot setup SSL, is ext/openssl loaded?");
php_stream_close(stream);
return NULL;
}
if (php_stream_xport_crypto_enable(stream, 1 TSRMLS_CC) < 0) {
/* Setting up crypto failed. Thats only OK if we only preferred it */
if (options->ssl == MONGO_SSL_PREFER) {
/* FIXME: We can't actually get here because we reject setting
* this option to prefer in mcon/parse.c. This is however
* probably what we need to do in the future when mongod starts
* actually supporting this! :) */
mongo_manager_log(manager, MLOG_CON, MLOG_INFO, "stream_connect: Failed establishing SSL for %s:%d", server->host, server->port);
php_stream_xport_crypto_enable(stream, 0 TSRMLS_CC);
} else {
*error_message = strdup("Can't connect over SSL, is mongod running with SSL?");
php_stream_close(stream);
return NULL;
}
} else {
mongo_manager_log(manager, MLOG_CON, MLOG_INFO, "stream_connect: Establish SSL for %s:%d", server->host, server->port);
}
} else {
mongo_manager_log(manager, MLOG_CON, MLOG_INFO, "stream_connect: Not establishing SSL for %s:%d", server->host, server->port);
}
php_stream_notify_progress_init(stream->context, 0, 0);
if (options->socketTimeoutMS) {
struct timeval rtimeout = {0};
rtimeout.tv_sec = options->socketTimeoutMS / 1000;
rtimeout.tv_usec = (options->socketTimeoutMS % 1000) * 1000;
php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &rtimeout);
}
/* Avoid a weird leak warning in debug mode when freeing the stream */
#if ZEND_DEBUG
stream->__exposed = 1;
#endif
return stream;
}
/* Returns the bytes read on success
* Returns -31 on unknown failure
* Returns -80 on timeout
* Returns -32 when remote server closes the connection
*/
int php_mongo_io_stream_read(mongo_connection *con, mongo_server_options *options, int timeout, void *data, int size, char **error_message)
{
int num = 1, received = 0;
TSRMLS_FETCH();
if (timeout > 0 && options->socketTimeoutMS != timeout) {
struct timeval rtimeout = {0};
rtimeout.tv_sec = timeout / 1000;
rtimeout.tv_usec = (timeout % 1000) * 1000;
php_stream_set_option(con->socket, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &rtimeout);
}
/* this can return FAILED if there is just no more data from db */
while (received < size && num > 0) {
int len = 4096 < (size - received) ? 4096 : size - received;
num = php_stream_read(con->socket, (char *) data, len);
if (num < 0) {
/* Doesn't look like this can happen, php_sockop_read overwrites
* the failure from recv() to return 0 */
*error_message = strdup("Read from socket failed");
return -31;
}
/* It *may* have failed. It also may simply have no data */
if (num == 0) {
zval *metadata;
MAKE_STD_ZVAL(metadata);
array_init(metadata);
if (php_stream_populate_meta_data(con->socket, metadata)) {
zval **tmp;
if (zend_hash_find(Z_ARRVAL_P(metadata), "timed_out", sizeof("timed_out"), (void**)&tmp) == SUCCESS) {
convert_to_boolean_ex(tmp);
if (Z_BVAL_PP(tmp)) {
struct timeval rtimeout = {0};
if (timeout > 0 && options->socketTimeoutMS != timeout) {
rtimeout.tv_sec = timeout / 1000;
rtimeout.tv_usec = (timeout % 1000) * 1000;
} else {
rtimeout.tv_sec = options->socketTimeoutMS / 1000;
rtimeout.tv_usec = (options->socketTimeoutMS % 1000) * 1000;
}
*error_message = malloc(256);
snprintf(*error_message, 256, "Read timed out after reading %d bytes, waited for %d.%06d seconds", num, rtimeout.tv_sec, rtimeout.tv_usec);
zval_ptr_dtor(&metadata);
return -80;
}
}
if (zend_hash_find(Z_ARRVAL_P(metadata), "eof", sizeof("eof"), (void**)&tmp) == SUCCESS) {
convert_to_boolean_ex(tmp);
if (Z_BVAL_PP(tmp)) {
*error_message = strdup("Remote server has closed the connection");
zval_ptr_dtor(&metadata);
return -32;
}
}
}
zval_ptr_dtor(&metadata);
}
data = (char*)data + num;
received += num;
}
if (options && options->ctx) {
php_stream_notify_progress_increment((php_stream_context *)options->ctx, received, size);
}
if (timeout > 0 && options->socketTimeoutMS != timeout) {
struct timeval rtimeout = {0};
rtimeout.tv_sec = options->socketTimeoutMS / 1000;
rtimeout.tv_usec = (options->socketTimeoutMS % 1000) * 1000;
php_stream_set_option(con->socket, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &rtimeout);
}
return received;
}
int php_mongo_io_stream_send(mongo_connection *con, mongo_server_options *options, void *data, int size, char **error_message)
{
int retval;
TSRMLS_FETCH();
retval = php_stream_write(con->socket, (char *) data, size);
return retval;
}
void php_mongo_io_stream_close(mongo_connection *con, int why)
{
TSRMLS_FETCH();
if (why == MONGO_CLOSE_BROKEN) {
if (con->socket) {
php_stream_free(con->socket, PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR);
}
} else if (why == MONGO_CLOSE_SHUTDOWN) {
/* No need to do anything, it was freed from the persistent_list */
}
}
void php_mongo_io_stream_forget(mongo_con_manager *manager, mongo_connection *con)
{
zend_rsrc_list_entry *le;
TSRMLS_FETCH();
/* When we fork we need to unregister the parents hash so we don't
* accidentally destroy it */
if (zend_hash_find(&EG(persistent_list), con->hash, strlen(con->hash) + 1, (void*) &le) == SUCCESS) {
((php_stream *)con->socket)->in_free = 1;
zend_hash_del(&EG(persistent_list), con->hash, strlen(con->hash) + 1);
((php_stream *)con->socket)->in_free = 0;
}
}
#if HAVE_MONGO_SASL
int is_sasl_failure(sasl_conn_t *conn, int result, char **error_message)
{
if (result < 0) {
spprintf(error_message, 0, "Authentication error: %s", sasl_errdetail(conn));
return 1;
}
return 0;
}
sasl_conn_t *php_mongo_saslstart(mongo_con_manager *manager, mongo_connection *con, mongo_server_options *options, mongo_server_def *server_def, sasl_conn_t *conn, char **out_payload, int *out_payload_len, int32_t *conversation_id, char **error_message)
{
const char *raw_payload;
char encoded_payload[4096];
unsigned int raw_payload_len, encoded_payload_len;
int result;
char *mechanism_list = "GSSAPI";
const char *mechanism_selected;
sasl_interact_t *client_interact=NULL;
result = sasl_client_start(conn, mechanism_list, &client_interact, &raw_payload, &raw_payload_len, &mechanism_selected);
if (is_sasl_failure(conn, result, error_message)) {
return NULL;
}
/* TODO: Deal with interactions, if any, when mongod starts supporting such mechanisms */
if (result != SASL_CONTINUE) {
*error_message = strdup("Could not negotiate SASL mechanism");
return NULL;
}
mechanism_selected = "GSSAPI";
result = sasl_encode64(raw_payload, raw_payload_len, encoded_payload, sizeof(encoded_payload), &encoded_payload_len);
if (is_sasl_failure(conn, result, error_message)) {
return NULL;
}
if (!mongo_connection_authenticate_saslstart(manager, con, options, server_def, (char *)mechanism_selected, encoded_payload, encoded_payload_len + 1, out_payload, out_payload_len, conversation_id, error_message)) {
free(out_payload);
*error_message = strdup("saslStart failed miserably");
return NULL;
}
return conn;
}
int php_mongo_saslcontinue(mongo_con_manager *manager, mongo_connection *con, mongo_server_options *options, mongo_server_def *server_def, sasl_conn_t *conn, char *step_payload, int step_payload_len, int32_t conversation_id, char **error_message) {
int result;
sasl_interact_t *client_interact=NULL;
do {
char base_payload[4096], payload[4096];
unsigned int base_payload_len, payload_len;
unsigned char done;
const char *out;
unsigned int outlen;
result = sasl_decode64(step_payload, step_payload_len, base_payload, sizeof(base_payload), &base_payload_len);
if (is_sasl_failure(conn, result, error_message)) {
return result;
}
result = sasl_client_step(conn, (const char *)base_payload, base_payload_len, &client_interact, &out, &outlen);
if (is_sasl_failure(conn, result, error_message)) {
return result;
}
/* TODO : Deal with interaction */
if (result == SASL_OK) {
/* We are all done */
break;
}
result = sasl_encode64(out, outlen, payload, sizeof(base_payload), &payload_len);
if (is_sasl_failure(conn, result, error_message)) {
return result;
}
if (!mongo_connection_authenticate_saslcontinue(manager, con, options, server_def, conversation_id, payload, payload_len + 1, &step_payload, &step_payload_len, &done, (char **)&error_message)) {
*error_message = strdup("saslStart failed miserably");
return result;
}
} while (result == SASL_INTERACT || result == SASL_CONTINUE);
return result;
}
int php_mongo_io_authenticate_gssapi(mongo_con_manager *manager, mongo_connection *con, mongo_server_options *options, mongo_server_def *server_def, char **error_message)
{
int result;
char *initpayload;
int initpayload_len;
sasl_conn_t *conn;
int32_t conversation_id;
result = sasl_client_new(options->gssapiServiceName, server_def->host, NULL, NULL, NULL, 0, &conn);
if (result != SASL_OK) {
sasl_dispose(&conn);
sasl_client_done();
*error_message = strdup("Could not initialize a client exchange (SASL) to MongoDB");
return 0;
}
conn = php_mongo_saslstart(manager, con, options, server_def, conn, &initpayload, &initpayload_len, &conversation_id, error_message);
if (!conn) {
sasl_dispose(&conn);
sasl_client_done();
/* error message populate by php_mongo_saslstart() */
return 0;
}
php_mongo_saslcontinue(manager, con, options, server_def, conn, initpayload, initpayload_len, conversation_id, error_message);
free(initpayload);
sasl_dispose(&conn);
sasl_client_done();
return 1;
}
int php_mongo_io_authenticate_plain(mongo_con_manager *manager, mongo_connection *con, mongo_server_options *options, mongo_server_def *server_def, char **error_message)
{
char step_payload[4096];
char *out, *plain;
char *mechanism = "PLAIN";
unsigned int step_payload_len, plain_len;
int outlen;
int32_t step_conversation_id;
int result;
plain_len = spprintf(&plain, 0, "%c%s%c%s", '\0', server_def->username, '\0', server_def->password);
result = sasl_encode64(plain, plain_len, step_payload, sizeof(step_payload), &step_payload_len);
if (result != SASL_OK) {
*error_message = strdup("SASL authentication: Could not base64 encode payload");
efree(plain);
return 0;
}
efree(plain);
if (!mongo_connection_authenticate_saslstart(manager, con, options, server_def, mechanism, step_payload, step_payload_len + 1, &out, &outlen, &step_conversation_id, error_message)) {
return 0;
}
free(out);
return 1;
}
#endif
int php_mongo_io_stream_authenticate(mongo_con_manager *manager, mongo_connection *con, mongo_server_options *options, mongo_server_def *server_def, char **error_message)
{
/* Use the mcon implementation of MongoDB-CR (default) */
if (server_def->mechanism == MONGO_AUTH_MECHANISM_MONGODB_CR) {
return mongo_connection_authenticate(manager, con, options, server_def, error_message);
}
#if HAVE_MONGO_SASL
if (server_def->mechanism == MONGO_AUTH_MECHANISM_GSSAPI) {
return php_mongo_io_authenticate_gssapi(manager, con, options, server_def, error_message);
}
if (server_def->mechanism == MONGO_AUTH_MECHANISM_PLAIN) {
return php_mongo_io_authenticate_plain(manager, con, options, server_def, error_message);
}
*error_message = strdup("Unknown authentication mechanism. Only MongoDB-CR, GSSAPI and PLAIN is supported by this build");
#else
*error_message = strdup("Unknown authentication mechanism. Only MongoDB-CR is supported by this build");
#endif
return 0;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/