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 | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 26 | #include "common/privilege-helper.hpp" |
| 27 | #include "common/logger.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 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 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 36 | #ifdef NFD_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(); |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 42 | #endif // NFD_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 | { |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 47 | #ifdef NFD_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 | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 66 | group gr; |
| 67 | group* grResult = nullptr; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 68 | |
Davide Pesavento | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 69 | int errorCode = getgrnam_r(groupName.data(), &gr, groupBuffer.data(), groupBuffer.size(), &grResult); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 70 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 71 | while (errorCode == ERANGE) { |
| 72 | if (groupBuffer.size() * 2 > MAX_GROUP_BUFFER_SIZE) |
| 73 | throw Error("Cannot allocate large enough buffer for struct group"); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 74 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 75 | groupBuffer.resize(groupBuffer.size() * 2); |
Davide Pesavento | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 76 | errorCode = getgrnam_r(groupName.data(), &gr, groupBuffer.data(), groupBuffer.size(), &grResult); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 77 | } |
| 78 | |
Davide Pesavento | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 79 | if (errorCode != 0 || !grResult) |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 80 | throw Error("Failed to get gid for \"" + groupName + "\""); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 81 | |
Davide Pesavento | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 82 | s_normalGid = gr.gr_gid; |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 83 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 84 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 85 | if (!userName.empty()) { |
| 86 | static long passwdSize = ::sysconf(_SC_GETPW_R_SIZE_MAX); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 87 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 88 | if (passwdSize == -1) |
| 89 | passwdSize = FALLBACK_PASSWD_BUFFER_SIZE; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 90 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 91 | std::vector<char> passwdBuffer(static_cast<size_t>(passwdSize)); |
Davide Pesavento | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 92 | passwd pw; |
| 93 | passwd* pwResult = nullptr; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 94 | |
Davide Pesavento | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 95 | int errorCode = getpwnam_r(userName.data(), &pw, passwdBuffer.data(), passwdBuffer.size(), &pwResult); |
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 | while (errorCode == ERANGE) { |
| 98 | if (passwdBuffer.size() * 2 > MAX_PASSWD_BUFFER_SIZE) |
| 99 | throw Error("Cannot allocate large enough buffer for struct passwd"); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 100 | |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 101 | passwdBuffer.resize(passwdBuffer.size() * 2); |
Davide Pesavento | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 102 | errorCode = getpwnam_r(userName.data(), &pw, passwdBuffer.data(), passwdBuffer.size(), &pwResult); |
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 | |
Davide Pesavento | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 105 | if (errorCode != 0 || !pwResult) |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 106 | throw Error("Failed to get uid for \"" + userName + "\""); |
| 107 | |
Davide Pesavento | 1752159 | 2020-05-14 19:01:32 -0400 | [diff] [blame] | 108 | s_normalUid = pw.pw_uid; |
Davide Pesavento | acd0087 | 2018-02-15 18:24:06 -0500 | [diff] [blame] | 109 | } |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 110 | #else |
Alexander Afanasyev | 9cfeeca | 2015-02-12 16:44:24 -0800 | [diff] [blame] | 111 | if (!userName.empty() || !groupName.empty()) { |
| 112 | throw Error("Dropping and raising privileges is not supported on this platform"); |
| 113 | } |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 114 | #endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
| 118 | PrivilegeHelper::drop() |
| 119 | { |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 120 | #ifdef NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 121 | if (::geteuid() == s_normalUid && ::getegid() == s_normalGid) |
| 122 | return; |
| 123 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 124 | NFD_LOG_TRACE("dropping to effective gid=" << s_normalGid); |
| 125 | if (::setegid(s_normalGid) != 0) |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 126 | throw Error("Failed to drop to effective gid=" + std::to_string(s_normalGid)); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 127 | |
| 128 | NFD_LOG_TRACE("dropping to effective uid=" << s_normalUid); |
| 129 | if (::seteuid(s_normalUid) != 0) |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 130 | throw Error("Failed to drop to effective uid=" + std::to_string(s_normalUid)); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 131 | |
| 132 | NFD_LOG_INFO("dropped to effective uid=" << ::geteuid() << " gid=" << ::getegid()); |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 133 | #else |
Alexander Afanasyev | 6f570de | 2015-02-11 16:15:25 -0800 | [diff] [blame] | 134 | NFD_LOG_WARN("Dropping privileges is not supported on this platform"); |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 135 | #endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void |
| 139 | PrivilegeHelper::raise() |
| 140 | { |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 141 | #ifdef NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 142 | if (::geteuid() == s_privilegedUid && ::getegid() == s_privilegedGid) |
| 143 | return; |
| 144 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 145 | NFD_LOG_TRACE("elevating to effective uid=" << s_privilegedUid); |
| 146 | if (::seteuid(s_privilegedUid) != 0) |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 147 | throw Error("Failed to elevate to effective uid=" + std::to_string(s_privilegedUid)); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 148 | |
| 149 | NFD_LOG_TRACE("elevating to effective gid=" << s_privilegedGid); |
| 150 | if (::setegid(s_privilegedGid) != 0) |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 151 | throw Error("Failed to elevate to effective gid=" + std::to_string(s_privilegedGid)); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 152 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 153 | NFD_LOG_INFO("elevated to effective uid=" << ::geteuid() << " gid=" << ::getegid()); |
Alexander Afanasyev | 49343f6 | 2015-01-26 21:58:07 -0800 | [diff] [blame] | 154 | #else |
Alexander Afanasyev | 6f570de | 2015-02-11 16:15:25 -0800 | [diff] [blame] | 155 | NFD_LOG_WARN("Elevating privileges is not supported on this platform"); |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 156 | #endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 157 | } |
| 158 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 159 | } // namespace nfd |