xref: /PHP-7.4/ext/mysqlnd/mysqlnd_commands.c (revision 651fd1a9)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 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 
20 #include "php.h"
21 #include "mysqlnd.h"
22 #include "mysqlnd_connection.h"
23 #include "mysqlnd_priv.h"
24 #include "mysqlnd_auth.h"
25 #include "mysqlnd_wireprotocol.h"
26 #include "mysqlnd_debug.h"
27 
28 
29 /* {{{ mysqlnd_command::set_option */
30 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,set_option)31 MYSQLND_METHOD(mysqlnd_command, set_option)(MYSQLND_CONN_DATA * const conn, const enum_mysqlnd_server_option option)
32 {
33 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
34 	const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
35 	zend_uchar buffer[2];
36 	enum_func_status ret = FAIL;
37 
38 	DBG_ENTER("mysqlnd_command::set_option");
39 	int2store(buffer, (unsigned int) option);
40 
41 	ret = send_command(conn->payload_decoder_factory, COM_SET_OPTION, buffer, sizeof(buffer), FALSE,
42 					   &conn->state,
43 					   conn->error_info,
44 					   conn->upsert_status,
45 					   conn->stats,
46 					   conn->m->send_close,
47 					   conn);
48 	if (PASS == ret) {
49 		ret = send_command_handle_response(conn->payload_decoder_factory, PROT_EOF_PACKET, FALSE, COM_SET_OPTION, TRUE,
50 		                                   conn->error_info, conn->upsert_status, &conn->last_message);
51 	}
52 	DBG_RETURN(ret);
53 }
54 /* }}} */
55 
56 
57 /* {{{ mysqlnd_command::debug */
58 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,debug)59 MYSQLND_METHOD(mysqlnd_command, debug)(MYSQLND_CONN_DATA * const conn)
60 {
61 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
62 	const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
63 	enum_func_status ret = FAIL;
64 
65 	DBG_ENTER("mysqlnd_command::debug");
66 
67 	ret = send_command(conn->payload_decoder_factory, COM_DEBUG, NULL, 0, FALSE,
68 					   &conn->state,
69 					   conn->error_info,
70 					   conn->upsert_status,
71 					   conn->stats,
72 					   conn->m->send_close,
73 					   conn);
74 	if (PASS == ret) {
75 		ret = send_command_handle_response(conn->payload_decoder_factory, PROT_EOF_PACKET, FALSE, COM_DEBUG, TRUE,
76 										   conn->error_info, conn->upsert_status, &conn->last_message);
77 	}
78 
79 	DBG_RETURN(ret);
80 }
81 /* }}} */
82 
83 
84 /* {{{ mysqlnd_command::init_db */
85 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,init_db)86 MYSQLND_METHOD(mysqlnd_command, init_db)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING db)
87 {
88 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
89 	const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
90 	enum_func_status ret = FAIL;
91 
92 	DBG_ENTER("mysqlnd_command::init_db");
93 
94 	ret = send_command(conn->payload_decoder_factory, COM_INIT_DB, (const zend_uchar*) db.s, db.l, FALSE,
95 					   &conn->state,
96 					   conn->error_info,
97 					   conn->upsert_status,
98 					   conn->stats,
99 					   conn->m->send_close,
100 					   conn);
101 	if (PASS == ret) {
102 		ret = send_command_handle_response(conn->payload_decoder_factory, PROT_OK_PACKET, FALSE, COM_INIT_DB, TRUE,
103 										   conn->error_info, conn->upsert_status, &conn->last_message);
104 	}
105 
106 	/*
107 	  The server sends 0 but libmysql doesn't read it and has established
108 	  a protocol of giving back -1. Thus we have to follow it :(
109 	*/
110 	UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
111 	if (ret == PASS) {
112 		if (conn->connect_or_select_db.s) {
113 			mnd_pefree(conn->connect_or_select_db.s, conn->persistent);
114 		}
115 		conn->connect_or_select_db.s = mnd_pestrndup(db.s, db.l, conn->persistent);
116 		conn->connect_or_select_db.l = db.l;
117 		if (!conn->connect_or_select_db.s) {
118 			/* OOM */
119 			SET_OOM_ERROR(conn->error_info);
120 			ret = FAIL;
121 		}
122 	}
123 
124 	DBG_RETURN(ret);
125 }
126 /* }}} */
127 
128 
129 /* {{{ mysqlnd_command::ping */
130 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,ping)131 MYSQLND_METHOD(mysqlnd_command, ping)(MYSQLND_CONN_DATA * const conn)
132 {
133 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
134 	const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
135 	enum_func_status ret = FAIL;
136 
137 	DBG_ENTER("mysqlnd_command::ping");
138 
139 	ret = send_command(conn->payload_decoder_factory, COM_PING, NULL, 0, TRUE,
140 					   &conn->state,
141 					   conn->error_info,
142 					   conn->upsert_status,
143 					   conn->stats,
144 					   conn->m->send_close,
145 					   conn);
146 	if (PASS == ret) {
147 		ret = send_command_handle_response(conn->payload_decoder_factory, PROT_OK_PACKET, TRUE, COM_PING, TRUE,
148 										   conn->error_info, conn->upsert_status, &conn->last_message);
149 	}
150 	/*
151 	  The server sends 0 but libmysql doesn't read it and has established
152 	  a protocol of giving back -1. Thus we have to follow it :(
153 	*/
154 	UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
155 
156 	DBG_RETURN(ret);
157 }
158 /* }}} */
159 
160 
161 /* {{{ mysqlnd_command::statistics */
162 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,statistics)163 MYSQLND_METHOD(mysqlnd_command, statistics)(MYSQLND_CONN_DATA * const conn, zend_string ** message)
164 {
165 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
166 	enum_func_status ret = FAIL;
167 
168 	DBG_ENTER("mysqlnd_command::statistics");
169 
170 	ret = send_command(conn->payload_decoder_factory, COM_STATISTICS, NULL, 0, FALSE,
171 					   &conn->state,
172 					   conn->error_info,
173 					   conn->upsert_status,
174 					   conn->stats,
175 					   conn->m->send_close,
176 					   conn);
177 
178 	if (PASS == ret) {
179 		MYSQLND_PACKET_STATS stats_header;
180 
181 		conn->payload_decoder_factory->m.init_stats_packet(&stats_header);
182 		if (PASS == (ret = PACKET_READ(conn, &stats_header))) {
183 			/* will be freed by Zend, thus don't use the mnd_ allocator */
184 			*message = zend_string_init(stats_header.message.s, stats_header.message.l, 0);
185 			DBG_INF(ZSTR_VAL(*message));
186 		}
187 		PACKET_FREE(&stats_header);
188 	}
189 
190 	DBG_RETURN(ret);
191 }
192 /* }}} */
193 
194 
195 /* {{{ mysqlnd_command::process_kill */
196 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,process_kill)197 MYSQLND_METHOD(mysqlnd_command, process_kill)(MYSQLND_CONN_DATA * const conn, const unsigned int process_id, const zend_bool read_response)
198 {
199 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
200 	const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
201 	zend_uchar buff[4];
202 	enum_func_status ret = FAIL;
203 
204 	DBG_ENTER("mysqlnd_command::process_kill");
205 	int4store(buff, process_id);
206 
207 	ret = send_command(conn->payload_decoder_factory, COM_PROCESS_KILL, buff, 4, FALSE,
208 					   &conn->state,
209 					   conn->error_info,
210 					   conn->upsert_status,
211 					   conn->stats,
212 					   conn->m->send_close,
213 					   conn);
214 	if (PASS == ret && read_response) {
215 		ret = send_command_handle_response(conn->payload_decoder_factory, PROT_OK_PACKET, FALSE, COM_PROCESS_KILL, TRUE,
216 										   conn->error_info, conn->upsert_status, &conn->last_message);
217 	}
218 
219 	if (read_response) {
220 		/*
221 		  The server sends 0 but libmysql doesn't read it and has established
222 		  a protocol of giving back -1. Thus we have to follow it :(
223 		*/
224 		UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
225 	} else if (PASS == ret) {
226 		SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
227 		conn->m->send_close(conn);
228 	}
229 
230 	DBG_RETURN(ret);
231 }
232 /* }}} */
233 
234 
235 /* {{{ mysqlnd_command::refresh */
236 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,refresh)237 MYSQLND_METHOD(mysqlnd_command, refresh)(MYSQLND_CONN_DATA * const conn, const uint8_t options)
238 {
239 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
240 	const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
241 	zend_uchar bits[1];
242 	enum_func_status ret = FAIL;
243 
244 	DBG_ENTER("mysqlnd_command::refresh");
245 	int1store(bits, options);
246 
247 	ret = send_command(conn->payload_decoder_factory, COM_REFRESH, bits, 1, FALSE,
248 					   &conn->state,
249 					   conn->error_info,
250 					   conn->upsert_status,
251 					   conn->stats,
252 					   conn->m->send_close,
253 					   conn);
254 	if (PASS == ret) {
255 		ret = send_command_handle_response(conn->payload_decoder_factory, PROT_OK_PACKET, FALSE, COM_REFRESH, TRUE,
256 										   conn->error_info, conn->upsert_status, &conn->last_message);
257 	}
258 
259 	DBG_RETURN(ret);
260 }
261 /* }}} */
262 
263 
264 /* {{{ mysqlnd_command::shutdown */
265 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,shutdown)266 MYSQLND_METHOD(mysqlnd_command, shutdown)(MYSQLND_CONN_DATA * const conn, const uint8_t level)
267 {
268 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
269 	const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
270 	zend_uchar bits[1];
271 	enum_func_status ret = FAIL;
272 
273 	DBG_ENTER("mysqlnd_command::shutdown");
274 	int1store(bits, level);
275 
276 	ret = send_command(conn->payload_decoder_factory, COM_SHUTDOWN, bits, 1, FALSE,
277 					   &conn->state,
278 					   conn->error_info,
279 					   conn->upsert_status,
280 					   conn->stats,
281 					   conn->m->send_close,
282 					   conn);
283 	if (PASS == ret) {
284 		ret = send_command_handle_response(conn->payload_decoder_factory, PROT_OK_PACKET, FALSE, COM_SHUTDOWN, TRUE,
285 										   conn->error_info, conn->upsert_status, &conn->last_message);
286 	}
287 
288 	DBG_RETURN(ret);
289 }
290 /* }}} */
291 
292 
293 /* {{{ mysqlnd_command::quit */
294 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,quit)295 MYSQLND_METHOD(mysqlnd_command, quit)(MYSQLND_CONN_DATA * const conn)
296 {
297 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
298 	enum_func_status ret = FAIL;
299 
300 	DBG_ENTER("mysqlnd_command::quit");
301 
302 	ret = send_command(conn->payload_decoder_factory, COM_QUIT, NULL, 0, TRUE,
303 					   &conn->state,
304 					   conn->error_info,
305 					   conn->upsert_status,
306 					   conn->stats,
307 					   conn->m->send_close,
308 					   conn);
309 
310 	DBG_RETURN(ret);
311 }
312 /* }}} */
313 
314 
315 /* {{{ mysqlnd_command::query */
316 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,query)317 MYSQLND_METHOD(mysqlnd_command, query)(MYSQLND_CONN_DATA * const conn, MYSQLND_CSTRING query)
318 {
319 	func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
320 	enum_func_status ret = FAIL;
321 
322 	DBG_ENTER("mysqlnd_command::query");
323 
324 	ret = send_command(conn->payload_decoder_factory, COM_QUERY, (const zend_uchar*) query.s, query.l, FALSE,
325 					   &conn->state,
326 					   conn->error_info,
327 					   conn->upsert_status,
328 					   conn->stats,
329 					   conn->m->send_close,
330 					   conn);
331 
332 	if (PASS == ret) {
333 		SET_CONNECTION_STATE(&conn->state, CONN_QUERY_SENT);
334 	}
335 
336 	DBG_RETURN(ret);
337 }
338 /* }}} */
339 
340 
341 /* {{{ mysqlnd_command::change_user */
342 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,change_user)343 MYSQLND_METHOD(mysqlnd_command, change_user)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING payload, const zend_bool silent)
344 {
345 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
346 	enum_func_status ret = FAIL;
347 
348 	DBG_ENTER("mysqlnd_command::change_user");
349 
350 	ret = send_command(conn->payload_decoder_factory, COM_CHANGE_USER, (const zend_uchar*) payload.s, payload.l, silent,
351 					   &conn->state,
352 					   conn->error_info,
353 					   conn->upsert_status,
354 					   conn->stats,
355 					   conn->m->send_close,
356 					   conn);
357 
358 	DBG_RETURN(ret);
359 }
360 /* }}} */
361 
362 
363 /* {{{ mysqlnd_command::reap_result */
364 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,reap_result)365 MYSQLND_METHOD(mysqlnd_command, reap_result)(MYSQLND_CONN_DATA * const conn)
366 {
367 	const enum_mysqlnd_connection_state state = GET_CONNECTION_STATE(&conn->state);
368 	enum_func_status ret = FAIL;
369 
370 	DBG_ENTER("mysqlnd_command::reap_result");
371 	if (state <= CONN_READY || state == CONN_QUIT_SENT) {
372 		php_error_docref(NULL, E_WARNING, "Connection not opened, clear or has been closed");
373 		DBG_ERR_FMT("Connection not opened, clear or has been closed. State=%u", state);
374 		DBG_RETURN(ret);
375 	}
376 	ret = conn->m->query_read_result_set_header(conn, NULL);
377 
378 	DBG_RETURN(ret);
379 }
380 /* }}} */
381 
382 
383 /* {{{ mysqlnd_command::stmt_prepare */
384 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_prepare)385 MYSQLND_METHOD(mysqlnd_command, stmt_prepare)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING query)
386 {
387 	func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
388 	enum_func_status ret = FAIL;
389 
390 	DBG_ENTER("mysqlnd_command::stmt_prepare");
391 
392 	ret = send_command(conn->payload_decoder_factory, COM_STMT_PREPARE, (const zend_uchar*) query.s, query.l, FALSE,
393 					   &conn->state,
394 					   conn->error_info,
395 					   conn->upsert_status,
396 					   conn->stats,
397 					   conn->m->send_close,
398 					   conn);
399 
400 	DBG_RETURN(ret);
401 }
402 /* }}} */
403 
404 
405 /* {{{ mysqlnd_command::stmt_execute */
406 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_execute)407 MYSQLND_METHOD(mysqlnd_command, stmt_execute)(MYSQLND_CONN_DATA * conn, const MYSQLND_CSTRING payload)
408 {
409 	func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
410 	enum_func_status ret = FAIL;
411 
412 	DBG_ENTER("mysqlnd_command::stmt_execute");
413 
414 	ret = send_command(conn->payload_decoder_factory, COM_STMT_EXECUTE,
415 					   (const unsigned char *) payload.s, payload.l, FALSE,
416 					   &conn->state,
417 					   conn->error_info,
418 					   conn->upsert_status,
419 					   conn->stats,
420 					   conn->m->send_close,
421 					   conn);
422 
423 	DBG_RETURN(ret);
424 }
425 /* }}} */
426 
427 
428 /* {{{ mysqlnd_command::stmt_fetch */
429 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_fetch)430 MYSQLND_METHOD(mysqlnd_command, stmt_fetch)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING payload)
431 {
432 	func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
433 	enum_func_status ret = FAIL;
434 
435 	DBG_ENTER("mysqlnd_command::stmt_fetch");
436 
437 	ret = send_command(conn->payload_decoder_factory, COM_STMT_FETCH, (const zend_uchar*) payload.s, payload.l, FALSE,
438 					   &conn->state,
439 					   conn->error_info,
440 					   conn->upsert_status,
441 					   conn->stats,
442 					   conn->m->send_close,
443 					   conn);
444 
445 	DBG_RETURN(ret);
446 }
447 /* }}} */
448 
449 
450 /* {{{ mysqlnd_command::stmt_reset */
451 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_reset)452 MYSQLND_METHOD(mysqlnd_command, stmt_reset)(MYSQLND_CONN_DATA * const conn, const zend_ulong stmt_id)
453 {
454 	const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
455 	const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
456 	zend_uchar cmd_buf[MYSQLND_STMT_ID_LENGTH /* statement id */];
457 	enum_func_status ret = FAIL;
458 
459 	DBG_ENTER("mysqlnd_command::stmt_reset");
460 
461 	int4store(cmd_buf, stmt_id);
462 	ret = send_command(conn->payload_decoder_factory, COM_STMT_RESET, cmd_buf, sizeof(cmd_buf), FALSE,
463 					   &conn->state,
464 					   conn->error_info,
465 					   conn->upsert_status,
466 					   conn->stats,
467 					   conn->m->send_close,
468 					   conn);
469 	if (PASS == ret) {
470 		ret = send_command_handle_response(conn->payload_decoder_factory, PROT_OK_PACKET, FALSE, COM_STMT_RESET, TRUE,
471 										   conn->error_info, conn->upsert_status, &conn->last_message);
472 	}
473 
474 	DBG_RETURN(ret);
475 }
476 /* }}} */
477 
478 
479 /* {{{ mysqlnd_command::stmt_send_long_data */
480 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_send_long_data)481 MYSQLND_METHOD(mysqlnd_command, stmt_send_long_data)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING payload)
482 {
483 	func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
484 	enum_func_status ret = FAIL;
485 
486 	DBG_ENTER("mysqlnd_command::stmt_send_long_data");
487 
488 	ret = send_command(conn->payload_decoder_factory, COM_STMT_SEND_LONG_DATA, (const zend_uchar*) payload.s, payload.l, FALSE,
489 					   &conn->state,
490 					   conn->error_info,
491 					   conn->upsert_status,
492 					   conn->stats,
493 					   conn->m->send_close,
494 					   conn);
495 	/* COM_STMT_SEND_LONG_DATA - doesn't read result, the server doesn't send ACK */
496 	DBG_RETURN(ret);
497 }
498 /* }}} */
499 
500 
501 /* {{{ mysqlnd_command::stmt_close */
502 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_close)503 MYSQLND_METHOD(mysqlnd_command, stmt_close)(MYSQLND_CONN_DATA * const conn, const zend_ulong stmt_id)
504 {
505 	func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
506 	zend_uchar cmd_buf[MYSQLND_STMT_ID_LENGTH /* statement id */];
507 	enum_func_status ret = FAIL;
508 
509 	DBG_ENTER("mysqlnd_command::stmt_close");
510 
511 	int4store(cmd_buf, stmt_id);
512 	ret = send_command(conn->payload_decoder_factory, COM_STMT_CLOSE, cmd_buf, sizeof(cmd_buf), FALSE,
513 					   &conn->state,
514 					   conn->error_info,
515 					   conn->upsert_status,
516 					   conn->stats,
517 					   conn->m->send_close,
518 					   conn);
519 
520 	DBG_RETURN(ret);
521 }
522 /* }}} */
523 
524 
525 /* {{{ mysqlnd_command::enable_ssl */
526 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,enable_ssl)527 MYSQLND_METHOD(mysqlnd_command, enable_ssl)(MYSQLND_CONN_DATA * const conn, const size_t client_capabilities, const size_t server_capabilities, const unsigned int charset_no)
528 {
529 	enum_func_status ret = FAIL;
530 	MYSQLND_PACKET_AUTH auth_packet;
531 
532 	DBG_ENTER("mysqlnd_command::enable_ssl");
533 
534 	DBG_INF_FMT("client_capability_flags=%lu", client_capabilities);
535 	DBG_INF_FMT("CLIENT_LONG_PASSWORD=	%d", client_capabilities & CLIENT_LONG_PASSWORD? 1:0);
536 	DBG_INF_FMT("CLIENT_FOUND_ROWS=		%d", client_capabilities & CLIENT_FOUND_ROWS? 1:0);
537 	DBG_INF_FMT("CLIENT_LONG_FLAG=		%d", client_capabilities & CLIENT_LONG_FLAG? 1:0);
538 	DBG_INF_FMT("CLIENT_NO_SCHEMA=		%d", client_capabilities & CLIENT_NO_SCHEMA? 1:0);
539 	DBG_INF_FMT("CLIENT_COMPRESS=		%d", client_capabilities & CLIENT_COMPRESS? 1:0);
540 	DBG_INF_FMT("CLIENT_ODBC=			%d", client_capabilities & CLIENT_ODBC? 1:0);
541 	DBG_INF_FMT("CLIENT_LOCAL_FILES=	%d", client_capabilities & CLIENT_LOCAL_FILES? 1:0);
542 	DBG_INF_FMT("CLIENT_IGNORE_SPACE=	%d", client_capabilities & CLIENT_IGNORE_SPACE? 1:0);
543 	DBG_INF_FMT("CLIENT_PROTOCOL_41=	%d", client_capabilities & CLIENT_PROTOCOL_41? 1:0);
544 	DBG_INF_FMT("CLIENT_INTERACTIVE=	%d", client_capabilities & CLIENT_INTERACTIVE? 1:0);
545 	DBG_INF_FMT("CLIENT_SSL=			%d", client_capabilities & CLIENT_SSL? 1:0);
546 	DBG_INF_FMT("CLIENT_IGNORE_SIGPIPE=	%d", client_capabilities & CLIENT_IGNORE_SIGPIPE? 1:0);
547 	DBG_INF_FMT("CLIENT_TRANSACTIONS=	%d", client_capabilities & CLIENT_TRANSACTIONS? 1:0);
548 	DBG_INF_FMT("CLIENT_RESERVED=		%d", client_capabilities & CLIENT_RESERVED? 1:0);
549 	DBG_INF_FMT("CLIENT_SECURE_CONNECTION=%d", client_capabilities & CLIENT_SECURE_CONNECTION? 1:0);
550 	DBG_INF_FMT("CLIENT_MULTI_STATEMENTS=%d", client_capabilities & CLIENT_MULTI_STATEMENTS? 1:0);
551 	DBG_INF_FMT("CLIENT_MULTI_RESULTS=	%d", client_capabilities & CLIENT_MULTI_RESULTS? 1:0);
552 	DBG_INF_FMT("CLIENT_PS_MULTI_RESULTS=%d", client_capabilities & CLIENT_PS_MULTI_RESULTS? 1:0);
553 	DBG_INF_FMT("CLIENT_CONNECT_ATTRS=	%d", client_capabilities & CLIENT_PLUGIN_AUTH? 1:0);
554 	DBG_INF_FMT("CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA=	%d", client_capabilities & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA? 1:0);
555 	DBG_INF_FMT("CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS=	%d", client_capabilities & CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS? 1:0);
556 	DBG_INF_FMT("CLIENT_SESSION_TRACK=		%d", client_capabilities & CLIENT_SESSION_TRACK? 1:0);
557 	DBG_INF_FMT("CLIENT_SSL_VERIFY_SERVER_CERT=	%d", client_capabilities & CLIENT_SSL_VERIFY_SERVER_CERT? 1:0);
558 	DBG_INF_FMT("CLIENT_REMEMBER_OPTIONS=		%d", client_capabilities & CLIENT_REMEMBER_OPTIONS? 1:0);
559 
560 	conn->payload_decoder_factory->m.init_auth_packet(&auth_packet);
561 	auth_packet.client_flags = client_capabilities;
562 	auth_packet.max_packet_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
563 
564 	auth_packet.charset_no = charset_no;
565 
566 #ifdef MYSQLND_SSL_SUPPORTED
567 	if (client_capabilities & CLIENT_SSL) {
568 		const zend_bool server_has_ssl = (server_capabilities & CLIENT_SSL)? TRUE:FALSE;
569 		if (server_has_ssl == FALSE) {
570 			goto close_conn;
571 		} else {
572 			enum mysqlnd_ssl_peer verify = client_capabilities & CLIENT_SSL_VERIFY_SERVER_CERT?
573 												MYSQLND_SSL_PEER_VERIFY:
574 												(client_capabilities & CLIENT_SSL_DONT_VERIFY_SERVER_CERT?
575 													MYSQLND_SSL_PEER_DONT_VERIFY:
576 													MYSQLND_SSL_PEER_DEFAULT);
577 			DBG_INF("Switching to SSL");
578 			if (!PACKET_WRITE(conn, &auth_packet)) {
579 				goto close_conn;
580 			}
581 
582 			conn->vio->data->m.set_client_option(conn->vio, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char *) &verify);
583 
584 			if (FAIL == conn->vio->data->m.enable_ssl(conn->vio)) {
585 				goto end;
586 			}
587 		}
588 	}
589 #else
590 	auth_packet.client_flags &= ~CLIENT_SSL;
591 	if (!PACKET_WRITE(conn, &auth_packet)) {
592 		goto close_conn;
593 	}
594 #endif
595 	ret = PASS;
596 end:
597 	PACKET_FREE(&auth_packet);
598 	DBG_RETURN(ret);
599 
600 close_conn:
601 	SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
602 	conn->m->send_close(conn);
603 	SET_CLIENT_ERROR(conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
604 	PACKET_FREE(&auth_packet);
605 	DBG_RETURN(ret);
606 }
607 /* }}} */
608 
609 
610 /* {{{ mysqlnd_command::handshake */
611 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,handshake)612 MYSQLND_METHOD(mysqlnd_command, handshake)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING username, const MYSQLND_CSTRING password, const MYSQLND_CSTRING database, const size_t client_flags)
613 {
614 	const char * const user = username.s;
615 
616 	const char * const passwd = password.s;
617 	const size_t passwd_len = password.l;
618 
619 	const char * const db = database.s;
620 	const size_t db_len = database.l;
621 
622 	const size_t mysql_flags = client_flags;
623 
624 	MYSQLND_PACKET_GREET greet_packet;
625 
626 	DBG_ENTER("mysqlnd_command::handshake");
627 
628 	DBG_INF_FMT("stream=%p", conn->vio->data->m.get_stream(conn->vio));
629 	DBG_INF_FMT("[user=%s] [db=%s:%d] [flags=%llu]", user, db, db_len, mysql_flags);
630 
631 	conn->payload_decoder_factory->m.init_greet_packet(&greet_packet);
632 
633 	if (FAIL == PACKET_READ(conn, &greet_packet)) {
634 		DBG_ERR("Error while reading greeting packet");
635 		php_error_docref(NULL, E_WARNING, "Error while reading greeting packet. PID=%d", getpid());
636 		goto err;
637 	} else if (greet_packet.error_no) {
638 		DBG_ERR_FMT("errorno=%u error=%s", greet_packet.error_no, greet_packet.error);
639 		SET_CLIENT_ERROR(conn->error_info, greet_packet.error_no, greet_packet.sqlstate, greet_packet.error);
640 		goto err;
641 	} else if (greet_packet.pre41) {
642 		DBG_ERR_FMT("Connecting to 3.22, 3.23 & 4.0 is not supported. Server is %-.32s", greet_packet.server_version);
643 		php_error_docref(NULL, E_WARNING, "Connecting to 3.22, 3.23 & 4.0 "
644 						" is not supported. Server is %-.32s", greet_packet.server_version);
645 		SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
646 						 "Connecting to 3.22, 3.23 & 4.0 servers is not supported");
647 		goto err;
648 	}
649 
650 	conn->thread_id			= greet_packet.thread_id;
651 	conn->protocol_version	= greet_packet.protocol_version;
652 	conn->server_version	= mnd_pestrdup(greet_packet.server_version, conn->persistent);
653 
654 	conn->greet_charset = mysqlnd_find_charset_nr(greet_packet.charset_no);
655 	if (!conn->greet_charset) {
656 		php_error_docref(NULL, E_WARNING,
657 			"Server sent charset (%d) unknown to the client. Please, report to the developers", greet_packet.charset_no);
658 		SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
659 			"Server sent charset unknown to the client. Please, report to the developers");
660 		goto err;
661 	}
662 
663 	conn->server_capabilities 	= greet_packet.server_capabilities;
664 
665 	if (FAIL == mysqlnd_connect_run_authentication(conn, user, passwd, db, db_len, (size_t) passwd_len,
666 												   greet_packet.authentication_plugin_data, greet_packet.auth_protocol,
667 												   greet_packet.charset_no, greet_packet.server_capabilities,
668 												   conn->options, mysql_flags))
669 	{
670 		goto err;
671 	}
672 
673 	UPSERT_STATUS_RESET(conn->upsert_status);
674 	UPSERT_STATUS_SET_SERVER_STATUS(conn->upsert_status, greet_packet.server_status);
675 
676 	PACKET_FREE(&greet_packet);
677 	DBG_RETURN(PASS);
678 err:
679 	conn->server_capabilities = 0;
680 	PACKET_FREE(&greet_packet);
681 	DBG_RETURN(FAIL);
682 }
683 /* }}} */
684 
685 
686 MYSQLND_CLASS_METHODS_START(mysqlnd_command)
687 	MYSQLND_METHOD(mysqlnd_command, set_option),
688 	MYSQLND_METHOD(mysqlnd_command, debug),
689 	MYSQLND_METHOD(mysqlnd_command, init_db),
690 	MYSQLND_METHOD(mysqlnd_command, ping),
691 	MYSQLND_METHOD(mysqlnd_command, statistics),
692 	MYSQLND_METHOD(mysqlnd_command, process_kill),
693 	MYSQLND_METHOD(mysqlnd_command, refresh),
694 	MYSQLND_METHOD(mysqlnd_command, shutdown),
695 	MYSQLND_METHOD(mysqlnd_command, quit),
696 	MYSQLND_METHOD(mysqlnd_command, query),
697 	MYSQLND_METHOD(mysqlnd_command, change_user),
698 	MYSQLND_METHOD(mysqlnd_command, reap_result),
699 	MYSQLND_METHOD(mysqlnd_command, stmt_prepare),
700 	MYSQLND_METHOD(mysqlnd_command, stmt_execute),
701 	MYSQLND_METHOD(mysqlnd_command, stmt_fetch),
702 	MYSQLND_METHOD(mysqlnd_command, stmt_reset),
703 	MYSQLND_METHOD(mysqlnd_command, stmt_send_long_data),
704 	MYSQLND_METHOD(mysqlnd_command, stmt_close),
705 	MYSQLND_METHOD(mysqlnd_command, enable_ssl),
706 	MYSQLND_METHOD(mysqlnd_command, handshake),
707 MYSQLND_CLASS_METHODS_END;
708