MySQL 8.0.39
Source Code Documentation
mysql_config_editor.cc File Reference

MySQL Configuration Utility. More...

#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/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 "print_version.h"
#include "welcome_copyright_notice.h"

Classes

struct  my_command_data
 

Macros

#define MY_LINE_MAX   4096
 
#define MAX_COMMAND_LIMIT   100
 
#define MY_LOGIN_HEADER_LEN   (4 + LOGIN_KEY_LEN)
 

Enumerations

enum  commands {
  MY_CONFIG_SET , MY_CONFIG_REMOVE , MY_CONFIG_PRINT , MY_CONFIG_RESET ,
  MY_CONFIG_HELP , ADMIN_ERROR , ADMIN_CREATE , ADMIN_DROP ,
  ADMIN_SHUTDOWN , ADMIN_RELOAD , ADMIN_REFRESH , ADMIN_VER ,
  ADMIN_PROCESSLIST , ADMIN_STATUS , ADMIN_KILL , ADMIN_DEBUG ,
  ADMIN_VARIABLES , ADMIN_FLUSH_LOGS , ADMIN_FLUSH_HOSTS , ADMIN_FLUSH_TABLES ,
  ADMIN_PASSWORD , ADMIN_PING , ADMIN_EXTENDED_STATUS , ADMIN_FLUSH_STATUS ,
  ADMIN_FLUSH_PRIVILEGES , ADMIN_START_REPLICA , ADMIN_STOP_REPLICA , ADMIN_FLUSH_THREADS ,
  ADMIN_START_SLAVE , ADMIN_STOP_SLAVE
}
 

Functions

static int execute_commands (int command)
 
static int set_command (void)
 Execute 'set' command. More...
 
static int remove_command (void)
 
static int print_command (void)
 Execute 'print' command. More...
 
static void print_login_path (DYNAMIC_STRING *file_buf, const char *path_name)
 Print options under the specified login path. More...
 
static void remove_login_path (DYNAMIC_STRING *file_buf, const char *path_name)
 Remove the specified login path from the login file. More...
 
static char * locate_login_path (DYNAMIC_STRING *file_buf, const char *path_name)
 Find the specified login path in the login file buffer and return the starting address. More...
 
static bool check_and_create_login_file (void)
 Create the login file if it does not exist, check and set its permissions and modes. More...
 
static void mask_password_and_print (char *buf)
 Print the specified buffer by masking the actual password string. More...
 
static int reset_login_file (bool gen_key)
 Remove all the contents from the login file. More...
 
static int encrypt_buffer (const char *plain, int plain_len, char cipher[], const int aes_len)
 Encrypt the given plain text. More...
 
static int decrypt_buffer (const char *cipher, int cipher_len, char plain[])
 Decrypt the given cipher text. More...
 
static int encrypt_and_write_file (DYNAMIC_STRING *file_buf)
 Encrypt the file buffer and write it to the login file. More...
 
static int read_and_decrypt_file (DYNAMIC_STRING *file_buf)
 Read the login file, unscramble its contents and store them into the file buffer. More...
 
static int do_handle_options (int argc, char *argv[])
 Handle all the command line arguments. More...
 
static void remove_options (DYNAMIC_STRING *file_buf, const char *path_name)
 Remove multiple options from a login path. More...
 
static void remove_option (DYNAMIC_STRING *file_buf, const char *path_name, const char *option_name)
 Remove an option from a login path. More...
 
bool generate_login_key ()
 Algorithm to generate key. More...
 
static int read_login_key (void)
 Read the stored login key. More...
 
static int add_header (void)
 Add unused bytes alongwith the to the login key to the login file. More...
 
static void my_perror (const char *msg)
 
static void verbose_msg (const char *fmt,...)
 
static void usage_program (void)
 
static void usage_command (int command)
 
bool get_one_option (int optid, const struct my_option *opt, char *argument)
 
static bool my_program_get_one_option (int optid, const struct my_option *opt, char *argument)
 
static bool my_set_command_get_one_option (int optid, const struct my_option *, char *)
 
static bool my_remove_command_get_one_option (int optid, const struct my_option *, char *)
 
static bool my_print_command_get_one_option (int optid, const struct my_option *, char *)
 
static bool my_reset_command_get_one_option (int optid, const struct my_option *, char *)
 
int main (int argc, char *argv[])
 

Variables

static int g_fd
 
static size_t file_size
 
static const char * opt_user = nullptr
 
static const char * opt_password = nullptr
 
static const char * opt_host = nullptr
 
static const char * opt_login_path = "client"
 
static const char * opt_socket = nullptr
 
static const char * opt_port = nullptr
 
static char my_login_file [FN_REFLEN]
 
static char my_key [LOGIN_KEY_LEN]
 
static bool opt_verbose
 
static bool opt_all
 
static bool tty_password = false
 
static bool opt_warn
 
static bool opt_remove_host
 
static bool opt_remove_pass
 
static bool opt_remove_user
 
static bool opt_remove_socket
 
static bool opt_remove_port
 
static bool login_path_specified = false
 
static struct my_option my_program_long_options []
 
static struct my_option my_set_command_options []
 
static struct my_option my_remove_command_options []
 
static struct my_option my_print_command_options []
 
static struct my_option my_reset_command_options []
 
static struct my_option my_help_command_options []
 
static struct my_command_data command_data []
 

Detailed Description

MySQL Configuration Utility.

Macro Definition Documentation

◆ MAX_COMMAND_LIMIT

#define MAX_COMMAND_LIMIT   100

◆ MY_LINE_MAX

#define MY_LINE_MAX   4096

◆ MY_LOGIN_HEADER_LEN

#define MY_LOGIN_HEADER_LEN   (4 + LOGIN_KEY_LEN)

Enumeration Type Documentation

◆ commands

enum commands
Enumerator
MY_CONFIG_SET 
MY_CONFIG_REMOVE 
MY_CONFIG_PRINT 
MY_CONFIG_RESET 
MY_CONFIG_HELP 
ADMIN_ERROR 
ADMIN_CREATE 
ADMIN_DROP 
ADMIN_SHUTDOWN 
ADMIN_RELOAD 
ADMIN_REFRESH 
ADMIN_VER 
ADMIN_PROCESSLIST 
ADMIN_STATUS 
ADMIN_KILL 
ADMIN_DEBUG 
ADMIN_VARIABLES 
ADMIN_FLUSH_LOGS 
ADMIN_FLUSH_HOSTS 
ADMIN_FLUSH_TABLES 
ADMIN_PASSWORD 
ADMIN_PING 
ADMIN_EXTENDED_STATUS 
ADMIN_FLUSH_STATUS 
ADMIN_FLUSH_PRIVILEGES 
ADMIN_START_REPLICA 
ADMIN_STOP_REPLICA 
ADMIN_FLUSH_THREADS 
ADMIN_START_SLAVE 
ADMIN_STOP_SLAVE 

Function Documentation

◆ add_header()

static int add_header ( void  )
static

Add unused bytes alongwith the to the login key to the login file.

Returns
-1 if error encountered, length written, otherwise.

◆ check_and_create_login_file()

static bool check_and_create_login_file ( void  )
static

Create the login file if it does not exist, check and set its permissions and modes.

Returns
true Error false Success

◆ decrypt_buffer()

static int decrypt_buffer ( const char *  cipher,
int  cipher_len,
char  plain[] 
)
static

Decrypt the given cipher text.

Parameters
[in]cipherCipher text to be decrypted.
[in]cipher_lenLength of the cipher text.
[out]plainDecrypted plain text.
Returns
-1 if error encountered, length decrypted, otherwise.

◆ do_handle_options()

static int do_handle_options ( int  argc,
char *  argv[] 
)
static

Handle all the command line arguments.

program_name [program options] [command [command options]]

◆ encrypt_and_write_file()

static int encrypt_and_write_file ( DYNAMIC_STRING file_buf)
static

Encrypt the file buffer and write it to the login file.

Parameters
[in]file_bufBuffer storing the unscrambled login file contents.
Returns
-1 Error 0 Success
Note
The contents of the file buffer are encrypted on a line-by-line basis with each line having the following format : [<first 4 bytes store cipher-length> |<Next cipher-length bytes store actual cipher>]

◆ encrypt_buffer()

static int encrypt_buffer ( const char *  plain,
int  plain_len,
char  cipher[],
const int  aes_len 
)
static

Encrypt the given plain text.

Parameters
plainPlain text to be encrypted
plain_lenLength of the plain text
[out]cipherEncrypted cipher text
aes_lenLength of the cypher
Returns
-1 if error encountered, length encrypted, otherwise.

◆ execute_commands()

static int execute_commands ( int  command)
static

◆ generate_login_key()

bool generate_login_key ( void  )

Algorithm to generate key.

Return values
trueerror
falsesuccess

◆ get_one_option()

bool get_one_option ( int  optid,
const struct my_option opt,
char *  argument 
)

