Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 17 | #include "config.h" |
| 18 | |
| 19 | #include "user_mgr.hpp" |
| 20 | |
| 21 | #include "file.hpp" |
| 22 | #include "shadowlock.hpp" |
| 23 | #include "users.hpp" |
| 24 | |
| 25 | #include <grp.h> |
| 26 | #include <pwd.h> |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 27 | #include <shadow.h> |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 30 | #include <time.h> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <boost/algorithm/string/split.hpp> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 34 | #include <phosphor-logging/elog-errors.hpp> |
| 35 | #include <phosphor-logging/elog.hpp> |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 36 | #include <phosphor-logging/lg2.hpp> |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 37 | #include <xyz/openbmc_project/Common/error.hpp> |
| 38 | #include <xyz/openbmc_project/User/Common/error.hpp> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 39 | |
| 40 | #include <algorithm> |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 41 | #include <array> |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 42 | #include <ctime> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 43 | #include <fstream> |
| 44 | #include <numeric> |
| 45 | #include <regex> |
Nan Zhou | e47c09d | 2022-10-25 00:06:41 +0000 | [diff] [blame] | 46 | #include <span> |
| 47 | #include <string> |
| 48 | #include <string_view> |
| 49 | #include <vector> |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 50 | |
| 51 | namespace phosphor |
| 52 | { |
| 53 | namespace user |
| 54 | { |
| 55 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 56 | static constexpr const char* passwdFileName = "/etc/passwd"; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 57 | static constexpr size_t ipmiMaxUserNameLen = 16; |
| 58 | static constexpr size_t systemMaxUserNameLen = 30; |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 59 | static constexpr const char* grpSsh = "ssh"; |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 60 | static constexpr int success = 0; |
| 61 | static constexpr int failure = -1; |
| 62 | |
| 63 | // pam modules related |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 64 | static constexpr const char* pamTally2 = "pam_tally2.so"; |
| 65 | static constexpr const char* pamCrackLib = "pam_cracklib.so"; |
| 66 | static constexpr const char* pamPWHistory = "pam_pwhistory.so"; |
| 67 | static constexpr const char* minPasswdLenProp = "minlen"; |
| 68 | static constexpr const char* remOldPasswdCount = "remember"; |
| 69 | static constexpr const char* maxFailedAttempt = "deny"; |
| 70 | static constexpr const char* unlockTimeout = "unlock_time"; |
Nan Zhou | e48085d | 2022-10-25 00:07:04 +0000 | [diff] [blame] | 71 | static constexpr const char* defaultPamPasswdConfigFile = |
| 72 | "/etc/pam.d/common-password"; |
| 73 | static constexpr const char* defaultPamAuthConfigFile = |
| 74 | "/etc/pam.d/common-auth"; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 75 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 76 | // Object Manager related |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 77 | static constexpr const char* ldapMgrObjBasePath = |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 78 | "/xyz/openbmc_project/user/ldap"; |
| 79 | |
| 80 | // Object Mapper related |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 81 | static constexpr const char* objMapperService = |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 82 | "xyz.openbmc_project.ObjectMapper"; |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 83 | static constexpr const char* objMapperPath = |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 84 | "/xyz/openbmc_project/object_mapper"; |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 85 | static constexpr const char* objMapperInterface = |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 86 | "xyz.openbmc_project.ObjectMapper"; |
| 87 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 88 | using namespace phosphor::logging; |
| 89 | using InsufficientPermission = |
| 90 | sdbusplus::xyz::openbmc_project::Common::Error::InsufficientPermission; |
| 91 | using InternalFailure = |
| 92 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 93 | using InvalidArgument = |
| 94 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 95 | using UserNameExists = |
| 96 | sdbusplus::xyz::openbmc_project::User::Common::Error::UserNameExists; |
| 97 | using UserNameDoesNotExist = |
| 98 | sdbusplus::xyz::openbmc_project::User::Common::Error::UserNameDoesNotExist; |
| 99 | using UserNameGroupFail = |
| 100 | sdbusplus::xyz::openbmc_project::User::Common::Error::UserNameGroupFail; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 101 | using NoResource = |
| 102 | sdbusplus::xyz::openbmc_project::User::Common::Error::NoResource; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 103 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 104 | using GroupNameExists = |
| 105 | sdbusplus::xyz::openbmc_project::User::Common::Error::GroupNameExists; |
| 106 | using GroupNameDoesNotExists = |
| 107 | sdbusplus::xyz::openbmc_project::User::Common::Error::GroupNameDoesNotExist; |
| 108 | |
| 109 | namespace |
| 110 | { |
| 111 | |
| 112 | // The hardcoded groups in OpenBMC projects |
| 113 | constexpr std::array<const char*, 4> predefinedGroups = {"web", "redfish", |
| 114 | "ipmi", "ssh"}; |
| 115 | |
| 116 | // These prefixes are for Dynamic Redfish authorization. See |
| 117 | // https://github.com/openbmc/docs/blob/master/designs/redfish-authorization.md |
| 118 | |
| 119 | // Base role and base privileges are added by Redfish implementation (e.g., |
| 120 | // BMCWeb) at compile time |
| 121 | constexpr std::array<const char*, 4> allowedGroupPrefix = { |
| 122 | "openbmc_rfr_", // OpenBMC Redfish Base Role |
| 123 | "openbmc_rfp_", // OpenBMC Redfish Base Privileges |
| 124 | "openbmc_orfr_", // OpenBMC Redfish OEM Role |
| 125 | "openbmc_orfp_", // OpenBMC Redfish OEM Privileges |
| 126 | }; |
| 127 | |
| 128 | void checkAndThrowsForGroupChangeAllowed(const std::string& groupName) |
| 129 | { |
| 130 | bool allowed = false; |
| 131 | for (std::string_view prefix : allowedGroupPrefix) |
| 132 | { |
| 133 | if (groupName.starts_with(prefix)) |
| 134 | { |
| 135 | allowed = true; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | if (!allowed) |
| 140 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 141 | lg2::error("Group name '{GROUP}' is not in the allowed list", "GROUP", |
| 142 | groupName); |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 143 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group Name"), |
| 144 | Argument::ARGUMENT_VALUE(groupName.c_str())); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } // namespace |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 149 | |
Nan Zhou | e47c09d | 2022-10-25 00:06:41 +0000 | [diff] [blame] | 150 | std::string getCSVFromVector(std::span<const std::string> vec) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 151 | { |
Nan Zhou | e47c09d | 2022-10-25 00:06:41 +0000 | [diff] [blame] | 152 | if (vec.empty()) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 153 | { |
Nan Zhou | e47c09d | 2022-10-25 00:06:41 +0000 | [diff] [blame] | 154 | return ""; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 155 | } |
Nan Zhou | e47c09d | 2022-10-25 00:06:41 +0000 | [diff] [blame] | 156 | return std::accumulate(std::next(vec.begin()), vec.end(), vec[0], |
| 157 | [](std::string&& val, std::string_view element) { |
| 158 | val += ','; |
| 159 | val += element; |
| 160 | return val; |
| 161 | }); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 162 | } |
| 163 | |
Nan Zhou | 332fb9d | 2022-10-25 00:07:03 +0000 | [diff] [blame] | 164 | bool removeStringFromCSV(std::string& csvStr, const std::string& delStr) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 165 | { |
| 166 | std::string::size_type delStrPos = csvStr.find(delStr); |
| 167 | if (delStrPos != std::string::npos) |
| 168 | { |
| 169 | // need to also delete the comma char |
| 170 | if (delStrPos == 0) |
| 171 | { |
| 172 | csvStr.erase(delStrPos, delStr.size() + 1); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | csvStr.erase(delStrPos - 1, delStr.size() + 1); |
| 177 | } |
| 178 | return true; |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 183 | bool UserMgr::isUserExist(const std::string& userName) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 184 | { |
| 185 | if (userName.empty()) |
| 186 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 187 | lg2::error("User name is empty"); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 188 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("User name"), |
| 189 | Argument::ARGUMENT_VALUE("Null")); |
| 190 | } |
| 191 | if (usersList.find(userName) == usersList.end()) |
| 192 | { |
| 193 | return false; |
| 194 | } |
| 195 | return true; |
| 196 | } |
| 197 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 198 | void UserMgr::throwForUserDoesNotExist(const std::string& userName) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 199 | { |
Nan Zhou | 8a11d99 | 2022-10-25 00:07:06 +0000 | [diff] [blame] | 200 | if (!isUserExist(userName)) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 201 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 202 | lg2::error("User '{USERNAME}' does not exist", "USERNAME", userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 203 | elog<UserNameDoesNotExist>(); |
| 204 | } |
| 205 | } |
| 206 | |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 207 | void UserMgr::checkAndThrowForDisallowedGroupCreation( |
| 208 | const std::string& groupName) |
| 209 | { |
| 210 | if (groupName.size() > maxSystemGroupNameLength || |
| 211 | !std::regex_match(groupName.c_str(), |
| 212 | std::regex("[a-zA-z_][a-zA-Z_0-9]*"))) |
| 213 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 214 | lg2::error("Invalid group name '{GROUP}'", "GROUP", groupName); |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 215 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group Name"), |
| 216 | Argument::ARGUMENT_VALUE(groupName.c_str())); |
| 217 | } |
| 218 | checkAndThrowsForGroupChangeAllowed(groupName); |
| 219 | } |
| 220 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 221 | void UserMgr::throwForUserExists(const std::string& userName) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 222 | { |
Nan Zhou | 8a11d99 | 2022-10-25 00:07:06 +0000 | [diff] [blame] | 223 | if (isUserExist(userName)) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 224 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 225 | lg2::error("User '{USERNAME}' already exists", "USERNAME", userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 226 | elog<UserNameExists>(); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void UserMgr::throwForUserNameConstraints( |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 231 | const std::string& userName, const std::vector<std::string>& groupNames) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 232 | { |
| 233 | if (std::find(groupNames.begin(), groupNames.end(), "ipmi") != |
| 234 | groupNames.end()) |
| 235 | { |
| 236 | if (userName.length() > ipmiMaxUserNameLen) |
| 237 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 238 | lg2::error("User '{USERNAME}' exceeds IPMI username length limit " |
| 239 | "({LENGTH} > {LIMIT})", |
| 240 | "USERNAME", userName, "LENGTH", userName.length(), |
| 241 | "LIMIT", ipmiMaxUserNameLen); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 242 | elog<UserNameGroupFail>( |
| 243 | xyz::openbmc_project::User::Common::UserNameGroupFail::REASON( |
| 244 | "IPMI length")); |
| 245 | } |
| 246 | } |
| 247 | if (userName.length() > systemMaxUserNameLen) |
| 248 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 249 | lg2::error("User '{USERNAME}' exceeds system username length limit " |
| 250 | "({LENGTH} > {LIMIT})", |
| 251 | "USERNAME", userName, "LENGTH", userName.length(), "LIMIT", |
| 252 | systemMaxUserNameLen); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 253 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("User name"), |
| 254 | Argument::ARGUMENT_VALUE("Invalid length")); |
| 255 | } |
| 256 | if (!std::regex_match(userName.c_str(), |
| 257 | std::regex("[a-zA-z_][a-zA-Z_0-9]*"))) |
| 258 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 259 | lg2::error("Invalid username '{USERNAME}'", "USERNAME", userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 260 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("User name"), |
| 261 | Argument::ARGUMENT_VALUE("Invalid data")); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void UserMgr::throwForMaxGrpUserCount( |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 266 | const std::vector<std::string>& groupNames) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 267 | { |
| 268 | if (std::find(groupNames.begin(), groupNames.end(), "ipmi") != |
| 269 | groupNames.end()) |
| 270 | { |
| 271 | if (getIpmiUsersCount() >= ipmiMaxUsers) |
| 272 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 273 | lg2::error("IPMI user limit reached"); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 274 | elog<NoResource>( |
| 275 | xyz::openbmc_project::User::Common::NoResource::REASON( |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 276 | "IPMI user limit reached")); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | if (usersList.size() > 0 && (usersList.size() - getIpmiUsersCount()) >= |
| 282 | (maxSystemUsers - ipmiMaxUsers)) |
| 283 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 284 | lg2::error("Non-ipmi User limit reached"); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 285 | elog<NoResource>( |
| 286 | xyz::openbmc_project::User::Common::NoResource::REASON( |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 287 | "Non-ipmi user limit reached")); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | return; |
| 291 | } |
| 292 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 293 | void UserMgr::throwForInvalidPrivilege(const std::string& priv) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 294 | { |
| 295 | if (!priv.empty() && |
| 296 | (std::find(privMgr.begin(), privMgr.end(), priv) == privMgr.end())) |
| 297 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 298 | lg2::error("Invalid privilege '{PRIVILEGE}'", "PRIVILEGE", priv); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 299 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege"), |
| 300 | Argument::ARGUMENT_VALUE(priv.c_str())); |
| 301 | } |
| 302 | } |
| 303 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 304 | void UserMgr::throwForInvalidGroups(const std::vector<std::string>& groupNames) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 305 | { |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 306 | for (auto& group : groupNames) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 307 | { |
| 308 | if (std::find(groupsMgr.begin(), groupsMgr.end(), group) == |
| 309 | groupsMgr.end()) |
| 310 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 311 | lg2::error("Invalid Group Name '{GROUPNAME}'", "GROUPNAME", group); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 312 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GroupName"), |
| 313 | Argument::ARGUMENT_VALUE(group.c_str())); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 318 | std::vector<std::string> UserMgr::readAllGroupsOnSystem() |
| 319 | { |
| 320 | std::vector<std::string> allGroups = {predefinedGroups.begin(), |
| 321 | predefinedGroups.end()}; |
| 322 | // rewinds to the beginning of the group database |
| 323 | setgrent(); |
| 324 | struct group* gr = getgrent(); |
| 325 | while (gr != nullptr) |
| 326 | { |
| 327 | std::string group(gr->gr_name); |
| 328 | for (std::string_view prefix : allowedGroupPrefix) |
| 329 | { |
| 330 | if (group.starts_with(prefix)) |
| 331 | { |
| 332 | allGroups.push_back(gr->gr_name); |
| 333 | } |
| 334 | } |
| 335 | gr = getgrent(); |
| 336 | } |
| 337 | // close the group database |
| 338 | endgrent(); |
| 339 | return allGroups; |
| 340 | } |
| 341 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 342 | void UserMgr::createUser(std::string userName, |
| 343 | std::vector<std::string> groupNames, std::string priv, |
| 344 | bool enabled) |
| 345 | { |
| 346 | throwForInvalidPrivilege(priv); |
| 347 | throwForInvalidGroups(groupNames); |
| 348 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 349 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 350 | throwForUserExists(userName); |
| 351 | throwForUserNameConstraints(userName, groupNames); |
| 352 | throwForMaxGrpUserCount(groupNames); |
| 353 | |
| 354 | std::string groups = getCSVFromVector(groupNames); |
| 355 | bool sshRequested = removeStringFromCSV(groups, grpSsh); |
| 356 | |
| 357 | // treat privilege as a group - This is to avoid using different file to |
| 358 | // store the same. |
Richard Marian Thomaiyar | 2cb2e72 | 2018-09-27 14:22:42 +0530 | [diff] [blame] | 359 | if (!priv.empty()) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 360 | { |
Richard Marian Thomaiyar | 2cb2e72 | 2018-09-27 14:22:42 +0530 | [diff] [blame] | 361 | if (groups.size() != 0) |
| 362 | { |
| 363 | groups += ","; |
| 364 | } |
| 365 | groups += priv; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 366 | } |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 367 | try |
| 368 | { |
Nan Zhou | 49c8136 | 2022-10-25 00:07:08 +0000 | [diff] [blame] | 369 | executeUserAdd(userName.c_str(), groups.c_str(), sshRequested, enabled); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 370 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 371 | catch (const InternalFailure& e) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 372 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 373 | lg2::error("Unable to create new user '{USERNAME}'", "USERNAME", |
| 374 | userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 375 | elog<InternalFailure>(); |
| 376 | } |
| 377 | |
| 378 | // Add the users object before sending out the signal |
P Dheeraj Srujan Kumar | b01e2fe | 2021-12-13 09:43:28 +0530 | [diff] [blame] | 379 | sdbusplus::message::object_path tempObjPath(usersObjPath); |
| 380 | tempObjPath /= userName; |
| 381 | std::string userObj(tempObjPath); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 382 | std::sort(groupNames.begin(), groupNames.end()); |
| 383 | usersList.emplace( |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 384 | userName, std::make_unique<phosphor::user::Users>( |
| 385 | bus, userObj.c_str(), groupNames, priv, enabled, *this)); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 386 | |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 387 | lg2::info("User '{USERNAME}' created successfully", "USERNAME", userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | |
| 391 | void UserMgr::deleteUser(std::string userName) |
| 392 | { |
| 393 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 394 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 395 | throwForUserDoesNotExist(userName); |
| 396 | try |
| 397 | { |
Nan Zhou | 49c8136 | 2022-10-25 00:07:08 +0000 | [diff] [blame] | 398 | executeUserDelete(userName.c_str()); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 399 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 400 | catch (const InternalFailure& e) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 401 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 402 | lg2::error("Delete User '{USERNAME}' failed", "USERNAME", userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 403 | elog<InternalFailure>(); |
| 404 | } |
| 405 | |
| 406 | usersList.erase(userName); |
| 407 | |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 408 | lg2::info("User '{USERNAME}' deleted successfully", "USERNAME", userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 409 | return; |
| 410 | } |
| 411 | |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 412 | void UserMgr::checkDeleteGroupConstraints(const std::string& groupName) |
| 413 | { |
| 414 | if (std::find(groupsMgr.begin(), groupsMgr.end(), groupName) == |
| 415 | groupsMgr.end()) |
| 416 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 417 | lg2::error("Group '{GROUP}' already exists", "GROUP", groupName); |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 418 | elog<GroupNameDoesNotExists>(); |
| 419 | } |
| 420 | checkAndThrowsForGroupChangeAllowed(groupName); |
| 421 | } |
| 422 | |
| 423 | void UserMgr::deleteGroup(std::string groupName) |
| 424 | { |
| 425 | checkDeleteGroupConstraints(groupName); |
| 426 | try |
| 427 | { |
| 428 | executeGroupDeletion(groupName.c_str()); |
| 429 | } |
| 430 | catch (const InternalFailure& e) |
| 431 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 432 | lg2::error("Failed to delete group '{GROUP}'", "GROUP", groupName); |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 433 | elog<InternalFailure>(); |
| 434 | } |
| 435 | |
| 436 | groupsMgr.erase(std::find(groupsMgr.begin(), groupsMgr.end(), groupName)); |
| 437 | UserMgrIface::allGroups(groupsMgr); |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 438 | lg2::info("Successfully deleted group '{GROUP}'", "GROUP", groupName); |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void UserMgr::checkCreateGroupConstraints(const std::string& groupName) |
| 442 | { |
| 443 | if (std::find(groupsMgr.begin(), groupsMgr.end(), groupName) != |
| 444 | groupsMgr.end()) |
| 445 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 446 | lg2::error("Group '{GROUP}' already exists", "GROUP", groupName); |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 447 | elog<GroupNameExists>(); |
| 448 | } |
| 449 | checkAndThrowForDisallowedGroupCreation(groupName); |
| 450 | if (groupsMgr.size() >= maxSystemGroupCount) |
| 451 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 452 | lg2::error("Group limit reached"); |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 453 | elog<NoResource>(xyz::openbmc_project::User::Common::NoResource::REASON( |
| 454 | "Group limit reached")); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void UserMgr::createGroup(std::string groupName) |
| 459 | { |
| 460 | checkCreateGroupConstraints(groupName); |
| 461 | try |
| 462 | { |
| 463 | executeGroupCreation(groupName.c_str()); |
| 464 | } |
| 465 | catch (const InternalFailure& e) |
| 466 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 467 | lg2::error("Failed to create group '{GROUP}'", "GROUP", groupName); |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 468 | elog<InternalFailure>(); |
| 469 | } |
| 470 | groupsMgr.push_back(groupName); |
| 471 | UserMgrIface::allGroups(groupsMgr); |
| 472 | } |
| 473 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 474 | void UserMgr::renameUser(std::string userName, std::string newUserName) |
| 475 | { |
| 476 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 477 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 478 | throwForUserDoesNotExist(userName); |
| 479 | throwForUserExists(newUserName); |
| 480 | throwForUserNameConstraints(newUserName, |
| 481 | usersList[userName].get()->userGroups()); |
| 482 | try |
| 483 | { |
Nan Zhou | f25443e | 2022-10-25 00:07:11 +0000 | [diff] [blame] | 484 | executeUserRename(userName.c_str(), newUserName.c_str()); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 485 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 486 | catch (const InternalFailure& e) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 487 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 488 | lg2::error("Rename '{USERNAME}' to '{NEWUSERNAME}' failed", "USERNAME", |
| 489 | userName, "NEWUSERNAME", newUserName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 490 | elog<InternalFailure>(); |
| 491 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 492 | const auto& user = usersList[userName]; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 493 | std::string priv = user.get()->userPrivilege(); |
| 494 | std::vector<std::string> groupNames = user.get()->userGroups(); |
| 495 | bool enabled = user.get()->userEnabled(); |
P Dheeraj Srujan Kumar | b01e2fe | 2021-12-13 09:43:28 +0530 | [diff] [blame] | 496 | sdbusplus::message::object_path tempObjPath(usersObjPath); |
| 497 | tempObjPath /= newUserName; |
| 498 | std::string newUserObj(tempObjPath); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 499 | // Special group 'ipmi' needs a way to identify user renamed, in order to |
| 500 | // update encrypted password. It can't rely only on InterfacesRemoved & |
| 501 | // InterfacesAdded. So first send out userRenamed signal. |
| 502 | this->userRenamed(userName, newUserName); |
| 503 | usersList.erase(userName); |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 504 | usersList.emplace(newUserName, std::make_unique<phosphor::user::Users>( |
| 505 | bus, newUserObj.c_str(), groupNames, |
| 506 | priv, enabled, *this)); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 510 | void UserMgr::updateGroupsAndPriv(const std::string& userName, |
Nan Zhou | fef6303 | 2022-10-25 00:07:12 +0000 | [diff] [blame] | 511 | std::vector<std::string> groupNames, |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 512 | const std::string& priv) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 513 | { |
| 514 | throwForInvalidPrivilege(priv); |
| 515 | throwForInvalidGroups(groupNames); |
| 516 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 517 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 518 | throwForUserDoesNotExist(userName); |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 519 | const std::vector<std::string>& oldGroupNames = |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 520 | usersList[userName].get()->userGroups(); |
| 521 | std::vector<std::string> groupDiff; |
| 522 | // Note: already dealing with sorted group lists. |
| 523 | std::set_symmetric_difference(oldGroupNames.begin(), oldGroupNames.end(), |
| 524 | groupNames.begin(), groupNames.end(), |
| 525 | std::back_inserter(groupDiff)); |
| 526 | if (std::find(groupDiff.begin(), groupDiff.end(), "ipmi") != |
| 527 | groupDiff.end()) |
| 528 | { |
| 529 | throwForUserNameConstraints(userName, groupNames); |
| 530 | throwForMaxGrpUserCount(groupNames); |
| 531 | } |
| 532 | |
| 533 | std::string groups = getCSVFromVector(groupNames); |
| 534 | bool sshRequested = removeStringFromCSV(groups, grpSsh); |
| 535 | |
| 536 | // treat privilege as a group - This is to avoid using different file to |
| 537 | // store the same. |
Richard Marian Thomaiyar | 2cb2e72 | 2018-09-27 14:22:42 +0530 | [diff] [blame] | 538 | if (!priv.empty()) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 539 | { |
Richard Marian Thomaiyar | 2cb2e72 | 2018-09-27 14:22:42 +0530 | [diff] [blame] | 540 | if (groups.size() != 0) |
| 541 | { |
| 542 | groups += ","; |
| 543 | } |
| 544 | groups += priv; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 545 | } |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 546 | try |
| 547 | { |
Nan Zhou | fef6303 | 2022-10-25 00:07:12 +0000 | [diff] [blame] | 548 | executeUserModify(userName.c_str(), groups.c_str(), sshRequested); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 549 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 550 | catch (const InternalFailure& e) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 551 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 552 | lg2::error( |
| 553 | "Unable to modify user privilege / groups for user '{USERNAME}'", |
| 554 | "USERNAME", userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 555 | elog<InternalFailure>(); |
| 556 | } |
| 557 | |
Nan Zhou | fef6303 | 2022-10-25 00:07:12 +0000 | [diff] [blame] | 558 | std::sort(groupNames.begin(), groupNames.end()); |
| 559 | usersList[userName]->setUserGroups(groupNames); |
| 560 | usersList[userName]->setUserPrivilege(priv); |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 561 | lg2::info("User '{USERNAME}' groups / privilege updated successfully", |
| 562 | "USERNAME", userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 563 | } |
| 564 | |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 565 | uint8_t UserMgr::minPasswordLength(uint8_t value) |
| 566 | { |
| 567 | if (value == AccountPolicyIface::minPasswordLength()) |
| 568 | { |
| 569 | return value; |
| 570 | } |
| 571 | if (value < minPasswdLength) |
| 572 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 573 | lg2::error("Attempting to set minPasswordLength to {VALUE}, less than " |
| 574 | "{MINVALUE}", |
| 575 | "VALUE", value, "MINVALUE", minPasswdLength); |
Paul Fertser | 6dc7ed9 | 2022-01-21 19:36:34 +0000 | [diff] [blame] | 576 | elog<InvalidArgument>( |
| 577 | Argument::ARGUMENT_NAME("minPasswordLength"), |
| 578 | Argument::ARGUMENT_VALUE(std::to_string(value).c_str())); |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 579 | } |
| 580 | if (setPamModuleArgValue(pamCrackLib, minPasswdLenProp, |
| 581 | std::to_string(value)) != success) |
| 582 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 583 | lg2::error("Unable to set minPasswordLength to {VALUE}", "VALUE", |
| 584 | value); |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 585 | elog<InternalFailure>(); |
| 586 | } |
| 587 | return AccountPolicyIface::minPasswordLength(value); |
| 588 | } |
| 589 | |
| 590 | uint8_t UserMgr::rememberOldPasswordTimes(uint8_t value) |
| 591 | { |
| 592 | if (value == AccountPolicyIface::rememberOldPasswordTimes()) |
| 593 | { |
| 594 | return value; |
| 595 | } |
| 596 | if (setPamModuleArgValue(pamPWHistory, remOldPasswdCount, |
| 597 | std::to_string(value)) != success) |
| 598 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 599 | lg2::error("Unable to set rememberOldPasswordTimes to {VALUE}", "VALUE", |
| 600 | value); |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 601 | elog<InternalFailure>(); |
| 602 | } |
| 603 | return AccountPolicyIface::rememberOldPasswordTimes(value); |
| 604 | } |
| 605 | |
| 606 | uint16_t UserMgr::maxLoginAttemptBeforeLockout(uint16_t value) |
| 607 | { |
| 608 | if (value == AccountPolicyIface::maxLoginAttemptBeforeLockout()) |
| 609 | { |
| 610 | return value; |
| 611 | } |
| 612 | if (setPamModuleArgValue(pamTally2, maxFailedAttempt, |
| 613 | std::to_string(value)) != success) |
| 614 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 615 | lg2::error("Unable to set maxLoginAttemptBeforeLockout to {VALUE}", |
| 616 | "VALUE", value); |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 617 | elog<InternalFailure>(); |
| 618 | } |
| 619 | return AccountPolicyIface::maxLoginAttemptBeforeLockout(value); |
| 620 | } |
| 621 | |
| 622 | uint32_t UserMgr::accountUnlockTimeout(uint32_t value) |
| 623 | { |
| 624 | if (value == AccountPolicyIface::accountUnlockTimeout()) |
| 625 | { |
| 626 | return value; |
| 627 | } |
| 628 | if (setPamModuleArgValue(pamTally2, unlockTimeout, std::to_string(value)) != |
| 629 | success) |
| 630 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 631 | lg2::error("Unable to set accountUnlockTimeout to {VALUE}", "VALUE", |
| 632 | value); |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 633 | elog<InternalFailure>(); |
| 634 | } |
| 635 | return AccountPolicyIface::accountUnlockTimeout(value); |
| 636 | } |
| 637 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 638 | int UserMgr::getPamModuleArgValue(const std::string& moduleName, |
| 639 | const std::string& argName, |
| 640 | std::string& argValue) |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 641 | { |
| 642 | std::string fileName; |
| 643 | if (moduleName == pamTally2) |
| 644 | { |
| 645 | fileName = pamAuthConfigFile; |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | fileName = pamPasswdConfigFile; |
| 650 | } |
| 651 | std::ifstream fileToRead(fileName, std::ios::in); |
| 652 | if (!fileToRead.is_open()) |
| 653 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 654 | lg2::error("Failed to open pam configuration file {FILENAME}", |
| 655 | "FILENAME", fileName); |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 656 | return failure; |
| 657 | } |
| 658 | std::string line; |
| 659 | auto argSearch = argName + "="; |
| 660 | size_t startPos = 0; |
| 661 | size_t endPos = 0; |
| 662 | while (getline(fileToRead, line)) |
| 663 | { |
| 664 | // skip comments section starting with # |
| 665 | if ((startPos = line.find('#')) != std::string::npos) |
| 666 | { |
| 667 | if (startPos == 0) |
| 668 | { |
| 669 | continue; |
| 670 | } |
| 671 | // skip comments after meaningful section and process those |
| 672 | line = line.substr(0, startPos); |
| 673 | } |
| 674 | if (line.find(moduleName) != std::string::npos) |
| 675 | { |
| 676 | if ((startPos = line.find(argSearch)) != std::string::npos) |
| 677 | { |
| 678 | if ((endPos = line.find(' ', startPos)) == std::string::npos) |
| 679 | { |
| 680 | endPos = line.size(); |
| 681 | } |
| 682 | startPos += argSearch.size(); |
| 683 | argValue = line.substr(startPos, endPos - startPos); |
| 684 | return success; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | return failure; |
| 689 | } |
| 690 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 691 | int UserMgr::setPamModuleArgValue(const std::string& moduleName, |
| 692 | const std::string& argName, |
| 693 | const std::string& argValue) |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 694 | { |
| 695 | std::string fileName; |
| 696 | if (moduleName == pamTally2) |
| 697 | { |
| 698 | fileName = pamAuthConfigFile; |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | fileName = pamPasswdConfigFile; |
| 703 | } |
| 704 | std::string tmpFileName = fileName + "_tmp"; |
| 705 | std::ifstream fileToRead(fileName, std::ios::in); |
| 706 | std::ofstream fileToWrite(tmpFileName, std::ios::out); |
| 707 | if (!fileToRead.is_open() || !fileToWrite.is_open()) |
| 708 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 709 | lg2::error("Failed to open pam configuration file {FILENAME}", |
| 710 | "FILENAME", fileName); |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 711 | return failure; |
| 712 | } |
| 713 | std::string line; |
| 714 | auto argSearch = argName + "="; |
| 715 | size_t startPos = 0; |
| 716 | size_t endPos = 0; |
| 717 | bool found = false; |
| 718 | while (getline(fileToRead, line)) |
| 719 | { |
| 720 | // skip comments section starting with # |
| 721 | if ((startPos = line.find('#')) != std::string::npos) |
| 722 | { |
| 723 | if (startPos == 0) |
| 724 | { |
| 725 | fileToWrite << line << std::endl; |
| 726 | continue; |
| 727 | } |
| 728 | // skip comments after meaningful section and process those |
| 729 | line = line.substr(0, startPos); |
| 730 | } |
| 731 | if (line.find(moduleName) != std::string::npos) |
| 732 | { |
| 733 | if ((startPos = line.find(argSearch)) != std::string::npos) |
| 734 | { |
| 735 | if ((endPos = line.find(' ', startPos)) == std::string::npos) |
| 736 | { |
| 737 | endPos = line.size(); |
| 738 | } |
| 739 | startPos += argSearch.size(); |
| 740 | fileToWrite << line.substr(0, startPos) << argValue |
| 741 | << line.substr(endPos, line.size() - endPos) |
| 742 | << std::endl; |
| 743 | found = true; |
| 744 | continue; |
| 745 | } |
| 746 | } |
| 747 | fileToWrite << line << std::endl; |
| 748 | } |
| 749 | fileToWrite.close(); |
| 750 | fileToRead.close(); |
| 751 | if (found) |
| 752 | { |
| 753 | if (std::rename(tmpFileName.c_str(), fileName.c_str()) == 0) |
| 754 | { |
| 755 | return success; |
| 756 | } |
| 757 | } |
| 758 | return failure; |
| 759 | } |
| 760 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 761 | void UserMgr::userEnable(const std::string& userName, bool enabled) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 762 | { |
| 763 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 764 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 765 | throwForUserDoesNotExist(userName); |
| 766 | try |
| 767 | { |
Nan Zhou | 6b6f2d8 | 2022-10-25 00:07:17 +0000 | [diff] [blame] | 768 | executeUserModifyUserEnable(userName.c_str(), enabled); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 769 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 770 | catch (const InternalFailure& e) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 771 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 772 | lg2::error("Unable to modify user enabled state for '{USERNAME}'", |
| 773 | "USERNAME", userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 774 | elog<InternalFailure>(); |
| 775 | } |
| 776 | |
Nan Zhou | 6b6f2d8 | 2022-10-25 00:07:17 +0000 | [diff] [blame] | 777 | usersList[userName]->setUserEnabled(enabled); |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 778 | lg2::info("User '{USERNAME}' has been {STATUS}", "USERNAME", userName, |
| 779 | "STATUS", enabled ? "Enabled" : "Disabled"); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 780 | } |
| 781 | |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 782 | /** |
| 783 | * pam_tally2 app will provide the user failure count and failure status |
| 784 | * in second line of output with words position [0] - user name, |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 785 | * [1] - failure count, [2] - latest failure date, [3] - latest failure time |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 786 | * [4] - failure app |
| 787 | **/ |
| 788 | |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 789 | static constexpr size_t t2FailCntIdx = 1; |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 790 | static constexpr size_t t2FailDateIdx = 2; |
| 791 | static constexpr size_t t2FailTimeIdx = 3; |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 792 | static constexpr size_t t2OutputIndex = 1; |
| 793 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 794 | bool UserMgr::userLockedForFailedAttempt(const std::string& userName) |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 795 | { |
| 796 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 797 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 798 | if (AccountPolicyIface::maxLoginAttemptBeforeLockout() == 0) |
| 799 | { |
| 800 | return false; |
| 801 | } |
| 802 | |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 803 | std::vector<std::string> output; |
Jiaqing Zhao | 8557d32 | 2022-06-23 23:00:01 +0800 | [diff] [blame] | 804 | try |
| 805 | { |
Nan Zhou | a295303 | 2022-11-11 21:50:32 +0000 | [diff] [blame] | 806 | output = getFailedAttempt(userName.c_str()); |
Jiaqing Zhao | 8557d32 | 2022-06-23 23:00:01 +0800 | [diff] [blame] | 807 | } |
| 808 | catch (const InternalFailure& e) |
| 809 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 810 | lg2::error("Unable to read login failure counter"); |
Jiaqing Zhao | 8557d32 | 2022-06-23 23:00:01 +0800 | [diff] [blame] | 811 | elog<InternalFailure>(); |
| 812 | } |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 813 | |
| 814 | std::vector<std::string> splitWords; |
| 815 | boost::algorithm::split(splitWords, output[t2OutputIndex], |
| 816 | boost::algorithm::is_any_of("\t "), |
| 817 | boost::token_compress_on); |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 818 | uint16_t failAttempts = 0; |
Richard Marian Thomaiyar | f5c2df5 | 2018-11-22 23:24:25 +0530 | [diff] [blame] | 819 | try |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 820 | { |
Richard Marian Thomaiyar | f5c2df5 | 2018-11-22 23:24:25 +0530 | [diff] [blame] | 821 | unsigned long tmp = std::stoul(splitWords[t2FailCntIdx], nullptr); |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 822 | if (tmp > std::numeric_limits<decltype(failAttempts)>::max()) |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 823 | { |
Richard Marian Thomaiyar | f5c2df5 | 2018-11-22 23:24:25 +0530 | [diff] [blame] | 824 | throw std::out_of_range("Out of range"); |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 825 | } |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 826 | failAttempts = static_cast<decltype(failAttempts)>(tmp); |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 827 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 828 | catch (const std::exception& e) |
Richard Marian Thomaiyar | f5c2df5 | 2018-11-22 23:24:25 +0530 | [diff] [blame] | 829 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 830 | lg2::error("Exception for userLockedForFailedAttempt: {ERR}", "ERR", e); |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 831 | elog<InternalFailure>(); |
Richard Marian Thomaiyar | f5c2df5 | 2018-11-22 23:24:25 +0530 | [diff] [blame] | 832 | } |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 833 | |
| 834 | if (failAttempts < AccountPolicyIface::maxLoginAttemptBeforeLockout()) |
| 835 | { |
| 836 | return false; |
| 837 | } |
| 838 | |
| 839 | // When failedAttempts is not 0, Latest failure date/time should be |
| 840 | // available |
| 841 | if (splitWords.size() < 4) |
| 842 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 843 | lg2::error("Unable to read latest failure date/time"); |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 844 | elog<InternalFailure>(); |
| 845 | } |
| 846 | |
| 847 | const std::string failDateTime = |
| 848 | splitWords[t2FailDateIdx] + ' ' + splitWords[t2FailTimeIdx]; |
| 849 | |
| 850 | // NOTE: Cannot use std::get_time() here as the implementation of %y in |
| 851 | // libstdc++ does not match POSIX strptime() before gcc 12.1.0 |
| 852 | // https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=a8d3c98746098e2784be7144c1ccc9fcc34a0888 |
| 853 | std::tm tmStruct = {}; |
| 854 | if (!strptime(failDateTime.c_str(), "%D %H:%M:%S", &tmStruct)) |
| 855 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 856 | lg2::error("Failed to parse latest failure date/time"); |
Jiaqing Zhao | fba4bb1 | 2022-06-24 01:30:57 +0800 | [diff] [blame] | 857 | elog<InternalFailure>(); |
| 858 | } |
| 859 | |
| 860 | time_t failTimestamp = std::mktime(&tmStruct); |
| 861 | if (failTimestamp + |
| 862 | static_cast<time_t>(AccountPolicyIface::accountUnlockTimeout()) <= |
| 863 | std::time(NULL)) |
| 864 | { |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | return true; |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 869 | } |
| 870 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 871 | bool UserMgr::userLockedForFailedAttempt(const std::string& userName, |
| 872 | const bool& value) |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 873 | { |
| 874 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 875 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 876 | if (value == true) |
| 877 | { |
| 878 | return userLockedForFailedAttempt(userName); |
| 879 | } |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 880 | |
Jiaqing Zhao | 8557d32 | 2022-06-23 23:00:01 +0800 | [diff] [blame] | 881 | try |
| 882 | { |
| 883 | executeCmd("/usr/sbin/pam_tally2", "-u", userName.c_str(), "-r"); |
| 884 | } |
| 885 | catch (const InternalFailure& e) |
| 886 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 887 | lg2::error("Unable to reset login failure counter"); |
Jiaqing Zhao | 8557d32 | 2022-06-23 23:00:01 +0800 | [diff] [blame] | 888 | elog<InternalFailure>(); |
| 889 | } |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 890 | |
Richard Marian Thomaiyar | f5c2df5 | 2018-11-22 23:24:25 +0530 | [diff] [blame] | 891 | return userLockedForFailedAttempt(userName); |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 892 | } |
| 893 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 894 | bool UserMgr::userPasswordExpired(const std::string& userName) |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 895 | { |
| 896 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 897 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 898 | |
| 899 | struct spwd spwd |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 900 | {}; |
| 901 | struct spwd* spwdPtr = nullptr; |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 902 | auto buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 903 | if (buflen < -1) |
| 904 | { |
| 905 | // Use a default size if there is no hard limit suggested by sysconf() |
| 906 | buflen = 1024; |
| 907 | } |
| 908 | std::vector<char> buffer(buflen); |
| 909 | auto status = |
| 910 | getspnam_r(userName.c_str(), &spwd, buffer.data(), buflen, &spwdPtr); |
| 911 | // On success, getspnam_r() returns zero, and sets *spwdPtr to spwd. |
| 912 | // If no matching password record was found, these functions return 0 |
| 913 | // and store NULL in *spwdPtr |
| 914 | if ((status == 0) && (&spwd == spwdPtr)) |
| 915 | { |
| 916 | // Determine password validity per "chage" docs, where: |
| 917 | // spwd.sp_lstchg == 0 means password is expired, and |
| 918 | // spwd.sp_max == -1 means the password does not expire. |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 919 | constexpr long secondsPerDay = 60 * 60 * 24; |
| 920 | long today = static_cast<long>(time(NULL)) / secondsPerDay; |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 921 | if ((spwd.sp_lstchg == 0) || |
| 922 | ((spwd.sp_max != -1) && ((spwd.sp_max + spwd.sp_lstchg) < today))) |
| 923 | { |
| 924 | return true; |
| 925 | } |
| 926 | } |
| 927 | else |
| 928 | { |
Jayaprakash Mutyala | 75be4e6 | 2020-09-18 15:59:06 +0000 | [diff] [blame] | 929 | // User entry is missing in /etc/shadow, indicating no SHA password. |
| 930 | // Treat this as new user without password entry in /etc/shadow |
| 931 | // TODO: Add property to indicate user password was not set yet |
| 932 | // https://github.com/openbmc/phosphor-user-manager/issues/8 |
| 933 | return false; |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | return false; |
| 937 | } |
| 938 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 939 | UserSSHLists UserMgr::getUserAndSshGrpList() |
| 940 | { |
| 941 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 942 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 943 | |
| 944 | std::vector<std::string> userList; |
| 945 | std::vector<std::string> sshUsersList; |
| 946 | struct passwd pw, *pwp = nullptr; |
| 947 | std::array<char, 1024> buffer{}; |
| 948 | |
| 949 | phosphor::user::File passwd(passwdFileName, "r"); |
| 950 | if ((passwd)() == NULL) |
| 951 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 952 | lg2::error("Error opening {FILENAME}", "FILENAME", passwdFileName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 953 | elog<InternalFailure>(); |
| 954 | } |
| 955 | |
| 956 | while (true) |
| 957 | { |
| 958 | auto r = fgetpwent_r((passwd)(), &pw, buffer.data(), buffer.max_size(), |
| 959 | &pwp); |
| 960 | if ((r != 0) || (pwp == NULL)) |
| 961 | { |
| 962 | // Any error, break the loop. |
| 963 | break; |
| 964 | } |
Richard Marian Thomaiyar | d4d6550 | 2019-11-02 21:02:03 +0530 | [diff] [blame] | 965 | #ifdef ENABLE_ROOT_USER_MGMT |
Richard Marian Thomaiyar | 7ba3c71 | 2018-07-31 13:41:36 +0530 | [diff] [blame] | 966 | // Add all users whose UID >= 1000 and < 65534 |
| 967 | // and special UID 0. |
| 968 | if ((pwp->pw_uid == 0) || |
| 969 | ((pwp->pw_uid >= 1000) && (pwp->pw_uid < 65534))) |
Richard Marian Thomaiyar | d4d6550 | 2019-11-02 21:02:03 +0530 | [diff] [blame] | 970 | #else |
| 971 | // Add all users whose UID >=1000 and < 65534 |
| 972 | if ((pwp->pw_uid >= 1000) && (pwp->pw_uid < 65534)) |
| 973 | #endif |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 974 | { |
| 975 | std::string userName(pwp->pw_name); |
| 976 | userList.emplace_back(userName); |
| 977 | |
| 978 | // ssh doesn't have separate group. Check login shell entry to |
| 979 | // get all users list which are member of ssh group. |
| 980 | std::string loginShell(pwp->pw_shell); |
| 981 | if (loginShell == "/bin/sh") |
| 982 | { |
| 983 | sshUsersList.emplace_back(userName); |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | endpwent(); |
| 988 | return std::make_pair(std::move(userList), std::move(sshUsersList)); |
| 989 | } |
| 990 | |
| 991 | size_t UserMgr::getIpmiUsersCount() |
| 992 | { |
| 993 | std::vector<std::string> userList = getUsersInGroup("ipmi"); |
| 994 | return userList.size(); |
| 995 | } |
| 996 | |
Nan Zhou | 49c8136 | 2022-10-25 00:07:08 +0000 | [diff] [blame] | 997 | size_t UserMgr::getNonIpmiUsersCount() |
| 998 | { |
| 999 | std::vector<std::string> ipmiUsers = getUsersInGroup("ipmi"); |
| 1000 | return usersList.size() - ipmiUsers.size(); |
| 1001 | } |
| 1002 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1003 | bool UserMgr::isUserEnabled(const std::string& userName) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1004 | { |
| 1005 | // All user management lock has to be based on /etc/shadow |
Andrew Geissler | a260f18 | 2021-05-14 12:20:12 -0500 | [diff] [blame] | 1006 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1007 | std::array<char, 4096> buffer{}; |
| 1008 | struct spwd spwd; |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1009 | struct spwd* resultPtr = nullptr; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1010 | int status = getspnam_r(userName.c_str(), &spwd, buffer.data(), |
| 1011 | buffer.max_size(), &resultPtr); |
| 1012 | if (!status && (&spwd == resultPtr)) |
| 1013 | { |
| 1014 | if (resultPtr->sp_expire >= 0) |
| 1015 | { |
| 1016 | return false; // user locked out |
| 1017 | } |
| 1018 | return true; |
| 1019 | } |
| 1020 | return false; // assume user is disabled for any error. |
| 1021 | } |
| 1022 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1023 | std::vector<std::string> UserMgr::getUsersInGroup(const std::string& groupName) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1024 | { |
| 1025 | std::vector<std::string> usersInGroup; |
| 1026 | // Should be more than enough to get the pwd structure. |
| 1027 | std::array<char, 4096> buffer{}; |
| 1028 | struct group grp; |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1029 | struct group* resultPtr = nullptr; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1030 | |
| 1031 | int status = getgrnam_r(groupName.c_str(), &grp, buffer.data(), |
| 1032 | buffer.max_size(), &resultPtr); |
| 1033 | |
| 1034 | if (!status && (&grp == resultPtr)) |
| 1035 | { |
| 1036 | for (; *(grp.gr_mem) != NULL; ++(grp.gr_mem)) |
| 1037 | { |
| 1038 | usersInGroup.emplace_back(*(grp.gr_mem)); |
| 1039 | } |
| 1040 | } |
| 1041 | else |
| 1042 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1043 | lg2::error("Group '{GROUPNAME}' not found", "GROUPNAME", groupName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1044 | // Don't throw error, just return empty userList - fallback |
| 1045 | } |
| 1046 | return usersInGroup; |
| 1047 | } |
| 1048 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1049 | DbusUserObj UserMgr::getPrivilegeMapperObject(void) |
| 1050 | { |
| 1051 | DbusUserObj objects; |
| 1052 | try |
| 1053 | { |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1054 | std::string basePath = "/xyz/openbmc_project/user/ldap/openldap"; |
| 1055 | std::string interface = "xyz.openbmc_project.User.Ldap.Config"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1056 | |
| 1057 | auto ldapMgmtService = |
| 1058 | getServiceName(std::move(basePath), std::move(interface)); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1059 | auto method = bus.new_method_call( |
| 1060 | ldapMgmtService.c_str(), ldapMgrObjBasePath, |
| 1061 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 1062 | |
| 1063 | auto reply = bus.call(method); |
| 1064 | reply.read(objects); |
| 1065 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1066 | catch (const InternalFailure& e) |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1067 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1068 | lg2::error("Unable to get the User Service: {ERR}", "ERR", e); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1069 | throw; |
| 1070 | } |
Patrick Williams | b3ef4e1 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 1071 | catch (const sdbusplus::exception_t& e) |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1072 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1073 | lg2::error("Failed to excute GetManagedObjects at {PATH}: {ERR}", |
| 1074 | "PATH", ldapMgrObjBasePath, "ERR", e); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1075 | throw; |
| 1076 | } |
| 1077 | return objects; |
| 1078 | } |
| 1079 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1080 | std::string UserMgr::getServiceName(std::string&& path, std::string&& intf) |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1081 | { |
| 1082 | auto mapperCall = bus.new_method_call(objMapperService, objMapperPath, |
| 1083 | objMapperInterface, "GetObject"); |
| 1084 | |
| 1085 | mapperCall.append(std::move(path)); |
| 1086 | mapperCall.append(std::vector<std::string>({std::move(intf)})); |
| 1087 | |
| 1088 | auto mapperResponseMsg = bus.call(mapperCall); |
| 1089 | |
| 1090 | if (mapperResponseMsg.is_method_error()) |
| 1091 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1092 | lg2::error("Error in mapper call"); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1093 | elog<InternalFailure>(); |
| 1094 | } |
| 1095 | |
| 1096 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 1097 | mapperResponseMsg.read(mapperResponse); |
| 1098 | |
| 1099 | if (mapperResponse.begin() == mapperResponse.end()) |
| 1100 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1101 | lg2::error("Invalid response from mapper"); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1102 | elog<InternalFailure>(); |
| 1103 | } |
| 1104 | |
| 1105 | return mapperResponse.begin()->first; |
| 1106 | } |
| 1107 | |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1108 | gid_t UserMgr::getPrimaryGroup(const std::string& userName) const |
| 1109 | { |
| 1110 | static auto buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 1111 | if (buflen <= 0) |
| 1112 | { |
| 1113 | // Use a default size if there is no hard limit suggested by sysconf() |
| 1114 | buflen = 1024; |
| 1115 | } |
| 1116 | |
| 1117 | struct passwd pwd; |
| 1118 | struct passwd* pwdPtr = nullptr; |
| 1119 | std::vector<char> buffer(buflen); |
| 1120 | |
| 1121 | auto status = getpwnam_r(userName.c_str(), &pwd, buffer.data(), |
| 1122 | buffer.size(), &pwdPtr); |
| 1123 | // On success, getpwnam_r() returns zero, and set *pwdPtr to pwd. |
| 1124 | // If no matching password record was found, these functions return 0 |
| 1125 | // and store NULL in *pwdPtr |
| 1126 | if (!status && (&pwd == pwdPtr)) |
| 1127 | { |
| 1128 | return pwd.pw_gid; |
| 1129 | } |
| 1130 | |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1131 | lg2::error("User {USERNAME} does not exist", "USERNAME", userName); |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1132 | elog<UserNameDoesNotExist>(); |
| 1133 | } |
| 1134 | |
| 1135 | bool UserMgr::isGroupMember(const std::string& userName, gid_t primaryGid, |
| 1136 | const std::string& groupName) const |
| 1137 | { |
| 1138 | static auto buflen = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 1139 | if (buflen <= 0) |
| 1140 | { |
| 1141 | // Use a default size if there is no hard limit suggested by sysconf() |
| 1142 | buflen = 1024; |
| 1143 | } |
| 1144 | |
| 1145 | struct group grp; |
| 1146 | struct group* grpPtr = nullptr; |
| 1147 | std::vector<char> buffer(buflen); |
| 1148 | |
| 1149 | auto status = getgrnam_r(groupName.c_str(), &grp, buffer.data(), |
| 1150 | buffer.size(), &grpPtr); |
| 1151 | |
| 1152 | // Groups with a lot of members may require a buffer of bigger size than |
| 1153 | // suggested by _SC_GETGR_R_SIZE_MAX. |
| 1154 | // 32K should be enough for about 2K members. |
| 1155 | constexpr auto maxBufferLength = 32 * 1024; |
| 1156 | while (status == ERANGE && buflen < maxBufferLength) |
| 1157 | { |
| 1158 | buflen *= 2; |
| 1159 | buffer.resize(buflen); |
| 1160 | |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1161 | lg2::debug("Increase buffer for getgrnam_r() to {SIZE}", "SIZE", |
| 1162 | buflen); |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1163 | |
| 1164 | status = getgrnam_r(groupName.c_str(), &grp, buffer.data(), |
| 1165 | buffer.size(), &grpPtr); |
| 1166 | } |
| 1167 | |
| 1168 | // On success, getgrnam_r() returns zero, and set *grpPtr to grp. |
| 1169 | // If no matching group record was found, these functions return 0 |
| 1170 | // and store NULL in *grpPtr |
| 1171 | if (!status && (&grp == grpPtr)) |
| 1172 | { |
| 1173 | if (primaryGid == grp.gr_gid) |
| 1174 | { |
| 1175 | return true; |
| 1176 | } |
| 1177 | |
| 1178 | for (auto i = 0; grp.gr_mem && grp.gr_mem[i]; ++i) |
| 1179 | { |
| 1180 | if (userName == grp.gr_mem[i]) |
| 1181 | { |
| 1182 | return true; |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | else if (status == ERANGE) |
| 1187 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1188 | lg2::error("Group info of {GROUP} requires too much memory", "GROUP", |
| 1189 | groupName); |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1190 | } |
| 1191 | else |
| 1192 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1193 | lg2::error("Group {GROUP} does not exist", "GROUP", groupName); |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 1199 | void UserMgr::executeGroupCreation(const char* groupName) |
| 1200 | { |
| 1201 | executeCmd("/usr/sbin/groupadd", groupName); |
| 1202 | } |
| 1203 | |
| 1204 | void UserMgr::executeGroupDeletion(const char* groupName) |
| 1205 | { |
| 1206 | executeCmd("/usr/sbin/groupdel", groupName); |
| 1207 | } |
| 1208 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1209 | UserInfoMap UserMgr::getUserInfo(std::string userName) |
| 1210 | { |
| 1211 | UserInfoMap userInfo; |
| 1212 | // Check whether the given user is local user or not. |
Nan Zhou | 8a11d99 | 2022-10-25 00:07:06 +0000 | [diff] [blame] | 1213 | if (isUserExist(userName)) |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1214 | { |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1215 | const auto& user = usersList[userName]; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1216 | userInfo.emplace("UserPrivilege", user.get()->userPrivilege()); |
| 1217 | userInfo.emplace("UserGroups", user.get()->userGroups()); |
| 1218 | userInfo.emplace("UserEnabled", user.get()->userEnabled()); |
| 1219 | userInfo.emplace("UserLockedForFailedAttempt", |
| 1220 | user.get()->userLockedForFailedAttempt()); |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 1221 | userInfo.emplace("UserPasswordExpired", |
| 1222 | user.get()->userPasswordExpired()); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1223 | userInfo.emplace("RemoteUser", false); |
| 1224 | } |
| 1225 | else |
| 1226 | { |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1227 | auto primaryGid = getPrimaryGroup(userName); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1228 | |
| 1229 | DbusUserObj objects = getPrivilegeMapperObject(); |
| 1230 | |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1231 | std::string ldapConfigPath; |
Jiaqing Zhao | 745ce2e | 2022-05-31 17:11:31 +0800 | [diff] [blame] | 1232 | std::string userPrivilege; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1233 | |
| 1234 | try |
| 1235 | { |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1236 | for (const auto& [path, interfaces] : objects) |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1237 | { |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1238 | auto it = interfaces.find("xyz.openbmc_project.Object.Enable"); |
| 1239 | if (it != interfaces.end()) |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1240 | { |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1241 | auto propIt = it->second.find("Enabled"); |
| 1242 | if (propIt != it->second.end() && |
| 1243 | std::get<bool>(propIt->second)) |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1244 | { |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1245 | ldapConfigPath = path.str + '/'; |
| 1246 | break; |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1247 | } |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1248 | } |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | if (ldapConfigPath.empty()) |
| 1252 | { |
| 1253 | return userInfo; |
| 1254 | } |
| 1255 | |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1256 | for (const auto& [path, interfaces] : objects) |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1257 | { |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1258 | if (!path.str.starts_with(ldapConfigPath)) |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1259 | { |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1260 | continue; |
| 1261 | } |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1262 | |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1263 | auto it = interfaces.find( |
| 1264 | "xyz.openbmc_project.User.PrivilegeMapperEntry"); |
| 1265 | if (it != interfaces.end()) |
| 1266 | { |
| 1267 | std::string privilege; |
| 1268 | std::string groupName; |
| 1269 | |
| 1270 | for (const auto& [propName, propValue] : it->second) |
| 1271 | { |
| 1272 | if (propName == "GroupName") |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1273 | { |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1274 | groupName = std::get<std::string>(propValue); |
Jiaqing Zhao | 745ce2e | 2022-05-31 17:11:31 +0800 | [diff] [blame] | 1275 | } |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1276 | else if (propName == "Privilege") |
Jiaqing Zhao | 745ce2e | 2022-05-31 17:11:31 +0800 | [diff] [blame] | 1277 | { |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1278 | privilege = std::get<std::string>(propValue); |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1279 | } |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1280 | } |
Alexander Filippov | 7562658 | 2022-02-09 18:42:37 +0300 | [diff] [blame] | 1281 | |
| 1282 | if (!groupName.empty() && !privilege.empty() && |
| 1283 | isGroupMember(userName, primaryGid, groupName)) |
| 1284 | { |
| 1285 | userPrivilege = privilege; |
| 1286 | break; |
| 1287 | } |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1288 | } |
Jiaqing Zhao | 745ce2e | 2022-05-31 17:11:31 +0800 | [diff] [blame] | 1289 | if (!userPrivilege.empty()) |
| 1290 | { |
| 1291 | break; |
| 1292 | } |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1293 | } |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 1294 | |
Jiaqing Zhao | 5686206 | 2022-05-31 19:16:09 +0800 | [diff] [blame] | 1295 | if (!userPrivilege.empty()) |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1296 | { |
Jiaqing Zhao | 5686206 | 2022-05-31 19:16:09 +0800 | [diff] [blame] | 1297 | userInfo.emplace("UserPrivilege", userPrivilege); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1298 | } |
Jiaqing Zhao | 5686206 | 2022-05-31 19:16:09 +0800 | [diff] [blame] | 1299 | else |
| 1300 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1301 | lg2::warning("LDAP group privilege mapping does not exist, " |
| 1302 | "default \"priv-user\" is used"); |
Jiaqing Zhao | 5686206 | 2022-05-31 19:16:09 +0800 | [diff] [blame] | 1303 | userInfo.emplace("UserPrivilege", "priv-user"); |
| 1304 | } |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1305 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1306 | catch (const std::bad_variant_access& e) |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1307 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1308 | lg2::error("Error while accessing variant: {ERR}", "ERR", e); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 1309 | elog<InternalFailure>(); |
| 1310 | } |
| 1311 | userInfo.emplace("RemoteUser", true); |
| 1312 | } |
| 1313 | |
| 1314 | return userInfo; |
| 1315 | } |
| 1316 | |
Nan Zhou | 4bc6981 | 2022-10-25 00:07:13 +0000 | [diff] [blame] | 1317 | void UserMgr::initializeAccountPolicy() |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1318 | { |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 1319 | std::string valueStr; |
| 1320 | auto value = minPasswdLength; |
| 1321 | unsigned long tmp = 0; |
| 1322 | if (getPamModuleArgValue(pamCrackLib, minPasswdLenProp, valueStr) != |
| 1323 | success) |
| 1324 | { |
| 1325 | AccountPolicyIface::minPasswordLength(minPasswdLength); |
| 1326 | } |
| 1327 | else |
| 1328 | { |
| 1329 | try |
| 1330 | { |
| 1331 | tmp = std::stoul(valueStr, nullptr); |
| 1332 | if (tmp > std::numeric_limits<decltype(value)>::max()) |
| 1333 | { |
| 1334 | throw std::out_of_range("Out of range"); |
| 1335 | } |
| 1336 | value = static_cast<decltype(value)>(tmp); |
| 1337 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1338 | catch (const std::exception& e) |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 1339 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1340 | lg2::error("Exception for MinPasswordLength: {ERR}", "ERR", e); |
Patrick Venture | 045b112 | 2018-10-16 15:59:29 -0700 | [diff] [blame] | 1341 | throw; |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 1342 | } |
| 1343 | AccountPolicyIface::minPasswordLength(value); |
| 1344 | } |
| 1345 | valueStr.clear(); |
| 1346 | if (getPamModuleArgValue(pamPWHistory, remOldPasswdCount, valueStr) != |
| 1347 | success) |
| 1348 | { |
| 1349 | AccountPolicyIface::rememberOldPasswordTimes(0); |
| 1350 | } |
| 1351 | else |
| 1352 | { |
| 1353 | value = 0; |
| 1354 | try |
| 1355 | { |
| 1356 | tmp = std::stoul(valueStr, nullptr); |
| 1357 | if (tmp > std::numeric_limits<decltype(value)>::max()) |
| 1358 | { |
| 1359 | throw std::out_of_range("Out of range"); |
| 1360 | } |
| 1361 | value = static_cast<decltype(value)>(tmp); |
| 1362 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1363 | catch (const std::exception& e) |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 1364 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1365 | lg2::error("Exception for RememberOldPasswordTimes: {ERR}", "ERR", |
| 1366 | e); |
Patrick Venture | 045b112 | 2018-10-16 15:59:29 -0700 | [diff] [blame] | 1367 | throw; |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 1368 | } |
| 1369 | AccountPolicyIface::rememberOldPasswordTimes(value); |
| 1370 | } |
| 1371 | valueStr.clear(); |
| 1372 | if (getPamModuleArgValue(pamTally2, maxFailedAttempt, valueStr) != success) |
| 1373 | { |
| 1374 | AccountPolicyIface::maxLoginAttemptBeforeLockout(0); |
| 1375 | } |
| 1376 | else |
| 1377 | { |
| 1378 | uint16_t value16 = 0; |
| 1379 | try |
| 1380 | { |
| 1381 | tmp = std::stoul(valueStr, nullptr); |
| 1382 | if (tmp > std::numeric_limits<decltype(value16)>::max()) |
| 1383 | { |
| 1384 | throw std::out_of_range("Out of range"); |
| 1385 | } |
| 1386 | value16 = static_cast<decltype(value16)>(tmp); |
| 1387 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1388 | catch (const std::exception& e) |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 1389 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1390 | lg2::error("Exception for MaxLoginAttemptBeforLockout: {ERR}", |
| 1391 | "ERR", e); |
Patrick Venture | 045b112 | 2018-10-16 15:59:29 -0700 | [diff] [blame] | 1392 | throw; |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 1393 | } |
| 1394 | AccountPolicyIface::maxLoginAttemptBeforeLockout(value16); |
| 1395 | } |
| 1396 | valueStr.clear(); |
| 1397 | if (getPamModuleArgValue(pamTally2, unlockTimeout, valueStr) != success) |
| 1398 | { |
| 1399 | AccountPolicyIface::accountUnlockTimeout(0); |
| 1400 | } |
| 1401 | else |
| 1402 | { |
| 1403 | uint32_t value32 = 0; |
| 1404 | try |
| 1405 | { |
| 1406 | tmp = std::stoul(valueStr, nullptr); |
| 1407 | if (tmp > std::numeric_limits<decltype(value32)>::max()) |
| 1408 | { |
| 1409 | throw std::out_of_range("Out of range"); |
| 1410 | } |
| 1411 | value32 = static_cast<decltype(value32)>(tmp); |
| 1412 | } |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1413 | catch (const std::exception& e) |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 1414 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 1415 | lg2::error("Exception for AccountUnlockTimeout: {ERR}", "ERR", e); |
Patrick Venture | 045b112 | 2018-10-16 15:59:29 -0700 | [diff] [blame] | 1416 | throw; |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 1417 | } |
| 1418 | AccountPolicyIface::accountUnlockTimeout(value32); |
| 1419 | } |
Nan Zhou | 4bc6981 | 2022-10-25 00:07:13 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | void UserMgr::initUserObjects(void) |
| 1423 | { |
| 1424 | // All user management lock has to be based on /etc/shadow |
| 1425 | // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{}; |
| 1426 | std::vector<std::string> userNameList; |
| 1427 | std::vector<std::string> sshGrpUsersList; |
| 1428 | UserSSHLists userSSHLists = getUserAndSshGrpList(); |
| 1429 | userNameList = std::move(userSSHLists.first); |
| 1430 | sshGrpUsersList = std::move(userSSHLists.second); |
| 1431 | |
| 1432 | if (!userNameList.empty()) |
| 1433 | { |
| 1434 | std::map<std::string, std::vector<std::string>> groupLists; |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 1435 | // We only track users that are in the |predefinedGroups| |
| 1436 | // The other groups don't contain real BMC users. |
| 1437 | for (const char* grp : predefinedGroups) |
Nan Zhou | 4bc6981 | 2022-10-25 00:07:13 +0000 | [diff] [blame] | 1438 | { |
| 1439 | if (grp == grpSsh) |
| 1440 | { |
| 1441 | groupLists.emplace(grp, sshGrpUsersList); |
| 1442 | } |
| 1443 | else |
| 1444 | { |
| 1445 | std::vector<std::string> grpUsersList = getUsersInGroup(grp); |
| 1446 | groupLists.emplace(grp, grpUsersList); |
| 1447 | } |
| 1448 | } |
| 1449 | for (auto& grp : privMgr) |
| 1450 | { |
| 1451 | std::vector<std::string> grpUsersList = getUsersInGroup(grp); |
| 1452 | groupLists.emplace(grp, grpUsersList); |
| 1453 | } |
| 1454 | |
| 1455 | for (auto& user : userNameList) |
| 1456 | { |
| 1457 | std::vector<std::string> userGroups; |
| 1458 | std::string userPriv; |
| 1459 | for (const auto& grp : groupLists) |
| 1460 | { |
| 1461 | std::vector<std::string> tempGrp = grp.second; |
| 1462 | if (std::find(tempGrp.begin(), tempGrp.end(), user) != |
| 1463 | tempGrp.end()) |
| 1464 | { |
| 1465 | if (std::find(privMgr.begin(), privMgr.end(), grp.first) != |
| 1466 | privMgr.end()) |
| 1467 | { |
| 1468 | userPriv = grp.first; |
| 1469 | } |
| 1470 | else |
| 1471 | { |
| 1472 | userGroups.emplace_back(grp.first); |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | // Add user objects to the Users path. |
| 1477 | sdbusplus::message::object_path tempObjPath(usersObjPath); |
| 1478 | tempObjPath /= user; |
| 1479 | std::string objPath(tempObjPath); |
| 1480 | std::sort(userGroups.begin(), userGroups.end()); |
| 1481 | usersList.emplace(user, std::make_unique<phosphor::user::Users>( |
| 1482 | bus, objPath.c_str(), userGroups, |
| 1483 | userPriv, isUserEnabled(user), *this)); |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | UserMgr::UserMgr(sdbusplus::bus_t& bus, const char* path) : |
| 1489 | Ifaces(bus, path, Ifaces::action::defer_emit), bus(bus), path(path), |
| 1490 | pamPasswdConfigFile(defaultPamPasswdConfigFile), |
| 1491 | pamAuthConfigFile(defaultPamAuthConfigFile) |
| 1492 | { |
| 1493 | UserMgrIface::allPrivileges(privMgr); |
Nan Zhou | da401fe | 2022-10-25 00:07:18 +0000 | [diff] [blame] | 1494 | groupsMgr = readAllGroupsOnSystem(); |
Nan Zhou | 4bc6981 | 2022-10-25 00:07:13 +0000 | [diff] [blame] | 1495 | std::sort(groupsMgr.begin(), groupsMgr.end()); |
| 1496 | UserMgrIface::allGroups(groupsMgr); |
| 1497 | initializeAccountPolicy(); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1498 | initUserObjects(); |
Ratan Gupta | 1af1223 | 2018-11-03 00:35:38 +0530 | [diff] [blame] | 1499 | |
| 1500 | // emit the signal |
| 1501 | this->emit_object_added(); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1502 | } |
| 1503 | |
Nan Zhou | 49c8136 | 2022-10-25 00:07:08 +0000 | [diff] [blame] | 1504 | void UserMgr::executeUserAdd(const char* userName, const char* groups, |
| 1505 | bool sshRequested, bool enabled) |
| 1506 | { |
| 1507 | // set EXPIRE_DATE to 0 to disable user, PAM takes 0 as expire on |
| 1508 | // 1970-01-01, that's an implementation-defined behavior |
| 1509 | executeCmd("/usr/sbin/useradd", userName, "-G", groups, "-m", "-N", "-s", |
Tang Yiwei | 75ea3e4 | 2022-04-25 10:48:15 +0800 | [diff] [blame] | 1510 | (sshRequested ? "/bin/sh" : "/sbin/nologin"), "-e", |
Nan Zhou | 49c8136 | 2022-10-25 00:07:08 +0000 | [diff] [blame] | 1511 | (enabled ? "" : "1970-01-01")); |
| 1512 | } |
| 1513 | |
| 1514 | void UserMgr::executeUserDelete(const char* userName) |
| 1515 | { |
| 1516 | executeCmd("/usr/sbin/userdel", userName, "-r"); |
| 1517 | } |
| 1518 | |
Nan Zhou | f25443e | 2022-10-25 00:07:11 +0000 | [diff] [blame] | 1519 | void UserMgr::executeUserRename(const char* userName, const char* newUserName) |
| 1520 | { |
| 1521 | std::string newHomeDir = "/home/"; |
| 1522 | newHomeDir += newUserName; |
| 1523 | executeCmd("/usr/sbin/usermod", "-l", newUserName, userName, "-d", |
| 1524 | newHomeDir.c_str(), "-m"); |
| 1525 | } |
| 1526 | |
Nan Zhou | fef6303 | 2022-10-25 00:07:12 +0000 | [diff] [blame] | 1527 | void UserMgr::executeUserModify(const char* userName, const char* newGroups, |
| 1528 | bool sshRequested) |
| 1529 | { |
| 1530 | executeCmd("/usr/sbin/usermod", userName, "-G", newGroups, "-s", |
Tang Yiwei | 75ea3e4 | 2022-04-25 10:48:15 +0800 | [diff] [blame] | 1531 | (sshRequested ? "/bin/sh" : "/sbin/nologin")); |
Nan Zhou | fef6303 | 2022-10-25 00:07:12 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Nan Zhou | 6b6f2d8 | 2022-10-25 00:07:17 +0000 | [diff] [blame] | 1534 | void UserMgr::executeUserModifyUserEnable(const char* userName, bool enabled) |
| 1535 | { |
| 1536 | // set EXPIRE_DATE to 0 to disable user, PAM takes 0 as expire on |
| 1537 | // 1970-01-01, that's an implementation-defined behavior |
| 1538 | executeCmd("/usr/sbin/usermod", userName, "-e", |
| 1539 | (enabled ? "" : "1970-01-01")); |
| 1540 | } |
| 1541 | |
Nan Zhou | a295303 | 2022-11-11 21:50:32 +0000 | [diff] [blame] | 1542 | std::vector<std::string> UserMgr::getFailedAttempt(const char* userName) |
| 1543 | { |
| 1544 | return executeCmd("/usr/sbin/pam_tally2", "-u", userName); |
| 1545 | } |
| 1546 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 1547 | } // namespace user |
| 1548 | } // namespace phosphor |