26 #include "my_config.h"
27 #include <welcome_copyright_notice.h>
32 #include "client_priv.h"
33 #include "my_default.h"
34 #include "my_default_priv.h"
36 #define MYSQL_CONFIG_EDITOR_VERSION "1.0"
37 #define MY_LINE_MAX 4096
38 #define MAX_COMMAND_LIMIT 100
43 #define MY_LOGIN_HEADER_LEN (4 + LOGIN_KEY_LEN)
51 static size_t file_size;
52 static char *opt_user= NULL, *opt_password= NULL, *opt_host=NULL,
53 *opt_login_path= NULL, *opt_socket= NULL, *opt_port= NULL;
55 static char my_login_file[FN_REFLEN];
56 static char my_key[LOGIN_KEY_LEN];
58 static my_bool opt_verbose, opt_all, tty_password= 0, opt_warn,
59 opt_remove_host, opt_remove_pass, opt_remove_user,
60 opt_remove_socket, opt_remove_port, login_path_specified= FALSE;
62 static int execute_commands(
int command);
63 static int set_command(
void);
64 static int remove_command(
void);
65 static int print_command(
void);
66 static void print_login_path(
DYNAMIC_STRING *file_buf,
const char *path_name);
67 static void remove_login_path(
DYNAMIC_STRING *file_buf,
const char *path_name);
68 static char* locate_login_path(
DYNAMIC_STRING *file_buf,
const char *path_name);
69 static my_bool check_and_create_login_file(
void);
70 static void mask_password_and_print(
char *
buf);
71 static int reset_login_file(
bool gen_key);
73 static int encrypt_buffer(
const char *plain,
int plain_len,
char cipher[]);
74 static int decrypt_buffer(
const char *cipher,
int cipher_len,
char plain[]);
77 static int do_handle_options(
int argc,
char *argv[]);
78 static void remove_options(
DYNAMIC_STRING *file_buf,
const char *path_name);
79 static void remove_option(
DYNAMIC_STRING *file_buf,
const char *path_name,
80 const char* option_name);
82 static int read_login_key(
void);
83 static int add_header(
void);
84 static void my_perror(
const char *
msg);
86 static void verbose_msg(
const char *
fmt, ...);
87 static void print_version(
void);
88 static void usage_program(
void);
89 static void usage_command(
int command);
90 extern "C" my_bool get_one_option(
int optid,
const struct my_option *opt,
104 const char *description;
106 my_bool (*get_one_option_func)(
int optid,
113 static struct my_option my_program_long_options[]=
116 {
"debug",
'#',
"Output debug log. Often this is 'd:t:o,filename'.",
117 0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
119 {
"help",
'?',
"Display this help and exit.", 0, 0, 0, GET_NO_ARG,
120 NO_ARG, 0, 0, 0, 0, 0, 0},
121 {
"verbose",
'v',
"Write more information.", &opt_verbose,
122 &opt_verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
123 {
"version",
'V',
"Output version information and exit.", 0, 0, 0, GET_NO_ARG,
124 NO_ARG, 0, 0, 0, 0, 0, 0},
125 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
131 static struct my_option my_set_command_options[]=
133 {
"help",
'?',
"Display this help and exit.", 0, 0, 0, GET_NO_ARG,
134 NO_ARG, 0, 0, 0, 0, 0, 0},
135 {
"host",
'h',
"Host name to be entered into the login file.", &opt_host,
136 &opt_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
137 {
"login-path",
'G',
"Name of the login path to use in the login file. "
138 "(Default : client)", &opt_login_path, &opt_login_path, 0, GET_STR,
139 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
140 {
"password",
'p',
"Prompt for password to be entered into the login file.",
141 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
142 {
"user",
'u',
"User name to be entered into the login file.", &opt_user,
143 &opt_user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
144 {
"socket",
'S',
"Socket path to be entered into login file.", &opt_socket,
145 &opt_socket, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
146 {
"port",
'P',
"Port number to be entered into login file.", &opt_port,
147 &opt_port, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
148 {
"warn",
'w',
"Warn and ask for confirmation if set command attempts to "
149 "overwrite an existing login path (enabled by default).",
150 &opt_warn, &opt_warn, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
151 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
155 static struct my_option my_remove_command_options[]=
157 {
"help",
'?',
"Display this help and exit.", 0, 0, 0, GET_NO_ARG,
158 NO_ARG, 0, 0, 0, 0, 0, 0},
159 {
"host",
'h',
"Remove host name from the login path.",
160 &opt_remove_host, &opt_remove_host, 0, GET_BOOL, NO_ARG, 0, 0, 0,
162 {
"login-path",
'G',
"Name of the login path from which options to "
163 "be removed (entire path would be removed if none of user, password, "
164 "host, socket, or port options are specified). (Default : client)",
165 &opt_login_path, &opt_login_path, 0, GET_STR, REQUIRED_ARG,
167 {
"password",
'p',
"Remove password from the login path.",
168 &opt_remove_pass, &opt_remove_pass, 0, GET_BOOL, NO_ARG, 0, 0, 0,
170 {
"user",
'u',
"Remove user name from the login path.", &opt_remove_user,
171 &opt_remove_user, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
172 {
"warn",
'w',
"Warn and ask for confirmation if remove command attempts "
173 "to remove the default login path (client) if no login path is specified "
174 "(enabled by default).", &opt_warn, &opt_warn, 0, GET_BOOL, NO_ARG, 1,
176 {
"socket",
'S',
"Remove socket path from the login path.", &opt_remove_socket,
177 &opt_remove_socket, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
178 {
"port",
'P',
"Remove port number from the login path.", &opt_remove_port,
179 &opt_remove_port, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
180 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
184 static struct my_option my_print_command_options[]=
186 {
"all", OPT_CONFIG_ALL,
"Used with print command to print all login paths.",
187 &opt_all, &opt_all, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
188 {
"help",
'?',
"Display this help and exit.", 0, 0, 0, GET_NO_ARG,
189 NO_ARG, 0, 0, 0, 0, 0, 0},
190 {
"login-path",
'G',
"Name of the login path to use in the login file. "
191 "(Default : client)", &opt_login_path, &opt_login_path, 0, GET_STR,
192 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
193 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
197 static struct my_option my_reset_command_options[]=
199 {
"help",
'?',
"Display this help and exit.", 0, 0, 0, GET_NO_ARG,
200 NO_ARG, 0, 0, 0, 0, 0, 0},
201 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
205 static struct my_option my_help_command_options[]=
207 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
211 my_program_get_one_option(
int optid,
212 const struct my_option *opt __attribute__((unused)),
217 DBUG_PUSH(argument ? argument :
"d:t:o,/tmp/mysql_config_editor.trace");
232 my_set_command_get_one_option(
int optid,
233 const struct my_option *opt __attribute__((unused)),
241 if (login_path_specified)
244 my_perror(
"Error: Use of multiple login paths is not supported. "
248 login_path_specified= TRUE;
251 usage_command(MY_CONFIG_SET);
259 my_remove_command_get_one_option(
int optid,
260 const struct my_option *opt __attribute__((unused)),
265 if (login_path_specified)
268 my_perror(
"Error: Use of multiple login paths is not supported. "
272 login_path_specified= TRUE;
275 usage_command(MY_CONFIG_REMOVE);
283 my_print_command_get_one_option(
int optid,
284 const struct my_option *opt __attribute__((unused)),
289 if (login_path_specified)
292 my_perror(
"Error: Use of multiple login paths is not supported. "
296 login_path_specified= TRUE;
299 usage_command(MY_CONFIG_PRINT);
307 my_reset_command_get_one_option(
int optid,
308 const struct my_option *opt __attribute__((unused)),
313 usage_command(MY_CONFIG_RESET);
325 "Write a login path to the login file.",
326 my_set_command_options,
327 my_set_command_get_one_option
332 "Remove a login path from the login file.",
333 my_remove_command_options,
334 my_remove_command_get_one_option
339 "Print the contents of login file in unencrypted form.",
340 my_print_command_options,
341 my_print_command_get_one_option
346 "Empty the contents of the login file. The file is created\n"
347 "if it does not exist.",
348 my_reset_command_options,
349 my_reset_command_get_one_option
354 "Display a help message and exit.",
355 my_help_command_options,
359 0, NULL, NULL, NULL, NULL
364 int main(
int argc,
char *argv[])
370 command= do_handle_options(argc, argv);
373 rc= execute_commands(command);
377 my_perror(
"operation failed.");
390 static int do_handle_options(
int argc,
char *argv[])
392 DBUG_ENTER(
"do_handle_options");
394 const char *command_list[MAX_COMMAND_LIMIT + 1];
395 char **saved_argv= argv;
399 int rc,
i, command= -1;
407 if (!(ptr= (
char *) my_malloc((argc + 2) *
sizeof(
char *),
414 for (i= 0; (command_data[
i].name != NULL) && (i < MAX_COMMAND_LIMIT); i++)
415 command_list[i]= (
char *) command_data[
i].name;
416 command_list[
i]= NULL;
418 if ((rc= my_handle_options(&argc, &argv, my_program_long_options,
419 my_program_get_one_option, command_list)))
425 for (i= 0; command_data[
i].name != NULL; i++)
427 if (!strcmp(command_data[i].
name, argv[0]))
439 argv_cmd= (
char **)ptr;
443 argv_cmd[0]= saved_argv[0];
444 memcpy((uchar *) (argv_cmd + 1), (uchar *) (argv),
445 (argc *
sizeof(
char *)));
446 argv_cmd[argc_cmd]= 0;
448 if ((rc= handle_options(&argc_cmd, &argv_cmd,
449 command_data[command].options,
450 command_data[command].get_one_option_func)))
459 opt_login_path= my_strdup(
"client", MYF(MY_WME));
463 DBUG_RETURN(command);
472 static int execute_commands(
int command)
474 DBUG_ENTER(
"execute_commands");
477 if ((rc= check_and_create_login_file()))
480 switch(command_data[command].
id) {
482 verbose_msg(
"Executing set command.\n");
486 case MY_CONFIG_REMOVE :
487 verbose_msg(
"Executing remove command.\n");
488 rc= remove_command();
491 case MY_CONFIG_PRINT :
492 verbose_msg(
"Executing print command.\n");
496 case MY_CONFIG_RESET :
497 verbose_msg(
"Resetting login file.\n");
498 rc= reset_login_file(1);
501 case MY_CONFIG_HELP :
502 verbose_msg(
"Printing usage info.\n");
507 my_perror(
"unknown command.");
511 my_close(g_fd, MYF(MY_WME));
527 static int set_command(
void)
529 DBUG_ENTER(
"set_command");
533 init_dynamic_string(&path_buf,
"", MY_LINE_MAX, MY_LINE_MAX);
534 init_dynamic_string(&file_buf,
"", file_size, 3 * MY_LINE_MAX);
537 opt_password= get_tty_password(NullS);
541 if (read_and_decrypt_file(&file_buf) == -1)
545 dynstr_append(&path_buf,
"[");
547 dynstr_append(&path_buf, opt_login_path);
549 dynstr_append(&path_buf,
"client");
550 dynstr_append(&path_buf,
"]");
554 dynstr_append(&path_buf,
"\nuser = ");
555 dynstr_append(&path_buf, opt_user);
560 dynstr_append(&path_buf,
"\npassword = ");
561 dynstr_append(&path_buf, opt_password);
566 dynstr_append(&path_buf,
"\nhost = ");
567 dynstr_append(&path_buf, opt_host);
572 dynstr_append(&path_buf,
"\nsocket = ");
573 dynstr_append(&path_buf, opt_socket);
578 dynstr_append(&path_buf,
"\nport = ");
579 dynstr_append(&path_buf, opt_port);
582 dynstr_append(&path_buf,
"\n");
585 if (opt_warn && ((locate_login_path (&file_buf, opt_login_path))
589 printf (
"WARNING : \'%s\' path already exists and will be "
590 "overwritten. \n Continue? (Press y|Y for Yes, any "
591 "other key for No) : ",
595 if (choice != (
int)
'y' && choice != (
int)
'Y')
600 remove_login_path(&file_buf, opt_login_path);
603 dynstr_append(&file_buf, path_buf.str);
605 if (encrypt_and_write_file(&file_buf) == -1)
609 dynstr_free(&file_buf);
610 dynstr_free(&path_buf);
614 dynstr_free(&file_buf);
615 dynstr_free(&path_buf);
619 static int remove_command(
void) {
620 DBUG_ENTER(
"remove_command");
624 init_dynamic_string(&path_buf,
"", MY_LINE_MAX, MY_LINE_MAX);
625 init_dynamic_string(&file_buf,
"", file_size, 3 * MY_LINE_MAX);
629 if (read_and_decrypt_file(&file_buf) == -1)
637 ((locate_login_path (&file_buf, opt_login_path)) != NULL) &&
638 (login_path_specified == FALSE)
642 printf (
"WARNING : No login path specified, so options from the default "
643 "login path will be removed.\nContinue? (Press y|Y for Yes, "
644 "any other key for No) : ");
647 if (choice != (
int)
'y' && choice != (
int)
'Y')
651 remove_options(&file_buf, opt_login_path);
653 if (encrypt_and_write_file(&file_buf) == -1)
657 dynstr_free(&file_buf);
658 dynstr_free(&path_buf);
662 dynstr_free(&file_buf);
663 dynstr_free(&path_buf);
676 static int print_command(
void)
678 DBUG_ENTER(
"print_command");
681 init_dynamic_string(&file_buf,
"", file_size, 3 * MY_LINE_MAX);
685 if (read_and_decrypt_file(&file_buf) == -1)
691 print_login_path(&file_buf, opt_login_path);
694 dynstr_free(&file_buf);
698 dynstr_free(&file_buf);
713 static my_bool check_and_create_login_file(
void)
715 DBUG_ENTER(
"check_and_create_login_file");
721 #define S_IRUSR 00400
722 #define S_IWUSR 00200
723 #define S_IRWXU 00700
724 #define S_IRWXG 00070
725 #define S_IRWXO 00007
728 const int access_flag= (O_RDWR | O_BINARY);
729 const ushort create_mode= (S_IRUSR | S_IWUSR );
732 if (! my_default_get_login_file(my_login_file,
sizeof(my_login_file)))
734 my_perror(
"failed to set login file name");
744 if (! (getenv(
"MYSQL_TEST_LOGIN_FILE")))
747 MY_STAT stat_info_dir;
748 char login_dir[FN_REFLEN];
751 dirname_part(login_dir, my_login_file, &size);
753 if (login_dir[-- size] == FN_LIBCHAR)
757 if ( my_stat(login_dir, &stat_info_dir, MYF(0)))
759 verbose_msg(
"%s directory exists.\n", login_dir);
764 verbose_msg(
"%s directory doesn't exist, creating it.\n", login_dir);
765 if (my_mkdir(login_dir, 0, MYF(0)))
767 my_perror(
"failed to create the directory");
775 if (my_stat(my_login_file, &stat_info, MYF(0)))
777 verbose_msg(
"File exists.\n");
779 file_size= stat_info.st_size;
784 if (!(stat_info.st_mode & (S_IXUSR | S_IRWXG | S_IRWXO)))
787 verbose_msg(
"File has the required permission.\nOpening the file.\n");
788 if ((g_fd= my_open(my_login_file, access_flag, MYF(MY_WME))) == -1)
790 my_perror(
"couldn't open the file");
796 verbose_msg(
"File does not have the required permission.\n");
797 printf (
"WARNING : Login file does not have the required"
798 " file permissions.\nPlease set the mode to 600 &"
799 " run the command again.\n");
805 verbose_msg(
"File does not exist.\nCreating login file.\n");
806 if ((g_fd= my_create(my_login_file, create_mode, access_flag,
809 my_perror(
"couldn't create the login file");
814 verbose_msg(
"Login file created.\n");
815 verbose_msg(
"Opening the file.\n");
817 if((g_fd= my_open(my_login_file, access_flag, MYF(MY_WME))) == -1)
819 my_perror(
"couldn't open the file");
829 if(add_header() == -1)
834 if (read_login_key() == -1)
858 static void print_login_path(
DYNAMIC_STRING *file_buf,
const char *path_name)
860 DBUG_ENTER(
"print_login_path");
862 char *start= NULL, *end= NULL,
temp=
'\0';
864 if (file_buf->length == 0)
869 start= file_buf->str;
870 end= file_buf->str + file_buf->length;
874 start= locate_login_path(file_buf, path_name);
879 end= strstr(start,
"\n[");
888 mask_password_and_print(start);
907 static void mask_password_and_print(
char *
buf)
909 DBUG_ENTER(
"mask_password_and_print");
910 const char *password_str=
"\npassword = ", *mask =
"*****";
913 while ((next= strstr(buf, password_str)) != NULL)
915 while ( *buf != 0 && buf != next)
916 putc( *(buf ++), stdout);
917 printf(
"%s", password_str);
918 printf(
"%s\n", mask);
923 while( *buf && *(buf ++) !=
'\n')
931 while ( *buf) putc( *(buf ++), stdout);
933 if (* (buf - 1) !=
'\n')
943 static void remove_options(
DYNAMIC_STRING *file_buf,
const char *path_name)
946 if (!opt_remove_host && !opt_remove_pass && !opt_remove_user
947 && !opt_remove_socket && !opt_remove_port)
949 remove_login_path(file_buf, path_name);
954 remove_option(file_buf, path_name,
"user");
957 remove_option(file_buf, path_name,
"password");
960 remove_option(file_buf, path_name,
"host");
962 if (opt_remove_socket)
963 remove_option(file_buf, path_name,
"socket");
966 remove_option(file_buf, path_name,
"port");
973 static void remove_option(
DYNAMIC_STRING *file_buf,
const char *path_name,
974 const char* option_name)
976 DBUG_ENTER(
"remove_option");
978 char *start= NULL, *end= NULL;
980 int search_len, shift_len;
981 bool option_found= FALSE;
983 search_str= (
char *) my_malloc((uint) strlen(option_name) + 2, MYF(MY_WME));
984 sprintf(search_str,
"\n%s", option_name);
986 if ((start= locate_login_path(file_buf, path_name)) == NULL)
990 end= strstr(start,
"\n[");
993 search_len= end - start;
995 search_len= file_buf->length - (start - file_buf->str);
997 while(search_len > 1)
999 if (!strncmp(start, search_str, strlen(search_str)))
1003 while(*(++ end) !=
'\n')
1011 while( (-- search_len > 1) && (*(++ start) !=
'\n'))
1018 shift_len= file_buf->length - (end - file_buf->str);
1020 file_buf->length -= (end - start);
1023 *(start ++)= *(end ++);
1028 my_free(search_str);
1044 static void remove_login_path(
DYNAMIC_STRING *file_buf,
const char *path_name)
1046 DBUG_ENTER(
"remove_login_path");
1048 char *start=NULL, *end= NULL;
1049 int tot_len, len, diff;
1051 if((start= locate_login_path(file_buf, path_name)) == NULL)
1055 end= strstr(start,
"\n[");
1060 len= ((diff= (start - end)) > 0) ? diff : - diff;
1061 tot_len= file_buf->length - len ;
1066 file_buf->length= ((diff= (file_buf->str - start)) > 0) ? diff : - diff;
1071 *(start ++)= *(end ++);
1074 file_buf->length -= len;
1090 static int reset_login_file(
bool gen_key)
1092 DBUG_ENTER(
"reset_login_file");
1094 if (my_chsize(g_fd, 0, 0, MYF(MY_WME)))
1096 verbose_msg(
"Error while truncating the file.\n");
1101 if (my_seek(g_fd, 0L, SEEK_SET, MYF(MY_WME) == MY_FILEPOS_ERROR))
1107 if (add_header() == -1)
1129 static char* locate_login_path(
DYNAMIC_STRING *file_buf,
const char *path_name)
1131 DBUG_ENTER(
"locate_login_path");
1136 init_dynamic_string(&dy_path_name,
"", 512, 512);
1138 dynstr_append(&dy_path_name,
"\n[");
1139 dynstr_append(&dy_path_name, path_name);
1140 dynstr_append(&dy_path_name,
"]");
1143 if (file_buf->str == strstr(file_buf->str, dy_path_name.str + 1))
1144 addr= file_buf->str;
1148 addr= strstr(file_buf->str, dy_path_name.str);
1153 dynstr_free(&dy_path_name);
1176 DBUG_ENTER(
"encrypt_and_write_file");
1178 my_bool done= FALSE;
1179 char cipher[MY_LINE_MAX], *tmp= NULL;
1180 uint bytes_read=0, len= 0;
1183 if (reset_login_file(0) == -1)
1187 if (my_seek(g_fd, MY_LOGIN_HEADER_LEN, SEEK_SET, MYF(MY_WME))
1188 != (MY_LOGIN_HEADER_LEN))
1192 tmp= &file_buf->str[bytes_read];
1198 while(*tmp++ !=
'\n')
1199 if (len < (file_buf->length - bytes_read))
1210 if ((enc_len= encrypt_buffer(&file_buf->str[bytes_read],
1211 ++ len, cipher + MAX_CIPHER_STORE_LEN)) < 0)
1216 if (enc_len > MY_LINE_MAX)
1220 int4store(cipher, enc_len);
1222 if ((my_write(g_fd, (
const uchar *)cipher, enc_len + MAX_CIPHER_STORE_LEN,
1223 MYF(MY_WME))) != (enc_len + MAX_CIPHER_STORE_LEN))
1227 verbose_msg(
"Successfully written encrypted data to the login file.\n");
1230 file_size= bytes_read;
1235 my_perror(
"couldn't encrypt the file");
1253 DBUG_ENTER(
"read_and_decrypt_file");
1255 char cipher[MY_LINE_MAX], plain[MY_LINE_MAX];
1256 uchar len_buf[MAX_CIPHER_STORE_LEN];
1257 int cipher_len= 0, dec_len= 0;
1260 if (my_seek(g_fd, MY_LOGIN_HEADER_LEN, SEEK_SET, MYF(MY_WME))
1261 != (MY_LOGIN_HEADER_LEN))
1265 while (my_read(g_fd, len_buf, MAX_CIPHER_STORE_LEN,
1266 MYF(MY_WME)) == MAX_CIPHER_STORE_LEN)
1268 cipher_len= sint4korr(len_buf);
1270 if (cipher_len > MY_LINE_MAX)
1274 if ((
int) my_read(g_fd, (uchar *) cipher, cipher_len, MYF(MY_WME)) == cipher_len)
1276 if ((dec_len= decrypt_buffer(cipher, cipher_len, plain)) < 0)
1280 dynstr_append(file_buf, plain);
1284 verbose_msg(
"Successfully decrypted the login file.\n");
1288 my_perror(
"couldn't decrypt the file");
1304 static int encrypt_buffer(
const char *plain,
int plain_len,
char cipher[])
1306 DBUG_ENTER(
"encrypt_buffer");
1309 aes_len= my_aes_get_size(plain_len);
1311 if (my_aes_encrypt(plain, plain_len, cipher, my_key, LOGIN_KEY_LEN) == aes_len)
1312 DBUG_RETURN(aes_len);
1314 verbose_msg(
"Error! Couldn't encrypt the buffer.\n");
1330 static int decrypt_buffer(
const char *cipher,
int cipher_len,
char plain[])
1332 DBUG_ENTER(
"decrypt_buffer");
1335 if ((aes_length= my_aes_decrypt(cipher, cipher_len, (
char *) plain,
1336 my_key, LOGIN_KEY_LEN)) > 0)
1337 DBUG_RETURN(aes_length);
1339 verbose_msg(
"Error! Couldn't decrypt the buffer.\n");
1352 static int add_header(
void)
1354 DBUG_ENTER(
"add_header");
1357 const char unused[]= {
'\0',
'\0',
'\0',
'\0'};
1360 if ((my_write(g_fd, (
const uchar *) unused, 4, MYF(MY_WME))) != 4)
1364 if ((my_write(g_fd, (
const uchar *)my_key, LOGIN_KEY_LEN, MYF(MY_WME)))
1368 verbose_msg(
"Key successfully written to the file.\n");
1369 DBUG_RETURN(MY_LOGIN_HEADER_LEN);
1372 my_perror(
"file write operation failed");
1383 DBUG_ENTER(
"generate_login_key");
1386 verbose_msg(
"Generating a new key.\n");
1388 for (uint i= 0; i < LOGIN_KEY_LEN; i++)
1389 my_key[i]= (
char)((int)(my_rnd_ssl(&rnd) * 100000) % 32);
1401 static int read_login_key(
void)
1403 DBUG_ENTER(
"read_login_key");
1405 verbose_msg(
"Reading the login key.\n");
1407 if (my_seek(g_fd, 4, SEEK_SET, MYF(MY_WME)) != 4)
1410 if (my_read(g_fd, (uchar *)my_key, LOGIN_KEY_LEN, MYF(MY_WME))
1414 verbose_msg(
"Login key read successfully.\n");
1418 my_perror(
"file read operation failed");
1423 static void verbose_msg(
const char *
fmt, ...)
1425 DBUG_ENTER(
"verbose_msg");
1431 va_start(args, fmt);
1432 vfprintf(stderr, fmt, args);
1439 static void my_perror(
const char *
msg)
1441 char errbuf[MYSYS_STRERROR_SIZE];
1444 fprintf(stderr,
"%s\n", (msg) ? msg :
"");
1446 fprintf(stderr,
"%s : %s\n", (msg) ? msg :
"",
1447 my_strerror(errbuf,
sizeof(errbuf), errno));
1452 static void usage_command(
int command)
1455 puts(ORACLE_WELCOME_COPYRIGHT_NOTICE(
"2012"));
1456 puts(
"MySQL Configuration Utility.");
1457 printf(
"\nDescription: %s\n", command_data[command].description);
1458 printf(
"Usage: %s [program options] [%s [command options]]\n",
1459 my_progname, command_data[command].
name);
1460 my_print_help(command_data[command].options);
1461 my_print_variables(command_data[command].options);
1465 static void usage_program(
void)
1468 puts(ORACLE_WELCOME_COPYRIGHT_NOTICE(
"2012"));
1469 puts(
"MySQL Configuration Utility.");
1470 printf(
"Usage: %s [program options] [command [command options]]\n",
1472 my_print_help(my_program_long_options);
1473 my_print_variables(my_program_long_options);
1474 puts(
"\nWhere command can be any one of the following :\n\
1475 set [command options] Sets user name/password/host name/socket/port\n\
1476 for a given login path (section).\n\
1477 remove [command options] Remove a login path from the login file.\n\
1478 print [command options] Print all the options for a specified\n\
1480 reset [command options] Deletes the contents of the login file.\n\
1481 help Display this usage/help information.\n");
1485 static void print_version(
void) {
1486 printf (
"%s Ver %s Distrib %s, for %s on %s\n", my_progname,
1487 MYSQL_CONFIG_EDITOR_VERSION, MYSQL_SERVER_VERSION,
1488 SYSTEM_TYPE, MACHINE_TYPE);