◆ locate_login_path()

static char * locate_login_path ( DYNAMIC_STRING file_buf,
const char *  path_name 
)
static

Find the specified login path in the login file buffer and return the starting address.

Parameters
[in]file_bufBuffer storing the unscrambled login file contents.
[in]path_namePath name.
Returns
If found, the starting address of the login path, NULL otherwise.

◆ main()

int main ( int  argc,
char *  argv[] 
)

◆ mask_password_and_print()

static void mask_password_and_print ( char *  buf)
static

Print the specified buffer by masking the actual password string.

Parameters
[in]bufBuffer to be printed.

◆ my_perror()

static void my_perror ( const char *  msg)
static

◆ my_print_command_get_one_option()

static bool my_print_command_get_one_option ( int  optid,
const struct my_option ,
char *   
)
static

◆ my_program_get_one_option()

static bool my_program_get_one_option ( int  optid,
const struct my_option opt,
char *  argument 
)
static

◆ my_remove_command_get_one_option()

static bool my_remove_command_get_one_option ( int  optid,
const struct my_option ,
char *   
)
static

◆ my_reset_command_get_one_option()

static bool my_reset_command_get_one_option ( int  optid,
const struct my_option ,
char *   
)
static

◆ my_set_command_get_one_option()

static bool my_set_command_get_one_option ( int  optid,
const struct my_option ,
char *   
)
static

◆ print_command()

static int print_command ( void  )
static

Execute 'print' command.

Returns
-1 Error 0 Success

◆ print_login_path()

static void print_login_path ( DYNAMIC_STRING file_buf,
const char *  path_name 
)
static

Print options under the specified login path.

If '–all' option is used, print all the optins stored in the login file.

Parameters
[in]file_bufBuffer storing the unscrambled login file contents.
[in]path_namePath name.

◆ read_and_decrypt_file()

static int read_and_decrypt_file ( DYNAMIC_STRING file_buf)
static

Read the login file, unscramble its contents and store them into the file buffer.

Parameters
[in]file_bufBuffer for storing the unscrambled login file contents.
Returns
-1 Error 0 Success

◆ read_login_key()

static int read_login_key ( void  )
static

Read the stored login key.

Returns
-1 Error 0 Success

◆ remove_command()

static int remove_command ( void  )
static

◆ remove_login_path()

static void remove_login_path ( DYNAMIC_STRING file_buf,
const char *  path_name 
)
static

Remove the specified login path from the login file.

Parameters
[in]file_bufBuffer storing the unscrambled login file contents.
[in]path_namePath name.

◆ remove_option()

static void remove_option ( DYNAMIC_STRING file_buf,
const char *  path_name,
const char *  option_name 
)
static

Remove an option from a login path.

◆ remove_options()

static void remove_options ( DYNAMIC_STRING file_buf,
const char *  path_name 
)
static

Remove multiple options from a login path.

◆ reset_login_file()

static int reset_login_file ( bool  gen_key)
static

Remove all the contents from the login file.

Parameters
[in]gen_keyFlag to control the generation of a new key.
Returns
-1 Error 0 Success

◆ set_command()

static int set_command ( void  )
static

Execute 'set' command.

Returns
-1 Error 0 Success

◆ usage_command()

static void usage_command ( int  command)
static

◆ usage_program()

static void usage_program ( void  )
static

◆ verbose_msg()

static void verbose_msg ( const char *  fmt,
  ... 
)
static

Variable Documentation

◆ command_data

