1 | #include <my_global.h> |
2 | #include <m_string.h> |
3 | #include <my_sys.h> |
4 | #include <my_pthread.h> |
5 | #ifdef HAVE_PWD_H |
6 | #include <pwd.h> |
7 | #endif |
8 | #ifdef HAVE_GRP_H |
9 | #include <grp.h> |
10 | #endif |
11 | |
12 | struct passwd *my_check_user(const char *user, myf MyFlags) |
13 | { |
14 | struct passwd *user_info; |
15 | uid_t user_id= geteuid(); |
16 | DBUG_ENTER("my_check_user" ); |
17 | |
18 | // Don't bother if we aren't superuser |
19 | if (user_id) |
20 | { |
21 | if (user) |
22 | { |
23 | /* Don't give a warning, if real user is same as given with --user */ |
24 | user_info= getpwnam(user); |
25 | if (!user_info || user_id != user_info->pw_uid) |
26 | { |
27 | my_errno= EPERM; |
28 | if (MyFlags & MY_WME) |
29 | my_printf_error(my_errno, "One can only use the --user switch if " |
30 | "running as root" , MYF(ME_JUST_WARNING|ME_NOREFRESH)); |
31 | } |
32 | } |
33 | DBUG_RETURN(NULL); |
34 | } |
35 | if (!user) |
36 | { |
37 | if (MyFlags & MY_FAE) |
38 | { |
39 | my_errno= EINVAL; |
40 | my_printf_error(my_errno, "Please consult the Knowledge Base to find " |
41 | "out how to run mysqld as root!" , MYF(ME_NOREFRESH)); |
42 | } |
43 | DBUG_RETURN(NULL); |
44 | } |
45 | if (!strcmp(user,"root" )) |
46 | DBUG_RETURN(NULL); |
47 | |
48 | if (!(user_info= getpwnam(user))) |
49 | { |
50 | // Allow a numeric uid to be used |
51 | int err= 0; |
52 | user_id= my_strtoll10(user, NULL, &err); |
53 | if (err || !(user_info= getpwuid(user_id))) |
54 | { |
55 | my_errno= EINVAL; |
56 | my_printf_error(my_errno, "Can't change to run as user '%s'. Please " |
57 | "check that the user exists!" , MYF(ME_NOREFRESH), user); |
58 | DBUG_RETURN(NULL); |
59 | } |
60 | } |
61 | DBUG_ASSERT(user_info); |
62 | DBUG_RETURN(user_info); |
63 | } |
64 | |
65 | int my_set_user(const char *user, struct passwd *user_info, myf MyFlags) |
66 | { |
67 | DBUG_ENTER("my_set_user" ); |
68 | |
69 | DBUG_ASSERT(user_info != 0); |
70 | #ifdef HAVE_INITGROUPS |
71 | initgroups(user, user_info->pw_gid); |
72 | #endif |
73 | if (setgid(user_info->pw_gid) == -1 || setuid(user_info->pw_uid) == -1) |
74 | { |
75 | my_errno= errno; |
76 | if (MyFlags & MY_WME) |
77 | my_printf_error(errno, "Cannot change uid/gid (errno: %d)" , MYF(ME_NOREFRESH), |
78 | errno); |
79 | DBUG_RETURN(my_errno); |
80 | } |
81 | DBUG_RETURN(0); |
82 | } |
83 | |