mysqlnd_vio.c 25 KB

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