Commit 11f86cb9 authored by unknown's avatar unknown

--user option added to mysqlmanager


server-tools/instance-manager/mysql_connection.cc:
  removed unsed method
server-tools/instance-manager/mysqlmanager.cc:
  add --user option handling
server-tools/instance-manager/options.cc:
  --user option added
server-tools/instance-manager/options.h:
  user option added + some cleanup
parent 55a06409
...@@ -82,7 +82,6 @@ class Mysql_connection_thread: public Mysql_connection_thread_args ...@@ -82,7 +82,6 @@ class Mysql_connection_thread: public Mysql_connection_thread_args
private: private:
/* Names are conventionally the same as in mysqld */ /* Names are conventionally the same as in mysqld */
int check_connection(); int check_connection();
int check_user(const char *user, const char *password);
int do_command(); int do_command();
int dispatch_command(enum enum_server_command command, int dispatch_command(enum enum_server_command command,
const char *text, uint len); const char *text, uint len);
...@@ -287,12 +286,6 @@ int Mysql_connection_thread::check_connection() ...@@ -287,12 +286,6 @@ int Mysql_connection_thread::check_connection()
} }
int Mysql_connection_thread::check_user(const char *user, const char *password)
{
return 0;
}
int Mysql_connection_thread::do_command() int Mysql_connection_thread::do_command()
{ {
char *packet; char *packet;
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include <my_sys.h> #include <my_sys.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -54,6 +56,8 @@ ...@@ -54,6 +56,8 @@
static void init_environment(char *progname); static void init_environment(char *progname);
static void daemonize(const char *log_file_name); static void daemonize(const char *log_file_name);
static void angel(const Options &options); static void angel(const Options &options);
static struct passwd *check_user(const char *user);
static int set_user(const char *user, struct passwd *user_info);
/* /*
...@@ -68,7 +72,19 @@ int main(int argc, char *argv[]) ...@@ -68,7 +72,19 @@ int main(int argc, char *argv[])
{ {
init_environment(argv[0]); init_environment(argv[0]);
Options options; Options options;
struct passwd *user_info;
options.load(argc, argv); options.load(argc, argv);
if ((user_info= check_user(options.user)))
{
if (set_user(options.user, user_info))
{
options.cleanup();
return 1;
}
}
if (options.run_as_service) if (options.run_as_service)
{ {
/* forks, and returns only in child */ /* forks, and returns only in child */
...@@ -84,6 +100,74 @@ int main(int argc, char *argv[]) ...@@ -84,6 +100,74 @@ int main(int argc, char *argv[])
/******************* Auxilary functions implementation **********************/ /******************* Auxilary functions implementation **********************/
/* Change to run as another user if started with --user */
static struct passwd *check_user(const char *user)
{
#if !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__)
struct passwd *user_info;
uid_t user_id= geteuid();
/* Don't bother if we aren't superuser */
if (user_id)
{
if (user)
{
/* Don't give a warning, if real user is same as given with --user */
user_info= getpwnam(user);
if ((!user_info || user_id != user_info->pw_uid))
log_info("One can only use the --user switch if running as root\n");
}
return NULL;
}
if (!user)
{
log_info("You are running mysqlmanager as root! This might introduce security problems. It is safer to use --user option istead.\n");
return NULL;
}
if (!strcmp(user, "root"))
return NULL; /* Avoid problem with dynamic libraries */
if (!(user_info= getpwnam(user)))
{
/* Allow a numeric uid to be used */
const char *pos;
for (pos= user; my_isdigit(default_charset_info, *pos); pos++) ;
if (*pos) /* Not numeric id */
goto err;
if (!(user_info= getpwuid(atoi(user))))
goto err;
else
return user_info;
}
else
return user_info;
err:
log_error("Fatal error: Can't change to run as user '%s' ; Please check that the user exists!\n", user);
#endif
return NULL;
}
static int set_user(const char *user, struct passwd *user_info)
{
DBUG_ASSERT(user_info);
#ifdef HAVE_INITGROUPS
initgroups((char*) user,user_info->pw_gid);
#endif
if (setgid(user_info->pw_gid) == -1)
{
log_error("setgid() failed");
return 1;
}
if (setuid(user_info->pw_uid) == -1)
{
log_error("setuid() failed");
return 1;
}
return 0;
}
/* /*
Init environment, common for daemon and non-daemon Init environment, common for daemon and non-daemon
......
...@@ -35,7 +35,8 @@ const char *Options::pid_file_name= QUOTE(DEFAULT_PID_FILE_NAME); ...@@ -35,7 +35,8 @@ const char *Options::pid_file_name= QUOTE(DEFAULT_PID_FILE_NAME);
const char *Options::socket_file_name= QUOTE(DEFAULT_SOCKET_FILE_NAME); const char *Options::socket_file_name= QUOTE(DEFAULT_SOCKET_FILE_NAME);
const char *Options::password_file_name= QUOTE(DEFAULT_PASSWORD_FILE_NAME); const char *Options::password_file_name= QUOTE(DEFAULT_PASSWORD_FILE_NAME);
const char *Options::default_mysqld_path= QUOTE(DEFAULT_MYSQLD_PATH); const char *Options::default_mysqld_path= QUOTE(DEFAULT_MYSQLD_PATH);
const char *Options::bind_address= 0; /* No default value */ const char *Options::bind_address= 0; /* No default value */
const char *Options::user= 0; /* No default value */
uint Options::monitoring_interval= DEFAULT_MONITORING_INTERVAL; uint Options::monitoring_interval= DEFAULT_MONITORING_INTERVAL;
uint Options::port_number= DEFAULT_PORT; uint Options::port_number= DEFAULT_PORT;
/* just to declare */ /* just to declare */
...@@ -54,7 +55,6 @@ enum options { ...@@ -54,7 +55,6 @@ enum options {
OPT_MYSQLD_PATH, OPT_MYSQLD_PATH,
OPT_RUN_AS_SERVICE, OPT_RUN_AS_SERVICE,
OPT_USER, OPT_USER,
OPT_PASSWORD,
OPT_MONITORING_INTERVAL, OPT_MONITORING_INTERVAL,
OPT_PORT, OPT_PORT,
OPT_BIND_ADDRESS OPT_BIND_ADDRESS
...@@ -107,6 +107,11 @@ static struct my_option my_long_options[] = ...@@ -107,6 +107,11 @@ static struct my_option my_long_options[] =
"Daemonize and start angel process.", (gptr *) &Options::run_as_service, "Daemonize and start angel process.", (gptr *) &Options::run_as_service,
0, 0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 }, 0, 0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 },
{ "user", OPT_USER, "Username to start mysqlmanager",
(gptr *) &Options::user,
(gptr *) &Options::user,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "version", 'V', "Output version information and exit.", 0, 0, 0, { "version", 'V', "Output version information and exit.", 0, 0, 0,
GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 }, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
......
...@@ -34,8 +34,7 @@ struct Options ...@@ -34,8 +34,7 @@ struct Options
static const char *socket_file_name; static const char *socket_file_name;
static const char *password_file_name; static const char *password_file_name;
static const char *default_mysqld_path; static const char *default_mysqld_path;
static const char *default_admin_user; static const char *user;
static const char *default_admin_password;
static uint monitoring_interval; static uint monitoring_interval;
static uint port_number; static uint port_number;
static const char *bind_address; static const char *bind_address;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment