PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/mysqlnd/mysqlnd_wireprotocol.c

https://github.com/php/php-src
C | 2649 lines | 1986 code | 360 blank | 303 comment | 300 complexity | 5fdb330865dc5bb8244bcd99a849fee4 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  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. #include "php.h"
  18. #include "mysqlnd.h"
  19. #include "mysqlnd_connection.h"
  20. #include "mysqlnd_ps.h"
  21. #include "mysqlnd_priv.h"
  22. #include "mysqlnd_wireprotocol.h"
  23. #include "mysqlnd_statistics.h"
  24. #include "mysqlnd_debug.h"
  25. #define BAIL_IF_NO_MORE_DATA \
  26. if (UNEXPECTED((size_t)(p - begin) > packet->header.size)) { \
  27. php_error_docref(NULL, E_WARNING, "Premature end of data (mysqlnd_wireprotocol.c:%u)", __LINE__); \
  28. goto premature_end; \
  29. } \
  30. static const char *unknown_sqlstate= "HY000";
  31. const char * const mysqlnd_empty_string = "";
  32. /* Used in mysqlnd_debug.c */
  33. const char mysqlnd_read_header_name[] = "mysqlnd_read_header";
  34. const char mysqlnd_read_body_name[] = "mysqlnd_read_body";
  35. #define ERROR_MARKER 0xFF
  36. #define EODATA_MARKER 0xFE
  37. /* {{{ mysqlnd_command_to_text */
  38. const char * const mysqlnd_command_to_text[COM_END] =
  39. {
  40. "SLEEP", "QUIT", "INIT_DB", "QUERY", "FIELD_LIST",
  41. "CREATE_DB", "DROP_DB", "REFRESH", "SHUTDOWN", "STATISTICS",
  42. "PROCESS_INFO", "CONNECT", "PROCESS_KILL", "DEBUG", "PING",
  43. "TIME", "DELAYED_INSERT", "CHANGE_USER", "BINLOG_DUMP",
  44. "TABLE_DUMP", "CONNECT_OUT", "REGISTER_SLAVE",
  45. "STMT_PREPARE", "STMT_EXECUTE", "STMT_SEND_LONG_DATA", "STMT_CLOSE",
  46. "STMT_RESET", "SET_OPTION", "STMT_FETCH", "DAEMON", "BINLOG_DUMP_GTID",
  47. "RESET_CONNECTION"
  48. };
  49. /* }}} */
  50. static enum_mysqlnd_collected_stats packet_type_to_statistic_byte_count[PROT_LAST] =
  51. {
  52. STAT_LAST,
  53. STAT_LAST,
  54. STAT_BYTES_RECEIVED_OK,
  55. STAT_BYTES_RECEIVED_EOF,
  56. STAT_LAST,
  57. STAT_BYTES_RECEIVED_RSET_HEADER,
  58. STAT_BYTES_RECEIVED_RSET_FIELD_META,
  59. STAT_BYTES_RECEIVED_RSET_ROW,
  60. STAT_BYTES_RECEIVED_PREPARE_RESPONSE,
  61. STAT_BYTES_RECEIVED_CHANGE_USER,
  62. };
  63. static enum_mysqlnd_collected_stats packet_type_to_statistic_packet_count[PROT_LAST] =
  64. {
  65. STAT_LAST,
  66. STAT_LAST,
  67. STAT_PACKETS_RECEIVED_OK,
  68. STAT_PACKETS_RECEIVED_EOF,
  69. STAT_LAST,
  70. STAT_PACKETS_RECEIVED_RSET_HEADER,
  71. STAT_PACKETS_RECEIVED_RSET_FIELD_META,
  72. STAT_PACKETS_RECEIVED_RSET_ROW,
  73. STAT_PACKETS_RECEIVED_PREPARE_RESPONSE,
  74. STAT_PACKETS_RECEIVED_CHANGE_USER,
  75. };
  76. /* {{{ php_mysqlnd_net_field_length
  77. Get next field's length */
  78. zend_ulong
  79. php_mysqlnd_net_field_length(const zend_uchar **packet)
  80. {
  81. const zend_uchar *p= (const zend_uchar *)*packet;
  82. if (*p < 251) {
  83. (*packet)++;
  84. return (zend_ulong) *p;
  85. }
  86. switch (*p) {
  87. case 251:
  88. (*packet)++;
  89. return MYSQLND_NULL_LENGTH;
  90. case 252:
  91. (*packet) += 3;
  92. return (zend_ulong) uint2korr(p+1);
  93. case 253:
  94. (*packet) += 4;
  95. return (zend_ulong) uint3korr(p+1);
  96. default:
  97. (*packet) += 9;
  98. return (zend_ulong) uint4korr(p+1);
  99. }
  100. }
  101. /* }}} */
  102. /* {{{ php_mysqlnd_net_field_length_ll
  103. Get next field's length */
  104. uint64_t
  105. php_mysqlnd_net_field_length_ll(const zend_uchar **packet)
  106. {
  107. const zend_uchar *p = (zend_uchar *)*packet;
  108. if (*p < 251) {
  109. (*packet)++;
  110. return (uint64_t) *p;
  111. }
  112. switch (*p) {
  113. case 251:
  114. (*packet)++;
  115. return (uint64_t) MYSQLND_NULL_LENGTH;
  116. case 252:
  117. (*packet) += 3;
  118. return (uint64_t) uint2korr(p + 1);
  119. case 253:
  120. (*packet) += 4;
  121. return (uint64_t) uint3korr(p + 1);
  122. default:
  123. (*packet) += 9;
  124. return (uint64_t) uint8korr(p + 1);
  125. }
  126. }
  127. /* }}} */
  128. /* {{{ php_mysqlnd_net_store_length */
  129. zend_uchar *
  130. php_mysqlnd_net_store_length(zend_uchar *packet, const uint64_t length)
  131. {
  132. if (length < (uint64_t) L64(251)) {
  133. *packet = (zend_uchar) length;
  134. return packet + 1;
  135. }
  136. if (length < (uint64_t) L64(65536)) {
  137. *packet++ = 252;
  138. int2store(packet,(unsigned int) length);
  139. return packet + 2;
  140. }
  141. if (length < (uint64_t) L64(16777216)) {
  142. *packet++ = 253;
  143. int3store(packet,(zend_ulong) length);
  144. return packet + 3;
  145. }
  146. *packet++ = 254;
  147. int8store(packet, length);
  148. return packet + 8;
  149. }
  150. /* }}} */
  151. /* {{{ php_mysqlnd_net_store_length_size */
  152. size_t
  153. php_mysqlnd_net_store_length_size(uint64_t length)
  154. {
  155. if (length < (uint64_t) L64(251)) {
  156. return 1;
  157. }
  158. if (length < (uint64_t) L64(65536)) {
  159. return 3;
  160. }
  161. if (length < (uint64_t) L64(16777216)) {
  162. return 4;
  163. }
  164. return 9;
  165. }
  166. /* }}} */
  167. /* {{{ php_mysqlnd_read_error_from_line */
  168. static enum_func_status
  169. php_mysqlnd_read_error_from_line(const zend_uchar * const buf, const size_t buf_len,
  170. char *error, const size_t error_buf_len,
  171. unsigned int *error_no, char *sqlstate)
  172. {
  173. const zend_uchar *p = buf;
  174. size_t error_msg_len = 0;
  175. DBG_ENTER("php_mysqlnd_read_error_from_line");
  176. *error_no = CR_UNKNOWN_ERROR;
  177. memcpy(sqlstate, unknown_sqlstate, MYSQLND_SQLSTATE_LENGTH);
  178. if (buf_len > 2) {
  179. *error_no = uint2korr(p);
  180. p+= 2;
  181. /*
  182. sqlstate is following. No need to check for buf_left_len as we checked > 2 above,
  183. if it was >=2 then we would need a check
  184. */
  185. if (*p == '#') {
  186. ++p;
  187. if ((buf_len - (p - buf)) >= MYSQLND_SQLSTATE_LENGTH) {
  188. memcpy(sqlstate, p, MYSQLND_SQLSTATE_LENGTH);
  189. p+= MYSQLND_SQLSTATE_LENGTH;
  190. } else {
  191. goto end;
  192. }
  193. }
  194. if ((buf_len - (p - buf)) > 0) {
  195. error_msg_len = MIN((int)((buf_len - (p - buf))), (int) (error_buf_len - 1));
  196. memcpy(error, p, error_msg_len);
  197. }
  198. }
  199. end:
  200. sqlstate[MYSQLND_SQLSTATE_LENGTH] = '\0';
  201. error[error_msg_len]= '\0';
  202. DBG_RETURN(FAIL);
  203. }
  204. /* }}} */
  205. /* {{{ mysqlnd_read_header */
  206. static enum_func_status
  207. mysqlnd_read_header(MYSQLND_PFC * pfc, MYSQLND_VIO * vio, MYSQLND_PACKET_HEADER * header,
  208. MYSQLND_STATS * conn_stats, MYSQLND_ERROR_INFO * error_info)
  209. {
  210. zend_uchar buffer[MYSQLND_HEADER_SIZE];
  211. DBG_ENTER(mysqlnd_read_header_name);
  212. DBG_INF_FMT("compressed=%u", pfc->data->compressed);
  213. if (FAIL == pfc->data->m.receive(pfc, vio, buffer, MYSQLND_HEADER_SIZE, conn_stats, error_info)) {
  214. DBG_RETURN(FAIL);
  215. }
  216. header->size = uint3korr(buffer);
  217. header->packet_no = uint1korr(buffer + 3);
  218. DBG_INF_FMT("HEADER: prot_packet_no=%u size=%3zu", header->packet_no, header->size);
  219. MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn_stats,
  220. STAT_PROTOCOL_OVERHEAD_IN, MYSQLND_HEADER_SIZE,
  221. STAT_PACKETS_RECEIVED, 1);
  222. if (pfc->data->compressed || pfc->data->packet_no == header->packet_no) {
  223. /*
  224. Have to increase the number, so we can send correct number back. It will
  225. round at 255 as this is unsigned char. The server needs this for simple
  226. flow control checking.
  227. */
  228. pfc->data->packet_no++;
  229. DBG_RETURN(PASS);
  230. }
  231. DBG_ERR_FMT("Logical link: packets out of order. Expected %u received %u. Packet size=%zu",
  232. pfc->data->packet_no, header->packet_no, header->size);
  233. php_error(E_WARNING, "Packets out of order. Expected %u received %u. Packet size=%zu",
  234. pfc->data->packet_no, header->packet_no, header->size);
  235. DBG_RETURN(FAIL);
  236. }
  237. /* }}} */
  238. /* {{{ mysqlnd_read_packet_header_and_body */
  239. static enum_func_status
  240. mysqlnd_read_packet_header_and_body(MYSQLND_PACKET_HEADER * packet_header,
  241. MYSQLND_PFC * pfc,
  242. MYSQLND_VIO * vio,
  243. MYSQLND_STATS * stats,
  244. MYSQLND_ERROR_INFO * error_info,
  245. MYSQLND_CONNECTION_STATE * connection_state,
  246. zend_uchar * const buf, const size_t buf_size,
  247. const char * const packet_type_as_text,
  248. enum mysqlnd_packet_type packet_type)
  249. {
  250. DBG_ENTER("mysqlnd_read_packet_header_and_body");
  251. DBG_INF_FMT("buf=%p size=%zu", buf, buf_size);
  252. if (FAIL == mysqlnd_read_header(pfc, vio, packet_header, stats, error_info)) {
  253. SET_CONNECTION_STATE(connection_state, CONN_QUIT_SENT);
  254. SET_CLIENT_ERROR(error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  255. DBG_ERR_FMT("Can't read %s's header", packet_type_as_text);
  256. DBG_RETURN(FAIL);
  257. }
  258. if (buf_size < packet_header->size) {
  259. DBG_ERR_FMT("Packet buffer %zu wasn't big enough %zu, %zu bytes will be unread",
  260. buf_size, packet_header->size, packet_header->size - buf_size);
  261. DBG_RETURN(FAIL);
  262. }
  263. if (FAIL == pfc->data->m.receive(pfc, vio, buf, packet_header->size, stats, error_info)) {
  264. SET_CONNECTION_STATE(connection_state, CONN_QUIT_SENT);
  265. SET_CLIENT_ERROR(error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  266. DBG_ERR_FMT("Empty '%s' packet body", packet_type_as_text);
  267. DBG_RETURN(FAIL);
  268. }
  269. MYSQLND_INC_CONN_STATISTIC_W_VALUE2(stats, packet_type_to_statistic_byte_count[packet_type],
  270. MYSQLND_HEADER_SIZE + packet_header->size,
  271. packet_type_to_statistic_packet_count[packet_type],
  272. 1);
  273. DBG_RETURN(PASS);
  274. }
  275. /* }}} */
  276. /* {{{ php_mysqlnd_greet_read */
  277. static enum_func_status
  278. php_mysqlnd_greet_read(MYSQLND_CONN_DATA * conn, void * _packet)
  279. {
  280. zend_uchar buf[2048];
  281. const zend_uchar * p = buf;
  282. const zend_uchar * const begin = buf;
  283. const zend_uchar * pad_start = NULL;
  284. MYSQLND_PACKET_GREET *packet= (MYSQLND_PACKET_GREET *) _packet;
  285. MYSQLND_ERROR_INFO * error_info = conn->error_info;
  286. MYSQLND_PFC * pfc = conn->protocol_frame_codec;
  287. MYSQLND_VIO * vio = conn->vio;
  288. MYSQLND_STATS * stats = conn->stats;
  289. MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
  290. DBG_ENTER("php_mysqlnd_greet_read");
  291. if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, sizeof(buf), "greeting", PROT_GREET_PACKET)) {
  292. DBG_RETURN(FAIL);
  293. }
  294. BAIL_IF_NO_MORE_DATA;
  295. packet->authentication_plugin_data.s = packet->intern_auth_plugin_data;
  296. packet->authentication_plugin_data.l = sizeof(packet->intern_auth_plugin_data);
  297. if (packet->header.size < sizeof(buf)) {
  298. /*
  299. Null-terminate the string, so strdup can work even if the packets have a string at the end,
  300. which is not ASCIIZ
  301. */
  302. buf[packet->header.size] = '\0';
  303. }
  304. packet->protocol_version = uint1korr(p);
  305. p++;
  306. BAIL_IF_NO_MORE_DATA;
  307. if (ERROR_MARKER == packet->protocol_version) {
  308. php_mysqlnd_read_error_from_line(p, packet->header.size - 1,
  309. packet->error, sizeof(packet->error),
  310. &packet->error_no, packet->sqlstate
  311. );
  312. /*
  313. The server doesn't send sqlstate in the greet packet.
  314. It's a bug#26426 , so we have to set it correctly ourselves.
  315. It's probably "Too many connections, which has SQL state 08004".
  316. */
  317. if (packet->error_no == 1040) {
  318. memcpy(packet->sqlstate, "08004", MYSQLND_SQLSTATE_LENGTH);
  319. }
  320. DBG_RETURN(PASS);
  321. }
  322. packet->server_version = estrdup((char *)p);
  323. p+= strlen(packet->server_version) + 1; /* eat the '\0' */
  324. BAIL_IF_NO_MORE_DATA;
  325. packet->thread_id = uint4korr(p);
  326. p+=4;
  327. BAIL_IF_NO_MORE_DATA;
  328. memcpy(packet->authentication_plugin_data.s, p, SCRAMBLE_LENGTH_323);
  329. p+= SCRAMBLE_LENGTH_323;
  330. BAIL_IF_NO_MORE_DATA;
  331. /* pad1 */
  332. p++;
  333. BAIL_IF_NO_MORE_DATA;
  334. packet->server_capabilities = uint2korr(p);
  335. p+= 2;
  336. BAIL_IF_NO_MORE_DATA;
  337. DBG_INF_FMT("4.1 server_caps=%u\n", (uint32_t) packet->server_capabilities);
  338. packet->charset_no = uint1korr(p);
  339. p++;
  340. BAIL_IF_NO_MORE_DATA;
  341. packet->server_status = uint2korr(p);
  342. p+= 2;
  343. BAIL_IF_NO_MORE_DATA;
  344. /* pad2 */
  345. pad_start = p;
  346. p+= 13;
  347. BAIL_IF_NO_MORE_DATA;
  348. if ((size_t) (p - buf) < packet->header.size) {
  349. /* auth_plugin_data is split into two parts */
  350. memcpy(packet->authentication_plugin_data.s + SCRAMBLE_LENGTH_323, p, SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
  351. p+= SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323;
  352. p++; /* 0x0 at the end of the scramble and thus last byte in the packet in 5.1 and previous */
  353. } else {
  354. packet->pre41 = TRUE;
  355. }
  356. /* Is this a 5.5+ server ? */
  357. if ((size_t) (p - buf) < packet->header.size) {
  358. /* backtrack one byte, the 0x0 at the end of the scramble in 5.1 and previous */
  359. p--;
  360. /* Additional 16 bits for server capabilities */
  361. DBG_INF_FMT("additional 5.5+ caps=%u\n", (uint32_t) uint2korr(pad_start));
  362. packet->server_capabilities |= ((uint32_t) uint2korr(pad_start)) << 16;
  363. /* And a length of the server scramble in one byte */
  364. packet->authentication_plugin_data.l = uint1korr(pad_start + 2);
  365. if (packet->authentication_plugin_data.l > SCRAMBLE_LENGTH) {
  366. /* more data*/
  367. char * new_auth_plugin_data = emalloc(packet->authentication_plugin_data.l);
  368. /* copy what we already have */
  369. memcpy(new_auth_plugin_data, packet->authentication_plugin_data.s, SCRAMBLE_LENGTH);
  370. /* add additional scramble data 5.5+ sent us */
  371. memcpy(new_auth_plugin_data + SCRAMBLE_LENGTH, p, packet->authentication_plugin_data.l - SCRAMBLE_LENGTH);
  372. p+= (packet->authentication_plugin_data.l - SCRAMBLE_LENGTH);
  373. packet->authentication_plugin_data.s = new_auth_plugin_data;
  374. }
  375. }
  376. if (packet->server_capabilities & CLIENT_PLUGIN_AUTH) {
  377. BAIL_IF_NO_MORE_DATA;
  378. /* The server is 5.5.x and supports authentication plugins */
  379. packet->auth_protocol = estrdup((char *)p);
  380. p+= strlen(packet->auth_protocol) + 1; /* eat the '\0' */
  381. }
  382. DBG_INF_FMT("proto=%u server=%s thread_id=%u",
  383. packet->protocol_version, packet->server_version, packet->thread_id);
  384. DBG_INF_FMT("server_capabilities=%u charset_no=%u server_status=%i auth_protocol=%s scramble_length=%zu",
  385. packet->server_capabilities, packet->charset_no, packet->server_status,
  386. packet->auth_protocol? packet->auth_protocol:"n/a", packet->authentication_plugin_data.l);
  387. DBG_RETURN(PASS);
  388. premature_end:
  389. DBG_ERR_FMT("GREET packet %zu bytes shorter than expected", p - begin - packet->header.size);
  390. php_error_docref(NULL, E_WARNING, "GREET packet %zu bytes shorter than expected",
  391. p - begin - packet->header.size);
  392. DBG_RETURN(FAIL);
  393. }
  394. /* }}} */
  395. /* {{{ php_mysqlnd_greet_free_mem */
  396. static
  397. void php_mysqlnd_greet_free_mem(void * _packet)
  398. {
  399. MYSQLND_PACKET_GREET *p= (MYSQLND_PACKET_GREET *) _packet;
  400. if (p->server_version) {
  401. efree(p->server_version);
  402. p->server_version = NULL;
  403. }
  404. if (p->authentication_plugin_data.s && p->authentication_plugin_data.s != p->intern_auth_plugin_data) {
  405. efree(p->authentication_plugin_data.s);
  406. p->authentication_plugin_data.s = NULL;
  407. }
  408. if (p->auth_protocol) {
  409. efree(p->auth_protocol);
  410. p->auth_protocol = NULL;
  411. }
  412. }
  413. /* }}} */
  414. #define AUTH_WRITE_BUFFER_LEN (MYSQLND_HEADER_SIZE + MYSQLND_MAX_ALLOWED_USER_LEN + SCRAMBLE_LENGTH + MYSQLND_MAX_ALLOWED_DB_LEN + 1 + 4096)
  415. /* {{{ php_mysqlnd_auth_write */
  416. static
  417. size_t php_mysqlnd_auth_write(MYSQLND_CONN_DATA * conn, void * _packet)
  418. {
  419. zend_uchar buffer[AUTH_WRITE_BUFFER_LEN];
  420. zend_uchar *p = buffer + MYSQLND_HEADER_SIZE; /* start after the header */
  421. size_t len;
  422. MYSQLND_PACKET_AUTH * packet= (MYSQLND_PACKET_AUTH *) _packet;
  423. MYSQLND_ERROR_INFO * error_info = conn->error_info;
  424. MYSQLND_PFC * pfc = conn->protocol_frame_codec;
  425. MYSQLND_VIO * vio = conn->vio;
  426. MYSQLND_STATS * stats = conn->stats;
  427. MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
  428. DBG_ENTER("php_mysqlnd_auth_write");
  429. if (!packet->is_change_user_packet) {
  430. int4store(p, packet->client_flags);
  431. p+= 4;
  432. int4store(p, packet->max_packet_size);
  433. p+= 4;
  434. int1store(p, packet->charset_no);
  435. p++;
  436. memset(p, 0, 23); /* filler */
  437. p+= 23;
  438. }
  439. if (packet->send_auth_data || packet->is_change_user_packet) {
  440. len = MIN(strlen(packet->user), MYSQLND_MAX_ALLOWED_USER_LEN);
  441. memcpy(p, packet->user, len);
  442. p+= len;
  443. *p++ = '\0';
  444. /* defensive coding */
  445. if (packet->auth_data == NULL) {
  446. packet->auth_data_len = 0;
  447. }
  448. if (packet->auth_data_len > 0xFF) {
  449. const char * const msg = "Authentication data too long. "
  450. "Won't fit into the buffer and will be truncated. Authentication will thus fail";
  451. SET_CLIENT_ERROR(error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, msg);
  452. php_error_docref(NULL, E_WARNING, "%s", msg);
  453. DBG_RETURN(0);
  454. }
  455. int1store(p, (int8_t)packet->auth_data_len);
  456. ++p;
  457. /*!!!!! is the buffer big enough ??? */
  458. if (sizeof(buffer) < (packet->auth_data_len + (p - buffer))) {
  459. DBG_ERR("the stack buffer was not enough!!");
  460. DBG_RETURN(0);
  461. }
  462. if (packet->auth_data_len) {
  463. memcpy(p, packet->auth_data, packet->auth_data_len);
  464. p+= packet->auth_data_len;
  465. }
  466. if (packet->db_len > 0) {
  467. /* CLIENT_CONNECT_WITH_DB should have been set */
  468. size_t real_db_len = MIN(MYSQLND_MAX_ALLOWED_DB_LEN, packet->db_len);
  469. memcpy(p, packet->db, real_db_len);
  470. p+= real_db_len;
  471. *p++= '\0';
  472. } else if (packet->is_change_user_packet) {
  473. *p++= '\0';
  474. }
  475. /* no \0 for no DB */
  476. if (packet->is_change_user_packet) {
  477. if (packet->charset_no) {
  478. int2store(p, packet->charset_no);
  479. p+= 2;
  480. }
  481. }
  482. if (packet->auth_plugin_name) {
  483. len = MIN(strlen(packet->auth_plugin_name), sizeof(buffer) - (p - buffer) - 1);
  484. memcpy(p, packet->auth_plugin_name, len);
  485. p+= len;
  486. *p++= '\0';
  487. }
  488. if (packet->connect_attr && zend_hash_num_elements(packet->connect_attr)) {
  489. size_t ca_payload_len = 0;
  490. {
  491. zend_string * key;
  492. zval * entry_value;
  493. ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(packet->connect_attr, key, entry_value) {
  494. if (key) { /* HASH_KEY_IS_STRING */
  495. size_t value_len = Z_STRLEN_P(entry_value);
  496. ca_payload_len += php_mysqlnd_net_store_length_size(ZSTR_LEN(key));
  497. ca_payload_len += ZSTR_LEN(key);
  498. ca_payload_len += php_mysqlnd_net_store_length_size(value_len);
  499. ca_payload_len += value_len;
  500. }
  501. } ZEND_HASH_FOREACH_END();
  502. }
  503. if (sizeof(buffer) >= (ca_payload_len + php_mysqlnd_net_store_length_size(ca_payload_len) + (p - buffer))) {
  504. p = php_mysqlnd_net_store_length(p, ca_payload_len);
  505. {
  506. zend_string * key;
  507. zval * entry_value;
  508. ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(packet->connect_attr, key, entry_value) {
  509. if (key) { /* HASH_KEY_IS_STRING */
  510. size_t value_len = Z_STRLEN_P(entry_value);
  511. /* copy key */
  512. p = php_mysqlnd_net_store_length(p, ZSTR_LEN(key));
  513. memcpy(p, ZSTR_VAL(key), ZSTR_LEN(key));
  514. p+= ZSTR_LEN(key);
  515. /* copy value */
  516. p = php_mysqlnd_net_store_length(p, value_len);
  517. memcpy(p, Z_STRVAL_P(entry_value), value_len);
  518. p+= value_len;
  519. }
  520. } ZEND_HASH_FOREACH_END();
  521. }
  522. } else {
  523. /* cannot put the data - skip */
  524. }
  525. }
  526. }
  527. if (packet->is_change_user_packet) {
  528. enum_func_status ret = FAIL;
  529. const MYSQLND_CSTRING payload = {(char*) buffer + MYSQLND_HEADER_SIZE, p - (buffer + MYSQLND_HEADER_SIZE)};
  530. const unsigned int silent = packet->silent;
  531. ret = conn->command->change_user(conn, payload, silent);
  532. DBG_RETURN(ret == PASS? (p - buffer - MYSQLND_HEADER_SIZE) : 0);
  533. } else {
  534. /*
  535. The auth handshake packet has no command in it. Thus we can't go over conn->command directly.
  536. Well, we can have a command->no_command(conn, payload)
  537. */
  538. const size_t sent = pfc->data->m.send(pfc, vio, buffer, p - buffer - MYSQLND_HEADER_SIZE, stats, error_info);
  539. if (!sent) {
  540. SET_CONNECTION_STATE(connection_state, CONN_QUIT_SENT);
  541. }
  542. DBG_RETURN(sent);
  543. }
  544. }
  545. /* }}} */
  546. #define AUTH_RESP_BUFFER_SIZE 2048
  547. /* {{{ php_mysqlnd_auth_response_read */
  548. static enum_func_status
  549. php_mysqlnd_auth_response_read(MYSQLND_CONN_DATA * conn, void * _packet)
  550. {
  551. MYSQLND_PACKET_AUTH_RESPONSE * packet= (MYSQLND_PACKET_AUTH_RESPONSE *) _packet;
  552. MYSQLND_ERROR_INFO * error_info = conn->error_info;
  553. MYSQLND_PFC * pfc = conn->protocol_frame_codec;
  554. MYSQLND_VIO * vio = conn->vio;
  555. MYSQLND_STATS * stats = conn->stats;
  556. MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
  557. zend_uchar local_buf[AUTH_RESP_BUFFER_SIZE];
  558. size_t buf_len = pfc->cmd_buffer.buffer? pfc->cmd_buffer.length: AUTH_RESP_BUFFER_SIZE;
  559. zend_uchar *buf = pfc->cmd_buffer.buffer? (zend_uchar *) pfc->cmd_buffer.buffer : local_buf;
  560. const zend_uchar * p = buf;
  561. const zend_uchar * const begin = buf;
  562. DBG_ENTER("php_mysqlnd_auth_response_read");
  563. /* leave space for terminating safety \0 */
  564. buf_len--;
  565. if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, buf_len, "OK", PROT_OK_PACKET)) {
  566. DBG_RETURN(FAIL);
  567. }
  568. BAIL_IF_NO_MORE_DATA;
  569. /*
  570. zero-terminate the buffer for safety. We are sure there is place for the \0
  571. because buf_len is -1 the size of the buffer pointed
  572. */
  573. buf[packet->header.size] = '\0';
  574. /* Should be always 0x0 or ERROR_MARKER for error */
  575. packet->response_code = uint1korr(p);
  576. p++;
  577. BAIL_IF_NO_MORE_DATA;
  578. if (ERROR_MARKER == packet->response_code) {
  579. php_mysqlnd_read_error_from_line(p, packet->header.size - 1,
  580. packet->error, sizeof(packet->error),
  581. &packet->error_no, packet->sqlstate
  582. );
  583. DBG_RETURN(PASS);
  584. }
  585. if (0xFE == packet->response_code) {
  586. /* Authentication Switch Response */
  587. if (packet->header.size > (size_t) (p - buf)) {
  588. packet->new_auth_protocol = mnd_pestrdup((char *)p, FALSE);
  589. packet->new_auth_protocol_len = strlen(packet->new_auth_protocol);
  590. p+= packet->new_auth_protocol_len + 1; /* +1 for the \0 */
  591. packet->new_auth_protocol_data_len = packet->header.size - (size_t) (p - buf);
  592. if (packet->new_auth_protocol_data_len) {
  593. packet->new_auth_protocol_data = mnd_emalloc(packet->new_auth_protocol_data_len);
  594. memcpy(packet->new_auth_protocol_data, p, packet->new_auth_protocol_data_len);
  595. }
  596. DBG_INF_FMT("The server requested switching auth plugin to : %s", packet->new_auth_protocol);
  597. DBG_INF_FMT("Server salt : [%zu][%.*s]", packet->new_auth_protocol_data_len, (int) packet->new_auth_protocol_data_len, packet->new_auth_protocol_data);
  598. }
  599. } else {
  600. zend_ulong net_len;
  601. /* Everything was fine! */
  602. packet->affected_rows = php_mysqlnd_net_field_length_ll(&p);
  603. BAIL_IF_NO_MORE_DATA;
  604. packet->last_insert_id = php_mysqlnd_net_field_length_ll(&p);
  605. BAIL_IF_NO_MORE_DATA;
  606. packet->server_status = uint2korr(p);
  607. p+= 2;
  608. BAIL_IF_NO_MORE_DATA;
  609. packet->warning_count = uint2korr(p);
  610. p+= 2;
  611. BAIL_IF_NO_MORE_DATA;
  612. /* There is a message */
  613. if (packet->header.size > (size_t) (p - buf) && (net_len = php_mysqlnd_net_field_length(&p))) {
  614. packet->message_len = MIN(net_len, buf_len - (p - begin));
  615. packet->message = mnd_pestrndup((char *)p, packet->message_len, FALSE);
  616. } else {
  617. packet->message = NULL;
  618. packet->message_len = 0;
  619. }
  620. DBG_INF_FMT("OK packet: aff_rows=%" PRIu64 " last_ins_id=%" PRIu64 " server_status=%u warnings=%u",
  621. packet->affected_rows, packet->last_insert_id, packet->server_status,
  622. packet->warning_count);
  623. }
  624. DBG_RETURN(PASS);
  625. premature_end:
  626. DBG_ERR_FMT("OK packet %zu bytes shorter than expected", p - begin - packet->header.size);
  627. php_error_docref(NULL, E_WARNING, "AUTH_RESPONSE packet %zu bytes shorter than expected",
  628. p - begin - packet->header.size);
  629. DBG_RETURN(FAIL);
  630. }
  631. /* }}} */
  632. /* {{{ php_mysqlnd_auth_response_free_mem */
  633. static void
  634. php_mysqlnd_auth_response_free_mem(void * _packet)
  635. {
  636. MYSQLND_PACKET_AUTH_RESPONSE * p = (MYSQLND_PACKET_AUTH_RESPONSE *) _packet;
  637. if (p->message) {
  638. mnd_efree(p->message);
  639. p->message = NULL;
  640. }
  641. if (p->new_auth_protocol) {
  642. mnd_efree(p->new_auth_protocol);
  643. p->new_auth_protocol = NULL;
  644. }
  645. p->new_auth_protocol_len = 0;
  646. if (p->new_auth_protocol_data) {
  647. mnd_efree(p->new_auth_protocol_data);
  648. p->new_auth_protocol_data = NULL;
  649. }
  650. p->new_auth_protocol_data_len = 0;
  651. }
  652. /* }}} */
  653. /* {{{ php_mysqlnd_change_auth_response_write */
  654. static size_t
  655. php_mysqlnd_change_auth_response_write(MYSQLND_CONN_DATA * conn, void * _packet)
  656. {
  657. MYSQLND_PACKET_CHANGE_AUTH_RESPONSE *packet= (MYSQLND_PACKET_CHANGE_AUTH_RESPONSE *) _packet;
  658. MYSQLND_ERROR_INFO * error_info = conn->error_info;
  659. MYSQLND_PFC * pfc = conn->protocol_frame_codec;
  660. MYSQLND_VIO * vio = conn->vio;
  661. MYSQLND_STATS * stats = conn->stats;
  662. MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
  663. zend_uchar * const buffer = pfc->cmd_buffer.length >= packet->auth_data_len? pfc->cmd_buffer.buffer : mnd_emalloc(packet->auth_data_len);
  664. zend_uchar * p = buffer + MYSQLND_HEADER_SIZE; /* start after the header */
  665. DBG_ENTER("php_mysqlnd_change_auth_response_write");
  666. if (packet->auth_data_len) {
  667. memcpy(p, packet->auth_data, packet->auth_data_len);
  668. p+= packet->auth_data_len;
  669. }
  670. {
  671. /*
  672. The auth handshake packet has no command in it. Thus we can't go over conn->command directly.
  673. Well, we can have a command->no_command(conn, payload)
  674. */
  675. const size_t sent = pfc->data->m.send(pfc, vio, buffer, p - buffer - MYSQLND_HEADER_SIZE, stats, error_info);
  676. if (buffer != pfc->cmd_buffer.buffer) {
  677. mnd_efree(buffer);
  678. }
  679. if (!sent) {
  680. SET_CONNECTION_STATE(connection_state, CONN_QUIT_SENT);
  681. }
  682. DBG_RETURN(sent);
  683. }
  684. }
  685. /* }}} */
  686. #define OK_BUFFER_SIZE 2048
  687. /* {{{ php_mysqlnd_ok_read */
  688. static enum_func_status
  689. php_mysqlnd_ok_read(MYSQLND_CONN_DATA * conn, void * _packet)
  690. {
  691. MYSQLND_PACKET_OK *packet= (MYSQLND_PACKET_OK *) _packet;
  692. MYSQLND_ERROR_INFO * error_info = conn->error_info;
  693. MYSQLND_PFC * pfc = conn->protocol_frame_codec;
  694. MYSQLND_VIO * vio = conn->vio;
  695. MYSQLND_STATS * stats = conn->stats;
  696. MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
  697. zend_uchar local_buf[OK_BUFFER_SIZE];
  698. const size_t buf_len = pfc->cmd_buffer.buffer? pfc->cmd_buffer.length : OK_BUFFER_SIZE;
  699. zend_uchar * const buf = pfc->cmd_buffer.buffer? (zend_uchar *) pfc->cmd_buffer.buffer : local_buf;
  700. const zend_uchar * p = buf;
  701. const zend_uchar * const begin = buf;
  702. zend_ulong net_len;
  703. DBG_ENTER("php_mysqlnd_ok_read");
  704. if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, buf_len, "OK", PROT_OK_PACKET)) {
  705. DBG_RETURN(FAIL);
  706. }
  707. BAIL_IF_NO_MORE_DATA;
  708. /* Should be always 0x0 or ERROR_MARKER for error */
  709. packet->field_count = uint1korr(p);
  710. p++;
  711. BAIL_IF_NO_MORE_DATA;
  712. if (ERROR_MARKER == packet->field_count) {
  713. php_mysqlnd_read_error_from_line(p, packet->header.size - 1,
  714. packet->error, sizeof(packet->error),
  715. &packet->error_no, packet->sqlstate
  716. );
  717. DBG_RETURN(PASS);
  718. }
  719. /* Everything was fine! */
  720. packet->affected_rows = php_mysqlnd_net_field_length_ll(&p);
  721. BAIL_IF_NO_MORE_DATA;
  722. packet->last_insert_id = php_mysqlnd_net_field_length_ll(&p);
  723. BAIL_IF_NO_MORE_DATA;
  724. packet->server_status = uint2korr(p);
  725. p+= 2;
  726. BAIL_IF_NO_MORE_DATA;
  727. packet->warning_count = uint2korr(p);
  728. p+= 2;
  729. BAIL_IF_NO_MORE_DATA;
  730. /* There is a message */
  731. if (packet->header.size > (size_t) (p - buf) && (net_len = php_mysqlnd_net_field_length(&p))) {
  732. packet->message_len = MIN(net_len, buf_len - (p - begin));
  733. packet->message = mnd_pestrndup((char *)p, packet->message_len, FALSE);
  734. } else {
  735. packet->message = NULL;
  736. packet->message_len = 0;
  737. }
  738. DBG_INF_FMT("OK packet: aff_rows=%" PRIu64 " last_ins_id=%" PRIu64 " server_status=%u warnings=%u",
  739. packet->affected_rows, packet->last_insert_id, packet->server_status,
  740. packet->warning_count);
  741. BAIL_IF_NO_MORE_DATA;
  742. DBG_RETURN(PASS);
  743. premature_end:
  744. DBG_ERR_FMT("OK packet %zu bytes shorter than expected", p - begin - packet->header.size);
  745. php_error_docref(NULL, E_WARNING, "OK packet %zu bytes shorter than expected",
  746. p - begin - packet->header.size);
  747. DBG_RETURN(FAIL);
  748. }
  749. /* }}} */
  750. /* {{{ php_mysqlnd_ok_free_mem */
  751. static void
  752. php_mysqlnd_ok_free_mem(void * _packet)
  753. {
  754. MYSQLND_PACKET_OK *p= (MYSQLND_PACKET_OK *) _packet;
  755. if (p->message) {
  756. mnd_efree(p->message);
  757. p->message = NULL;
  758. }
  759. }
  760. /* }}} */
  761. /* {{{ php_mysqlnd_eof_read */
  762. static enum_func_status
  763. php_mysqlnd_eof_read(MYSQLND_CONN_DATA * conn, void * _packet)
  764. {
  765. /*
  766. EOF packet is since 4.1 five bytes long,
  767. but we can get also an error, make it bigger.
  768. Error : error_code + '#' + sqlstate + MYSQLND_ERRMSG_SIZE
  769. */
  770. MYSQLND_PACKET_EOF *packet= (MYSQLND_PACKET_EOF *) _packet;
  771. MYSQLND_ERROR_INFO * error_info = conn->error_info;
  772. MYSQLND_PFC * pfc = conn->protocol_frame_codec;
  773. MYSQLND_VIO * vio = conn->vio;
  774. MYSQLND_STATS * stats = conn->stats;
  775. MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
  776. const size_t buf_len = pfc->cmd_buffer.length;
  777. zend_uchar * const buf = (zend_uchar *) pfc->cmd_buffer.buffer;
  778. const zend_uchar * p = buf;
  779. const zend_uchar * const begin = buf;
  780. DBG_ENTER("php_mysqlnd_eof_read");
  781. if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, buf_len, "EOF", PROT_EOF_PACKET)) {
  782. DBG_RETURN(FAIL);
  783. }
  784. BAIL_IF_NO_MORE_DATA;
  785. /* Should be always EODATA_MARKER */
  786. packet->field_count = uint1korr(p);
  787. p++;
  788. BAIL_IF_NO_MORE_DATA;
  789. if (ERROR_MARKER == packet->field_count) {
  790. php_mysqlnd_read_error_from_line(p, packet->header.size - 1,
  791. packet->error, sizeof(packet->error),
  792. &packet->error_no, packet->sqlstate
  793. );
  794. DBG_RETURN(PASS);
  795. }
  796. /*
  797. 4.1 sends 1 byte EOF packet after metadata of
  798. PREPARE/EXECUTE but 5 bytes after the result. This is not
  799. according to the Docs@Forge!!!
  800. */
  801. if (packet->header.size > 1) {
  802. packet->warning_count = uint2korr(p);
  803. p+= 2;
  804. BAIL_IF_NO_MORE_DATA;
  805. packet->server_status = uint2korr(p);
  806. p+= 2;
  807. BAIL_IF_NO_MORE_DATA;
  808. } else {
  809. packet->warning_count = 0;
  810. packet->server_status = 0;
  811. }
  812. BAIL_IF_NO_MORE_DATA;
  813. DBG_INF_FMT("EOF packet: fields=%u status=%u warnings=%u",
  814. packet->field_count, packet->server_status, packet->warning_count);
  815. DBG_RETURN(PASS);
  816. premature_end:
  817. DBG_ERR_FMT("EOF packet %zu bytes shorter than expected", p - begin - packet->header.size);
  818. php_error_docref(NULL, E_WARNING, "EOF packet %zu bytes shorter than expected",
  819. p - begin - packet->header.size);
  820. DBG_RETURN(FAIL);
  821. }
  822. /* }}} */
  823. /* {{{ php_mysqlnd_cmd_write */
  824. size_t php_mysqlnd_cmd_write(MYSQLND_CONN_DATA * conn, void * _packet)
  825. {
  826. /* Let's have some space, which we can use, if not enough, we will allocate new buffer */
  827. MYSQLND_PACKET_COMMAND * packet= (MYSQLND_PACKET_COMMAND *) _packet;
  828. MYSQLND_ERROR_INFO * error_info = conn->error_info;
  829. MYSQLND_PFC * pfc = conn->protocol_frame_codec;
  830. MYSQLND_VIO * vio = conn->vio;
  831. MYSQLND_STATS * stats = conn->stats;
  832. MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
  833. size_t sent = 0;
  834. DBG_ENTER("php_mysqlnd_cmd_write");
  835. /*
  836. Reset packet_no, or we will get bad handshake!
  837. Every command starts a new TX and packet numbers are reset to 0.
  838. */
  839. pfc->data->m.reset(pfc, stats, error_info);
  840. MYSQLND_INC_CONN_STATISTIC(stats, STAT_PACKETS_SENT_CMD);
  841. #ifdef MYSQLND_DO_WIRE_CHECK_BEFORE_COMMAND
  842. vio->data->m.consume_uneaten_data(vio, packet->command);
  843. #endif
  844. if (!packet->argument.s || !packet->argument.l) {
  845. zend_uchar buffer[MYSQLND_HEADER_SIZE + 1];
  846. int1store(buffer + MYSQLND_HEADER_SIZE, packet->command);
  847. sent = pfc->data->m.send(pfc, vio, buffer, 1, stats, error_info);
  848. } else {
  849. size_t tmp_len = packet->argument.l + 1 + MYSQLND_HEADER_SIZE;
  850. zend_uchar *tmp, *p;
  851. tmp = (tmp_len > pfc->cmd_buffer.length)? mnd_emalloc(tmp_len):pfc->cmd_buffer.buffer;
  852. if (!tmp) {
  853. goto end;
  854. }
  855. p = tmp + MYSQLND_HEADER_SIZE; /* skip the header */
  856. int1store(p, packet->command);
  857. p++;
  858. memcpy(p, packet->argument.s, packet->argument.l);
  859. sent = pfc->data->m.send(pfc, vio, tmp, tmp_len - MYSQLND_HEADER_SIZE, stats, error_info);
  860. if (tmp != pfc->cmd_buffer.buffer) {
  861. MYSQLND_INC_CONN_STATISTIC(stats, STAT_CMD_BUFFER_TOO_SMALL);
  862. mnd_efree(tmp);
  863. }
  864. }
  865. end:
  866. if (!sent) {
  867. SET_CONNECTION_STATE(connection_state, CONN_QUIT_SENT);
  868. }
  869. DBG_RETURN(sent);
  870. }
  871. /* }}} */
  872. /* {{{ php_mysqlnd_rset_header_read */
  873. static enum_func_status
  874. php_mysqlnd_rset_header_read(MYSQLND_CONN_DATA * conn, void * _packet)
  875. {
  876. MYSQLND_PACKET_RSET_HEADER * packet= (MYSQLND_PACKET_RSET_HEADER *) _packet;
  877. MYSQLND_ERROR_INFO * error_info = conn->error_info;
  878. MYSQLND_PFC * pfc = conn->protocol_frame_codec;
  879. MYSQLND_VIO * vio = conn->vio;
  880. MYSQLND_STATS * stats = conn->stats;
  881. MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
  882. enum_func_status ret = PASS;
  883. const size_t buf_len = pfc->cmd_buffer.length;
  884. zend_uchar * const buf = (zend_uchar *) pfc->cmd_buffer.buffer;
  885. const zend_uchar * p = buf;
  886. const zend_uchar * const begin = buf;
  887. size_t len;
  888. DBG_ENTER("php_mysqlnd_rset_header_read");
  889. if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, buf_len, "resultset header", PROT_RSET_HEADER_PACKET)) {
  890. DBG_RETURN(FAIL);
  891. }
  892. BAIL_IF_NO_MORE_DATA;
  893. /*
  894. Don't increment. First byte is ERROR_MARKER on error, but otherwise is starting byte
  895. of encoded sequence for length.
  896. */
  897. if (ERROR_MARKER == *p) {
  898. /* Error */
  899. p++;
  900. BAIL_IF_NO_MORE_DATA;
  901. php_mysqlnd_read_error_from_line(p, packet->header.size - 1,
  902. packet->error_info.error, sizeof(packet->error_info.error),
  903. &packet->error_info.error_no, packet->error_info.sqlstate
  904. );
  905. DBG_RETURN(PASS);
  906. }
  907. packet->field_count = php_mysqlnd_net_field_length(&p);
  908. BAIL_IF_NO_MORE_DATA;
  909. switch (packet->field_count) {
  910. case MYSQLND_NULL_LENGTH:
  911. DBG_INF("LOAD LOCAL");
  912. /*
  913. First byte in the packet is the field count.
  914. Thus, the name is size - 1. And we add 1 for a trailing \0.
  915. Because we have BAIL_IF_NO_MORE_DATA before the switch, we are guaranteed
  916. that packet->header.size is > 0. Which means that len can't underflow, that
  917. would lead to 0 byte allocation but 2^32 or 2^64 bytes copied.
  918. */
  919. len = packet->header.size - 1;
  920. packet->info_or_local_file.s = mnd_emalloc(len + 1);
  921. memcpy(packet->info_or_local_file.s, p, len);
  922. packet->info_or_local_file.s[len] = '\0';
  923. packet->info_or_local_file.l = len;
  924. break;
  925. case 0x00:
  926. DBG_INF("UPSERT");
  927. packet->affected_rows = php_mysqlnd_net_field_length_ll(&p);
  928. BAIL_IF_NO_MORE_DATA;
  929. packet->last_insert_id = php_mysqlnd_net_field_length_ll(&p);
  930. BAIL_IF_NO_MORE_DATA;
  931. packet->server_status = uint2korr(p);
  932. p+=2;
  933. BAIL_IF_NO_MORE_DATA;
  934. packet->warning_count = uint2korr(p);
  935. p+=2;
  936. BAIL_IF_NO_MORE_DATA;
  937. /* Check for additional textual data */
  938. if (packet->header.size > (size_t) (p - buf) && (len = php_mysqlnd_net_field_length(&p))) {
  939. packet->info_or_local_file.s = mnd_emalloc(len + 1);
  940. memcpy(packet->info_or_local_file.s, p, len);
  941. packet->info_or_local_file.s[len] = '\0';
  942. packet->info_or_local_file.l = len;
  943. }
  944. DBG_INF_FMT("affected_rows=%" PRIu64 " last_insert_id=%" PRIu64 " server_status=%u warning_count=%u",
  945. packet->affected_rows, packet->last_insert_id,
  946. packet->server_status, packet->warning_count);
  947. break;
  948. default:
  949. DBG_INF("SELECT");
  950. /* Result set */
  951. break;
  952. }
  953. BAIL_IF_NO_MORE_DATA;
  954. DBG_RETURN(ret);
  955. premature_end:
  956. DBG_ERR_FMT("RSET_HEADER packet %zu bytes shorter than expected", p - begin - packet->header.size);
  957. php_error_docref(NULL, E_WARNING, "RSET_HEADER packet %zu bytes shorter than expected",
  958. p - begin - packet->header.size);
  959. DBG_RETURN(FAIL);
  960. }
  961. /* }}} */
  962. /* {{{ php_mysqlnd_rset_header_free_mem */
  963. static
  964. void php_mysqlnd_rset_header_free_mem(void * _packet)
  965. {
  966. MYSQLND_PACKET_RSET_HEADER *p= (MYSQLND_PACKET_RSET_HEADER *) _packet;
  967. DBG_ENTER("php_mysqlnd_rset_header_free_mem");
  968. mysqlnd_set_string(&p->info_or_local_file, NULL, 0);
  969. DBG_VOID_RETURN;
  970. }
  971. /* }}} */
  972. #define READ_RSET_FIELD(field_name) do { \
  973. len = php_mysqlnd_net_field_length(&p); \
  974. if (UNEXPECTED(len == MYSQLND_NULL_LENGTH)) { \
  975. goto faulty_or_fake; \
  976. } else if (len != 0) { \
  977. meta->field_name = (const char *)p; \
  978. meta->field_name ## _length = len; \
  979. p += len; \
  980. total_len += len + 1; \
  981. } else { \
  982. meta->field_name = mysqlnd_empty_string; \
  983. meta->field_name ## _length = 0; \
  984. } \
  985. } while (0)
  986. /* {{{ php_mysqlnd_rset_field_read */
  987. static enum_func_status
  988. php_mysqlnd_rset_field_read(MYSQLND_CONN_DATA * conn, void * _packet)
  989. {
  990. /* Should be enough for the metadata of a single row */
  991. MYSQLND_PACKET_RES_FIELD *packet = (MYSQLND_PACKET_RES_FIELD *) _packet;
  992. MYSQLND_ERROR_INFO * error_info = conn->error_info;
  993. MYSQLND_PFC * pfc = conn->protocol_frame_codec;
  994. MYSQLND_VIO * vio = conn->vio;
  995. MYSQLND_STATS * stats = conn->stats;
  996. MYSQLND_CONNECTION_STATE * connection_state = &conn->state;
  997. const size_t buf_len = pfc->cmd_buffer.length;
  998. size_t total_len = 0;
  999. zend_uchar * const buf = (zend_uchar *) pfc->cmd_buffer.buffer;
  1000. const zend_uchar * p = buf;
  1001. const zend_uchar * const begin = buf;
  1002. char *root_ptr;
  1003. zend_ulong len;
  1004. MYSQLND_FIELD *meta;
  1005. DBG_ENTER("php_mysqlnd_rset_field_read");
  1006. if (FAIL == mysqlnd_read_packet_header_and_body(&(packet->header), pfc, vio, stats, error_info, connection_state, buf, buf_len, "field", PROT_RSET_FLD_PACKET)) {
  1007. DBG_RETURN(FAIL);
  1008. }
  1009. if (packet->skip_parsing) {
  1010. DBG_RETURN(PASS);
  1011. }
  1012. BAIL_IF_NO_MORE_DATA;
  1013. if (ERROR_MARKER == *p) {
  1014. /* Error */
  1015. p++;
  1016. BAIL_IF_NO_MORE_DATA;
  1017. php_mysqlnd_read_error_from_line(p, packet->header.size - 1,
  1018. packet->error_info.error, sizeof(packet->error_info.error),
  1019. &packet->error_info.error_no, packet->error_info.sqlstate
  1020. );
  1021. DBG_ERR_FMT("Server error : (%u) %s", packet->error_info.error_no, packet->error_info.error);
  1022. DBG_RETURN(PASS);
  1023. } else if (EODATA_MARKER == *p && packet->header.size < 8) {
  1024. /* Premature EOF. That should be COM_FIELD_LIST. But we don't support COM_FIELD_LIST anymore, thus this should not happen */
  1025. DBG_ERR("Premature EOF. That should be COM_FIELD_LIST");
  1026. php_error_docref(NULL, E_WARNING, "Premature EOF in result field metadata");
  1027. DBG_RETURN(FAIL);
  1028. }
  1029. meta = packet->metadata;
  1030. READ_RSET_FIELD(catalog);
  1031. READ_RSET_FIELD(db);
  1032. READ_RSET_FIELD(table);
  1033. READ_RSET_FIELD(org_table);
  1034. READ_RSET_FIELD(name);
  1035. READ_RSET_FIELD(org_name);
  1036. /* 1 byte length */
  1037. if (UNEXPECTED(12 != *p)) {
  1038. DBG_ERR_FMT("Protocol error. Server sent false length. Expected 12 got %d", (int) *p);
  1039. php_error_docref(NULL, E_WARNING, "Protocol error. Server sent false length. Expected 12");
  1040. }
  1041. if ((size_t)((p - begin) + 12) > packet->header.size) {
  1042. php_error_docref(NULL, E_WARNING, "Premature end of data (mysqlnd_wireprotocol.c:%u)", __LINE__);
  1043. goto premature_end;
  1044. }
  1045. p++;
  1046. meta->charsetnr = uint2korr(p);
  1047. p += 2;
  1048. meta->length = uint4korr(p);
  1049. p += 4;
  1050. meta->type = uint1korr(p);
  1051. p += 1;
  1052. meta->flags = uint2korr(p);
  1053. p += 2;
  1054. meta->decimals = uint1korr(p);
  1055. p += 1;
  1056. /* 2 byte filler */
  1057. p +=2;
  1058. /* Should we set NUM_FLAG (libmysql does it) ? */
  1059. if (
  1060. (meta->type <= MYSQL_TYPE_INT24 &&
  1061. (meta->type != MYSQL_TYPE_TIMESTAMP || meta->length == 14 || meta->length == 8)
  1062. ) || meta->type == MYSQL_TYPE_YEAR)
  1063. {
  1064. meta->flags |= NUM_FLAG;
  1065. }
  1066. /*
  1067. def could be empty, thus don't allocate on the root.
  1068. NULL_LENGTH (0xFB) comes from COM_FIELD_LIST when the default value is NULL.
  1069. Otherwise the string is length encoded.
  1070. */
  1071. if (packet->header.size > (size_t) (p - buf) &&
  1072. (len = php_mysqlnd_net_field_length(&p)) &&
  1073. len != MYSQLND_NULL_LENGTH)
  1074. {
  1075. BAIL_IF_NO_MORE_DATA;
  1076. DBG_INF_FMT("Def found, length " ZEND_ULONG_FMT, len);
  1077. meta->def = packet->memory_pool->get_chunk(packet->memory_pool, len + 1);
  1078. memcpy(meta->def, p, len);
  1079. meta->def[len] = '\0';
  1080. meta->def_length = len;
  1081. p += len;
  1082. }
  1083. root_ptr = meta->root = packet->memory_pool->get_chunk(packet->memory_pool, total_len);
  1084. meta->root_len = total_len;
  1085. if (EXPECTED(meta->name_length != 0)) {
  1086. meta->sname = zend_string_init_interned(meta->name, meta->name_length, 0);
  1087. meta->name = ZSTR_VAL(meta->sname);
  1088. } else {
  1089. meta->sname = ZSTR_EMPTY_ALLOC();
  1090. }
  1091. /* Now do allocs */
  1092. if (meta->catalog_length != 0) {
  1093. len = meta->catalog_length;
  1094. meta->catalog = memcpy(root_ptr, meta->catalog, len);
  1095. *(root_ptr +=len) = '\0';
  1096. root_ptr++;
  1097. }
  1098. if (meta->db_length != 0) {
  1099. len = meta->db_length;
  1100. meta->db = memcpy(root_ptr, meta->db, len);
  1101. *(root_ptr +=len) = '\0';
  1102. root_ptr++;
  1103. }
  1104. if (meta->table_length != 0) {
  1105. len = meta->table_length;
  1106. meta->table = memcpy(root_ptr, meta->table, len);
  1107. *(root_ptr +=len) = '\0';
  1108. root_ptr++;
  1109. }
  1110. if (meta->org_table_length != 0) {
  1111. len = meta->org_table_length;
  1112. meta->org_table = memcpy(root_ptr, meta->org_table, len);
  1113. *(root_ptr +=len) = '\0';
  1114. root_ptr++;
  1115. }
  1116. if (meta->org_name_length != 0) {
  1117. len = meta->org_name_length;
  1118. meta->org_name = memcpy(root_ptr, meta->org_name, len);
  1119. *(root_ptr +=len) = '\0';
  1120. root_ptr++;
  1121. }
  1122. DBG_INF_FMT("allocing root.");
  1123. DBG_INF_FMT("FIELD=[%s.%s.%s]", meta->db? meta->db:"*NA*", meta->table? meta->table:"*NA*",
  1124. meta->name? meta->name:"*NA*");
  1125. DBG_RETURN(PASS);
  1126. faulty_or_fake:
  1127. DBG_ERR_FMT("Protocol error. Server sent NULL_LENGTH. The server is faulty");
  1128. php_error_docref(NULL, E_WARNING, "Protocol error. Server sent NULL_LENGTH."
  1129. " The server is faulty");
  1130. DBG_RETURN(FAIL);
  1131. premature_end:
  1132. DBG_ERR_FMT("RSET field packet %zu bytes shorter than expected", p - begin - packet->header.size);
  1133. php_error_docref(NULL, E_WARNING, "Result set field packet %zu bytes "
  1134. "shorter than expected", p - begin - packet->header.size);
  1135. DBG_RETURN(FAIL);
  1136. }
  1137. /* }}} */
  1138. /* Like SET_CLIENT_ERROR, but for packet error_info. The type is the same,
  1139. * but only some parts of it are used. */
  1140. static void set_packet_error(
  1141. MYSQLND_ERROR_INFO *info, unsigned err_no, const char *sqlstate, const char *error)
  1142. {
  1143. info->error_no = err_no;
  1144. strlcpy(info->sqlstate, sqlstate, sizeof(info->sqlstate));
  1145. strlcpy(info->error, error, sizeof(info->error));
  1146. }
  1147. /* {{{ php_mysqlnd_read_row_ex */
  1148. static enum_func_status
  1149. php_mysqlnd_read_row_ex(MYSQLND_PFC * pfc,
  1150. MYSQLND_VIO * vio,
  1151. MYSQLND_STATS * stats,
  1152. MYSQLND_ERROR_INFO * error_info,
  1153. MYSQLND_CONNECTION_STATE * connection_state,
  1154. MYSQLND_MEMORY_POOL * pool,
  1155. MYSQLND_ROW_BUFFER * buffer,
  1156. size_t * const data_size)
  1157. {
  1158. enum_func_status ret = PASS;
  1159. MYSQLND_PACKET_HEADER header;
  1160. zend_uchar * p = NULL;
  1161. size_t prealloc_more_bytes;
  1162. DBG_ENTER("php_mysqlnd_read_row_ex");
  1163. /*
  1164. To ease the process the server splits everything in packets up to 2^24 - 1.
  1165. Even in the case the payload is evenly divisible by this value, the last
  1166. packet will be empty, namely 0 bytes. Thus, we can read every packet and ask
  1167. for next one if they have 2^24 - 1 sizes. But just read the header of a
  1168. zero-length byte, don't read the body, there is no such.
  1169. */
  1170. /*
  1171. We're allocating an extra byte, as php_mysqlnd_rowp_read_text_protocol
  1172. needs to be able to append a terminating \0 for atoi/atof.
  1173. */
  1174. prealloc_more_bytes = 1;
  1175. *data_size = 0;
  1176. if (UNEXPECTED(FAIL == mysqlnd_read_header(pfc, vio, &header, stats, error_info))) {
  1177. ret = FAIL;
  1178. SET_CONNECTION_STATE(connection_state, CONN_QUIT_SENT);
  1179. set_packet_error(error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  1180. } else {
  1181. /* If the packet is split in multiple chunks, allocate a temporary buffer that we can
  1182. * reallocate, and only afterwards copy it to the pool when we know the final size. */
  1183. zend_uchar *buf = NULL;
  1184. while (header.size >= MYSQLND_MAX_PACKET_SIZE) {
  1185. buf = erealloc(buf, *data_size + header.size);
  1186. p = buf + *data_size;
  1187. *data_size += header.size;
  1188. if (UNEXPECTED(PASS != (ret = pfc->data->m.receive(pfc, vio, p, header.size, stats, error_info)))) {
  1189. DBG_ERR("Empty row packet body");
  1190. SET_CONNECTION_STATE(connection_state, CONN_QUIT_SENT);
  1191. set_packet_error(error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  1192. efree(buf);
  1193. DBG_RETURN(FAIL);
  1194. }
  1195. if (FAIL == mysqlnd_read_header(pfc, vio, &header, stats, error_info)) {
  1196. efree(buf);
  1197. DBG_RETURN(FAIL);
  1198. }
  1199. }
  1200. buffer->ptr = pool->get_chunk(pool, *data_size + header.size + prealloc_more_bytes);
  1201. if (buf) {
  1202. memcpy(buffer->ptr, buf, *data_size);
  1203. efree(buf);
  1204. }
  1205. p = (zend_uchar *) buffer->ptr + *data_size;
  1206. *data_size += header.size;
  1207. if (UNEXPECTED(PASS != (ret = pfc->data->m.receive(pfc, vio, p, header.size, stats, error_info)))) {
  1208. DBG_ERR("Empty row packet body");
  1209. SET_CONNECTION_STATE(connection_state, CONN_QUIT_SENT);
  1210. set_packet_error(error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  1211. }
  1212. }
  1213. DBG_RETURN(ret);
  1214. }
  1215. /* }}} */
  1216. /* {{{ php_mysqlnd_rowp_read_binary_protocol */
  1217. enum_func_status
  1218. php_mysqlnd_rowp_read_binary_protocol(MYSQLND_ROW_BUFFER * row_buffer, zval * fields,
  1219. const unsigned int field_count, const MYSQLND_FIELD * const fields_metadata,
  1220. const bool as_int_or_float, MYSQLND_STATS * const stats)
  1221. {
  1222. unsigned int i;
  1223. const zend_uchar * p = row_buffer->ptr;
  1224. const zend_uchar * null_ptr;
  1225. zend_uchar bit;
  1226. zval *current_field, *end_field, *start_field;
  1227. DBG_ENTER("php_mysqlnd_rowp_read_binary_protocol");
  1228. if (!fields) {
  1229. DBG_RETURN(FAIL);
  1230. }
  1231. end_field = (start_field = fields) + field_count;
  1232. /* skip the first byte, not EODATA_MARKER -> 0x0, status */
  1233. p++;
  1234. null_ptr= p;
  1235. p += (field_count + 9)/8; /* skip null bits */
  1236. bit = 4; /* first 2 bits are reserved */
  1237. for (i = 0, current_field = start_field; current_field < end_field; current_field++, i++) {
  1238. enum_mysqlnd_collected_stats statistic;
  1239. const zend_uchar * orig_p = p;
  1240. DBG_INF_FMT("Into zval=%p decoding column %u [%s.%s.%s] type=%u field->flags&unsigned=%u flags=%u is_bit=%u",
  1241. current_field, i,
  1242. fields_metadata[i].db, fields_metadata[i].table, fields_metadata[i].name, fields_metadata[i].type,
  1243. fields_metadata[i].flags & UNSIGNED_FLAG, fields_metadata[i].flags, fields_metadata[i].type == MYSQL_TYPE_BIT);
  1244. if (*null_ptr & bit) {
  1245. DBG_INF("It's null");
  1246. ZVAL_NULL(current_field);
  1247. statistic = STAT_BINARY_TYPE_FETCHED_NULL;
  1248. } else {
  1249. enum_mysqlnd_field_types type = fields_metadata[i].type;
  1250. mysqlnd_ps_fetch_functions[type].func(current_field, &fields_metadata[i], 0, &p);
  1251. if (MYSQLND_G(collect_statistics)) {
  1252. switch (fields_metadata[i].type) {
  1253. case MYSQL_TYPE_DECIMAL: statistic = STAT_BINARY_TYPE_FETCHED_DECIMAL; break;
  1254. case MYSQL_TYPE_TINY: statistic = STAT_BINARY_TYPE_FETCHED_INT8; break;
  1255. case MYSQL_TYPE_SHORT: statistic = STAT_BINARY_TYPE_FETCHED_INT16; break;
  1256. case MYSQL_TYPE_LONG: statistic = STAT_BINARY_TYPE_FETCHED_INT32; break;
  1257. case MYSQL_TYPE_FLOAT: statistic = STAT_BINARY_TYPE_FETCHED_FLOAT; break;
  1258. case MYSQL_TYPE_DOUBLE: statistic = STAT_BINARY_TYPE_FETCHED_DOUBLE; break;
  1259. case MYSQL_TYPE_NULL: statistic = STAT_BINARY_TYPE_FETCHED_NULL; break;
  1260. case MYSQL_TYPE_TIMESTAMP: statistic = STAT_BINARY_TYPE_FETCHED_TIMESTAMP; break;
  1261. case MYSQL_TYPE_LONGLONG: statistic = STAT_BINARY_TYPE_FETCHED_INT64; break;
  1262. case MYSQL_TYPE_INT24: statistic = STAT_BINARY_TYPE_FETCHED_INT24; break;
  1263. case MYSQL_TYPE_DATE: statistic = STAT_BINARY_TYPE_FETCHED_DATE; break;
  1264. case MYSQL_TYPE_TIME: statistic = STAT_BINARY_TYPE_FETCHED_TIME; break;
  1265. case MYSQL_TYPE_DATETIME: statistic = STAT_BINARY_TYPE_FETCHED_DATETIME; break;
  1266. case MYSQL_TYPE_YEAR: statistic = STAT_BINARY_TYPE_FETCHED_YEAR; break;
  1267. case MYSQL_TYPE_NEWDATE: statistic = STAT_BINARY_TYPE_FETCHED_DATE; break;
  1268. case MYSQL_TYPE_VARCHAR: statistic = STAT_BINARY_TYPE_FETCHED_STRING; break;
  1269. case MYSQL_TYPE_BIT: statistic = STAT_BINARY_TYPE_FETCHED_BIT; break;
  1270. case MYSQL_TYPE_NEWDECIMAL: statistic = STAT_BINARY_TYPE_FETCHED_DECIMAL; break;
  1271. case MYSQL_TYPE_ENUM: statistic = STAT_BINARY_TYPE_FETCHED_ENUM; break;
  1272. case MYSQL_TYPE_SET: statistic = STAT_BINARY_TYPE_FETCHED_SET; break;
  1273. case MYSQL_TYPE_TINY_BLOB: statistic = STAT_BINARY_TYPE_FETCHED_BLOB; break;
  1274. case MYSQL_TYPE_MEDIUM_BLOB:statistic = STAT_BINARY_TYPE_FETCHED_BLOB; break;
  1275. case MYSQL_TYPE_LONG_BLOB: statistic = STAT_BINARY_TYPE_FETCHED_BLOB; break;
  1276. case MYSQL_TYPE_BLOB: statistic = STAT_BINARY_TYPE_FETCHED_BLOB; break;
  1277. case MYSQL_TYPE_VAR_STRING: statistic = STAT_BINARY_TYPE_FETCHED_STRING; break;
  1278. case MYSQL_TYPE_STRING: statistic = STAT_BINARY_TYPE_FETCHED_STRING; break;
  1279. case MYSQL_TYPE_GEOMETRY: statistic = STAT_BINARY_TYPE_FETCHED_GEOMETRY; break;
  1280. default: statistic = STAT_BINARY_TYPE_FETCHED_OTHER; break;
  1281. }
  1282. }
  1283. }
  1284. MYSQLND_INC_CONN_STATISTIC_W_VALUE2(stats, statistic, 1,
  1285. STAT_BYTES_RECEIVED_PURE_DATA_PS,
  1286. (Z_TYPE_P(current_field) == IS_STRING)?
  1287. Z_STRLEN_P(current_field) : (size_t)(p - orig_p));
  1288. if (!((bit<<=1) & 255)) {
  1289. bit = 1; /* to the following byte */
  1290. null_ptr++;
  1291. }
  1292. }
  1293. DBG_RETURN(PASS);
  1294. }
  1295. /* }}} */
  1296. /* {{{ php_mysqlnd_rowp_read_text_protocol */
  1297. enum_func_status
  1298. php_mysqlnd_rowp_read_text_protocol(MYSQLND_ROW_BUFFER * row_buffer, zval * fields,
  1299. unsigned int field_count, const MYSQLND_FIELD * fields_metadata,
  1300. bool as_int_or_float, MYSQLND_STATS * stats)
  1301. {
  1302. unsigned int i;
  1303. zval *current_field, *end_field, *start_field;
  1304. zend_uchar * p = row_buffer->ptr;
  1305. const size_t data_size = row_buffer->size;
  1306. const zend_uchar * const packet_end = (zend_uchar*) p + data_size;
  1307. DBG_ENTER("php_mysqlnd_rowp_read_text_protocol");
  1308. if (!fields) {
  1309. DBG_RETURN(FAIL);
  1310. }
  1311. end_field = (start_field = fields) + field_count;
  1312. for (i = 0, current_field = start_field; current_field < end_field; current_field++, i++) {
  1313. /* php_mysqlnd_net_field_length() call should be after *this_field_len_pos = p; */
  1314. const zend_ulong len = php_mysqlnd_net_field_length((const zend_uchar **) &p);
  1315. /* NULL or NOT NULL, this is the question! */
  1316. if (len == MYSQLND_NULL_LENGTH) {
  1317. ZVAL_NULL(current_field);
  1318. } else if ((p + len) > packet_end) {
  1319. php_error_docref(NULL, E_WARNING, "Malformed server packet. Field length pointing %zu"
  1320. " bytes after end of packet", (p + len) - packet_end - 1);
  1321. DBG_RETURN(FAIL);
  1322. } else {
  1323. struct st_mysqlnd_perm_bind perm_bind =

Large files files are truncated, but you can click here to view the full file