1 #pragma once 2 3 #include <app.hpp> 4 #include <async_resp.hpp> 5 #include <boost/algorithm/string.hpp> 6 #include <boost/container/flat_set.hpp> 7 #include <error_messages.hpp> 8 #include <event_service_manager.hpp> 9 #include <ibm/locks.hpp> 10 #include <nlohmann/json.hpp> 11 #include <resource_messages.hpp> 12 #include <sdbusplus/message/types.hpp> 13 #include <utils/json_utils.hpp> 14 15 #include <filesystem> 16 #include <fstream> 17 18 using SType = std::string; 19 using SegmentFlags = std::vector<std::pair<std::string, uint32_t>>; 20 using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>; 21 using LockRequests = std::vector<LockRequest>; 22 using Rc = std::pair<bool, std::variant<uint32_t, LockRequest>>; 23 using RcGetLockList = 24 std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>; 25 using ListOfSessionIds = std::vector<std::string>; 26 namespace crow 27 { 28 namespace ibm_mc 29 { 30 constexpr const char* methodNotAllowedMsg = "Method Not Allowed"; 31 constexpr const char* resourceNotFoundMsg = "Resource Not Found"; 32 constexpr const char* contentNotAcceptableMsg = "Content Not Acceptable"; 33 constexpr const char* internalServerError = "Internal Server Error"; 34 35 constexpr size_t maxSaveareaDirSize = 36 10000000; // Allow save area dir size to be max 10MB 37 constexpr size_t minSaveareaFileSize = 38 100; // Allow save area file size of minimum 100B 39 constexpr size_t maxSaveareaFileSize = 40 500000; // Allow save area file size upto 500KB 41 constexpr size_t maxBroadcastMsgSize = 42 1000; // Allow Broadcast message size upto 1KB 43 44 inline void handleFilePut(const crow::Request& req, 45 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 46 const std::string& fileID) 47 { 48 std::error_code ec; 49 // Check the content-type of the request 50 boost::beast::string_view contentType = req.getHeaderValue("content-type"); 51 if (!boost::iequals(contentType, "application/octet-stream")) 52 { 53 asyncResp->res.result(boost::beast::http::status::not_acceptable); 54 asyncResp->res.jsonValue["Description"] = contentNotAcceptableMsg; 55 return; 56 } 57 BMCWEB_LOG_DEBUG 58 << "File upload in application/octet-stream format. Continue.."; 59 60 BMCWEB_LOG_DEBUG 61 << "handleIbmPut: Request to create/update the save-area file"; 62 std::string_view path = 63 "/var/lib/bmcweb/ibm-management-console/configfiles"; 64 if (!crow::ibm_utils::createDirectory(path)) 65 { 66 asyncResp->res.result(boost::beast::http::status::not_found); 67 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; 68 return; 69 } 70 71 std::ofstream file; 72 std::filesystem::path loc( 73 "/var/lib/bmcweb/ibm-management-console/configfiles"); 74 75 // Get the current size of the savearea directory 76 std::filesystem::recursive_directory_iterator iter(loc, ec); 77 if (ec) 78 { 79 asyncResp->res.result( 80 boost::beast::http::status::internal_server_error); 81 asyncResp->res.jsonValue["Description"] = internalServerError; 82 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to prepare save-area " 83 "directory iterator. ec : " 84 << ec; 85 return; 86 } 87 std::uintmax_t saveAreaDirSize = 0; 88 for (const auto& it : iter) 89 { 90 if (!std::filesystem::is_directory(it, ec)) 91 { 92 if (ec) 93 { 94 asyncResp->res.result( 95 boost::beast::http::status::internal_server_error); 96 asyncResp->res.jsonValue["Description"] = internalServerError; 97 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area " 98 "directory . ec : " 99 << ec; 100 return; 101 } 102 std::uintmax_t fileSize = std::filesystem::file_size(it, ec); 103 if (ec) 104 { 105 asyncResp->res.result( 106 boost::beast::http::status::internal_server_error); 107 asyncResp->res.jsonValue["Description"] = internalServerError; 108 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area " 109 "file size inside the directory . ec : " 110 << ec; 111 return; 112 } 113 saveAreaDirSize += fileSize; 114 } 115 } 116 BMCWEB_LOG_DEBUG << "saveAreaDirSize: " << saveAreaDirSize; 117 118 // Get the file size getting uploaded 119 const std::string& data = req.body; 120 BMCWEB_LOG_DEBUG << "data length: " << data.length(); 121 122 if (data.length() < minSaveareaFileSize) 123 { 124 asyncResp->res.result(boost::beast::http::status::bad_request); 125 asyncResp->res.jsonValue["Description"] = 126 "File size is less than minimum allowed size[100B]"; 127 return; 128 } 129 if (data.length() > maxSaveareaFileSize) 130 { 131 asyncResp->res.result(boost::beast::http::status::bad_request); 132 asyncResp->res.jsonValue["Description"] = 133 "File size exceeds maximum allowed size[500KB]"; 134 return; 135 } 136 137 // Form the file path 138 loc /= fileID; 139 BMCWEB_LOG_DEBUG << "Writing to the file: " << loc.string(); 140 141 // Check if the same file exists in the directory 142 bool fileExists = std::filesystem::exists(loc, ec); 143 if (ec) 144 { 145 asyncResp->res.result( 146 boost::beast::http::status::internal_server_error); 147 asyncResp->res.jsonValue["Description"] = internalServerError; 148 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find if file exists. ec : " 149 << ec; 150 return; 151 } 152 153 std::uintmax_t newSizeToWrite = 0; 154 if (fileExists) 155 { 156 // File exists. Get the current file size 157 std::uintmax_t currentFileSize = std::filesystem::file_size(loc, ec); 158 if (ec) 159 { 160 asyncResp->res.result( 161 boost::beast::http::status::internal_server_error); 162 asyncResp->res.jsonValue["Description"] = internalServerError; 163 BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find file size. ec : " 164 << ec; 165 return; 166 } 167 // Calculate the difference in the file size. 168 // If the data.length is greater than the existing file size, then 169 // calculate the difference. Else consider the delta size as zero - 170 // because there is no increase in the total directory size. 171 // We need to add the diff only if the incoming data is larger than the 172 // existing filesize 173 if (data.length() > currentFileSize) 174 { 175 newSizeToWrite = data.length() - currentFileSize; 176 } 177 BMCWEB_LOG_DEBUG << "newSizeToWrite: " << newSizeToWrite; 178 } 179 else 180 { 181 // This is a new file upload 182 newSizeToWrite = data.length(); 183 } 184 185 // Calculate the total dir size before writing the new file 186 BMCWEB_LOG_DEBUG << "total new size: " << saveAreaDirSize + newSizeToWrite; 187 188 if ((saveAreaDirSize + newSizeToWrite) > maxSaveareaDirSize) 189 { 190 asyncResp->res.result(boost::beast::http::status::bad_request); 191 asyncResp->res.jsonValue["Description"] = 192 "File size does not fit in the savearea " 193 "directory maximum allowed size[10MB]"; 194 return; 195 } 196 197 file.open(loc, std::ofstream::out); 198 199 // set the permission of the file to 600 200 std::filesystem::perms permission = std::filesystem::perms::owner_write | 201 std::filesystem::perms::owner_read; 202 std::filesystem::permissions(loc, permission); 203 204 if (file.fail()) 205 { 206 BMCWEB_LOG_DEBUG << "Error while opening the file for writing"; 207 asyncResp->res.result( 208 boost::beast::http::status::internal_server_error); 209 asyncResp->res.jsonValue["Description"] = 210 "Error while creating the file"; 211 return; 212 } 213 file << data; 214 215 std::string origin = "/ibm/v1/Host/ConfigFiles/" + fileID; 216 // Push an event 217 if (fileExists) 218 { 219 BMCWEB_LOG_DEBUG << "config file is updated"; 220 asyncResp->res.jsonValue["Description"] = "File Updated"; 221 222 redfish::EventServiceManager::getInstance().sendEvent( 223 redfish::messages::resourceChanged(), origin, "IBMConfigFile"); 224 } 225 else 226 { 227 BMCWEB_LOG_DEBUG << "config file is created"; 228 asyncResp->res.jsonValue["Description"] = "File Created"; 229 230 redfish::EventServiceManager::getInstance().sendEvent( 231 redfish::messages::resourceCreated(), origin, "IBMConfigFile"); 232 } 233 } 234 235 inline void 236 handleConfigFileList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 237 { 238 std::vector<std::string> pathObjList; 239 std::filesystem::path loc( 240 "/var/lib/bmcweb/ibm-management-console/configfiles"); 241 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc)) 242 { 243 for (const auto& file : std::filesystem::directory_iterator(loc)) 244 { 245 const std::filesystem::path& pathObj = file.path(); 246 pathObjList.push_back("/ibm/v1/Host/ConfigFiles/" + 247 pathObj.filename().string()); 248 } 249 } 250 asyncResp->res.jsonValue["@odata.type"] = 251 "#IBMConfigFile.v1_0_0.IBMConfigFile"; 252 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/"; 253 asyncResp->res.jsonValue["Id"] = "ConfigFiles"; 254 asyncResp->res.jsonValue["Name"] = "ConfigFiles"; 255 256 asyncResp->res.jsonValue["Members"] = std::move(pathObjList); 257 asyncResp->res.jsonValue["Actions"]["#IBMConfigFiles.DeleteAll"] = { 258 {"target", 259 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll"}}; 260 } 261 262 inline void 263 deleteConfigFiles(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 264 { 265 std::error_code ec; 266 std::filesystem::path loc( 267 "/var/lib/bmcweb/ibm-management-console/configfiles"); 268 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc)) 269 { 270 std::filesystem::remove_all(loc, ec); 271 if (ec) 272 { 273 asyncResp->res.result( 274 boost::beast::http::status::internal_server_error); 275 asyncResp->res.jsonValue["Description"] = internalServerError; 276 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the " 277 "config files directory. ec : " 278 << ec; 279 } 280 } 281 } 282 283 inline void 284 getLockServiceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 285 { 286 asyncResp->res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService"; 287 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/"; 288 asyncResp->res.jsonValue["Id"] = "LockService"; 289 asyncResp->res.jsonValue["Name"] = "LockService"; 290 291 asyncResp->res.jsonValue["Actions"]["#LockService.AcquireLock"] = { 292 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}}; 293 asyncResp->res.jsonValue["Actions"]["#LockService.ReleaseLock"] = { 294 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}}; 295 asyncResp->res.jsonValue["Actions"]["#LockService.GetLockList"] = { 296 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}}; 297 } 298 299 inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 300 const std::string& fileID) 301 { 302 BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID; 303 std::filesystem::path loc( 304 "/var/lib/bmcweb/ibm-management-console/configfiles/" + fileID); 305 if (!std::filesystem::exists(loc)) 306 { 307 BMCWEB_LOG_ERROR << loc.string() << "Not found"; 308 asyncResp->res.result(boost::beast::http::status::not_found); 309 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; 310 return; 311 } 312 313 std::ifstream readfile(loc.string()); 314 if (!readfile) 315 { 316 BMCWEB_LOG_ERROR << loc.string() << "Not found"; 317 asyncResp->res.result(boost::beast::http::status::not_found); 318 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; 319 return; 320 } 321 322 std::string contentDispositionParam = 323 "attachment; filename=\"" + fileID + "\""; 324 asyncResp->res.addHeader("Content-Disposition", contentDispositionParam); 325 std::string fileData; 326 fileData = {std::istreambuf_iterator<char>(readfile), 327 std::istreambuf_iterator<char>()}; 328 asyncResp->res.jsonValue["Data"] = fileData; 329 } 330 331 inline void 332 handleFileDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 333 const std::string& fileID) 334 { 335 std::string filePath("/var/lib/bmcweb/ibm-management-console/configfiles/" + 336 fileID); 337 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n"; 338 std::ifstream fileOpen(filePath.c_str()); 339 if (static_cast<bool>(fileOpen)) 340 { 341 if (remove(filePath.c_str()) == 0) 342 { 343 BMCWEB_LOG_DEBUG << "File removed!\n"; 344 asyncResp->res.jsonValue["Description"] = "File Deleted"; 345 } 346 else 347 { 348 BMCWEB_LOG_ERROR << "File not removed!\n"; 349 asyncResp->res.result( 350 boost::beast::http::status::internal_server_error); 351 asyncResp->res.jsonValue["Description"] = internalServerError; 352 } 353 } 354 else 355 { 356 BMCWEB_LOG_ERROR << "File not found!\n"; 357 asyncResp->res.result(boost::beast::http::status::not_found); 358 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; 359 } 360 } 361 362 inline void 363 handleBroadcastService(const crow::Request& req, 364 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 365 { 366 std::string broadcastMsg; 367 368 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Message", 369 broadcastMsg)) 370 { 371 BMCWEB_LOG_DEBUG << "Not a Valid JSON"; 372 asyncResp->res.result(boost::beast::http::status::bad_request); 373 return; 374 } 375 if (broadcastMsg.size() > maxBroadcastMsgSize) 376 { 377 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]"; 378 asyncResp->res.result(boost::beast::http::status::bad_request); 379 return; 380 } 381 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg); 382 } 383 384 inline void handleFileUrl(const crow::Request& req, 385 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 386 const std::string& fileID) 387 { 388 if (req.method() == boost::beast::http::verb::put) 389 { 390 handleFilePut(req, asyncResp, fileID); 391 return; 392 } 393 if (req.method() == boost::beast::http::verb::get) 394 { 395 handleFileGet(asyncResp, fileID); 396 return; 397 } 398 if (req.method() == boost::beast::http::verb::delete_) 399 { 400 handleFileDelete(asyncResp, fileID); 401 return; 402 } 403 } 404 405 inline void 406 handleAcquireLockAPI(const crow::Request& req, 407 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 408 std::vector<nlohmann::json> body) 409 { 410 LockRequests lockRequestStructure; 411 for (auto& element : body) 412 { 413 std::string lockType; 414 uint64_t resourceId = 0; 415 416 SegmentFlags segInfo; 417 std::vector<nlohmann::json> segmentFlags; 418 419 if (!redfish::json_util::readJson(element, asyncResp->res, "LockType", 420 lockType, "ResourceID", resourceId, 421 "SegmentFlags", segmentFlags)) 422 { 423 BMCWEB_LOG_DEBUG << "Not a Valid JSON"; 424 asyncResp->res.result(boost::beast::http::status::bad_request); 425 return; 426 } 427 BMCWEB_LOG_DEBUG << lockType; 428 BMCWEB_LOG_DEBUG << resourceId; 429 430 BMCWEB_LOG_DEBUG << "Segment Flags are present"; 431 432 for (auto& e : segmentFlags) 433 { 434 std::string lockFlags; 435 uint32_t segmentLength = 0; 436 437 if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag", 438 lockFlags, "SegmentLength", 439 segmentLength)) 440 { 441 asyncResp->res.result(boost::beast::http::status::bad_request); 442 return; 443 } 444 445 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags; 446 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength; 447 448 segInfo.push_back(std::make_pair(lockFlags, segmentLength)); 449 } 450 lockRequestStructure.push_back( 451 make_tuple(req.session->uniqueId, req.session->clientId, lockType, 452 resourceId, segInfo)); 453 } 454 455 // print lock request into journal 456 457 for (auto& i : lockRequestStructure) 458 { 459 BMCWEB_LOG_DEBUG << std::get<0>(i); 460 BMCWEB_LOG_DEBUG << std::get<1>(i); 461 BMCWEB_LOG_DEBUG << std::get<2>(i); 462 BMCWEB_LOG_DEBUG << std::get<3>(i); 463 464 for (const auto& p : std::get<4>(i)) 465 { 466 BMCWEB_LOG_DEBUG << p.first << ", " << p.second; 467 } 468 } 469 470 const LockRequests& t = lockRequestStructure; 471 472 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t); 473 474 if (varAcquireLock.first) 475 { 476 // Either validity failure of there is a conflict with itself 477 478 auto validityStatus = 479 std::get<std::pair<bool, int>>(varAcquireLock.second); 480 481 if ((!validityStatus.first) && (validityStatus.second == 0)) 482 { 483 BMCWEB_LOG_DEBUG << "Not a Valid record"; 484 BMCWEB_LOG_DEBUG << "Bad json in request"; 485 asyncResp->res.result(boost::beast::http::status::bad_request); 486 return; 487 } 488 if (validityStatus.first && (validityStatus.second == 1)) 489 { 490 BMCWEB_LOG_DEBUG << "There is a conflict within itself"; 491 asyncResp->res.result(boost::beast::http::status::bad_request); 492 return; 493 } 494 } 495 else 496 { 497 auto conflictStatus = 498 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second); 499 if (!conflictStatus.first) 500 { 501 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable"; 502 asyncResp->res.result(boost::beast::http::status::ok); 503 504 auto var = std::get<uint32_t>(conflictStatus.second); 505 nlohmann::json returnJson; 506 returnJson["id"] = var; 507 asyncResp->res.jsonValue["TransactionID"] = var; 508 return; 509 } 510 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table"; 511 asyncResp->res.result(boost::beast::http::status::conflict); 512 auto var = 513 std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second); 514 nlohmann::json returnJson; 515 nlohmann::json segments; 516 nlohmann::json myarray = nlohmann::json::array(); 517 returnJson["TransactionID"] = var.first; 518 returnJson["SessionID"] = std::get<0>(var.second); 519 returnJson["HMCID"] = std::get<1>(var.second); 520 returnJson["LockType"] = std::get<2>(var.second); 521 returnJson["ResourceID"] = std::get<3>(var.second); 522 523 for (auto& i : std::get<4>(var.second)) 524 { 525 segments["LockFlag"] = i.first; 526 segments["SegmentLength"] = i.second; 527 myarray.push_back(segments); 528 } 529 530 returnJson["SegmentFlags"] = myarray; 531 532 asyncResp->res.jsonValue["Record"] = returnJson; 533 return; 534 } 535 } 536 inline void 537 handleRelaseAllAPI(const crow::Request& req, 538 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 539 { 540 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId); 541 asyncResp->res.result(boost::beast::http::status::ok); 542 } 543 544 inline void 545 handleReleaseLockAPI(const crow::Request& req, 546 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 547 const std::vector<uint32_t>& listTransactionIds) 548 { 549 BMCWEB_LOG_DEBUG << listTransactionIds.size(); 550 BMCWEB_LOG_DEBUG << "Data is present"; 551 for (unsigned int listTransactionId : listTransactionIds) 552 { 553 BMCWEB_LOG_DEBUG << listTransactionId; 554 } 555 556 // validate the request ids 557 558 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock( 559 listTransactionIds, 560 std::make_pair(req.session->clientId, req.session->uniqueId)); 561 562 if (!varReleaselock.first) 563 { 564 // validation Failed 565 asyncResp->res.result(boost::beast::http::status::bad_request); 566 return; 567 } 568 auto statusRelease = 569 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second); 570 if (statusRelease.first) 571 { 572 // The current hmc owns all the locks, so we already released 573 // them 574 return; 575 } 576 577 // valid rid, but the current hmc does not own all the locks 578 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks"; 579 asyncResp->res.result(boost::beast::http::status::unauthorized); 580 581 auto var = statusRelease.second; 582 nlohmann::json returnJson; 583 nlohmann::json segments; 584 nlohmann::json myArray = nlohmann::json::array(); 585 returnJson["TransactionID"] = var.first; 586 returnJson["SessionID"] = std::get<0>(var.second); 587 returnJson["HMCID"] = std::get<1>(var.second); 588 returnJson["LockType"] = std::get<2>(var.second); 589 returnJson["ResourceID"] = std::get<3>(var.second); 590 591 for (auto& i : std::get<4>(var.second)) 592 { 593 segments["LockFlag"] = i.first; 594 segments["SegmentLength"] = i.second; 595 myArray.push_back(segments); 596 } 597 598 returnJson["SegmentFlags"] = myArray; 599 asyncResp->res.jsonValue["Record"] = returnJson; 600 } 601 602 inline void 603 handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 604 const ListOfSessionIds& listSessionIds) 605 { 606 BMCWEB_LOG_DEBUG << listSessionIds.size(); 607 608 auto status = 609 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds); 610 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status); 611 612 nlohmann::json lockRecords = nlohmann::json::array(); 613 614 for (const auto& transactionId : var) 615 { 616 for (const auto& lockRecord : transactionId.second) 617 { 618 nlohmann::json returnJson; 619 620 returnJson["TransactionID"] = transactionId.first; 621 returnJson["SessionID"] = std::get<0>(lockRecord); 622 returnJson["HMCID"] = std::get<1>(lockRecord); 623 returnJson["LockType"] = std::get<2>(lockRecord); 624 returnJson["ResourceID"] = std::get<3>(lockRecord); 625 626 nlohmann::json segments; 627 nlohmann::json segmentInfoArray = nlohmann::json::array(); 628 629 for (const auto& segment : std::get<4>(lockRecord)) 630 { 631 segments["LockFlag"] = segment.first; 632 segments["SegmentLength"] = segment.second; 633 segmentInfoArray.push_back(segments); 634 } 635 636 returnJson["SegmentFlags"] = segmentInfoArray; 637 lockRecords.push_back(returnJson); 638 } 639 } 640 asyncResp->res.result(boost::beast::http::status::ok); 641 asyncResp->res.jsonValue["Records"] = lockRecords; 642 } 643 644 inline bool isValidConfigFileName(const std::string& fileName, 645 crow::Response& res) 646 { 647 if (fileName.empty()) 648 { 649 BMCWEB_LOG_ERROR << "Empty filename"; 650 res.jsonValue["Description"] = "Empty file path in the url"; 651 return false; 652 } 653 654 // ConfigFile name is allowed to take upper and lowercase letters, 655 // numbers and hyphen 656 std::size_t found = fileName.find_first_not_of( 657 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-"); 658 if (found != std::string::npos) 659 { 660 BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName; 661 res.jsonValue["Description"] = "Unsupported character in filename"; 662 return false; 663 } 664 665 // Check the filename length 666 if (fileName.length() > 20) 667 { 668 BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. " 669 "Input filename length is: " 670 << fileName.length(); 671 res.jsonValue["Description"] = "Filename must be maximum 20 characters"; 672 return false; 673 } 674 675 return true; 676 } 677 678 inline void requestRoutes(App& app) 679 { 680 681 // allowed only for admin 682 BMCWEB_ROUTE(app, "/ibm/v1/") 683 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 684 .methods(boost::beast::http::verb::get)( 685 [](const crow::Request&, 686 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 687 asyncResp->res.jsonValue["@odata.type"] = 688 "#ibmServiceRoot.v1_0_0.ibmServiceRoot"; 689 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/"; 690 asyncResp->res.jsonValue["Id"] = "IBM Rest RootService"; 691 asyncResp->res.jsonValue["Name"] = "IBM Service Root"; 692 asyncResp->res.jsonValue["ConfigFiles"]["@odata.id"] = 693 "/ibm/v1/Host/ConfigFiles"; 694 asyncResp->res.jsonValue["LockService"]["@odata.id"] = 695 "/ibm/v1/HMC/LockService"; 696 asyncResp->res.jsonValue["BroadcastService"]["@odata.id"] = 697 "/ibm/v1/HMC/BroadcastService"; 698 }); 699 700 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles") 701 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 702 .methods(boost::beast::http::verb::get)( 703 [](const crow::Request&, 704 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 705 handleConfigFileList(asyncResp); 706 }); 707 708 BMCWEB_ROUTE(app, 709 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll") 710 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 711 .methods(boost::beast::http::verb::post)( 712 [](const crow::Request&, 713 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 714 deleteConfigFiles(asyncResp); 715 }); 716 717 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>") 718 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 719 .methods(boost::beast::http::verb::put, boost::beast::http::verb::get, 720 boost::beast::http::verb::delete_)( 721 [](const crow::Request& req, 722 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 723 const std::string& fileName) { 724 BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName; 725 // Validate the incoming fileName 726 if (!isValidConfigFileName(fileName, asyncResp->res)) 727 { 728 asyncResp->res.result(boost::beast::http::status::bad_request); 729 return; 730 } 731 handleFileUrl(req, asyncResp, fileName); 732 }); 733 734 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService") 735 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 736 .methods(boost::beast::http::verb::get)( 737 [](const crow::Request&, 738 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 739 getLockServiceData(asyncResp); 740 }); 741 742 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock") 743 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 744 .methods(boost::beast::http::verb::post)( 745 [](const crow::Request& req, 746 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 747 std::vector<nlohmann::json> body; 748 if (!redfish::json_util::readJsonAction(req, asyncResp->res, "Request", 749 body)) 750 { 751 BMCWEB_LOG_DEBUG << "Not a Valid JSON"; 752 asyncResp->res.result(boost::beast::http::status::bad_request); 753 return; 754 } 755 handleAcquireLockAPI(req, asyncResp, body); 756 }); 757 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock") 758 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 759 .methods(boost::beast::http::verb::post)( 760 [](const crow::Request& req, 761 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 762 std::string type; 763 std::vector<uint32_t> listTransactionIds; 764 765 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Type", 766 type, "TransactionIDs", 767 listTransactionIds)) 768 { 769 asyncResp->res.result(boost::beast::http::status::bad_request); 770 return; 771 } 772 if (type == "Transaction") 773 { 774 handleReleaseLockAPI(req, asyncResp, listTransactionIds); 775 } 776 else if (type == "Session") 777 { 778 handleRelaseAllAPI(req, asyncResp); 779 } 780 else 781 { 782 BMCWEB_LOG_DEBUG << " Value of Type : " << type 783 << "is Not a Valid key"; 784 redfish::messages::propertyValueNotInList(asyncResp->res, type, 785 "Type"); 786 } 787 }); 788 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList") 789 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 790 .methods(boost::beast::http::verb::post)( 791 [](const crow::Request& req, 792 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 793 ListOfSessionIds listSessionIds; 794 795 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, 796 "SessionIDs", listSessionIds)) 797 { 798 asyncResp->res.result(boost::beast::http::status::bad_request); 799 return; 800 } 801 handleGetLockListAPI(asyncResp, listSessionIds); 802 }); 803 804 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService") 805 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 806 .methods(boost::beast::http::verb::post)( 807 [](const crow::Request& req, 808 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 809 handleBroadcastService(req, asyncResp); 810 }); 811 } 812 813 } // namespace ibm_mc 814 } // namespace crow 815