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