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