3131#include " my_aes.h"
3232#include " client_priv.h"
3333#include " my_default.h"
34+ #include " my_default_priv.h"
3435
3536#define MYSQL_CONFIG_EDITOR_VERSION " 1.0"
3637#define MY_LINE_MAX 4096
3738/*
3839 Header length for the login file.
39- 4-byte (unused) + MY_LOGIN_KEY_LEN
40+ 4-byte (unused) + LOGIN_KEY_LEN
4041 */
41- #define MY_LOGIN_HEADER_LEN (4 + MY_LOGIN_KEY_LEN )
42+ #define MY_LOGIN_HEADER_LEN (4 + LOGIN_KEY_LEN )
4243
4344static int g_fd;
4445
@@ -50,9 +51,10 @@ static size_t file_size;
5051static char *opt_user= NULL , *opt_password= NULL , *opt_host=NULL ,
5152 *opt_login_path= NULL ;
5253
53- static char my_key[MY_LOGIN_KEY_LEN];
54+ static char my_login_file[FN_REFLEN];
55+ static char my_key[LOGIN_KEY_LEN];
5456
55- static my_bool opt_verbose, opt_all, tty_password= 0 ;
57+ static my_bool opt_verbose, opt_all, tty_password= 0 , opt_warn ;
5658
5759int execute_commands (int argc, char **argv);
5860static void print_login_path (DYNAMIC_STRING *file_buf, const char *path_name);
@@ -125,6 +127,9 @@ static struct my_option my_long_options[] =
125127 &opt_verbose, 0 , GET_BOOL, NO_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
126128 {" version" , ' V' , " Output version information and exit." , 0 , 0 , 0 , GET_NO_ARG,
127129 NO_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
130+ {" warn" , ' w' , " Warn and ask for confirmation if set command attempts to "
131+ " overwrite an existing login path (enabled by default)." ,
132+ &opt_warn, &opt_warn, 0 , GET_BOOL, NO_ARG, 1 , 0 , 0 , 0 , 0 , 0 },
128133 { 0 , 0 , 0 , 0 , 0 , 0 , GET_NO_ARG, NO_ARG, 0 , 0 , 0 , 0 , 0 , 0 }
129134};
130135
@@ -135,7 +140,7 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
135140{
136141 switch (optid) {
137142 case ' #' :
138- DBUG_PUSH (argument ? argument : " d:t:o,/tmp/mysqladmin .trace" );
143+ DBUG_PUSH (argument ? argument : " d:t:o,/tmp/mysql_config_editor .trace" );
139144 break ;
140145 case ' p' :
141146 tty_password= 1 ;
@@ -234,7 +239,22 @@ int execute_commands(int argc, char **argv)
234239
235240 dynstr_append (&path_buf, " \n " );
236241
237- /* Check and remove opt_login_path if it already exists. */
242+ /* Warn if login path already exists */
243+ if (opt_warn && ((locate_login_path (&file_buf, opt_login_path))
244+ != NULL ))
245+ {
246+ int choice;
247+ printf (" WARNING : \' %s\' path already exists and will be "
248+ " overwritten. \n Continue? (Press y|Y for Yes, any "
249+ " other key for No) : " ,
250+ opt_login_path);
251+ choice= getchar ();
252+
253+ if (choice != (int ) ' y' && choice != (int ) ' Y' )
254+ break ; /* skip */
255+ }
256+
257+ /* Remove the login path. */
238258 remove_login_path (&file_buf, opt_login_path);
239259
240260 /* Append the new login path to the file buffer. */
@@ -329,7 +349,7 @@ static my_bool check_and_create_login_file(void)
329349 const ushort create_mode_all= (S_IRWXU | S_IRWXG | S_IRWXO);
330350
331351 /* Get the login file name. */
332- if (! set_login_file_name ( ))
352+ if (! my_default_get_login_file (my_login_file, sizeof (my_login_file) ))
333353 {
334354 verbose_msg (" Error! Failed to set login file name.\n " );
335355 goto error;
@@ -808,7 +828,7 @@ static int encrypt_buffer(const char *plain, int plain_len, char cipher[])
808828
809829 aes_len= my_aes_get_size (plain_len);
810830
811- if (my_aes_encrypt (plain, plain_len, cipher, my_key, MY_LOGIN_KEY_LEN ) == aes_len)
831+ if (my_aes_encrypt (plain, plain_len, cipher, my_key, LOGIN_KEY_LEN ) == aes_len)
812832 {
813833 DBUG_RETURN (aes_len);
814834 }
@@ -837,7 +857,7 @@ static int decrypt_buffer(const char *cipher, int cipher_len, char plain[])
837857 int aes_length;
838858
839859 if ((aes_length= my_aes_decrypt (cipher, cipher_len, (char *) plain,
840- my_key, MY_LOGIN_KEY_LEN )) > 0 )
860+ my_key, LOGIN_KEY_LEN )) > 0 )
841861 {
842862 DBUG_RETURN (aes_length);
843863 }
@@ -873,8 +893,8 @@ int add_header(void)
873893 }
874894
875895 /* Write the login key. */
876- if ((my_write (g_fd, (const uchar *)my_key, MY_LOGIN_KEY_LEN , MYF (MY_WME)))
877- != MY_LOGIN_KEY_LEN )
896+ if ((my_write (g_fd, (const uchar *)my_key, LOGIN_KEY_LEN , MYF (MY_WME)))
897+ != LOGIN_KEY_LEN )
878898 {
879899 verbose_msg (" Error! couldn't write to the file.\n " );
880900 goto error;
@@ -899,7 +919,7 @@ void generate_login_key()
899919
900920 verbose_msg (" Generating a new key.\n " );
901921 /* Get a sequence of random non-printable ASCII */
902- for (uint i= 0 ; i < MY_LOGIN_KEY_LEN ; i++)
922+ for (uint i= 0 ; i < LOGIN_KEY_LEN ; i++)
903923 my_key[i]= (char )((int )(my_rnd_ssl (&rnd) * 100000 ) % 32 );
904924
905925 DBUG_VOID_RETURN;
@@ -921,8 +941,8 @@ int read_login_key(void)
921941 if (my_seek (g_fd, 4 , SEEK_SET, MYF (MY_WME)) != 4 )
922942 DBUG_RETURN (-1 ); /* Error while seeking. */
923943
924- if (my_read (g_fd, (uchar *)my_key, MY_LOGIN_KEY_LEN , MYF (MY_WME))
925- != MY_LOGIN_KEY_LEN )
944+ if (my_read (g_fd, (uchar *)my_key, LOGIN_KEY_LEN , MYF (MY_WME))
945+ != LOGIN_KEY_LEN )
926946 {
927947 verbose_msg (" Failed to read login key.\n " );
928948 DBUG_RETURN (-1 );
0 commit comments