-
Notifications
You must be signed in to change notification settings - Fork 72
/
protocol.c
1212 lines (909 loc) · 39.8 KB
/
protocol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <openssl/evp.h>
#include "trilogy/builder.h"
#include "trilogy/error.h"
#include "trilogy/packet_parser.h"
#include "trilogy/protocol.h"
#include "trilogy/reader.h"
#define TRILOGY_CMD_QUIT 0x01
#define TRILOGY_CMD_CHANGE_DB 0x02
#define TRILOGY_CMD_QUERY 0x03
#define TRILOGY_CMD_PING 0x0e
#define TRILOGY_CMD_SET_OPTION 0x1b
#define TRILOGY_CMD_STMT_PREPARE 0x16
#define TRILOGY_CMD_STMT_EXECUTE 0x17
#define TRILOGY_CMD_STMT_SEND_LONG_DATA 0x18
#define TRILOGY_CMD_STMT_CLOSE 0x19
#define TRILOGY_CMD_STMT_RESET 0x1a
#define SCRAMBLE_LEN 20
static size_t min(size_t a, size_t b)
{
if (a < b) {
return a;
} else {
return b;
}
}
#define CHECKED(expr) \
if ((rc = (expr)) < 0) { \
goto fail; \
}
int trilogy_parse_ok_packet(const uint8_t *buff, size_t len, uint32_t capabilities, trilogy_ok_packet_t *out_packet)
{
int rc;
trilogy_reader_t reader = TRILOGY_READER(buff, len);
// skip packet type
CHECKED(trilogy_reader_get_uint8(&reader, NULL));
CHECKED(trilogy_reader_get_lenenc(&reader, &out_packet->affected_rows));
CHECKED(trilogy_reader_get_lenenc(&reader, &out_packet->last_insert_id));
out_packet->status_flags = 0;
out_packet->warning_count = 0;
out_packet->txn_status_flags = 0;
out_packet->session_status = NULL;
out_packet->session_status_len = 0;
out_packet->session_state_changes = NULL;
out_packet->session_state_changes_len = 0;
out_packet->info = NULL;
out_packet->info_len = 0;
out_packet->last_gtid_len = 0;
if (capabilities & TRILOGY_CAPABILITIES_PROTOCOL_41) {
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->status_flags));
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->warning_count));
} else if (capabilities & TRILOGY_CAPABILITIES_TRANSACTIONS) {
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->txn_status_flags));
}
if (capabilities & TRILOGY_CAPABILITIES_SESSION_TRACK && !trilogy_reader_eof(&reader)) {
CHECKED(trilogy_reader_get_lenenc_buffer(&reader, &out_packet->session_status_len,
(const void **)&out_packet->session_status));
if (out_packet->status_flags & TRILOGY_SERVER_STATUS_SESSION_STATE_CHANGED) {
CHECKED(trilogy_reader_get_lenenc_buffer(&reader, &out_packet->session_state_changes_len,
(const void **)&out_packet->session_state_changes));
TRILOGY_SESSION_TRACK_TYPE_t type = 0;
const char *state_info = NULL;
size_t state_info_len = 0;
trilogy_reader_t state_reader = TRILOGY_READER((const uint8_t *)out_packet->session_state_changes,
out_packet->session_state_changes_len);
while (!trilogy_reader_eof(&state_reader)) {
CHECKED(trilogy_reader_get_uint8(&state_reader, (uint8_t *)&type));
CHECKED(trilogy_reader_get_lenenc_buffer(&state_reader, &state_info_len, (const void **)&state_info));
switch (type) {
case TRILOGY_SESSION_TRACK_GTIDS: {
trilogy_reader_t gtid_reader = TRILOGY_READER((const uint8_t *)state_info, state_info_len);
// There's a type with value TRILOGY_SESSION_TRACK_GTIDS tag
// at the beginning here we can ignore since we already had
// the type one level higher as well.
CHECKED(trilogy_reader_get_uint8(>id_reader, NULL));
CHECKED(trilogy_reader_get_lenenc_buffer(>id_reader, &out_packet->last_gtid_len,
(const void **)&out_packet->last_gtid));
if (out_packet->last_gtid_len > TRILOGY_MAX_LAST_GTID_LEN) {
return TRILOGY_PROTOCOL_VIOLATION;
}
break;
}
default:
break;
}
}
}
} else {
CHECKED(trilogy_reader_get_eof_buffer(&reader, &out_packet->info_len, (const void **)&out_packet->info));
}
return trilogy_reader_finish(&reader);
fail:
return rc;
}
int trilogy_parse_eof_packet(const uint8_t *buff, size_t len, uint32_t capabilities, trilogy_eof_packet_t *out_packet)
{
int rc;
trilogy_reader_t reader = TRILOGY_READER(buff, len);
// skip packet type
CHECKED(trilogy_reader_get_uint8(&reader, NULL));
if (capabilities & TRILOGY_CAPABILITIES_PROTOCOL_41) {
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->warning_count));
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->status_flags));
} else {
out_packet->status_flags = 0;
out_packet->warning_count = 0;
}
return trilogy_reader_finish(&reader);
fail:
return rc;
}
int trilogy_parse_err_packet(const uint8_t *buff, size_t len, uint32_t capabilities, trilogy_err_packet_t *out_packet)
{
int rc;
trilogy_reader_t reader = TRILOGY_READER(buff, len);
// skip packet type
CHECKED(trilogy_reader_get_uint8(&reader, NULL));
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->error_code));
if (capabilities & TRILOGY_CAPABILITIES_PROTOCOL_41) {
CHECKED(trilogy_reader_get_uint8(&reader, out_packet->sql_state_marker));
CHECKED(trilogy_reader_copy_buffer(&reader, 5, out_packet->sql_state));
} else {
memset(out_packet->sql_state_marker, 0, sizeof out_packet->sql_state_marker);
memset(out_packet->sql_state, 0, sizeof out_packet->sql_state);
}
CHECKED(trilogy_reader_get_eof_buffer(&reader, &out_packet->error_message_len,
(const void **)&out_packet->error_message));
return trilogy_reader_finish(&reader);
fail:
return rc;
}
int trilogy_parse_auth_switch_request_packet(const uint8_t *buff, size_t len, uint32_t capabilities,
trilogy_auth_switch_request_packet_t *out_packet)
{
int rc;
trilogy_reader_t reader = TRILOGY_READER(buff, len);
// skip packet type
CHECKED(trilogy_reader_get_uint8(&reader, NULL));
if (capabilities & TRILOGY_CAPABILITIES_PLUGIN_AUTH) {
const char *auth_plugin;
size_t auth_plugin_len;
CHECKED(trilogy_reader_get_string(&reader, &auth_plugin, &auth_plugin_len));
if (auth_plugin_len > sizeof(out_packet->auth_plugin) - 1) {
return TRILOGY_AUTH_PLUGIN_TOO_LONG;
}
memcpy(out_packet->auth_plugin, auth_plugin, auth_plugin_len + 1);
const char *auth_data;
size_t auth_data_len;
CHECKED(trilogy_reader_get_eof_buffer(&reader, &auth_data_len, (const void **)&auth_data));
if (auth_data_len > 21) {
auth_data_len = 21;
}
memcpy(out_packet->scramble, auth_data, auth_data_len);
} else {
return TRILOGY_PROTOCOL_VIOLATION;
}
return trilogy_reader_finish(&reader);
fail:
return rc;
}
int trilogy_parse_handshake_packet(const uint8_t *buff, size_t len, trilogy_handshake_t *out_packet)
{
int rc;
trilogy_reader_t reader = TRILOGY_READER(buff, len);
CHECKED(trilogy_reader_get_uint8(&reader, &out_packet->proto_version));
if (out_packet->proto_version != 0xa) {
// incompatible protocol version
return TRILOGY_PROTOCOL_VIOLATION;
}
const char *server_version;
size_t server_version_len;
CHECKED(trilogy_reader_get_string(&reader, &server_version, &server_version_len));
server_version_len = min(server_version_len, sizeof(out_packet->server_version) - 1);
memcpy(out_packet->server_version, server_version, server_version_len);
out_packet->server_version[server_version_len] = '\0';
CHECKED(trilogy_reader_get_uint32(&reader, &out_packet->conn_id));
CHECKED(trilogy_reader_copy_buffer(&reader, 8, out_packet->scramble));
// this should be a NULL filler
uint8_t filler = 0;
CHECKED(trilogy_reader_get_uint8(&reader, &filler));
if (filler != '\0') {
// corrupt handshake packet
return TRILOGY_PROTOCOL_VIOLATION;
}
// lower two bytes of capabilities flags
uint16_t caps_part = 0;
CHECKED(trilogy_reader_get_uint16(&reader, &caps_part));
out_packet->capabilities = caps_part;
if (!(out_packet->capabilities & TRILOGY_CAPABILITIES_PROTOCOL_41)) {
// incompatible protocol version
return TRILOGY_PROTOCOL_VIOLATION;
}
uint8_t server_charset;
CHECKED(trilogy_reader_get_uint8(&reader, &server_charset));
out_packet->server_charset = server_charset;
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->server_status));
// upper 16 bits of capabilities flags
CHECKED(trilogy_reader_get_uint16(&reader, &caps_part));
out_packet->capabilities |= ((uint32_t)caps_part << 16);
uint8_t auth_data_len = 0;
CHECKED(trilogy_reader_get_uint8(&reader, &auth_data_len));
if (!(out_packet->capabilities & TRILOGY_CAPABILITIES_PLUGIN_AUTH)) {
// this should be a NULL filler
if (auth_data_len != '\0') {
// corrupt handshake packet
return TRILOGY_PROTOCOL_VIOLATION;
}
}
// This space is reserved. It should be all NULL bytes but some tools or
// future versions of MySQL-compatible clients may use it. This library
// opts to skip the validation as some servers don't respect the protocol.
CHECKED(trilogy_reader_get_buffer(&reader, 10, NULL));
if (out_packet->capabilities & TRILOGY_CAPABILITIES_SECURE_CONNECTION && auth_data_len > 8) {
uint8_t remaining_auth_data_len = auth_data_len - 8;
// The auth plugins we support all provide exactly 21 bytes of
// auth_data. Reject any other values for auth_data_len.
if (SCRAMBLE_LEN + 1 != auth_data_len) {
return TRILOGY_PROTOCOL_VIOLATION;
}
CHECKED(trilogy_reader_copy_buffer(&reader, remaining_auth_data_len, out_packet->scramble + 8));
} else {
// only support 4.1 protocol or newer with secure connection
return TRILOGY_PROTOCOL_VIOLATION;
}
if (out_packet->capabilities & TRILOGY_CAPABILITIES_PLUGIN_AUTH) {
const char *auth_plugin;
size_t auth_plugin_len;
CHECKED(trilogy_reader_get_string(&reader, &auth_plugin, &auth_plugin_len));
if (auth_plugin_len > sizeof(out_packet->auth_plugin) - 1) {
return TRILOGY_AUTH_PLUGIN_TOO_LONG;
}
memcpy(out_packet->auth_plugin, auth_plugin, auth_plugin_len + 1);
}
return trilogy_reader_finish(&reader);
fail:
return rc;
}
int trilogy_parse_result_packet(const uint8_t *buff, size_t len, trilogy_result_packet_t *out_packet)
{
int rc = 0;
trilogy_reader_t reader = TRILOGY_READER(buff, len);
CHECKED(trilogy_reader_get_lenenc(&reader, &out_packet->column_count));
return trilogy_reader_finish(&reader);
fail:
return rc;
}
int trilogy_parse_row_packet(const uint8_t *buff, size_t len, uint64_t column_count, trilogy_value_t *out_values)
{
trilogy_reader_t reader = TRILOGY_READER(buff, len);
for (uint64_t i = 0; i < column_count; i++) {
void *data = NULL;
size_t data_len = 0;
int rc = trilogy_reader_get_lenenc_buffer(&reader, &data_len, (const void **)&data);
switch (rc) {
case TRILOGY_OK:
out_values[i].is_null = false;
out_values[i].data = data;
out_values[i].data_len = data_len;
break;
case TRILOGY_NULL_VALUE:
out_values[i].is_null = true;
out_values[i].data_len = 0;
break;
default:
return rc;
}
}
return trilogy_reader_finish(&reader);
}
int trilogy_parse_column_packet(const uint8_t *buff, size_t len, bool field_list, trilogy_column_packet_t *out_packet)
{
int rc;
trilogy_reader_t reader = TRILOGY_READER(buff, len);
CHECKED(trilogy_reader_get_lenenc_buffer(&reader, &out_packet->catalog_len, (const void **)&out_packet->catalog));
CHECKED(trilogy_reader_get_lenenc_buffer(&reader, &out_packet->schema_len, (const void **)&out_packet->schema));
CHECKED(trilogy_reader_get_lenenc_buffer(&reader, &out_packet->table_len, (const void **)&out_packet->table));
CHECKED(trilogy_reader_get_lenenc_buffer(&reader, &out_packet->original_table_len,
(const void **)&out_packet->original_table));
CHECKED(trilogy_reader_get_lenenc_buffer(&reader, &out_packet->name_len, (const void **)&out_packet->name));
CHECKED(trilogy_reader_get_lenenc_buffer(&reader, &out_packet->original_name_len,
(const void **)&out_packet->original_name));
// skip length of fixed length field until we have something to use it for
CHECKED(trilogy_reader_get_lenenc(&reader, NULL));
uint16_t charset;
CHECKED(trilogy_reader_get_uint16(&reader, &charset));
out_packet->charset = charset;
CHECKED(trilogy_reader_get_uint32(&reader, &out_packet->len));
uint8_t type;
CHECKED(trilogy_reader_get_uint8(&reader, &type));
out_packet->type = type;
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->flags));
CHECKED(trilogy_reader_get_uint8(&reader, &out_packet->decimals));
// skip NULL filler
CHECKED(trilogy_reader_get_uint16(&reader, NULL));
out_packet->default_value_len = 0;
if (field_list) {
CHECKED(trilogy_reader_get_lenenc_buffer(&reader, &out_packet->default_value_len,
(const void **)&out_packet->default_value));
}
return trilogy_reader_finish(&reader);
fail:
return rc;
}
int trilogy_parse_stmt_ok_packet(const uint8_t *buff, size_t len, trilogy_stmt_ok_packet_t *out_packet)
{
int rc;
trilogy_reader_t reader = TRILOGY_READER(buff, len);
// skip packet type
CHECKED(trilogy_reader_get_uint8(&reader, NULL));
CHECKED(trilogy_reader_get_uint32(&reader, &out_packet->id));
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->column_count));
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->parameter_count));
uint8_t filler;
CHECKED(trilogy_reader_get_uint8(&reader, &filler));
if (filler != 0) {
return TRILOGY_PROTOCOL_VIOLATION;
}
CHECKED(trilogy_reader_get_uint16(&reader, &out_packet->warning_count));
return trilogy_reader_finish(&reader);
fail:
return rc;
}
static void trilogy_pack_scramble_native_hash(const char *scramble, const char *password, size_t password_len,
uint8_t *buffer, unsigned int *buffer_len)
{
EVP_MD_CTX *ctx;
const EVP_MD *alg;
unsigned int hash_size_tmp1;
unsigned int hash_size_tmp2;
unsigned int x;
#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
ctx = EVP_MD_CTX_new();
#else
ctx = EVP_MD_CTX_create();
EVP_MD_CTX_init(ctx);
#endif
alg = EVP_sha1();
hash_size_tmp1 = 0;
hash_size_tmp2 = 0;
uint8_t hash_tmp1[EVP_MAX_MD_SIZE];
uint8_t hash_tmp2[EVP_MAX_MD_SIZE];
/* First hash the password. */
EVP_DigestInit_ex(ctx, alg, NULL);
EVP_DigestUpdate(ctx, (unsigned char *)(password), password_len);
EVP_DigestFinal_ex(ctx, hash_tmp1, &hash_size_tmp1);
/* Second, hash the password hash. */
EVP_DigestInit_ex(ctx, alg, NULL);
EVP_DigestUpdate(ctx, hash_tmp1, (size_t)hash_size_tmp1);
EVP_DigestFinal_ex(ctx, hash_tmp2, &hash_size_tmp2);
/* Third, hash the scramble and the double password hash. */
EVP_DigestInit_ex(ctx, alg, NULL);
EVP_DigestUpdate(ctx, (unsigned char *)scramble, SCRAMBLE_LEN);
EVP_DigestUpdate(ctx, hash_tmp2, (size_t)hash_size_tmp2);
EVP_DigestFinal_ex(ctx, buffer, buffer_len);
#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
EVP_MD_CTX_free(ctx);
#else
EVP_MD_CTX_destroy(ctx);
#endif
/* Fourth, xor the last hash against the first password hash. */
for (x = 0; x < *buffer_len; x++) {
buffer[x] = buffer[x] ^ hash_tmp1[x];
}
}
static void trilogy_pack_scramble_sha2_hash(const char *scramble, const char *password, size_t password_len,
uint8_t *buffer, unsigned int *buffer_len)
{
EVP_MD_CTX *ctx;
const EVP_MD *alg;
unsigned int hash_size_tmp1;
unsigned int hash_size_tmp2;
unsigned int x;
#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
ctx = EVP_MD_CTX_new();
#else
ctx = EVP_MD_CTX_create();
EVP_MD_CTX_init(ctx);
#endif
alg = EVP_sha256();
hash_size_tmp1 = 0;
hash_size_tmp2 = 0;
uint8_t hash_tmp1[EVP_MAX_MD_SIZE];
uint8_t hash_tmp2[EVP_MAX_MD_SIZE];
/* First hash the password. */
EVP_DigestInit_ex(ctx, alg, NULL);
EVP_DigestUpdate(ctx, (unsigned char *)(password), password_len);
EVP_DigestFinal_ex(ctx, hash_tmp1, &hash_size_tmp1);
/* Second, hash the password hash. */
EVP_DigestInit_ex(ctx, alg, NULL);
EVP_DigestUpdate(ctx, hash_tmp1, (size_t)hash_size_tmp1);
EVP_DigestFinal_ex(ctx, hash_tmp2, &hash_size_tmp2);
/* Third, hash the scramble and the double password hash. */
EVP_DigestInit_ex(ctx, alg, NULL);
EVP_DigestUpdate(ctx, hash_tmp2, (size_t)hash_size_tmp2);
EVP_DigestUpdate(ctx, (unsigned char *)scramble, SCRAMBLE_LEN);
EVP_DigestFinal_ex(ctx, buffer, buffer_len);
#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
EVP_MD_CTX_free(ctx);
#else
EVP_MD_CTX_destroy(ctx);
#endif
/* Fourth, xor the first and last hash. */
for (x = 0; x < *buffer_len; x++) {
buffer[x] = hash_tmp1[x] ^ buffer[x];
}
}
int trilogy_build_auth_packet(trilogy_builder_t *builder, const char *user, const char *pass, size_t pass_len,
const char *database, TRILOGY_CHARSET_t client_encoding, const char *auth_plugin,
const char *scramble, TRILOGY_CAPABILITIES_t flags)
{
int rc = TRILOGY_OK;
const char *default_auth_plugin = "mysql_native_password";
uint32_t capabilities = flags;
// Add the default set of capabilities for this client
capabilities |= TRILOGY_CAPABILITIES_CLIENT;
uint32_t max_packet_len = TRILOGY_MAX_PACKET_LEN;
unsigned int auth_response_len = 0;
uint8_t auth_response[EVP_MAX_MD_SIZE];
if (database) {
capabilities |= TRILOGY_CAPABILITIES_CONNECT_WITH_DB;
}
CHECKED(trilogy_builder_write_uint32(builder, capabilities));
CHECKED(trilogy_builder_write_uint32(builder, max_packet_len));
CHECKED(trilogy_builder_write_uint8(builder, client_encoding));
static const char zeroes[23] = {0};
CHECKED(trilogy_builder_write_buffer(builder, zeroes, 23));
if (user) {
CHECKED(trilogy_builder_write_string(builder, user));
} else {
CHECKED(trilogy_builder_write_string(builder, "root"));
}
if (pass_len > 0) {
// Fallback to te default unless we have SHA2 requested
if (!strcmp("caching_sha2_password", auth_plugin)) {
trilogy_pack_scramble_sha2_hash(scramble, pass, pass_len, auth_response, &auth_response_len);
} else {
trilogy_pack_scramble_native_hash(scramble, pass, pass_len, auth_response, &auth_response_len);
auth_plugin = default_auth_plugin;
}
}
// auth data len
CHECKED(trilogy_builder_write_uint8(builder, (uint8_t)auth_response_len));
if (auth_response_len > 0) {
CHECKED(trilogy_builder_write_buffer(builder, auth_response, auth_response_len));
}
if (database) {
CHECKED(trilogy_builder_write_string(builder, database));
}
if (capabilities & TRILOGY_CAPABILITIES_PLUGIN_AUTH) {
CHECKED(trilogy_builder_write_string(builder, auth_plugin));
}
trilogy_builder_finalize(builder);
fail:
return rc;
}
int trilogy_build_auth_clear_password(trilogy_builder_t *builder, const char *pass, size_t pass_len) {
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_buffer(builder, pass, pass_len));
CHECKED(trilogy_builder_write_uint8(builder, 0));
trilogy_builder_finalize(builder);
fail:
return rc;
}
int trilogy_build_auth_switch_response_packet(trilogy_builder_t *builder, const char *pass, size_t pass_len,
const char *auth_plugin, const char *scramble, const bool enable_cleartext_plugin)
{
int rc = TRILOGY_OK;
unsigned int auth_response_len = 0;
uint8_t auth_response[EVP_MAX_MD_SIZE];
if (pass_len > 0) {
if (!strcmp("mysql_clear_password", auth_plugin)) {
if (enable_cleartext_plugin) {
CHECKED(trilogy_builder_write_buffer(builder, pass, pass_len));
} else {
return TRILOGY_AUTH_PLUGIN_ERROR;
}
} else {
if (!strcmp("caching_sha2_password", auth_plugin)) {
trilogy_pack_scramble_sha2_hash(scramble, pass, pass_len, auth_response, &auth_response_len);
} else if (!strcmp("mysql_native_password", auth_plugin)) {
trilogy_pack_scramble_native_hash(scramble, pass, pass_len, auth_response, &auth_response_len);
} else {
return TRILOGY_AUTH_PLUGIN_ERROR;
}
CHECKED(trilogy_builder_write_buffer(builder, auth_response, auth_response_len));
}
}
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_ping_packet(trilogy_builder_t *builder)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_PING));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_query_packet(trilogy_builder_t *builder, const char *sql, size_t sql_len)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_QUERY));
CHECKED(trilogy_builder_write_buffer(builder, sql, sql_len));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_change_db_packet(trilogy_builder_t *builder, const char *name, size_t name_len)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_CHANGE_DB));
CHECKED(trilogy_builder_write_buffer(builder, name, name_len));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_quit_packet(trilogy_builder_t *builder)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_QUIT));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_set_option_packet(trilogy_builder_t *builder, const uint16_t option)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_SET_OPTION));
CHECKED(trilogy_builder_write_uint16(builder, option));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_ssl_request_packet(trilogy_builder_t *builder, TRILOGY_CAPABILITIES_t flags,
TRILOGY_CHARSET_t client_encoding)
{
static const char zeroes[23] = {0};
const uint32_t max_packet_len = TRILOGY_MAX_PACKET_LEN;
const uint32_t capabilities = flags | TRILOGY_CAPABILITIES_CLIENT | TRILOGY_CAPABILITIES_SSL;
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint32(builder, capabilities));
CHECKED(trilogy_builder_write_uint32(builder, max_packet_len));
CHECKED(trilogy_builder_write_uint8(builder, client_encoding));
CHECKED(trilogy_builder_write_buffer(builder, zeroes, 23));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_stmt_prepare_packet(trilogy_builder_t *builder, const char *sql, size_t sql_len)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_STMT_PREPARE));
CHECKED(trilogy_builder_write_buffer(builder, sql, sql_len));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_stmt_execute_packet(trilogy_builder_t *builder, uint32_t stmt_id, uint8_t flags,
trilogy_binary_value_t *binds, uint16_t num_binds)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_STMT_EXECUTE));
CHECKED(trilogy_builder_write_uint32(builder, stmt_id));
CHECKED(trilogy_builder_write_uint8(builder, flags));
// apparently, iteration-count is always 1
CHECKED(trilogy_builder_write_uint32(builder, 1));
int i;
if (num_binds > 0) {
if (binds == NULL) {
return TRILOGY_PROTOCOL_VIOLATION;
}
uint8_t current_bits = 0;
for (i = 0; i < num_binds; i++) {
if (binds[i].is_null) {
current_bits |= 1 << (i % 8);
}
// If we hit a byte boundary, write the bits we have so far and continue
if ((i % 8) == 7) {
CHECKED(trilogy_builder_write_uint8(builder, current_bits))
current_bits = 0;
}
}
// If there would have been any remainder bits, finally write those as well
if (num_binds % 8) {
CHECKED(trilogy_builder_write_uint8(builder, current_bits))
}
// new params bound flag
CHECKED(trilogy_builder_write_uint8(builder, 0x1));
for (i = 0; i < num_binds; i++) {
CHECKED(trilogy_builder_write_uint8(builder, binds[i].type));
if (binds[i].is_unsigned) {
CHECKED(trilogy_builder_write_uint8(builder, 0x80));
} else {
CHECKED(trilogy_builder_write_uint8(builder, 0x00));
}
}
for (i = 0; i < num_binds; i++) {
trilogy_binary_value_t val = binds[i];
switch (val.type) {
case TRILOGY_TYPE_TINY:
CHECKED(trilogy_builder_write_uint8(builder, val.as.uint8));
break;
case TRILOGY_TYPE_SHORT:
CHECKED(trilogy_builder_write_uint16(builder, val.as.uint16));
break;
case TRILOGY_TYPE_INT24:
case TRILOGY_TYPE_LONG:
CHECKED(trilogy_builder_write_uint32(builder, val.as.uint32));
break;
case TRILOGY_TYPE_LONGLONG:
CHECKED(trilogy_builder_write_uint64(builder, val.as.uint64));
break;
case TRILOGY_TYPE_FLOAT:
CHECKED(trilogy_builder_write_float(builder, val.as.flt));
break;
case TRILOGY_TYPE_DOUBLE:
CHECKED(trilogy_builder_write_double(builder, val.as.dbl));
break;
case TRILOGY_TYPE_YEAR:
CHECKED(trilogy_builder_write_uint16(builder, val.as.year));
break;
case TRILOGY_TYPE_TIME: {
uint8_t field_len = 0;
if (val.as.time.micro_seconds) {
field_len = 12;
} else if (val.as.time.hour || val.as.time.minute || val.as.time.second) {
field_len = 8;
} else {
field_len = 0;
}
CHECKED(trilogy_builder_write_uint8(builder, field_len));
if (field_len > 0) {
CHECKED(trilogy_builder_write_uint8(builder, val.as.time.is_negative));
CHECKED(trilogy_builder_write_uint32(builder, val.as.time.days));
CHECKED(trilogy_builder_write_uint8(builder, val.as.time.hour));
CHECKED(trilogy_builder_write_uint8(builder, val.as.time.minute));
CHECKED(trilogy_builder_write_uint8(builder, val.as.time.second));
if (field_len > 8) {
CHECKED(trilogy_builder_write_uint32(builder, val.as.time.micro_seconds));
}
}
break;
}
case TRILOGY_TYPE_DATE:
case TRILOGY_TYPE_DATETIME:
case TRILOGY_TYPE_TIMESTAMP: {
uint8_t field_len = 0;
if (val.as.date.datetime.micro_seconds) {
field_len = 11;
} else if (val.as.date.datetime.hour || val.as.date.datetime.minute || val.as.date.datetime.second) {
field_len = 7;
} else if (val.as.date.year || val.as.date.month || val.as.date.day) {
field_len = 4;
} else {
field_len = 0;
}
CHECKED(trilogy_builder_write_uint8(builder, field_len));
if (field_len > 0) {
CHECKED(trilogy_builder_write_uint16(builder, val.as.date.year));
CHECKED(trilogy_builder_write_uint8(builder, val.as.date.month));
CHECKED(trilogy_builder_write_uint8(builder, val.as.date.day));
if (field_len > 4) {
CHECKED(trilogy_builder_write_uint8(builder, val.as.date.datetime.hour));
CHECKED(trilogy_builder_write_uint8(builder, val.as.date.datetime.minute));
CHECKED(trilogy_builder_write_uint8(builder, val.as.date.datetime.second));
if (field_len > 7) {
CHECKED(trilogy_builder_write_uint32(builder, val.as.date.datetime.micro_seconds));
}
}
}
break;
}
case TRILOGY_TYPE_DECIMAL:
case TRILOGY_TYPE_VARCHAR:
case TRILOGY_TYPE_BIT:
case TRILOGY_TYPE_NEWDECIMAL:
case TRILOGY_TYPE_ENUM:
case TRILOGY_TYPE_SET:
case TRILOGY_TYPE_TINY_BLOB:
case TRILOGY_TYPE_BLOB:
case TRILOGY_TYPE_MEDIUM_BLOB:
case TRILOGY_TYPE_LONG_BLOB:
case TRILOGY_TYPE_VAR_STRING:
case TRILOGY_TYPE_STRING:
case TRILOGY_TYPE_GEOMETRY:
case TRILOGY_TYPE_JSON:
case TRILOGY_TYPE_VECTOR:
CHECKED(trilogy_builder_write_lenenc_buffer(builder, val.as.str.data, val.as.str.len));
break;
case TRILOGY_TYPE_NULL:
// already handled by the null bitmap
break;
default:
return TRILOGY_UNKNOWN_TYPE;
}
}
}
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_stmt_bind_data_packet(trilogy_builder_t *builder, uint32_t stmt_id, uint16_t param_id, uint8_t *data,
size_t data_len)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_STMT_SEND_LONG_DATA));
CHECKED(trilogy_builder_write_uint32(builder, stmt_id));
CHECKED(trilogy_builder_write_uint16(builder, param_id));
CHECKED(trilogy_builder_write_buffer(builder, data, data_len));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_stmt_reset_packet(trilogy_builder_t *builder, uint32_t stmt_id)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_STMT_RESET));
CHECKED(trilogy_builder_write_uint32(builder, stmt_id));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail:
return rc;
}
int trilogy_build_stmt_close_packet(trilogy_builder_t *builder, uint32_t stmt_id)
{
int rc = TRILOGY_OK;
CHECKED(trilogy_builder_write_uint8(builder, TRILOGY_CMD_STMT_CLOSE));
CHECKED(trilogy_builder_write_uint32(builder, stmt_id));
trilogy_builder_finalize(builder);
return TRILOGY_OK;
fail: