| 1 | /* |
| 2 | * Copyright (C) 2001 Hakan Tandogan ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "common/setup_before.h" |
| 20 | #include "common/give_up_root_privileges.h" |
| 21 | #include <cerrno> |
| 22 | #include <cstring> |
| 23 | #include <cstdlib> |
| 24 | |
| 25 | #ifdef HAVE_SYS_TYPES_H |
| 26 | # include <sys/types.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_UNISTD_H |
| 29 | # include <unistd.h> |
| 30 | #endif |
| 31 | #ifdef HAVE_PWD_H |
| 32 | # include <pwd.h> |
| 33 | #endif |
| 34 | #ifdef HAVE_GRP_H |
| 35 | # include <grp.h> |
| 36 | #endif |
| 37 | |
| 38 | #include "common/eventlog.h" |
| 39 | #include "common/setup_after.h" |
| 40 | |
| 41 | |
| 42 | #define ILLEGAL_ID -1 |
| 43 | |
| 44 | |
| 45 | namespace pvpgn |
| 46 | { |
| 47 | |
| 48 | static int gurp_gname2id(const char *name); |
| 49 | static int gurp_uname2id(const char *name); |
| 50 | |
| 51 | |
| 52 | extern int give_up_root_privileges(char const * user_name, char const * group_name) |
| 53 | { |
| 54 | int user_id = 0; |
| 55 | int group_id = 0; |
| 56 | |
| 57 | eventlog(eventlog_level_debug, __FUNCTION__, "about to give up root privileges"); |
| 58 | |
| 59 | if (user_name) |
| 60 | { |
| 61 | if ((user_id = gurp_uname2id(user_name)) == ILLEGAL_ID) |
| 62 | { |
| 63 | return -1; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | eventlog(eventlog_level_debug, __FUNCTION__, "should change to user = '{}' ({})", user_name, user_id); |
| 68 | } |
| 69 | } |
| 70 | if (group_name) |
| 71 | { |
| 72 | if ((group_id = gurp_gname2id(group_name)) == ILLEGAL_ID) |
| 73 | { |
| 74 | return -1; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | eventlog(eventlog_level_debug, __FUNCTION__, "should change to group = '{}' ({})", group_name, group_id); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* Change first the group ID, later we might not be able to anymore |
| 83 | * We can use setgid safely because we don't want to return to root |
| 84 | * privileges anymore |
| 85 | */ |
| 86 | |
| 87 | #ifdef HAVE_SETGID |
| 88 | if (group_name) |
| 89 | { |
| 90 | if (-1 == setgid(group_id)) |
| 91 | { |
| 92 | eventlog(eventlog_level_fatal, __FUNCTION__, "could not set gid to {} (setgid: {})", group_id, std::strerror(errno)); |
| 93 | return -1; |
| 94 | } |
| 95 | # ifdef HAVE_GETUID |
| 96 | eventlog(eventlog_level_info, __FUNCTION__, "Changed privileges to gid = {}", getgid()); |
| 97 | # endif |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | #ifdef HAVE_SETUID |
| 102 | if (user_name) |
| 103 | { |
| 104 | if (-1 == setuid(user_id)) |
| 105 | { |
| 106 | eventlog(eventlog_level_fatal, __FUNCTION__, "could not set uid to {} (setuid: {})", user_id, std::strerror(errno)); |
| 107 | return -1; |
| 108 | } |
| 109 | # ifdef HAVE_GETGID |
| 110 | eventlog(eventlog_level_info, __FUNCTION__, "Changed privileges to uid = {}", getuid()); |
| 111 | # endif |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | static int gurp_uname2id(const char *name) |
| 120 | { |
| 121 | int id = ILLEGAL_ID; |
| 122 | |
| 123 | if (name != NULL) |
| 124 | { |
| 125 | if (name[0] == '#') |
| 126 | { |
| 127 | id = std::atoi(&name[1]); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | #ifdef HAVE_GETPWNAME |
| 132 | struct passwd * ent; |
| 133 | |
| 134 | eventlog(eventlog_level_debug, __FUNCTION__, "about to getpwnam({})", name); |
| 135 | |
| 136 | if (!(ent = getpwnam(name))) |
| 137 | { |
| 138 | eventlog(eventlog_level_fatal, __FUNCTION__, "cannot get password file entry for '{}' (getpwnam: {})", name, std::strerror(errno)); |
| 139 | return id; |
| 140 | } |
| 141 | id = ent->pw_uid; |
| 142 | #else |
| 143 | return id; |
| 144 | #endif |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return id; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static int gurp_gname2id(const char *name) |
| 153 | { |
| 154 | int id = ILLEGAL_ID; |
| 155 | |
| 156 | if (name != NULL) |
| 157 | { |
| 158 | if (name[0] == '#') |
| 159 | { |
| 160 | id = std::atoi(&name[1]); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | #ifdef HAVE_GETGRNAM |
| 165 | struct group * ent; |
| 166 | |
| 167 | eventlog(eventlog_level_debug, __FUNCTION__, "about to getgrnam({})", name); |
| 168 | |
| 169 | if (!(ent = getgrnam(name))) |
| 170 | { |
| 171 | eventlog(eventlog_level_fatal, __FUNCTION__, "cannot get group file entry for '{}' (getgrnam: {})", name, std::strerror(errno)); |
| 172 | return id; |
| 173 | } |
| 174 | id = ent->gr_gid; |
| 175 | #else |
| 176 | return id; |
| 177 | #endif |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return id; |
| 182 | } |
| 183 | |
| 184 | } |