forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_config_editor.cc
1335 lines (1079 loc) · 37.3 KB
/
mysql_config_editor.cc
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
/*
Copyright (c) 2012, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
@file
@brief
MySQL Configuration Utility
*/
#include "my_config.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include "client/include/client_priv.h"
#include "my_aes.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_default.h"
#include "my_dir.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_rnd.h"
#include "mysql/service_mysql_alloc.h"
#include "mysys/my_default_priv.h"
#include "nulls.h"
#include "print_version.h"
#include "welcome_copyright_notice.h"
#define MY_LINE_MAX 4096
#define MAX_COMMAND_LIMIT 100
/*
Header length for the login file.
4-byte (unused) + LOGIN_KEY_LEN
*/
#define MY_LOGIN_HEADER_LEN (4 + LOGIN_KEY_LEN)
static int g_fd;
/*
Length of the contents in login file
excluding the header part.
*/
static size_t file_size;
static const char *opt_user = nullptr, *opt_password = nullptr,
*opt_host = nullptr, *opt_login_path = "client",
*opt_socket = nullptr, *opt_port = nullptr;
static char my_login_file[FN_REFLEN];
static char my_key[LOGIN_KEY_LEN];
static bool opt_verbose, opt_all,
tty_password = false, opt_warn, opt_remove_host, opt_remove_pass,
opt_remove_user, opt_remove_socket, opt_remove_port,
login_path_specified = false;
static int execute_commands(int command);
static int set_command(void);
static int remove_command(void);
static int print_command(void);
static void print_login_path(DYNAMIC_STRING *file_buf, const char *path_name);
static void remove_login_path(DYNAMIC_STRING *file_buf, const char *path_name);
static char *locate_login_path(DYNAMIC_STRING *file_buf, const char *path_name);
static bool check_and_create_login_file(void);
static void mask_password_and_print(char *buf);
static int reset_login_file(bool gen_key);
static int encrypt_buffer(const char *plain, int plain_len, char cipher[],
const int aes_len);
static int decrypt_buffer(const char *cipher, int cipher_len, char plain[]);
static int encrypt_and_write_file(DYNAMIC_STRING *file_buf);
static int read_and_decrypt_file(DYNAMIC_STRING *file_buf);
static int do_handle_options(int argc, char *argv[]);
static void remove_options(DYNAMIC_STRING *file_buf, const char *path_name);
static void remove_option(DYNAMIC_STRING *file_buf, const char *path_name,
const char *option_name);
bool generate_login_key(void);
static int read_login_key(void);
static int add_header(void);
static void my_perror(const char *msg);
static void verbose_msg(const char *fmt, ...)
MY_ATTRIBUTE((format(printf, 1, 2)));
static void usage_program(void);
static void usage_command(int command);
extern "C" bool get_one_option(int optid, const struct my_option *opt,
char *argument);
enum commands {
MY_CONFIG_SET,
MY_CONFIG_REMOVE,
MY_CONFIG_PRINT,
MY_CONFIG_RESET,
MY_CONFIG_HELP
};
extern "C" {
struct my_command_data {
const int id;
const char *name;
const char *description;
my_option *options;
bool (*get_one_option_func)(int optid, const struct my_option *opt,
char *argument);
};
}
/* mysql_config_editor utility options. */
static struct my_option my_program_long_options[] = {
#ifdef NDEBUG
{"debug", '#', "This is a non-debug version. Catch this and exit.", nullptr,
nullptr, nullptr, GET_DISABLED, OPT_ARG, 0, 0, 0, nullptr, 0, nullptr},
#else
{"debug", '#', "Output debug log. Often this is 'd:t:o,filename'.", nullptr,
nullptr, nullptr, GET_STR, OPT_ARG, 0, 0, 0, nullptr, 0, nullptr},
#endif
{"help", '?', "Display this help and exit.", nullptr, nullptr, nullptr,
GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"verbose", 'v', "Write more information.", &opt_verbose, &opt_verbose,
nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"version", 'V', "Output version information and exit.", nullptr, nullptr,
nullptr, GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{nullptr, 0, nullptr, nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0,
0, nullptr, 0, nullptr}};
/* Command-specific options. */
/* SET command options. */
static struct my_option my_set_command_options[] = {
{"help", '?', "Display this help and exit.", nullptr, nullptr, nullptr,
GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"host", 'h', "Host name to be entered into the login file.", &opt_host,
&opt_host, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"login-path", 'G',
"Name of the login path to use in the login file. "
"(Default : client)",
&opt_login_path, &opt_login_path, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"password", 'p', "Prompt for password to be entered into the login file.",
nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0,
nullptr},
{"user", 'u', "User name to be entered into the login file.", &opt_user,
&opt_user, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"socket", 'S', "Socket path to be entered into login file.", &opt_socket,
&opt_socket, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"port", 'P', "Port number to be entered into login file.", &opt_port,
&opt_port, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"warn", 'w',
"Warn and ask for confirmation if set command attempts to "
"overwrite an existing login path (enabled by default).",
&opt_warn, &opt_warn, nullptr, GET_BOOL, NO_ARG, 1, 0, 0, nullptr, 0,
nullptr},
{nullptr, 0, nullptr, nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0,
0, nullptr, 0, nullptr}};
/* REMOVE command options. */
static struct my_option my_remove_command_options[] = {
{"help", '?', "Display this help and exit.", nullptr, nullptr, nullptr,
GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"host", 'h', "Remove host name from the login path.", &opt_remove_host,
&opt_remove_host, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"login-path", 'G',
"Name of the login path from which options to "
"be removed (entire path would be removed if none of user, password, "
"host, socket, or port options are specified). (Default : client)",
&opt_login_path, &opt_login_path, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"password", 'p', "Remove password from the login path.", &opt_remove_pass,
&opt_remove_pass, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"user", 'u', "Remove user name from the login path.", &opt_remove_user,
&opt_remove_user, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"warn", 'w',
"Warn and ask for confirmation if remove command attempts "
"to remove the default login path (client) if no login path is specified "
"(enabled by default).",
&opt_warn, &opt_warn, nullptr, GET_BOOL, NO_ARG, 1, 0, 0, nullptr, 0,
nullptr},
{"socket", 'S', "Remove socket path from the login path.",
&opt_remove_socket, &opt_remove_socket, nullptr, GET_BOOL, NO_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{"port", 'P', "Remove port number from the login path.", &opt_remove_port,
&opt_remove_port, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{nullptr, 0, nullptr, nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0,
0, nullptr, 0, nullptr}};
/* PRINT command options. */
static struct my_option my_print_command_options[] = {
{"all", OPT_CONFIG_ALL, "Used with print command to print all login paths.",
&opt_all, &opt_all, nullptr, GET_BOOL, NO_ARG, 0, 0, 0, nullptr, 0,
nullptr},
{"help", '?', "Display this help and exit.", nullptr, nullptr, nullptr,
GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{"login-path", 'G',
"Name of the login path to use in the login file. "
"(Default : client)",
&opt_login_path, &opt_login_path, nullptr, GET_STR, REQUIRED_ARG, 0, 0, 0,
nullptr, 0, nullptr},
{nullptr, 0, nullptr, nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0,
0, nullptr, 0, nullptr}};
/* RESET command options. */
static struct my_option my_reset_command_options[] = {
{"help", '?', "Display this help and exit.", nullptr, nullptr, nullptr,
GET_NO_ARG, NO_ARG, 0, 0, 0, nullptr, 0, nullptr},
{nullptr, 0, nullptr, nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0,
0, nullptr, 0, nullptr}};
/* HELP command options. */
static struct my_option my_help_command_options[] = {
{nullptr, 0, nullptr, nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0,
0, nullptr, 0, nullptr}};
extern "C" {
static bool my_program_get_one_option(int optid,
const struct my_option *opt
[[maybe_unused]],
char *argument [[maybe_unused]]) {
switch (optid) {
case '#':
DBUG_PUSH(argument ? argument : "d:t:o,/tmp/mysql_config_editor.trace");
break;
case 'V':
print_version();
exit(0);
break;
case '?':
usage_program();
exit(0);
break;
}
return false;
}
static bool my_set_command_get_one_option(int optid, const struct my_option *,
char *) {
switch (optid) {
case 'p':
tty_password = true;
break;
case 'G':
if (login_path_specified) {
/* Error, we do not support multiple login paths. */
my_perror(
"Error: Use of multiple login paths is not supported. "
"Exiting..");
return true;
}
login_path_specified = true;
break;
case '?':
usage_command(MY_CONFIG_SET);
exit(0);
break;
}
return false;
}
static bool my_remove_command_get_one_option(int optid,
const struct my_option *, char *) {
switch (optid) {
case 'G':
if (login_path_specified) {
/* Error, we do not support multiple login paths. */
my_perror(
"Error: Use of multiple login paths is not supported. "
"Exiting..");
return true;
}
login_path_specified = true;
break;
case '?':
usage_command(MY_CONFIG_REMOVE);
exit(0);
break;
}
return false;
}
static bool my_print_command_get_one_option(int optid, const struct my_option *,
char *) {
switch (optid) {
case 'G':
if (login_path_specified) {
/* Error, we do not support multiple login paths. */
my_perror(
"Error: Use of multiple login paths is not supported. "
"Exiting..");
return true;
}
login_path_specified = true;
break;
case '?':
usage_command(MY_CONFIG_PRINT);
exit(0);
break;
}
return false;
}
static bool my_reset_command_get_one_option(int optid, const struct my_option *,
char *) {
switch (optid) {
case '?':
usage_command(MY_CONFIG_RESET);
exit(0);
break;
}
return false;
}
}
static struct my_command_data command_data[] = {
{MY_CONFIG_SET, "set", "Write a login path to the login file.",
my_set_command_options, my_set_command_get_one_option},
{MY_CONFIG_REMOVE, "remove", "Remove a login path from the login file.",
my_remove_command_options, my_remove_command_get_one_option},
{MY_CONFIG_PRINT, "print",
"Print the contents of login file in unencrypted form.",
my_print_command_options, my_print_command_get_one_option},
{MY_CONFIG_RESET, "reset",
"Empty the contents of the login file. The file is created\n"
"if it does not exist.",
my_reset_command_options, my_reset_command_get_one_option},
{MY_CONFIG_HELP, "help", "Display a help message and exit.",
my_help_command_options, nullptr},
{0, nullptr, nullptr, nullptr, nullptr}};
int main(int argc, char *argv[]) {
MY_INIT(argv[0]);
DBUG_TRACE;
int command, rc = 0;
command = do_handle_options(argc, argv);
if (command > -1) rc = execute_commands(command);
if (rc != 0) {
my_perror("operation failed.");
return 1;
}
return 0;
}
/**
Handle all the command line arguments.
program_name [program options] [command [command options]]
*/
static int do_handle_options(int argc, char *argv[]) {
DBUG_TRACE;
const char *command_list[MAX_COMMAND_LIMIT + 1];
char **saved_argv = argv;
char **argv_cmd;
char *ptr; /* for free. */
int argc_cmd;
int rc, i, command = -1;
if (argc < 2) {
usage_program();
exit(1);
}
if (!(ptr = (char *)my_malloc(PSI_NOT_INSTRUMENTED,
(argc + 2) * sizeof(char *), MYF(MY_WME))))
goto error;
/* Handle program options. */
/* Prepare a list of supported commands to be used by my_handle_options(). */
for (i = 0; (command_data[i].name != nullptr) && (i < MAX_COMMAND_LIMIT); i++)
command_list[i] = command_data[i].name;
command_list[i] = nullptr;
if ((rc = my_handle_options(&argc, &argv, my_program_long_options,
my_program_get_one_option, command_list, false)))
exit(rc);
if (argc == 0) /* No command specified. */
goto done;
for (i = 0; command_data[i].name != nullptr; i++) {
if (!strcmp(command_data[i].name, argv[0])) {
command = i;
break;
}
}
if (command == -1) goto error;
/* Handle command options. */
argv_cmd = (char **)ptr;
argc_cmd = argc + 1;
/* Prepare a command line (argv) using the rest of the options. */
argv_cmd[0] = saved_argv[0];
memcpy((uchar *)(argv_cmd + 1), (uchar *)(argv), (argc * sizeof(char *)));
argv_cmd[argc_cmd] = nullptr;
if ((rc = handle_options(&argc_cmd, &argv_cmd, command_data[command].options,
command_data[command].get_one_option_func)))
exit(rc);
/* Do not allow multiple commands. */
if (argc_cmd > 1) goto error;
done:
my_free(ptr);
return command;
error:
my_free(ptr);
usage_program();
exit(1);
}
static int execute_commands(int command) {
DBUG_TRACE;
int rc = 0;
if ((rc = check_and_create_login_file())) goto done;
switch (command_data[command].id) {
case MY_CONFIG_SET:
verbose_msg("Executing set command.\n");
rc = set_command();
break;
case MY_CONFIG_REMOVE:
verbose_msg("Executing remove command.\n");
rc = remove_command();
break;
case MY_CONFIG_PRINT:
verbose_msg("Executing print command.\n");
rc = print_command();
break;
case MY_CONFIG_RESET:
verbose_msg("Resetting login file.\n");
rc = reset_login_file(true);
break;
case MY_CONFIG_HELP:
verbose_msg("Printing usage info.\n");
usage_program();
break;
default:
my_perror("unknown command.");
exit(1);
}
done:
my_close(g_fd, MYF(MY_WME));
return rc;
}
/**
Execute 'set' command.
@return -1 Error
0 Success
*/
static int set_command(void) {
DBUG_TRACE;
DYNAMIC_STRING file_buf, path_buf;
init_dynamic_string(&path_buf, "", MY_LINE_MAX);
init_dynamic_string(&file_buf, "", file_size);
if (tty_password) opt_password = get_tty_password(NullS);
if (file_size) {
if (read_and_decrypt_file(&file_buf) == -1) goto error;
}
dynstr_append(&path_buf, "["); /* --login=path */
if (opt_login_path)
dynstr_append(&path_buf, opt_login_path);
else
dynstr_append(&path_buf, "client");
dynstr_append(&path_buf, "]");
if (opt_user) /* --user */
{
dynstr_append(&path_buf, "\nuser = ");
dynstr_append_quoted(&path_buf, "\"", 1, opt_user, NullS);
}
if (opt_password) /* --password */
{
dynstr_append(&path_buf, "\npassword = ");
dynstr_append_quoted(&path_buf, "\"", 1, opt_password, NullS);
}
if (opt_host) /* --host */
{
dynstr_append(&path_buf, "\nhost = ");
dynstr_append_quoted(&path_buf, "\"", 1, opt_host, NullS);
}
if (opt_socket) {
dynstr_append(&path_buf, "\nsocket = ");
dynstr_append_quoted(&path_buf, "\"", 1, opt_socket, NullS);
}
if (opt_port) {
dynstr_append(&path_buf, "\nport = ");
dynstr_append(&path_buf, opt_port);
}
dynstr_append(&path_buf, "\n");
/* Warn if login path already exists */
if (opt_warn && ((locate_login_path(&file_buf, opt_login_path)) != nullptr)) {
int choice;
printf(
"WARNING : \'%s\' path already exists and will be "
"overwritten. \n Continue? (Press y|Y for Yes, any "
"other key for No) : ",
opt_login_path);
choice = getchar();
if (choice != (int)'y' && choice != (int)'Y') goto done; /* skip */
}
/* Remove the login path. */
remove_login_path(&file_buf, opt_login_path);
/* Append the new login path to the file buffer. */
dynstr_append(&file_buf, path_buf.str);
if (encrypt_and_write_file(&file_buf) == -1) goto error;
done:
dynstr_free(&file_buf);
dynstr_free(&path_buf);
return 0;
error:
dynstr_free(&file_buf);
dynstr_free(&path_buf);
return -1;
}
static int remove_command(void) {
DBUG_TRACE;
DYNAMIC_STRING file_buf, path_buf;
init_dynamic_string(&path_buf, "", MY_LINE_MAX);
init_dynamic_string(&file_buf, "", file_size);
if (file_size) {
if (read_and_decrypt_file(&file_buf) == -1) goto error;
} else
goto done; /* Nothing to remove, skip.. */
/* Warn if no login path is specified. */
if (opt_warn && ((locate_login_path(&file_buf, opt_login_path)) != nullptr) &&
(login_path_specified == false)) {
int choice;
printf(
"WARNING : No login path specified, so options from the default "
"login path will be removed.\nContinue? (Press y|Y for Yes, "
"any other key for No) : ");
choice = getchar();
if (choice != (int)'y' && choice != (int)'Y') goto done; /* skip */
}
remove_options(&file_buf, opt_login_path);
if (encrypt_and_write_file(&file_buf) == -1) goto error;
done:
dynstr_free(&file_buf);
dynstr_free(&path_buf);
return 0;
error:
dynstr_free(&file_buf);
dynstr_free(&path_buf);
return -1;
}
/**
Execute 'print' command.
@return -1 Error
0 Success
*/
static int print_command(void) {
DBUG_TRACE;
DYNAMIC_STRING file_buf;
init_dynamic_string(&file_buf, "", file_size);
if (file_size) {
if (read_and_decrypt_file(&file_buf) == -1) goto error;
} else
goto done; /* Nothing to print, skip..*/
print_login_path(&file_buf, opt_login_path);
done:
dynstr_free(&file_buf);
return 0;
error:
dynstr_free(&file_buf);
return -1;
}
/**
Create the login file if it does not exist, check
and set its permissions and modes.
@return true Error
false Success
*/
static bool check_and_create_login_file(void) {
DBUG_TRACE;
MY_STAT stat_info;
// This is a hack to make it compile. File permissions are different on Windows.
#ifdef _WIN32
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IRWXU 00700
#define S_IRWXG 00070
#define S_IRWXO 00007
#endif
const int access_flag = O_RDWR;
const ushort create_mode = (S_IRUSR | S_IWUSR);
/* Get the login file name. */
if (!my_default_get_login_file(my_login_file, sizeof(my_login_file))) {
my_perror("failed to set login file name");
goto error;
}
/*
NOTE : MYSQL_TEST_LOGIN_FILE env must be a full path,
where the directory structure must exist. However the
login file will be created if it does not exist.
*/
#ifdef _WIN32
if (!(getenv("MYSQL_TEST_LOGIN_FILE"))) {
/* Check if 'MySQL' directory is in place. */
MY_STAT stat_info_dir;
char login_dir[FN_REFLEN];
size_t size;
dirname_part(login_dir, my_login_file, &size);
/* Remove the trailing '\' */
if (is_directory_separator(login_dir[--size])) login_dir[size] = 0;
/* Now check if directory exists? */
if (my_stat(login_dir, &stat_info_dir, MYF(0))) {
verbose_msg("%s directory exists.\n", login_dir);
} else {
/* Create the login directory. */
verbose_msg("%s directory doesn't exist, creating it.\n", login_dir);
if (my_mkdir(login_dir, 0, MYF(0))) {
my_perror("failed to create the directory");
goto error;
}
}
}
#endif
/* Check for login file's existence and permissions (0600). */
if (my_stat(my_login_file, &stat_info, MYF(0))) {
verbose_msg("File exists.\n");
file_size = (size_t)stat_info.st_size;
#ifdef _WIN32
if (1)
#else
if (!(stat_info.st_mode & (S_IXUSR | S_IRWXG | S_IRWXO)))
#endif
{
verbose_msg("File has the required permission.\nOpening the file.\n");
if ((g_fd = my_open(my_login_file, access_flag, MYF(MY_WME))) == -1) {
my_perror("couldn't open the file");
goto error;
}
} else {
verbose_msg("File does not have the required permission.\n");
printf(
"WARNING : Login file does not have the required"
" file permissions.\nPlease set the mode to 600 &"
" run the command again.\n");
goto error;
}
} else {
verbose_msg("File does not exist.\nCreating login file.\n");
if ((g_fd = my_create(my_login_file, create_mode, access_flag,
MYF(MY_WME))) == -1) {
my_perror("couldn't create the login file");
goto error;
} else {
verbose_msg("Login file created.\n");
my_close(g_fd, MYF(MY_WME));
verbose_msg("Opening the file.\n");
if ((g_fd = my_open(my_login_file, access_flag, MYF(MY_WME))) == -1) {
my_perror("couldn't open the file");
goto error;
}
file_size = 0;
}
}
if (file_size == 0) {
if (generate_login_key()) goto error;
if (add_header() == -1) goto error;
} else {
if (read_login_key() == -1) goto error;
}
return false;
error:
return true;
}
/**
Print options under the specified login path. If '--all'
option is used, print all the optins stored in the login
file.
@param [in] file_buf Buffer storing the unscrambled login
file contents.
@param [in] path_name Path name.
*/
static void print_login_path(DYNAMIC_STRING *file_buf, const char *path_name) {
DBUG_TRACE;
char *start = nullptr, *end = nullptr, temp = '\0';
if (file_buf->length == 0) goto done; /* Nothing to print. */
if (opt_all) {
start = file_buf->str;
end = file_buf->str + file_buf->length;
} else {
start = locate_login_path(file_buf, path_name);
if (!start) /* login path not found, skip..*/
goto done;
end = strstr(start, "\n[");
}
if (end) {
temp = *end;
*end = '\0';
}
mask_password_and_print(start);
if (temp != '\0') *end = temp;
done:
return;
}
/**
Print the specified buffer by masking the actual
password string.
@param [in] buf Buffer to be printed.
*/
static void mask_password_and_print(char *buf) {
DBUG_TRACE;
const char *password_str = "\npassword = ", *mask = "*****";
char *next = nullptr;
while ((next = strstr(buf, password_str)) != nullptr) {
while (*buf != 0 && buf != next) putc(*(buf++), stdout);
printf("%s", password_str);
printf("%s\n", mask);
if (*buf == '\n') /* Move past \n' */
buf++;
/* Ignore the password. */
while (*buf && *(buf++) != '\n') {
}
if (!opt_all) break;
}
/* Now print the rest of the buffer. */
while (*buf) putc(*(buf++), stdout);
// And a new line.. if required.
if (*(buf - 1) != '\n') putc('\n', stdout);
}
/**
Remove multiple options from a login path.
*/
static void remove_options(DYNAMIC_STRING *file_buf, const char *path_name) {
/* If nope of the options are specified remove the entire path. */
if (!opt_remove_host && !opt_remove_pass && !opt_remove_user &&
!opt_remove_socket && !opt_remove_port) {
remove_login_path(file_buf, path_name);
return;
}
if (opt_remove_user) remove_option(file_buf, path_name, "user");
if (opt_remove_pass) remove_option(file_buf, path_name, "password");
if (opt_remove_host) remove_option(file_buf, path_name, "host");
if (opt_remove_socket) remove_option(file_buf, path_name, "socket");
if (opt_remove_port) remove_option(file_buf, path_name, "port");
}
/**
Remove an option from a login path.
*/
static void remove_option(DYNAMIC_STRING *file_buf, const char *path_name,
const char *option_name) {
DBUG_TRACE;
char *start = nullptr, *end = nullptr;
char *search_str;
size_t search_len, shift_len;
bool option_found = false;
search_str = (char *)my_malloc(PSI_NOT_INSTRUMENTED,
(uint)strlen(option_name) + 2, MYF(MY_WME));
sprintf(search_str, "\n%s", option_name);
if ((start = locate_login_path(file_buf, path_name)) == nullptr)
/* login path was not found, skip.. */
goto done;
end = strstr(start, "\n["); /* Next path. */
if (end)
search_len = end - start;
else
search_len = file_buf->length - (start - file_buf->str);
while (search_len > 1) {
if (!strncmp(start, search_str, strlen(search_str))) {
/* Option found. */
end = start;
while (*(++end) != '\n') {
}
option_found = true;
break;
} else {
/* Move to next line. */
while ((--search_len > 1) && (*(++start) != '\n')) {
}
}
}
if (option_found) {
shift_len = file_buf->length - (end - file_buf->str);
file_buf->length -= (end - start);
while (shift_len--) *(start++) = *(end++);
*start = '\0';
}
done:
my_free(search_str);
}
/**
Remove the specified login path from the login file.
@param [in] file_buf Buffer storing the unscrambled login
file contents.
@param [in] path_name Path name.
*/
static void remove_login_path(DYNAMIC_STRING *file_buf, const char *path_name) {
DBUG_TRACE;
char *start = nullptr, *end = nullptr;
int to_move, len, diff;
if ((start = locate_login_path(file_buf, path_name)) == nullptr)
/* login path was not found, skip.. */
goto done;
end = strstr(start, "\n[");
if (end) {
end++; /* Move past '\n' */
len = ((diff = (start - end)) > 0) ? diff : -diff;
to_move = file_buf->length - (end - file_buf->str);
} else {
*start = '\0';
file_buf->length = ((diff = (file_buf->str - start)) > 0) ? diff : -diff;
goto done;
}
while (to_move--) *(start++) = *(end++);
*start = '\0';
file_buf->length -= len;
done:
return;
}
/**
Remove all the contents from the login file.
@param [in] gen_key Flag to control the generation of
a new key.
@return -1 Error
0 Success
*/
static int reset_login_file(bool gen_key) {
DBUG_TRACE;
if (my_chsize(g_fd, 0, 0, MYF(MY_WME))) {
verbose_msg("Error while truncating the file.\n");
goto error;
}
/* Seek to the beginning of the file. */
if (my_seek(g_fd, 0L, SEEK_SET, MYF(MY_WME) == MY_FILEPOS_ERROR))
goto error; /* Error. */
if (gen_key && generate_login_key()) goto error; /* Generate a new key. */
if (add_header() == -1) goto error;
return 0;
error:
return 0;
}
/**
Find the specified login path in the login file buffer
and return the starting address.
@param [in] file_buf Buffer storing the unscrambled login
file contents.
@param [in] path_name Path name.
@return If found, the starting address of the
login path, NULL otherwise.
*/