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 #pragma once 17 18 #include "app.hpp" 19 #include "error_messages.hpp" 20 #include "http/utility.hpp" 21 #include "persistent_data.hpp" 22 #include "query.hpp" 23 #include "registries/privilege_registry.hpp" 24 #include "utils/json_utils.hpp" 25 26 #include <boost/url/format.hpp> 27 28 namespace redfish 29 { 30 31 inline void fillSessionObject(crow::Response& res, 32 const persistent_data::UserSession& session) 33 { 34 res.jsonValue["Id"] = session.uniqueId; 35 res.jsonValue["UserName"] = session.username; 36 res.jsonValue["@odata.id"] = boost::urls::format( 37 "/redfish/v1/SessionService/Sessions/{}", session.uniqueId); 38 res.jsonValue["@odata.type"] = "#Session.v1_5_0.Session"; 39 res.jsonValue["Name"] = "User Session"; 40 res.jsonValue["Description"] = "Manager User Session"; 41 res.jsonValue["ClientOriginIPAddress"] = session.clientIp; 42 if (session.clientId) 43 { 44 res.jsonValue["Context"] = *session.clientId; 45 } 46 } 47 48 inline void 49 handleSessionHead(crow::App& app, const crow::Request& req, 50 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 51 const std::string& /*sessionId*/) 52 { 53 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 54 { 55 return; 56 } 57 asyncResp->res.addHeader( 58 boost::beast::http::field::link, 59 "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby"); 60 } 61 62 inline void 63 handleSessionGet(crow::App& app, const crow::Request& req, 64 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 65 const std::string& sessionId) 66 { 67 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 68 { 69 return; 70 } 71 asyncResp->res.addHeader( 72 boost::beast::http::field::link, 73 "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby"); 74 75 // Note that control also reaches here via doPost and doDelete. 76 auto session = 77 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId); 78 79 if (session == nullptr) 80 { 81 messages::resourceNotFound(asyncResp->res, "Session", sessionId); 82 return; 83 } 84 85 fillSessionObject(asyncResp->res, *session); 86 } 87 88 inline void 89 handleSessionDelete(crow::App& app, const crow::Request& req, 90 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 91 const std::string& sessionId) 92 { 93 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 94 { 95 return; 96 } 97 auto session = 98 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId); 99 100 if (session == nullptr) 101 { 102 messages::resourceNotFound(asyncResp->res, "Session", sessionId); 103 return; 104 } 105 106 // Perform a proper ConfigureSelf authority check. If a 107 // session is being used to DELETE some other user's session, 108 // then the ConfigureSelf privilege does not apply. In that 109 // case, perform the authority check again without the user's 110 // ConfigureSelf privilege. 111 if (req.session != nullptr && !session->username.empty() && 112 session->username != req.session->username) 113 { 114 Privileges effectiveUserPrivileges = 115 redfish::getUserPrivileges(*req.session); 116 117 if (!effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"})) 118 { 119 messages::insufficientPrivilege(asyncResp->res); 120 return; 121 } 122 } 123 124 persistent_data::SessionStore::getInstance().removeSession(session); 125 messages::success(asyncResp->res); 126 } 127 128 inline nlohmann::json getSessionCollectionMembers() 129 { 130 std::vector<const std::string*> sessionIds = 131 persistent_data::SessionStore::getInstance().getUniqueIds( 132 false, persistent_data::PersistenceType::TIMEOUT); 133 nlohmann::json ret = nlohmann::json::array(); 134 for (const std::string* uid : sessionIds) 135 { 136 nlohmann::json::object_t session; 137 session["@odata.id"] = 138 boost::urls::format("/redfish/v1/SessionService/Sessions/{}", *uid); 139 ret.emplace_back(std::move(session)); 140 } 141 return ret; 142 } 143 144 inline void handleSessionCollectionHead( 145 crow::App& app, const crow::Request& req, 146 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 147 { 148 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 149 { 150 return; 151 } 152 asyncResp->res.addHeader( 153 boost::beast::http::field::link, 154 "</redfish/v1/JsonSchemas/SessionCollection.json>; rel=describedby"); 155 } 156 157 inline void handleSessionCollectionGet( 158 crow::App& app, const crow::Request& req, 159 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 160 { 161 handleSessionCollectionHead(app, req, asyncResp); 162 asyncResp->res.jsonValue["Members"] = getSessionCollectionMembers(); 163 asyncResp->res.jsonValue["Members@odata.count"] = 164 asyncResp->res.jsonValue["Members"].size(); 165 asyncResp->res.jsonValue["@odata.type"] = 166 "#SessionCollection.SessionCollection"; 167 asyncResp->res.jsonValue["@odata.id"] = 168 "/redfish/v1/SessionService/Sessions/"; 169 asyncResp->res.jsonValue["Name"] = "Session Collection"; 170 asyncResp->res.jsonValue["Description"] = "Session Collection"; 171 } 172 173 inline void handleSessionCollectionMembersGet( 174 crow::App& app, const crow::Request& req, 175 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 176 { 177 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 178 { 179 return; 180 } 181 asyncResp->res.jsonValue = getSessionCollectionMembers(); 182 } 183 184 inline void handleSessionCollectionPost( 185 crow::App& app, const crow::Request& req, 186 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 187 { 188 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 189 { 190 return; 191 } 192 std::string username; 193 std::string password; 194 std::optional<std::string> clientId; 195 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username, 196 "Password", password, "Context", clientId)) 197 { 198 return; 199 } 200 201 if (password.empty() || username.empty() || 202 asyncResp->res.result() != boost::beast::http::status::ok) 203 { 204 if (username.empty()) 205 { 206 messages::propertyMissing(asyncResp->res, "UserName"); 207 } 208 209 if (password.empty()) 210 { 211 messages::propertyMissing(asyncResp->res, "Password"); 212 } 213 214 return; 215 } 216 217 int pamrc = pamAuthenticateUser(username, password); 218 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD; 219 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly) 220 { 221 messages::resourceAtUriUnauthorized(asyncResp->res, req.url(), 222 "Invalid username or password"); 223 return; 224 } 225 226 // User is authenticated - create session 227 std::shared_ptr<persistent_data::UserSession> session = 228 persistent_data::SessionStore::getInstance().generateUserSession( 229 username, req.ipAddress, clientId, 230 persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly); 231 if (session == nullptr) 232 { 233 messages::internalError(asyncResp->res); 234 return; 235 } 236 237 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken); 238 asyncResp->res.addHeader( 239 "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId); 240 asyncResp->res.result(boost::beast::http::status::created); 241 if (session->isConfigureSelfOnly) 242 { 243 messages::passwordChangeRequired( 244 asyncResp->res, 245 boost::urls::format("/redfish/v1/AccountService/Accounts/{}", 246 session->username)); 247 } 248 249 fillSessionObject(asyncResp->res, *session); 250 } 251 inline void handleSessionServiceHead( 252 crow::App& app, const crow::Request& req, 253 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 254 { 255 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 256 { 257 return; 258 } 259 asyncResp->res.addHeader( 260 boost::beast::http::field::link, 261 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby"); 262 } 263 inline void 264 handleSessionServiceGet(crow::App& app, const crow::Request& req, 265 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 266 267 { 268 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 269 { 270 return; 271 } 272 asyncResp->res.addHeader( 273 boost::beast::http::field::link, 274 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby"); 275 276 asyncResp->res.jsonValue["@odata.type"] = 277 "#SessionService.v1_0_2.SessionService"; 278 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/"; 279 asyncResp->res.jsonValue["Name"] = "Session Service"; 280 asyncResp->res.jsonValue["Id"] = "SessionService"; 281 asyncResp->res.jsonValue["Description"] = "Session Service"; 282 asyncResp->res.jsonValue["SessionTimeout"] = 283 persistent_data::SessionStore::getInstance().getTimeoutInSeconds(); 284 asyncResp->res.jsonValue["ServiceEnabled"] = true; 285 286 asyncResp->res.jsonValue["Sessions"]["@odata.id"] = 287 "/redfish/v1/SessionService/Sessions"; 288 } 289 290 inline void handleSessionServicePatch( 291 crow::App& app, const crow::Request& req, 292 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 293 { 294 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 295 { 296 return; 297 } 298 std::optional<int64_t> sessionTimeout; 299 if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout", 300 sessionTimeout)) 301 { 302 return; 303 } 304 305 if (sessionTimeout) 306 { 307 // The mininum & maximum allowed values for session timeout 308 // are 30 seconds and 86400 seconds respectively as per the 309 // session service schema mentioned at 310 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json 311 312 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30) 313 { 314 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout); 315 persistent_data::SessionStore::getInstance().updateSessionTimeout( 316 sessionTimeoutInseconds); 317 messages::propertyValueModified(asyncResp->res, "SessionTimeOut", 318 std::to_string(*sessionTimeout)); 319 } 320 else 321 { 322 messages::propertyValueNotInList(asyncResp->res, 323 std::to_string(*sessionTimeout), 324 "SessionTimeOut"); 325 } 326 } 327 } 328 329 inline void requestRoutesSession(App& app) 330 { 331 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/") 332 .privileges(redfish::privileges::headSession) 333 .methods(boost::beast::http::verb::head)( 334 std::bind_front(handleSessionHead, std::ref(app))); 335 336 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/") 337 .privileges(redfish::privileges::getSession) 338 .methods(boost::beast::http::verb::get)( 339 std::bind_front(handleSessionGet, std::ref(app))); 340 341 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/") 342 .privileges(redfish::privileges::deleteSession) 343 .methods(boost::beast::http::verb::delete_)( 344 std::bind_front(handleSessionDelete, std::ref(app))); 345 346 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/") 347 .privileges(redfish::privileges::headSessionCollection) 348 .methods(boost::beast::http::verb::head)( 349 std::bind_front(handleSessionCollectionHead, std::ref(app))); 350 351 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/") 352 .privileges(redfish::privileges::getSessionCollection) 353 .methods(boost::beast::http::verb::get)( 354 std::bind_front(handleSessionCollectionGet, std::ref(app))); 355 356 // Note, the next two routes technically don't match the privilege 357 // registry given the way login mechanisms work. The base privilege 358 // registry lists this endpoint as requiring login privilege, but because 359 // this is the endpoint responsible for giving the login privilege, and it 360 // is itself its own route, it needs to not require Login 361 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/") 362 .privileges({}) 363 .methods(boost::beast::http::verb::post)( 364 std::bind_front(handleSessionCollectionPost, std::ref(app))); 365 366 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/") 367 .privileges({}) 368 .methods(boost::beast::http::verb::post)( 369 std::bind_front(handleSessionCollectionPost, std::ref(app))); 370 371 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/") 372 .privileges(redfish::privileges::headSessionService) 373 .methods(boost::beast::http::verb::head)( 374 std::bind_front(handleSessionServiceHead, std::ref(app))); 375 376 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/") 377 .privileges(redfish::privileges::getSessionService) 378 .methods(boost::beast::http::verb::get)( 379 std::bind_front(handleSessionServiceGet, std::ref(app))); 380 381 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/") 382 .privileges(redfish::privileges::patchSessionService) 383 .methods(boost::beast::http::verb::patch)( 384 std::bind_front(handleSessionServicePatch, std::ref(app))); 385 } 386 387 } // namespace redfish 388