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