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::shutdown */
255 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,shutdown)256 MYSQLND_METHOD(mysqlnd_command, shutdown)(MYSQLND_CONN_DATA * const conn, const uint8_t level)
257 {
258 const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
259 const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
260 zend_uchar bits[1];
261 enum_func_status ret = FAIL;
262
263 DBG_ENTER("mysqlnd_command::shutdown");
264 int1store(bits, level);
265
266 ret = send_command(conn->payload_decoder_factory, COM_SHUTDOWN, bits, 1, FALSE,
267 &conn->state,
268 conn->error_info,
269 conn->upsert_status,
270 conn->stats,
271 conn->m->send_close,
272 conn);
273 if (PASS == ret) {
274 ret = send_command_handle_response(conn->payload_decoder_factory, PROT_OK_PACKET, FALSE, COM_SHUTDOWN, TRUE,
275 conn->error_info, conn->upsert_status, &conn->last_message);
276 }
277
278 DBG_RETURN(ret);
279 }
280 /* }}} */
281
282
283 /* {{{ mysqlnd_command::quit */
284 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,quit)285 MYSQLND_METHOD(mysqlnd_command, quit)(MYSQLND_CONN_DATA * const conn)
286 {
287 const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
288 enum_func_status ret = FAIL;
289
290 DBG_ENTER("mysqlnd_command::quit");
291
292 ret = send_command(conn->payload_decoder_factory, COM_QUIT, NULL, 0, TRUE,
293 &conn->state,
294 conn->error_info,
295 conn->upsert_status,
296 conn->stats,
297 conn->m->send_close,
298 conn);
299
300 DBG_RETURN(ret);
301 }
302 /* }}} */
303
304
305 /* {{{ mysqlnd_command::query */
306 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,query)307 MYSQLND_METHOD(mysqlnd_command, query)(MYSQLND_CONN_DATA * const conn, MYSQLND_CSTRING query)
308 {
309 func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
310 enum_func_status ret = FAIL;
311
312 DBG_ENTER("mysqlnd_command::query");
313
314 ret = send_command(conn->payload_decoder_factory, COM_QUERY, (const zend_uchar*) query.s, query.l, FALSE,
315 &conn->state,
316 conn->error_info,
317 conn->upsert_status,
318 conn->stats,
319 conn->m->send_close,
320 conn);
321
322 if (PASS == ret) {
323 SET_CONNECTION_STATE(&conn->state, CONN_QUERY_SENT);
324 }
325
326 DBG_RETURN(ret);
327 }
328 /* }}} */
329
330
331 /* {{{ mysqlnd_command::change_user */
332 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,change_user)333 MYSQLND_METHOD(mysqlnd_command, change_user)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING payload, const bool silent)
334 {
335 const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
336 enum_func_status ret = FAIL;
337
338 DBG_ENTER("mysqlnd_command::change_user");
339
340 ret = send_command(conn->payload_decoder_factory, COM_CHANGE_USER, (const zend_uchar*) payload.s, payload.l, silent,
341 &conn->state,
342 conn->error_info,
343 conn->upsert_status,
344 conn->stats,
345 conn->m->send_close,
346 conn);
347
348 DBG_RETURN(ret);
349 }
350 /* }}} */
351
352
353 /* {{{ mysqlnd_command::reap_result */
354 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,reap_result)355 MYSQLND_METHOD(mysqlnd_command, reap_result)(MYSQLND_CONN_DATA * const conn)
356 {
357 const enum_mysqlnd_connection_state state = GET_CONNECTION_STATE(&conn->state);
358 enum_func_status ret = FAIL;
359
360 DBG_ENTER("mysqlnd_command::reap_result");
361 if (state <= CONN_READY || state == CONN_QUIT_SENT) {
362 php_error_docref(NULL, E_WARNING, "Connection not opened, clear or has been closed");
363 DBG_ERR_FMT("Connection not opened, clear or has been closed. State=%u", state);
364 DBG_RETURN(ret);
365 }
366 ret = conn->m->query_read_result_set_header(conn, NULL);
367
368 DBG_RETURN(ret);
369 }
370 /* }}} */
371
372
373 /* {{{ mysqlnd_command::stmt_prepare */
374 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_prepare)375 MYSQLND_METHOD(mysqlnd_command, stmt_prepare)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING query)
376 {
377 func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
378 enum_func_status ret = FAIL;
379
380 DBG_ENTER("mysqlnd_command::stmt_prepare");
381
382 ret = send_command(conn->payload_decoder_factory, COM_STMT_PREPARE, (const zend_uchar*) query.s, query.l, FALSE,
383 &conn->state,
384 conn->error_info,
385 conn->upsert_status,
386 conn->stats,
387 conn->m->send_close,
388 conn);
389
390 DBG_RETURN(ret);
391 }
392 /* }}} */
393
394
395 /* {{{ mysqlnd_command::stmt_execute */
396 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_execute)397 MYSQLND_METHOD(mysqlnd_command, stmt_execute)(MYSQLND_CONN_DATA * conn, const MYSQLND_CSTRING payload)
398 {
399 func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
400 enum_func_status ret = FAIL;
401
402 DBG_ENTER("mysqlnd_command::stmt_execute");
403
404 ret = send_command(conn->payload_decoder_factory, COM_STMT_EXECUTE,
405 (const unsigned char *) payload.s, payload.l, FALSE,
406 &conn->state,
407 conn->error_info,
408 conn->upsert_status,
409 conn->stats,
410 conn->m->send_close,
411 conn);
412
413 DBG_RETURN(ret);
414 }
415 /* }}} */
416
417
418 /* {{{ mysqlnd_command::stmt_fetch */
419 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_fetch)420 MYSQLND_METHOD(mysqlnd_command, stmt_fetch)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING payload)
421 {
422 func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
423 enum_func_status ret = FAIL;
424
425 DBG_ENTER("mysqlnd_command::stmt_fetch");
426
427 ret = send_command(conn->payload_decoder_factory, COM_STMT_FETCH, (const zend_uchar*) payload.s, payload.l, FALSE,
428 &conn->state,
429 conn->error_info,
430 conn->upsert_status,
431 conn->stats,
432 conn->m->send_close,
433 conn);
434
435 DBG_RETURN(ret);
436 }
437 /* }}} */
438
439
440 /* {{{ mysqlnd_command::stmt_reset */
441 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_reset)442 MYSQLND_METHOD(mysqlnd_command, stmt_reset)(MYSQLND_CONN_DATA * const conn, const zend_ulong stmt_id)
443 {
444 const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
445 const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
446 zend_uchar cmd_buf[MYSQLND_STMT_ID_LENGTH /* statement id */];
447 enum_func_status ret = FAIL;
448
449 DBG_ENTER("mysqlnd_command::stmt_reset");
450
451 int4store(cmd_buf, stmt_id);
452 ret = send_command(conn->payload_decoder_factory, COM_STMT_RESET, cmd_buf, sizeof(cmd_buf), FALSE,
453 &conn->state,
454 conn->error_info,
455 conn->upsert_status,
456 conn->stats,
457 conn->m->send_close,
458 conn);
459 if (PASS == ret) {
460 ret = send_command_handle_response(conn->payload_decoder_factory, PROT_OK_PACKET, FALSE, COM_STMT_RESET, TRUE,
461 conn->error_info, conn->upsert_status, &conn->last_message);
462 }
463
464 DBG_RETURN(ret);
465 }
466 /* }}} */
467
468
469 /* {{{ mysqlnd_command::stmt_send_long_data */
470 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_send_long_data)471 MYSQLND_METHOD(mysqlnd_command, stmt_send_long_data)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING payload)
472 {
473 func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
474 enum_func_status ret = FAIL;
475
476 DBG_ENTER("mysqlnd_command::stmt_send_long_data");
477
478 ret = send_command(conn->payload_decoder_factory, COM_STMT_SEND_LONG_DATA, (const zend_uchar*) payload.s, payload.l, FALSE,
479 &conn->state,
480 conn->error_info,
481 conn->upsert_status,
482 conn->stats,
483 conn->m->send_close,
484 conn);
485 /* COM_STMT_SEND_LONG_DATA - doesn't read result, the server doesn't send ACK */
486 DBG_RETURN(ret);
487 }
488 /* }}} */
489
490
491 /* {{{ mysqlnd_command::stmt_close */
492 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,stmt_close)493 MYSQLND_METHOD(mysqlnd_command, stmt_close)(MYSQLND_CONN_DATA * const conn, const zend_ulong stmt_id)
494 {
495 func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
496 zend_uchar cmd_buf[MYSQLND_STMT_ID_LENGTH /* statement id */];
497 enum_func_status ret = FAIL;
498
499 DBG_ENTER("mysqlnd_command::stmt_close");
500
501 int4store(cmd_buf, stmt_id);
502 ret = send_command(conn->payload_decoder_factory, COM_STMT_CLOSE, cmd_buf, sizeof(cmd_buf), FALSE,
503 &conn->state,
504 conn->error_info,
505 conn->upsert_status,
506 conn->stats,
507 conn->m->send_close,
508 conn);
509
510 DBG_RETURN(ret);
511 }
512 /* }}} */
513
514
515 /* {{{ mysqlnd_command::enable_ssl */
516 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,enable_ssl)517 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)
518 {
519 enum_func_status ret = FAIL;
520 MYSQLND_PACKET_AUTH auth_packet;
521
522 DBG_ENTER("mysqlnd_command::enable_ssl");
523
524 DBG_INF_FMT("client_capability_flags=%zu", client_capabilities);
525 DBG_INF_FMT("CLIENT_LONG_PASSWORD= %d", client_capabilities & CLIENT_LONG_PASSWORD? 1:0);
526 DBG_INF_FMT("CLIENT_FOUND_ROWS= %d", client_capabilities & CLIENT_FOUND_ROWS? 1:0);
527 DBG_INF_FMT("CLIENT_LONG_FLAG= %d", client_capabilities & CLIENT_LONG_FLAG? 1:0);
528 DBG_INF_FMT("CLIENT_NO_SCHEMA= %d", client_capabilities & CLIENT_NO_SCHEMA? 1:0);
529 DBG_INF_FMT("CLIENT_COMPRESS= %d", client_capabilities & CLIENT_COMPRESS? 1:0);
530 DBG_INF_FMT("CLIENT_ODBC= %d", client_capabilities & CLIENT_ODBC? 1:0);
531 DBG_INF_FMT("CLIENT_LOCAL_FILES= %d", client_capabilities & CLIENT_LOCAL_FILES? 1:0);
532 DBG_INF_FMT("CLIENT_IGNORE_SPACE= %d", client_capabilities & CLIENT_IGNORE_SPACE? 1:0);
533 DBG_INF_FMT("CLIENT_PROTOCOL_41= %d", client_capabilities & CLIENT_PROTOCOL_41? 1:0);
534 DBG_INF_FMT("CLIENT_INTERACTIVE= %d", client_capabilities & CLIENT_INTERACTIVE? 1:0);
535 DBG_INF_FMT("CLIENT_SSL= %d", client_capabilities & CLIENT_SSL? 1:0);
536 DBG_INF_FMT("CLIENT_IGNORE_SIGPIPE= %d", client_capabilities & CLIENT_IGNORE_SIGPIPE? 1:0);
537 DBG_INF_FMT("CLIENT_TRANSACTIONS= %d", client_capabilities & CLIENT_TRANSACTIONS? 1:0);
538 DBG_INF_FMT("CLIENT_RESERVED= %d", client_capabilities & CLIENT_RESERVED? 1:0);
539 DBG_INF_FMT("CLIENT_SECURE_CONNECTION=%d", client_capabilities & CLIENT_SECURE_CONNECTION? 1:0);
540 DBG_INF_FMT("CLIENT_MULTI_STATEMENTS=%d", client_capabilities & CLIENT_MULTI_STATEMENTS? 1:0);
541 DBG_INF_FMT("CLIENT_MULTI_RESULTS= %d", client_capabilities & CLIENT_MULTI_RESULTS? 1:0);
542 DBG_INF_FMT("CLIENT_PS_MULTI_RESULTS=%d", client_capabilities & CLIENT_PS_MULTI_RESULTS? 1:0);
543 DBG_INF_FMT("CLIENT_CONNECT_ATTRS= %d", client_capabilities & CLIENT_PLUGIN_AUTH? 1:0);
544 DBG_INF_FMT("CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA= %d", client_capabilities & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA? 1:0);
545 DBG_INF_FMT("CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS= %d", client_capabilities & CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS? 1:0);
546 DBG_INF_FMT("CLIENT_SESSION_TRACK= %d", client_capabilities & CLIENT_SESSION_TRACK? 1:0);
547 DBG_INF_FMT("CLIENT_SSL_VERIFY_SERVER_CERT= %d", client_capabilities & CLIENT_SSL_VERIFY_SERVER_CERT? 1:0);
548 DBG_INF_FMT("CLIENT_REMEMBER_OPTIONS= %d", client_capabilities & CLIENT_REMEMBER_OPTIONS? 1:0);
549
550 conn->payload_decoder_factory->m.init_auth_packet(&auth_packet);
551 auth_packet.client_flags = client_capabilities;
552 auth_packet.max_packet_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
553
554 auth_packet.charset_no = charset_no;
555
556 #ifdef MYSQLND_SSL_SUPPORTED
557 if (client_capabilities & CLIENT_SSL) {
558 const bool server_has_ssl = (server_capabilities & CLIENT_SSL)? TRUE:FALSE;
559 if (server_has_ssl == FALSE) {
560 goto close_conn;
561 } else {
562 enum mysqlnd_ssl_peer verify = client_capabilities & CLIENT_SSL_VERIFY_SERVER_CERT?
563 MYSQLND_SSL_PEER_VERIFY:
564 (client_capabilities & CLIENT_SSL_DONT_VERIFY_SERVER_CERT?
565 MYSQLND_SSL_PEER_DONT_VERIFY:
566 MYSQLND_SSL_PEER_DEFAULT);
567 DBG_INF("Switching to SSL");
568 if (!PACKET_WRITE(conn, &auth_packet)) {
569 goto close_conn;
570 }
571
572 conn->vio->data->m.set_client_option(conn->vio, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char *) &verify);
573
574 if (FAIL == conn->vio->data->m.enable_ssl(conn->vio)) {
575 SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
576 SET_CLIENT_ERROR(conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, "Cannot connect to MySQL using SSL");
577 goto end;
578 }
579 }
580 }
581 #else
582 auth_packet.client_flags &= ~CLIENT_SSL;
583 if (!PACKET_WRITE(conn, &auth_packet)) {
584 goto close_conn;
585 }
586 #endif
587 ret = PASS;
588 end:
589 PACKET_FREE(&auth_packet);
590 DBG_RETURN(ret);
591
592 close_conn:
593 SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
594 conn->m->send_close(conn);
595 SET_CLIENT_ERROR(conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
596 PACKET_FREE(&auth_packet);
597 DBG_RETURN(ret);
598 }
599 /* }}} */
600
601
602 /* {{{ mysqlnd_command::handshake */
603 static enum_func_status
MYSQLND_METHOD(mysqlnd_command,handshake)604 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)
605 {
606 const char * const user = username.s;
607
608 const char * const passwd = password.s;
609 const size_t passwd_len = password.l;
610
611 const char * const db = database.s;
612 const size_t db_len = database.l;
613
614 const size_t mysql_flags = client_flags;
615
616 MYSQLND_PACKET_GREET greet_packet;
617
618 DBG_ENTER("mysqlnd_command::handshake");
619
620 DBG_INF_FMT("stream=%p", conn->vio->data->m.get_stream(conn->vio));
621 DBG_INF_FMT("[user=%s] [db=%s:%zu] [flags=%zu]", user, db, db_len, mysql_flags);
622
623 conn->payload_decoder_factory->m.init_greet_packet(&greet_packet);
624
625 if (FAIL == PACKET_READ(conn, &greet_packet)) {
626 DBG_ERR("Error while reading greeting packet");
627 php_error_docref(NULL, E_WARNING, "Error while reading greeting packet. PID=%d", getpid());
628 goto err;
629 } else if (greet_packet.error_no) {
630 DBG_ERR_FMT("errorno=%u error=%s", greet_packet.error_no, greet_packet.error);
631 SET_CLIENT_ERROR(conn->error_info, greet_packet.error_no, greet_packet.sqlstate, greet_packet.error);
632 goto err;
633 } else if (greet_packet.pre41) {
634 char * msg;
635 mnd_sprintf(&msg, 0, "Connecting to 3.22, 3.23 & 4.0 is not supported. Server is %-.32s", greet_packet.server_version);
636 DBG_ERR(msg);
637 SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, msg);
638 mnd_sprintf_free(msg);
639 goto err;
640 }
641
642 conn->thread_id = greet_packet.thread_id;
643 conn->protocol_version = greet_packet.protocol_version;
644 conn->server_version = mnd_pestrdup(greet_packet.server_version, conn->persistent);
645
646 const MYSQLND_CHARSET *read_charset = mysqlnd_find_charset_nr(greet_packet.charset_no);
647 if (!read_charset) {
648 greet_packet.charset_no = conn->m->get_server_version(conn) >= 50500 ? MYSQLND_UTF8_MB4_DEFAULT_ID : MYSQLND_UTF8_MB3_DEFAULT_ID;
649 conn->greet_charset = mysqlnd_find_charset_nr(greet_packet.charset_no);
650 } else {
651 conn->greet_charset = read_charset;
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