struct my_command_data command_data[]
static
Initial value:
= {
{MY_CONFIG_SET, "set", "Write a login path to the login file.",
{MY_CONFIG_REMOVE, "remove", "Remove a login path from the login file.",
{MY_CONFIG_PRINT, "print",
"Print the contents of login file in unencrypted form.",
{MY_CONFIG_RESET, "reset",
"Empty the contents of the login file. The file is created\n"
"if it does not exist.",
{MY_CONFIG_HELP, "help", "Display a help message and exit.",
{0, nullptr, nullptr, nullptr, nullptr}}
static struct my_option my_remove_command_options[]
Definition: mysql_config_editor.cc:184
@ MY_CONFIG_PRINT
Definition: mysql_config_editor.cc:119
@ MY_CONFIG_REMOVE
Definition: mysql_config_editor.cc:118
@ MY_CONFIG_RESET
Definition: mysql_config_editor.cc:120
@ MY_CONFIG_HELP
Definition: mysql_config_editor.cc:121
@ MY_CONFIG_SET
Definition: mysql_config_editor.cc:117
static struct my_option my_help_command_options[]
Definition: mysql_config_editor.cc:236
static bool my_remove_command_get_one_option(int optid, const struct my_option *, char *)
Definition: mysql_config_editor.cc:285
static bool my_print_command_get_one_option(int optid, const struct my_option *, char *)
Definition: mysql_config_editor.cc:306
static bool my_set_command_get_one_option(int optid, const struct my_option *, char *)
Definition: mysql_config_editor.cc:261
static struct my_option my_set_command_options[]
Definition: mysql_config_editor.cc:156
static bool my_reset_command_get_one_option(int optid, const struct my_option *, char *)
Definition: mysql_config_editor.cc:327
static struct my_option my_reset_command_options[]
Definition: mysql_config_editor.cc:229
static struct my_option my_print_command_options[]
Definition: mysql_config_editor.cc:214

◆ file_size

size_t file_size
static

◆ g_fd

int g_fd
static

◆ login_path_specified

bool login_path_specified = false
static

◆ my_help_command_options

struct my_option my_help_command_options[]
static
Initial value:
= {
{nullptr, 0, nullptr, nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0,
0, nullptr, 0, nullptr}}
#define GET_NO_ARG
Definition: my_getopt.h:44
@ NO_ARG
Definition: my_getopt.h:81

◆ my_key

char my_key[LOGIN_KEY_LEN]
static

◆ my_login_file

char my_login_file[FN_REFLEN]
static

◆ my_print_command_options

struct my_option my_print_command_options[]
static
Initial value:
= {
{"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)",
nullptr, 0, nullptr},
{nullptr, 0, nullptr, nullptr, nullptr, nullptr, GET_NO_ARG, NO_ARG, 0, 0,
0, nullptr, 0, nullptr}}
@ OPT_CONFIG_ALL
Definition: client_priv.h:166
@ REQUIRED_ARG
Definition: my_getopt.h:81
#define GET_STR
Definition: my_getopt.h:52
#define GET_BOOL
Definition: my_getopt.h:45
static const char * opt_login_path
Definition: mysql_config_editor.cc:73
static bool opt_all
Definition: mysql_config_editor.cc:79

◆ my_program_long_options

struct my_option my_program_long_options[]
static
Initial value:
= {
{"debug", '#', "Output debug log. Often this is 'd:t:o,filename'.", nullptr,
nullptr, nullptr, GET_STR, OPT_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},
{"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}}
@ OPT_ARG
Definition: my_getopt.h:81
static bool opt_verbose
Definition: mysql_config_editor.cc:79

◆ my_remove_command_options

struct my_option my_remove_command_options[]
static
Initial value:
= {
{"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)",
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.",
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}}
static bool opt_warn
Definition: mysql_config_editor.cc:80
static bool opt_remove_port
Definition: mysql_config_editor.cc:81
static bool opt_remove_host
Definition: mysql_config_editor.cc:80
static bool opt_remove_socket
Definition: mysql_config_editor.cc:81
static bool opt_remove_pass
Definition: mysql_config_editor.cc:80
static bool opt_remove_user
Definition: mysql_config_editor.cc:81

◆ my_reset_command_options

struct my_option my_reset_command_options[]
static
Initial value:
= {
{"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}}

◆ my_set_command_options

struct my_option my_set_command_options[]
static
Initial value:
= {
{"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)",
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}}
static const char * opt_host
Definition: mysql_config_editor.cc:73
static const char * opt_port
Definition: mysql_config_editor.cc:74
static const char * opt_socket
Definition: mysql_config_editor.cc:74
static const char * opt_user
Definition: mysql_config_editor.cc:72

◆ opt_all

bool opt_all
static

◆ opt_host

const char * opt_host = nullptr
static

◆ opt_login_path

const char * opt_login_path = "client"
static

◆ opt_password

const char * opt_password = nullptr
static

◆ opt_port

const char * opt_port = nullptr
static

◆ opt_remove_host

bool opt_remove_host
static

◆ opt_remove_pass

bool opt_remove_pass
static

◆ opt_remove_port

bool opt_remove_port
static

◆ opt_remove_socket

bool opt_remove_socket
static

◆ opt_remove_user

bool opt_remove_user
static

◆ opt_socket

const char * opt_socket = nullptr
static

◆ opt_user

const char* opt_user = nullptr
static

◆ opt_verbose

bool opt_verbose
static

◆ opt_warn

bool opt_warn
static

◆ tty_password

bool tty_password = false
static