summaryrefslogtreecommitdiff
path: root/mysys
diff options
context:
space:
mode:
authorSergei Golubchik <serg@mariadb.org>2017-02-10 17:01:45 +0100
committerSergei Golubchik <serg@mariadb.org>2017-02-10 17:01:45 +0100
commit2195bb4e416232ab807ff67eecf03b1223bf6bff (patch)
tree4555af02df68cb0f26d454b1cf65086b62542875 /mysys
parent3ae038b732ce503fb839e9095355e05f5c6866f9 (diff)
parentbc4686f0f4d17dc57dd727c9f5390caa3022bdca (diff)
downloadmariadb-git-2195bb4e416232ab807ff67eecf03b1223bf6bff.tar.gz
Merge branch '10.1' into 10.2
Diffstat (limited to 'mysys')
-rw-r--r--mysys/CMakeLists.txt2
-rw-r--r--mysys/my_getopt.c72
-rw-r--r--mysys/my_setuser.c82
3 files changed, 143 insertions, 13 deletions
diff --git a/mysys/CMakeLists.txt b/mysys/CMakeLists.txt
index 6c43c29a758..13ea8dd9f35 100644
--- a/mysys/CMakeLists.txt
+++ b/mysys/CMakeLists.txt
@@ -50,7 +50,7 @@ IF (WIN32)
ENDIF()
IF(UNIX)
- SET (MYSYS_SOURCES ${MYSYS_SOURCES} my_addr_resolve.c)
+ SET (MYSYS_SOURCES ${MYSYS_SOURCES} my_addr_resolve.c my_setuser.c)
ENDIF()
IF(HAVE_ALARM)
diff --git a/mysys/my_getopt.c b/mysys/my_getopt.c
index 3f75f6553a2..57b28d1fd8a 100644
--- a/mysys/my_getopt.c
+++ b/mysys/my_getopt.c
@@ -949,15 +949,39 @@ my_bool getopt_compare_strings(register const char *s, register const char *t,
/*
function: eval_num_suffix
+ Transforms suffix like k/m/g to their real value.
+*/
+
+static inline long eval_num_suffix(char *suffix, int *error)
+{
+ long num= 1;
+ if (*suffix == 'k' || *suffix == 'K')
+ num*= 1024L;
+ else if (*suffix == 'm' || *suffix == 'M')
+ num*= 1024L * 1024L;
+ else if (*suffix == 'g' || *suffix == 'G')
+ num*= 1024L * 1024L * 1024L;
+ else if (*suffix)
+ {
+ *error= 1;
+ return 0;
+ }
+ return num;
+}
+
+/*
+ function: eval_num_suffix_ll
+
Transforms a number with a suffix to real number. Suffix can
be k|K for kilo, m|M for mega or g|G for giga.
*/
-static longlong eval_num_suffix(char *argument, int *error, char *option_name)
+static longlong eval_num_suffix_ll(char *argument,
+ int *error, char *option_name)
{
char *endchar;
longlong num;
- DBUG_ENTER("eval_num_suffix");
+ DBUG_ENTER("eval_num_suffix_ll");
*error= 0;
@@ -970,23 +994,47 @@ static longlong eval_num_suffix(char *argument, int *error, char *option_name)
*error= 1;
DBUG_RETURN(0);
}
- if (*endchar == 'k' || *endchar == 'K')
- num*= 1024L;
- else if (*endchar == 'm' || *endchar == 'M')
- num*= 1024L * 1024L;
- else if (*endchar == 'g' || *endchar == 'G')
- num*= 1024L * 1024L * 1024L;
- else if (*endchar)
- {
+ num*= eval_num_suffix(endchar, error);
+ if (*error)
fprintf(stderr,
"Unknown suffix '%c' used for variable '%s' (value '%s')\n",
*endchar, option_name, argument);
+ DBUG_RETURN(num);
+}
+
+/*
+ function: eval_num_suffix_ull
+
+ Transforms a number with a suffix to positive Integer. Suffix can
+ be k|K for kilo, m|M for mega or g|G for giga.
+*/
+
+static ulonglong eval_num_suffix_ull(char *argument,
+ int *error, char *option_name)
+{
+ char *endchar;
+ ulonglong num;
+ DBUG_ENTER("eval_num_suffix_ull");
+
+ *error= 0;
+ errno= 0;
+ num= strtoull(argument, &endchar, 10);
+ if (errno == ERANGE)
+ {
+ my_getopt_error_reporter(ERROR_LEVEL,
+ "Incorrect integer value: '%s'", argument);
*error= 1;
DBUG_RETURN(0);
}
+ num*= eval_num_suffix(endchar, error);
+ if (*error)
+ fprintf(stderr,
+ "Unknown suffix '%c' used for variable '%s' (value '%s')\n",
+ *endchar, option_name, argument);
DBUG_RETURN(num);
}
+
/*
function: getopt_ll
@@ -1000,7 +1048,7 @@ static longlong eval_num_suffix(char *argument, int *error, char *option_name)
static longlong getopt_ll(char *arg, const struct my_option *optp, int *err)
{
- longlong num=eval_num_suffix(arg, err, (char*) optp->name);
+ longlong num=eval_num_suffix_ll(arg, err, (char*) optp->name);
return getopt_ll_limit_value(num, optp, NULL);
}
@@ -1077,7 +1125,7 @@ longlong getopt_ll_limit_value(longlong num, const struct my_option *optp,
static ulonglong getopt_ull(char *arg, const struct my_option *optp, int *err)
{
- ulonglong num= eval_num_suffix(arg, err, (char*) optp->name);
+ ulonglong num= eval_num_suffix_ull(arg, err, (char*) optp->name);
return getopt_ull_limit_value(num, optp, NULL);
}
diff --git a/mysys/my_setuser.c b/mysys/my_setuser.c
new file mode 100644
index 00000000000..14ab04dd10f
--- /dev/null
+++ b/mysys/my_setuser.c
@@ -0,0 +1,82 @@
+#include <my_global.h>
+#include <m_string.h>
+#include <my_sys.h>
+#include <my_pthread.h>
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
+#ifdef HAVE_GRP_H
+#include <grp.h>
+#endif
+
+struct passwd *my_check_user(const char *user, myf MyFlags)
+{
+ struct passwd *user_info;
+ uid_t user_id= geteuid();
+ DBUG_ENTER("my_check_user");
+
+ // 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)
+ {
+ my_errno= EPERM;
+ if (MyFlags & MY_WME)
+ my_printf_error(my_errno, "One can only use the --user switch if "
+ "running as root", MYF(ME_JUST_WARNING|ME_NOREFRESH));
+ }
+ }
+ DBUG_RETURN(NULL);
+ }
+ if (!user)
+ {
+ if (MyFlags & MY_FAE)
+ {
+ my_errno= EINVAL;
+ my_printf_error(my_errno, "Please consult the Knowledge Base to find "
+ "out how to run mysqld as root!", MYF(ME_NOREFRESH));
+ }
+ DBUG_RETURN(NULL);
+ }
+ if (!strcmp(user,"root"))
+ DBUG_RETURN(NULL);
+
+ if (!(user_info= getpwnam(user)))
+ {
+ // Allow a numeric uid to be used
+ int err= 0;
+ user_id= my_strtoll10(user, NULL, &err);
+ if (err || !(user_info= getpwuid(user_id)))
+ {
+ my_errno= EINVAL;
+ my_printf_error(my_errno, "Can't change to run as user '%s'. Please "
+ "check that the user exists!", MYF(ME_NOREFRESH), user);
+ DBUG_RETURN(NULL);
+ }
+ }
+ DBUG_ASSERT(user_info);
+ DBUG_RETURN(user_info);
+}
+
+int my_set_user(const char *user, struct passwd *user_info, myf MyFlags)
+{
+ DBUG_ENTER("my_set_user");
+
+ DBUG_ASSERT(user_info != 0);
+#ifdef HAVE_INITGROUPS
+ initgroups(user, user_info->pw_gid);
+#endif
+ if (setgid(user_info->pw_gid) == -1 || setuid(user_info->pw_uid) == -1)
+ {
+ my_errno= errno;
+ if (MyFlags & MY_WME)
+ my_printf_error(errno, "Cannot change uid/gid (errno: %d)", MYF(ME_NOREFRESH),
+ errno);
+ DBUG_RETURN(my_errno);
+ }
+ DBUG_RETURN(0);
+}