xref: /PHP-8.1/ext/mysqlnd/mysqlnd_vio.c (revision c1103a97)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Authors: Andrey Hristov <andrey@php.net>                             |
14   |          Ulf Wendel <uw@php.net>                                     |
15   +----------------------------------------------------------------------+
16 */
17 
18 #include "php.h"
19 #include "mysqlnd.h"
20 #include "mysqlnd_priv.h"
21 #include "mysqlnd_statistics.h"
22 #include "mysqlnd_debug.h"
23 #include "mysqlnd_ext_plugin.h"
24 #include "php_network.h"
25 
26 #ifndef PHP_WIN32
27 #include <netinet/tcp.h>
28 #else
29 #include <winsock.h>
30 #endif
31 
32 
33 /* {{{ mysqlnd_set_sock_no_delay */
34 static int
mysqlnd_set_sock_no_delay(php_stream * stream)35 mysqlnd_set_sock_no_delay(php_stream * stream)
36 {
37 	int socketd = ((php_netstream_data_t*)stream->abstract)->socket;
38 	int ret = SUCCESS;
39 	int flag = 1;
40 	int result = setsockopt(socketd, IPPROTO_TCP,  TCP_NODELAY, (char *) &flag, sizeof(int));
41 
42 	DBG_ENTER("mysqlnd_set_sock_no_delay");
43 
44 	if (result == -1) {
45 		ret = FAILURE;
46 	}
47 
48 	DBG_RETURN(ret);
49 }
50 /* }}} */
51 
52 
53 /* {{{ mysqlnd_set_sock_keepalive */
54 static int
mysqlnd_set_sock_keepalive(php_stream * stream)55 mysqlnd_set_sock_keepalive(php_stream * stream)
56 {
57 	int socketd = ((php_netstream_data_t*)stream->abstract)->socket;
58 	int ret = SUCCESS;
59 	int flag = 1;
60 	int result = setsockopt(socketd, SOL_SOCKET, SO_KEEPALIVE, (char *) &flag, sizeof(int));
61 
62 	DBG_ENTER("mysqlnd_set_sock_keepalive");
63 
64 	if (result == -1) {
65 		ret = FAILURE;
66 	}
67 
68 	DBG_RETURN(ret);
69 }
70 /* }}} */
71 
72 
73 /* {{{ mysqlnd_vio::network_read */
74 static enum_func_status
MYSQLND_METHOD(mysqlnd_vio,network_read)75 MYSQLND_METHOD(mysqlnd_vio, network_read)(MYSQLND_VIO * const vio, zend_uchar * const buffer, const size_t count,
76 										  MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
77 {
78 	enum_func_status return_value = PASS;
79 	php_stream * net_stream = vio->data->m.get_stream(vio);
80 	size_t to_read = count;
81 	zend_uchar * p = buffer;
82 
83 	DBG_ENTER("mysqlnd_vio::network_read");
84 	DBG_INF_FMT("count=%zu", count);
85 
86 	while (to_read) {
87 		ssize_t ret = php_stream_read(net_stream, (char *) p, to_read);
88 		if (ret <= 0) {
89 			DBG_ERR_FMT("Error while reading header from socket");
90 			return_value = FAIL;
91 			break;
92 		}
93 		p += ret;
94 		to_read -= ret;
95 	}
96 	MYSQLND_INC_CONN_STATISTIC_W_VALUE(stats, STAT_BYTES_RECEIVED, count - to_read);
97 	DBG_RETURN(return_value);
98 }
99 /* }}} */
100 
101 
102 /* {{{ mysqlnd_vio::network_write */
103 static ssize_t
MYSQLND_METHOD(mysqlnd_vio,network_write)104 MYSQLND_METHOD(mysqlnd_vio, network_write)(MYSQLND_VIO * const vio, const zend_uchar * const buffer, const size_t count,
105 										   MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
106 {
107 	ssize_t ret;
108 	DBG_ENTER("mysqlnd_vio::network_write");
109 	DBG_INF_FMT("sending %zu bytes", count);
110 	ret = php_stream_write(vio->data->m.get_stream(vio), (char *)buffer, count);
111 	DBG_RETURN(ret);
112 }
113 /* }}} */
114 
115 
116 /* {{{ mysqlnd_vio::open_pipe */
117 static php_stream *
MYSQLND_METHOD(mysqlnd_vio,open_pipe)118 MYSQLND_METHOD(mysqlnd_vio, open_pipe)(MYSQLND_VIO * const vio, const MYSQLND_CSTRING scheme, const bool persistent,
119 									   MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
120 {
121 	unsigned int streams_options = 0;
122 	dtor_func_t origin_dtor;
123 	php_stream * net_stream = NULL;
124 
125 	DBG_ENTER("mysqlnd_vio::open_pipe");
126 	if (persistent) {
127 		streams_options |= STREAM_OPEN_PERSISTENT;
128 	}
129 	streams_options |= IGNORE_URL;
130 	net_stream = php_stream_open_wrapper(scheme.s + sizeof("pipe://") - 1, "r+", streams_options, NULL);
131 	if (!net_stream) {
132 		SET_CLIENT_ERROR(error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, "Unknown error while connecting");
133 		DBG_RETURN(NULL);
134 	}
135 	/*
136 	  Streams are not meant for C extensions! Thus we need a hack. Every connected stream will
137 	  be registered as resource (in EG(regular_list). So far, so good. However, it won't be
138 	  unregistered until the script ends. So, we need to take care of that.
139 	*/
140 	origin_dtor = EG(regular_list).pDestructor;
141 	EG(regular_list).pDestructor = NULL;
142 	zend_hash_index_del(&EG(regular_list), net_stream->res->handle); /* ToDO: should it be res->handle, do streams register with addref ?*/
143 	EG(regular_list).pDestructor = origin_dtor;
144 	net_stream->res = NULL;
145 
146 	DBG_RETURN(net_stream);
147 }
148 /* }}} */
149 
150 
151 /* {{{ mysqlnd_vio::open_tcp_or_unix */
152 static php_stream *
MYSQLND_METHOD(mysqlnd_vio,open_tcp_or_unix)153 MYSQLND_METHOD(mysqlnd_vio, open_tcp_or_unix)(MYSQLND_VIO * const vio, const MYSQLND_CSTRING scheme, const bool persistent,
154 											  MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
155 {
156 	unsigned int streams_options = 0;
157 	unsigned int streams_flags = STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT;
158 	char * hashed_details = NULL;
159 	int hashed_details_len = 0;
160 	zend_string *errstr = NULL;
161 	int errcode = 0;
162 	struct timeval tv;
163 	dtor_func_t origin_dtor;
164 	php_stream * net_stream = NULL;
165 
166 	DBG_ENTER("mysqlnd_vio::open_tcp_or_unix");
167 
168 	vio->data->stream = NULL;
169 
170 	if (persistent) {
171 		hashed_details_len = mnd_sprintf(&hashed_details, 0, "%p", vio);
172 		DBG_INF_FMT("hashed_details=%s", hashed_details);
173 	}
174 
175 	if (vio->data->options.timeout_connect) {
176 		tv.tv_sec = vio->data->options.timeout_connect;
177 		tv.tv_usec = 0;
178 	}
179 
180 	DBG_INF_FMT("calling php_stream_xport_create");
181 	net_stream = php_stream_xport_create(scheme.s, scheme.l, streams_options, streams_flags,
182 										  hashed_details, (vio->data->options.timeout_connect) ? &tv : NULL,
183 										  NULL /*ctx*/, &errstr, &errcode);
184 	if (errstr || !net_stream) {
185 		DBG_ERR("Error");
186 		if (hashed_details) {
187 			mnd_sprintf_free(hashed_details);
188 		}
189 		errcode = CR_CONNECTION_ERROR;
190 		SET_CLIENT_ERROR(error_info,
191 						 CR_CONNECTION_ERROR,
192 						 UNKNOWN_SQLSTATE,
193 						 errstr? ZSTR_VAL(errstr):"Unknown error while connecting");
194 		if (errstr) {
195 			zend_string_release_ex(errstr, 0);
196 		}
197 		DBG_RETURN(NULL);
198 	}
199 	if (hashed_details) {
200 		/*
201 		  If persistent, the streams register it in EG(persistent_list).
202 		  This is unwanted. ext/mysql or ext/mysqli are responsible to clean,
203 		  whatever they have to.
204 		*/
205 		zend_resource *le;
206 
207 		if ((le = zend_hash_str_find_ptr(&EG(persistent_list), hashed_details, hashed_details_len))) {
208 			origin_dtor = EG(persistent_list).pDestructor;
209 			/*
210 			  in_free will let streams code skip destructing - big HACK,
211 			  but STREAMS suck big time regarding persistent streams.
212 			  Just not compatible for extensions that need persistency.
213 			*/
214 			EG(persistent_list).pDestructor = NULL;
215 			zend_hash_str_del(&EG(persistent_list), hashed_details, hashed_details_len);
216 			EG(persistent_list).pDestructor = origin_dtor;
217 			pefree(le, 1);
218 		}
219 #if ZEND_DEBUG
220 		/* Shut-up the streams, they don't know what they are doing */
221 		net_stream->__exposed = 1;
222 #endif
223 		mnd_sprintf_free(hashed_details);
224 	}
225 
226 	/*
227 	  Streams are not meant for C extensions! Thus we need a hack. Every connected stream will
228 	  be registered as resource (in EG(regular_list). So far, so good. However, it won't be
229 	  unregistered until the script ends. So, we need to take care of that.
230 	*/
231 	origin_dtor = EG(regular_list).pDestructor;
232 	EG(regular_list).pDestructor = NULL;
233 	zend_hash_index_del(&EG(regular_list), net_stream->res->handle); /* ToDO: should it be res->handle, do streams register with addref ?*/
234 	efree(net_stream->res);
235 	net_stream->res = NULL;
236 	EG(regular_list).pDestructor = origin_dtor;
237 	DBG_RETURN(net_stream);
238 }
239 /* }}} */
240 
241 
242 /* {{{ mysqlnd_vio::post_connect_set_opt */
243 static void
MYSQLND_METHOD(mysqlnd_vio,post_connect_set_opt)244 MYSQLND_METHOD(mysqlnd_vio, post_connect_set_opt)(MYSQLND_VIO * const vio, const MYSQLND_CSTRING scheme,
245 												  MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
246 {
247 	php_stream * net_stream = vio->data->m.get_stream(vio);
248 	DBG_ENTER("mysqlnd_vio::post_connect_set_opt");
249 	if (net_stream) {
250 		if (vio->data->options.timeout_read) {
251 			struct timeval tv;
252 			DBG_INF_FMT("setting %u as PHP_STREAM_OPTION_READ_TIMEOUT", vio->data->options.timeout_read);
253 			tv.tv_sec = vio->data->options.timeout_read;
254 			tv.tv_usec = 0;
255 			php_stream_set_option(net_stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
256 		}
257 
258 		if (!memcmp(scheme.s, "tcp://", sizeof("tcp://") - 1)) {
259 			/* TCP -> Set TCP_NODELAY */
260 			mysqlnd_set_sock_no_delay(net_stream);
261 			/* TCP -> Set SO_KEEPALIVE */
262 			mysqlnd_set_sock_keepalive(net_stream);
263 		}
264 
265 		net_stream->chunk_size = vio->data->options.net_read_buffer_size;
266 		net_stream->flags |= PHP_STREAM_FLAG_SUPPRESS_ERRORS;
267 	}
268 
269 	DBG_VOID_RETURN;
270 }
271 /* }}} */
272 
273 
274 /* {{{ mysqlnd_vio::get_open_stream */
275 static func_mysqlnd_vio__open_stream
MYSQLND_METHOD(mysqlnd_vio,get_open_stream)276 MYSQLND_METHOD(mysqlnd_vio, get_open_stream)(MYSQLND_VIO * const vio, const MYSQLND_CSTRING scheme,
277 											 MYSQLND_ERROR_INFO * const error_info)
278 {
279 	func_mysqlnd_vio__open_stream ret = NULL;
280 	DBG_ENTER("mysqlnd_vio::get_open_stream");
281 	if (scheme.l > (sizeof("pipe://") - 1) && !memcmp(scheme.s, "pipe://", sizeof("pipe://") - 1)) {
282 		ret = vio->data->m.open_pipe;
283 	} else if ((scheme.l > (sizeof("tcp://") - 1) && !memcmp(scheme.s, "tcp://", sizeof("tcp://") - 1))
284 				||
285 				(scheme.l > (sizeof("unix://") - 1) && !memcmp(scheme.s, "unix://", sizeof("unix://") - 1)))
286 	{
287 		ret = vio->data->m.open_tcp_or_unix;
288 	}
289 
290 	if (!ret) {
291 		SET_CLIENT_ERROR(error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, "No handler for this scheme");
292 	}
293 
294 	DBG_RETURN(ret);
295 }
296 /* }}} */
297 
298 
299 /* {{{ mysqlnd_vio::connect */
300 static enum_func_status
MYSQLND_METHOD(mysqlnd_vio,connect)301 MYSQLND_METHOD(mysqlnd_vio, connect)(MYSQLND_VIO * const vio, const MYSQLND_CSTRING scheme, const bool persistent,
302 									 MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
303 {
304 	enum_func_status ret = FAIL;
305 	func_mysqlnd_vio__open_stream open_stream = NULL;
306 	DBG_ENTER("mysqlnd_vio::connect");
307 
308 	vio->data->m.close_stream(vio, conn_stats, error_info);
309 
310 	open_stream = vio->data->m.get_open_stream(vio, scheme, error_info);
311 	if (open_stream) {
312 		php_stream * net_stream = open_stream(vio, scheme, persistent, conn_stats, error_info);
313 		if (net_stream && PASS == vio->data->m.set_stream(vio, net_stream)) {
314 			vio->data->m.post_connect_set_opt(vio, scheme, conn_stats, error_info);
315 			ret = PASS;
316 		}
317 	}
318 
319 	DBG_RETURN(ret);
320 }
321 /* }}} */
322 
323 
324 /* {{{ mysqlnd_vio::set_client_option */
325 static enum_func_status
MYSQLND_METHOD(mysqlnd_vio,set_client_option)326 MYSQLND_METHOD(mysqlnd_vio, set_client_option)(MYSQLND_VIO * const net, enum_mysqlnd_client_option option, const char * const value)
327 {
328 	DBG_ENTER("mysqlnd_vio::set_client_option");
329 	DBG_INF_FMT("option=%u", option);
330 	switch (option) {
331 		case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
332 			DBG_INF("MYSQLND_OPT_NET_READ_BUFFER_SIZE");
333 			net->data->options.net_read_buffer_size = *(unsigned int*) value;
334 			DBG_INF_FMT("new_length=%zu", net->data->options.net_read_buffer_size);
335 			break;
336 		case MYSQL_OPT_CONNECT_TIMEOUT:
337 			DBG_INF("MYSQL_OPT_CONNECT_TIMEOUT");
338 			net->data->options.timeout_connect = *(unsigned int*) value;
339 			break;
340 		case MYSQLND_OPT_SSL_KEY:
341 			{
342 				bool pers = net->persistent;
343 				if (net->data->options.ssl_key) {
344 					mnd_pefree(net->data->options.ssl_key, pers);
345 				}
346 				net->data->options.ssl_key = value? mnd_pestrdup(value, pers) : NULL;
347 				break;
348 			}
349 		case MYSQLND_OPT_SSL_CERT:
350 			{
351 				bool pers = net->persistent;
352 				if (net->data->options.ssl_cert) {
353 					mnd_pefree(net->data->options.ssl_cert, pers);
354 				}
355 				net->data->options.ssl_cert = value? mnd_pestrdup(value, pers) : NULL;
356 				break;
357 			}
358 		case MYSQLND_OPT_SSL_CA:
359 			{
360 				bool pers = net->persistent;
361 				if (net->data->options.ssl_ca) {
362 					mnd_pefree(net->data->options.ssl_ca, pers);
363 				}
364 				net->data->options.ssl_ca = value? mnd_pestrdup(value, pers) : NULL;
365 				break;
366 			}
367 		case MYSQLND_OPT_SSL_CAPATH:
368 			{
369 				bool pers = net->persistent;
370 				if (net->data->options.ssl_capath) {
371 					mnd_pefree(net->data->options.ssl_capath, pers);
372 				}
373 				net->data->options.ssl_capath = value? mnd_pestrdup(value, pers) : NULL;
374 				break;
375 			}
376 		case MYSQLND_OPT_SSL_CIPHER:
377 			{
378 				bool pers = net->persistent;
379 				if (net->data->options.ssl_cipher) {
380 					mnd_pefree(net->data->options.ssl_cipher, pers);
381 				}
382 				net->data->options.ssl_cipher = value? mnd_pestrdup(value, pers) : NULL;
383 				break;
384 			}
385 		case MYSQLND_OPT_SSL_PASSPHRASE:
386 			{
387 				bool pers = net->persistent;
388 				if (net->data->options.ssl_passphrase) {
389 					mnd_pefree(net->data->options.ssl_passphrase, pers);
390 				}
391 				net->data->options.ssl_passphrase = value? mnd_pestrdup(value, pers) : NULL;
392 				break;
393 			}
394 		case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
395 		{
396 			enum mysqlnd_ssl_peer val = *((enum mysqlnd_ssl_peer *)value);
397 			switch (val) {
398 				case MYSQLND_SSL_PEER_VERIFY:
399 					DBG_INF("MYSQLND_SSL_PEER_VERIFY");
400 					break;
401 				case MYSQLND_SSL_PEER_DONT_VERIFY:
402 					DBG_INF("MYSQLND_SSL_PEER_DONT_VERIFY");
403 					break;
404 				case MYSQLND_SSL_PEER_DEFAULT:
405 					DBG_INF("MYSQLND_SSL_PEER_DEFAULT");
406 					val = MYSQLND_SSL_PEER_DEFAULT;
407 					break;
408 				default:
409 					DBG_INF("default = MYSQLND_SSL_PEER_DEFAULT_ACTION");
410 					val = MYSQLND_SSL_PEER_DEFAULT;
411 					break;
412 			}
413 			net->data->options.ssl_verify_peer = val;
414 			break;
415 		}
416 		case MYSQL_OPT_READ_TIMEOUT:
417 			net->data->options.timeout_read = *(unsigned int*) value;
418 			break;
419 #ifdef WHEN_SUPPORTED_BY_MYSQLI
420 		case MYSQL_OPT_WRITE_TIMEOUT:
421 			net->data->options.timeout_write = *(unsigned int*) value;
422 			break;
423 #endif
424 		default:
425 			DBG_RETURN(FAIL);
426 	}
427 	DBG_RETURN(PASS);
428 }
429 /* }}} */
430 
431 
432 /* {{{ mysqlnd_vio::consume_uneaten_data */
433 size_t
MYSQLND_METHOD(mysqlnd_vio,consume_uneaten_data)434 MYSQLND_METHOD(mysqlnd_vio, consume_uneaten_data)(MYSQLND_VIO * const net, enum php_mysqlnd_server_command cmd)
435 {
436 #ifdef MYSQLND_DO_WIRE_CHECK_BEFORE_COMMAND
437 	/*
438 	  Switch to non-blocking mode and try to consume something from
439 	  the line, if possible, then continue. This saves us from looking for
440 	  the actual place where out-of-order packets have been sent.
441 	  If someone is completely sure that everything is fine, he can switch it
442 	  off.
443 	*/
444 	char tmp_buf[256];
445 	size_t skipped_bytes = 0;
446 	int opt = PHP_STREAM_OPTION_BLOCKING;
447 	php_stream * net_stream = net->data->get_stream(net);
448 	int was_blocked = net_stream->ops->set_option(net_stream, opt, 0, NULL);
449 
450 	DBG_ENTER("mysqlnd_vio::consume_uneaten_data");
451 
452 	if (PHP_STREAM_OPTION_RETURN_ERR != was_blocked) {
453 		/* Do a read of 1 byte */
454 		ssize_t bytes_consumed;
455 
456 		do {
457 			bytes_consumed = php_stream_read(net_stream, tmp_buf, sizeof(tmp_buf));
458 			if (bytes_consumed <= 0) {
459 				break;
460 			}
461 			skipped_bytes += bytes_consumed;
462 		} while (bytes_consumed == sizeof(tmp_buf));
463 
464 		if (was_blocked) {
465 			net_stream->ops->set_option(net_stream, opt, 1, NULL);
466 		}
467 
468 		if (bytes_consumed) {
469 			DBG_ERR_FMT("Skipped %u bytes. Last command hasn't consumed all the output from the server",
470 						bytes_consumed, mysqlnd_command_to_text[net->last_command]);
471 			php_error_docref(NULL, E_WARNING, "Skipped %u bytes. Last command %s hasn't "
472 							 "consumed all the output from the server",
473 							 bytes_consumed, mysqlnd_command_to_text[net->last_command]);
474 		}
475 	}
476 	net->last_command = cmd;
477 
478 	DBG_RETURN(skipped_bytes);
479 #else
480 	return 0;
481 #endif
482 }
483 /* }}} */
484 
485 /*
486   in libmyusql, if cert and !key then key=cert
487 */
488 /* {{{ mysqlnd_vio::enable_ssl */
489 static enum_func_status
MYSQLND_METHOD(mysqlnd_vio,enable_ssl)490 MYSQLND_METHOD(mysqlnd_vio, enable_ssl)(MYSQLND_VIO * const net)
491 {
492 #ifdef MYSQLND_SSL_SUPPORTED
493 	php_stream_context * context = php_stream_context_alloc();
494 	php_stream * net_stream = net->data->m.get_stream(net);
495 	bool any_flag = FALSE;
496 
497 	DBG_ENTER("mysqlnd_vio::enable_ssl");
498 
499 	if (net->data->options.ssl_key) {
500 		zval key_zval;
501 		ZVAL_STRING(&key_zval, net->data->options.ssl_key);
502 		php_stream_context_set_option(context, "ssl", "local_pk", &key_zval);
503 		zval_ptr_dtor(&key_zval);
504 		any_flag = TRUE;
505 	}
506 	if (net->data->options.ssl_cert) {
507 		zval cert_zval;
508 		ZVAL_STRING(&cert_zval, net->data->options.ssl_cert);
509 		php_stream_context_set_option(context, "ssl", "local_cert", &cert_zval);
510 		if (!net->data->options.ssl_key) {
511 			php_stream_context_set_option(context, "ssl", "local_pk", &cert_zval);
512 		}
513 		zval_ptr_dtor(&cert_zval);
514 		any_flag = TRUE;
515 	}
516 	if (net->data->options.ssl_ca) {
517 		zval cafile_zval;
518 		ZVAL_STRING(&cafile_zval, net->data->options.ssl_ca);
519 		php_stream_context_set_option(context, "ssl", "cafile", &cafile_zval);
520 		zval_ptr_dtor(&cafile_zval);
521 		any_flag = TRUE;
522 	}
523 	if (net->data->options.ssl_capath) {
524 		zval capath_zval;
525 		ZVAL_STRING(&capath_zval, net->data->options.ssl_capath);
526 		php_stream_context_set_option(context, "ssl", "capath", &capath_zval);
527 		zval_ptr_dtor(&capath_zval);
528 		any_flag = TRUE;
529 	}
530 	if (net->data->options.ssl_passphrase) {
531 		zval passphrase_zval;
532 		ZVAL_STRING(&passphrase_zval, net->data->options.ssl_passphrase);
533 		php_stream_context_set_option(context, "ssl", "passphrase", &passphrase_zval);
534 		zval_ptr_dtor(&passphrase_zval);
535 		any_flag = TRUE;
536 	}
537 	if (net->data->options.ssl_cipher) {
538 		zval cipher_zval;
539 		ZVAL_STRING(&cipher_zval, net->data->options.ssl_cipher);
540 		php_stream_context_set_option(context, "ssl", "ciphers", &cipher_zval);
541 		zval_ptr_dtor(&cipher_zval);
542 		any_flag = TRUE;
543 	}
544 	{
545 		zval verify_peer_zval;
546 		bool verify;
547 
548 		if (net->data->options.ssl_verify_peer == MYSQLND_SSL_PEER_DEFAULT) {
549 			net->data->options.ssl_verify_peer = any_flag? MYSQLND_SSL_PEER_DEFAULT_ACTION:MYSQLND_SSL_PEER_DONT_VERIFY;
550 		}
551 
552 		verify = net->data->options.ssl_verify_peer == MYSQLND_SSL_PEER_VERIFY? TRUE:FALSE;
553 
554 		DBG_INF_FMT("VERIFY=%d", verify);
555 		ZVAL_BOOL(&verify_peer_zval, verify);
556 		php_stream_context_set_option(context, "ssl", "verify_peer", &verify_peer_zval);
557 		php_stream_context_set_option(context, "ssl", "verify_peer_name", &verify_peer_zval);
558 		if (net->data->options.ssl_verify_peer == MYSQLND_SSL_PEER_DONT_VERIFY) {
559 			ZVAL_TRUE(&verify_peer_zval);
560 			php_stream_context_set_option(context, "ssl", "allow_self_signed", &verify_peer_zval);
561 		}
562 	}
563 	php_stream_context_set(net_stream, context);
564 	/* php_stream_context_set() increases the refcount of context, but we just want to transfer ownership
565 	 * hence the need to decrease the refcount so the refcount will be equal to 1. */
566 	ZEND_ASSERT(GC_REFCOUNT(context->res) == 2);
567 	GC_DELREF(context->res);
568 	if (php_stream_xport_crypto_setup(net_stream, STREAM_CRYPTO_METHOD_TLS_CLIENT, NULL) < 0 ||
569 	    php_stream_xport_crypto_enable(net_stream, 1) < 0)
570 	{
571 		DBG_ERR("Cannot connect to MySQL by using SSL");
572 		DBG_RETURN(FAIL);
573 	}
574 	net->data->ssl = TRUE;
575 	/*
576 	  get rid of the context. we are persistent and if this is a real pconn used by mysql/mysqli,
577 	  then the context would not survive cleaning of EG(regular_list), where it is registered, as a
578 	  resource. What happens is that after this destruction any use of the network will mean usage
579 	  of the context, which means usage of already freed memory, bad. Actually we don't need this
580 	  context anymore after we have enabled SSL on the connection. Thus it is very simple, we remove it.
581 	*/
582 	php_stream_context_set(net_stream, NULL);
583 
584 	if (net->data->options.timeout_read) {
585 		struct timeval tv;
586 		DBG_INF_FMT("setting %u as PHP_STREAM_OPTION_READ_TIMEOUT", net->data->options.timeout_read);
587 		tv.tv_sec = net->data->options.timeout_read;
588 		tv.tv_usec = 0;
589 		php_stream_set_option(net_stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
590 	}
591 
592 	DBG_RETURN(PASS);
593 #else
594 	DBG_ENTER("mysqlnd_vio::enable_ssl");
595 	DBG_INF("MYSQLND_SSL_SUPPORTED is not defined");
596 	DBG_RETURN(PASS);
597 #endif
598 }
599 /* }}} */
600 
601 
602 /* {{{ mysqlnd_vio::disable_ssl */
603 static enum_func_status
MYSQLND_METHOD(mysqlnd_vio,disable_ssl)604 MYSQLND_METHOD(mysqlnd_vio, disable_ssl)(MYSQLND_VIO * const vio)
605 {
606 	DBG_ENTER("mysqlnd_vio::disable_ssl");
607 	DBG_RETURN(PASS);
608 }
609 /* }}} */
610 
611 
612 /* {{{ mysqlnd_vio::free_contents */
613 static void
MYSQLND_METHOD(mysqlnd_vio,free_contents)614 MYSQLND_METHOD(mysqlnd_vio, free_contents)(MYSQLND_VIO * net)
615 {
616 	bool pers = net->persistent;
617 	DBG_ENTER("mysqlnd_vio::free_contents");
618 
619 	if (net->data->options.ssl_key) {
620 		mnd_pefree(net->data->options.ssl_key, pers);
621 		net->data->options.ssl_key = NULL;
622 	}
623 	if (net->data->options.ssl_cert) {
624 		mnd_pefree(net->data->options.ssl_cert, pers);
625 		net->data->options.ssl_cert = NULL;
626 	}
627 	if (net->data->options.ssl_ca) {
628 		mnd_pefree(net->data->options.ssl_ca, pers);
629 		net->data->options.ssl_ca = NULL;
630 	}
631 	if (net->data->options.ssl_capath) {
632 		mnd_pefree(net->data->options.ssl_capath, pers);
633 		net->data->options.ssl_capath = NULL;
634 	}
635 	if (net->data->options.ssl_cipher) {
636 		mnd_pefree(net->data->options.ssl_cipher, pers);
637 		net->data->options.ssl_cipher = NULL;
638 	}
639 
640 	DBG_VOID_RETURN;
641 }
642 /* }}} */
643 
644 
645 /* {{{ mysqlnd_vio::close_stream */
646 static void
MYSQLND_METHOD(mysqlnd_vio,close_stream)647 MYSQLND_METHOD(mysqlnd_vio, close_stream)(MYSQLND_VIO * const net, MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
648 {
649 	php_stream * net_stream;
650 	DBG_ENTER("mysqlnd_vio::close_stream");
651 	if (net && (net_stream = net->data->m.get_stream(net))) {
652 		bool pers = net->persistent;
653 		DBG_INF_FMT("Freeing stream. abstract=%p", net_stream->abstract);
654 		/* We removed the resource from the stream, so pass FREE_RSRC_DTOR now to force
655 		 * destruction to occur during shutdown, because it won't happen through the resource. */
656 		/* TODO: The EG(active) check here is dead -- check IN_SHUTDOWN? */
657 		if (pers && EG(active)) {
658 			php_stream_free(net_stream, PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR);
659 		} else {
660 			/*
661 			  otherwise we will crash because the EG(persistent_list) has been freed already,
662 			  before the modules are shut down
663 			*/
664 			php_stream_free(net_stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR);
665 		}
666 		net->data->m.set_stream(net, NULL);
667 	}
668 
669 	DBG_VOID_RETURN;
670 }
671 /* }}} */
672 
673 
674 /* {{{ mysqlnd_vio::init */
675 static enum_func_status
MYSQLND_METHOD(mysqlnd_vio,init)676 MYSQLND_METHOD(mysqlnd_vio, init)(MYSQLND_VIO * const net, MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
677 {
678 	unsigned int buf_size;
679 	DBG_ENTER("mysqlnd_vio::init");
680 
681 	buf_size = MYSQLND_G(net_read_buffer_size); /* this is long, cast to unsigned int*/
682 	net->data->m.set_client_option(net, MYSQLND_OPT_NET_READ_BUFFER_SIZE, (char *)&buf_size);
683 
684 	buf_size = MYSQLND_G(net_read_timeout); /* this is long, cast to unsigned int*/
685 	net->data->m.set_client_option(net, MYSQL_OPT_READ_TIMEOUT, (char *)&buf_size);
686 
687 	DBG_RETURN(PASS);
688 }
689 /* }}} */
690 
691 
692 /* {{{ mysqlnd_vio::dtor */
693 static void
MYSQLND_METHOD(mysqlnd_vio,dtor)694 MYSQLND_METHOD(mysqlnd_vio, dtor)(MYSQLND_VIO * const vio, MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
695 {
696 	DBG_ENTER("mysqlnd_vio::dtor");
697 	if (vio) {
698 		vio->data->m.free_contents(vio);
699 		vio->data->m.close_stream(vio, stats, error_info);
700 
701 		mnd_pefree(vio, vio->persistent);
702 	}
703 	DBG_VOID_RETURN;
704 }
705 /* }}} */
706 
707 
708 /* {{{ mysqlnd_vio::get_stream */
709 static php_stream *
MYSQLND_METHOD(mysqlnd_vio,get_stream)710 MYSQLND_METHOD(mysqlnd_vio, get_stream)(const MYSQLND_VIO * const net)
711 {
712 	DBG_ENTER("mysqlnd_vio::get_stream");
713 	DBG_INF_FMT("%p", net? net->data->stream:NULL);
714 	DBG_RETURN(net? net->data->stream:NULL);
715 }
716 /* }}} */
717 
718 
719 /* {{{ mysqlnd_vio::set_stream */
720 static enum_func_status
MYSQLND_METHOD(mysqlnd_vio,set_stream)721 MYSQLND_METHOD(mysqlnd_vio, set_stream)(MYSQLND_VIO * const vio, php_stream * net_stream)
722 {
723 	DBG_ENTER("mysqlnd_vio::set_stream");
724 	if (vio) {
725 		vio->data->stream = net_stream;
726 		DBG_RETURN(PASS);
727 	}
728 	DBG_RETURN(FAIL);
729 }
730 /* }}} */
731 
732 
733 /* {{{ mysqlnd_vio::has_valid_stream */
734 static bool
MYSQLND_METHOD(mysqlnd_vio,has_valid_stream)735 MYSQLND_METHOD(mysqlnd_vio, has_valid_stream)(const MYSQLND_VIO * const vio)
736 {
737 	DBG_ENTER("mysqlnd_vio::has_valid_stream");
738 	DBG_INF_FMT("%p %p", vio, vio? vio->data->stream:NULL);
739 	DBG_RETURN((vio && vio->data->stream)? TRUE: FALSE);
740 }
741 /* }}} */
742 
743 
744 MYSQLND_CLASS_METHODS_START(mysqlnd_vio)
745 	MYSQLND_METHOD(mysqlnd_vio, init),
746 	MYSQLND_METHOD(mysqlnd_vio, dtor),
747 
748 	MYSQLND_METHOD(mysqlnd_vio, connect),
749 
750 	MYSQLND_METHOD(mysqlnd_vio, close_stream),
751 	MYSQLND_METHOD(mysqlnd_vio, open_pipe),
752 	MYSQLND_METHOD(mysqlnd_vio, open_tcp_or_unix),
753 
754 	MYSQLND_METHOD(mysqlnd_vio, get_stream),
755 	MYSQLND_METHOD(mysqlnd_vio, set_stream),
756 	MYSQLND_METHOD(mysqlnd_vio, has_valid_stream),
757 	MYSQLND_METHOD(mysqlnd_vio, get_open_stream),
758 
759 	MYSQLND_METHOD(mysqlnd_vio, set_client_option),
760 	MYSQLND_METHOD(mysqlnd_vio, post_connect_set_opt),
761 
762 	MYSQLND_METHOD(mysqlnd_vio, enable_ssl),
763 	MYSQLND_METHOD(mysqlnd_vio, disable_ssl),
764 
765 	MYSQLND_METHOD(mysqlnd_vio, network_read),
766 	MYSQLND_METHOD(mysqlnd_vio, network_write),
767 
768 	MYSQLND_METHOD(mysqlnd_vio, consume_uneaten_data),
769 
770 	MYSQLND_METHOD(mysqlnd_vio, free_contents),
771 MYSQLND_CLASS_METHODS_END;
772 
773 
774 /* {{{ mysqlnd_vio_init */
775 PHPAPI MYSQLND_VIO *
mysqlnd_vio_init(bool persistent,MYSQLND_CLASS_METHODS_TYPE (mysqlnd_object_factory)* object_factory,MYSQLND_STATS * stats,MYSQLND_ERROR_INFO * error_info)776 mysqlnd_vio_init(bool persistent, MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *object_factory, MYSQLND_STATS * stats, MYSQLND_ERROR_INFO * error_info)
777 {
778 	MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *factory = object_factory? object_factory : &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory);
779 	MYSQLND_VIO * vio;
780 	DBG_ENTER("mysqlnd_vio_init");
781 	vio = factory->get_vio(persistent, stats, error_info);
782 	DBG_RETURN(vio);
783 }
784 /* }}} */
785 
786 
787 /* {{{ mysqlnd_vio_free */
788 PHPAPI void
mysqlnd_vio_free(MYSQLND_VIO * const vio,MYSQLND_STATS * stats,MYSQLND_ERROR_INFO * error_info)789 mysqlnd_vio_free(MYSQLND_VIO * const vio, MYSQLND_STATS * stats, MYSQLND_ERROR_INFO * error_info)
790 {
791 	DBG_ENTER("mysqlnd_vio_free");
792 	if (vio) {
793 		vio->data->m.dtor(vio, stats, error_info);
794 	}
795 	DBG_VOID_RETURN;
796 }
797 /* }}} */
798