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