Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 25 | |
| 26 | #include "privilege-helper.hpp" |
| 27 | #include "core/logger.hpp" |
| 28 | |
| 29 | #include <pwd.h> |
| 30 | #include <grp.h> |
| 31 | |
| 32 | namespace nfd { |
| 33 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 34 | NFD_LOG_INIT(PrivilegeHelper); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 35 | |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 36 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Alexander Afanasyev | ff489a2 | 2014-04-22 23:50:39 -0700 | [diff] [blame] | 37 | uid_t PrivilegeHelper::s_normalUid = ::geteuid(); |
| 38 | gid_t PrivilegeHelper::s_normalGid = ::getegid(); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 39 | |
Alexander Afanasyev | ff489a2 | 2014-04-22 23:50:39 -0700 | [diff] [blame] | 40 | uid_t PrivilegeHelper::s_privilegedUid = ::geteuid(); |
| 41 | gid_t PrivilegeHelper::s_privilegedGid = ::getegid(); |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 42 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 43 | |
| 44 | void |
| 45 | PrivilegeHelper::initialize(const std::string& userName, const std::string& groupName) |
| 46 | { |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 47 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 48 | static const size_t MAX_GROUP_BUFFER_SIZE = 16384; // 16 KiB |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 49 | static const size_t MAX_PASSWD_BUFFER_SIZE = 16384; |
| 50 | |
| 51 | static const size_t FALLBACK_GROUP_BUFFER_SIZE = 1024; |
| 52 | static const size_t FALLBACK_PASSWD_BUFFER_SIZE = 1024; |
| 53 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 54 | NFD_LOG_TRACE("initializing with user \"" << userName << "\"" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 55 | << " group \"" << groupName << "\""); |
| 56 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 57 | // workflow from man getpwnam_r |
| 58 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 59 | if (!groupName.empty()) { |
| 60 | static long groupSize = ::sysconf(_SC_GETGR_R_SIZE_MAX); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 61 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 62 | if (groupSize == -1) |
| 63 | groupSize = FALLBACK_GROUP_BUFFER_SIZE; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 64 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 65 | std::vector<char> groupBuffer(static_cast<size_t>(groupSize)); |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 66 | struct group group; |
| 67 | struct group* groupResult = nullptr; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 68 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 69 | int errorCode = getgrnam_r(groupName.data(), &group, |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 70 | groupBuffer.data(), groupBuffer.size(), &groupResult); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 71 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 72 | while (errorCode == ERANGE) { |
| 73 | if (groupBuffer.size() * 2 > MAX_GROUP_BUFFER_SIZE) |
| 74 | throw Error("Cannot allocate large enough buffer for struct group"); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 75 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 76 | groupBuffer.resize(groupBuffer.size() * 2); |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 77 | errorCode = getgrnam_r(groupName.data(), &group, |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 78 | groupBuffer.data(), groupBuffer.size(), &groupResult); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 81 | if (errorCode != 0 || !groupResult) |
| 82 | throw Error("Failed to get gid for \"" + groupName + "\""); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 83 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 84 | s_normalGid = group.gr_gid; |
| 85 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 86 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 87 | if (!userName.empty()) { |
| 88 | static long passwdSize = ::sysconf(_SC_GETPW_R_SIZE_MAX); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 89 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 90 | if (passwdSize == -1) |
| 91 | passwdSize = FALLBACK_PASSWD_BUFFER_SIZE; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 92 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 93 | std::vector<char> passwdBuffer(static_cast<size_t>(passwdSize)); |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 94 | struct passwd passwd; |
| 95 | struct passwd* passwdResult = nullptr; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 96 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 97 | int errorCode = getpwnam_r(userName.data(), &passwd, |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 98 | passwdBuffer.data(), passwdBuffer.size(), &passwdResult); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 99 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 100 | while (errorCode == ERANGE) { |
| 101 | if (passwdBuffer.size() * 2 > MAX_PASSWD_BUFFER_SIZE) |
| 102 | throw Error("Cannot allocate large enough buffer for struct passwd"); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 103 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 104 | passwdBuffer.resize(passwdBuffer.size() * 2); |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 105 | errorCode = getpwnam_r(userName.data(), &passwd, |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 106 | passwdBuffer.data(), passwdBuffer.size(), &passwdResult); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 107 | } |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 108 | |
| 109 | if (errorCode != 0 || !passwdResult) |
| 110 | throw Error("Failed to get uid for \"" + userName + "\""); |
| 111 | |
| 112 | s_normalUid = passwd.pw_uid; |
| 113 | } |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 114 | #else |
Alexander Afanasyev | 9cfeeca | 2015-02-12 16:44:24 -0800 | [diff] [blame] | 115 | if (!userName.empty() || !groupName.empty()) { |
| 116 | throw Error("Dropping and raising privileges is not supported on this platform"); |
| 117 | } |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 118 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
| 122 | PrivilegeHelper::drop() |
| 123 | { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 124 | if (::geteuid() == s_normalUid && ::getegid() == s_normalGid) |
| 125 | return; |
| 126 | |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 127 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 128 | NFD_LOG_TRACE("dropping to effective gid=" << s_normalGid); |
| 129 | if (::setegid(s_normalGid) != 0) |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 130 | throw Error("Failed to drop to effective gid=" + to_string(s_normalGid)); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 131 | |
| 132 | NFD_LOG_TRACE("dropping to effective uid=" << s_normalUid); |
| 133 | if (::seteuid(s_normalUid) != 0) |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 134 | throw Error("Failed to drop to effective uid=" + to_string(s_normalUid)); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 135 | |
| 136 | NFD_LOG_INFO("dropped to effective uid=" << ::geteuid() << " gid=" << ::getegid()); |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 137 | #else |
Alexander Afanasyev | 6f570de | 2015-02-11 16:15:25 -0800 | [diff] [blame] | 138 | NFD_LOG_WARN("Dropping privileges is not supported on this platform"); |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 139 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
| 143 | PrivilegeHelper::raise() |
| 144 | { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 145 | if (::geteuid() == s_privilegedUid && ::getegid() == s_privilegedGid) |
| 146 | return; |
| 147 | |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 148 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 149 | NFD_LOG_TRACE("elevating to effective uid=" << s_privilegedUid); |
| 150 | if (::seteuid(s_privilegedUid) != 0) |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 151 | throw Error("Failed to elevate to effective uid=" + to_string(s_privilegedUid)); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 152 | |
| 153 | NFD_LOG_TRACE("elevating to effective gid=" << s_privilegedGid); |
| 154 | if (::setegid(s_privilegedGid) != 0) |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 155 | throw Error("Failed to elevate to effective gid=" + to_string(s_privilegedGid)); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 156 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 157 | NFD_LOG_INFO("elevated to effective uid=" << ::geteuid() << " gid=" << ::getegid()); |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 158 | #else |
Alexander Afanasyev | 6f570de | 2015-02-11 16:15:25 -0800 | [diff] [blame] | 159 | NFD_LOG_WARN("Elevating privileges is not supported on this platform"); |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 160 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 161 | } |
| 162 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 163 | } // namespace nfd |