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::vector<std::string> pathObjList; 266 std::error_code ec; 267 std::filesystem::path loc( 268 "/var/lib/bmcweb/ibm-management-console/configfiles"); 269 if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc)) 270 { 271 std::filesystem::remove_all(loc, ec); 272 if (ec) 273 { 274 asyncResp->res.result( 275 boost::beast::http::status::internal_server_error); 276 asyncResp->res.jsonValue["Description"] = internalServerError; 277 BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the " 278 "config files directory. ec : " 279 << ec; 280 } 281 } 282 } 283 284 inline void 285 getLockServiceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 286 { 287 asyncResp->res.jsonValue["@odata.type"] = "#LockService.v1_0_0.LockService"; 288 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/HMC/LockService/"; 289 asyncResp->res.jsonValue["Id"] = "LockService"; 290 asyncResp->res.jsonValue["Name"] = "LockService"; 291 292 asyncResp->res.jsonValue["Actions"]["#LockService.AcquireLock"] = { 293 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock"}}; 294 asyncResp->res.jsonValue["Actions"]["#LockService.ReleaseLock"] = { 295 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock"}}; 296 asyncResp->res.jsonValue["Actions"]["#LockService.GetLockList"] = { 297 {"target", "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList"}}; 298 } 299 300 inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 301 const std::string& fileID) 302 { 303 BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID; 304 std::filesystem::path loc( 305 "/var/lib/bmcweb/ibm-management-console/configfiles/" + fileID); 306 if (!std::filesystem::exists(loc)) 307 { 308 BMCWEB_LOG_ERROR << loc.string() << "Not found"; 309 asyncResp->res.result(boost::beast::http::status::not_found); 310 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; 311 return; 312 } 313 314 std::ifstream readfile(loc.string()); 315 if (!readfile) 316 { 317 BMCWEB_LOG_ERROR << loc.string() << "Not found"; 318 asyncResp->res.result(boost::beast::http::status::not_found); 319 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; 320 return; 321 } 322 323 std::string contentDispositionParam = 324 "attachment; filename=\"" + fileID + "\""; 325 asyncResp->res.addHeader("Content-Disposition", contentDispositionParam); 326 std::string fileData; 327 fileData = {std::istreambuf_iterator<char>(readfile), 328 std::istreambuf_iterator<char>()}; 329 asyncResp->res.jsonValue["Data"] = fileData; 330 } 331 332 inline void 333 handleFileDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 334 const std::string& fileID) 335 { 336 std::string filePath("/var/lib/bmcweb/ibm-management-console/configfiles/" + 337 fileID); 338 BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n"; 339 std::ifstream fileOpen(filePath.c_str()); 340 if (static_cast<bool>(fileOpen)) 341 { 342 if (remove(filePath.c_str()) == 0) 343 { 344 BMCWEB_LOG_DEBUG << "File removed!\n"; 345 asyncResp->res.jsonValue["Description"] = "File Deleted"; 346 } 347 else 348 { 349 BMCWEB_LOG_ERROR << "File not removed!\n"; 350 asyncResp->res.result( 351 boost::beast::http::status::internal_server_error); 352 asyncResp->res.jsonValue["Description"] = internalServerError; 353 } 354 } 355 else 356 { 357 BMCWEB_LOG_ERROR << "File not found!\n"; 358 asyncResp->res.result(boost::beast::http::status::not_found); 359 asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; 360 } 361 } 362 363 inline void 364 handleBroadcastService(const crow::Request& req, 365 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 366 { 367 std::string broadcastMsg; 368 369 if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Message", 370 broadcastMsg)) 371 { 372 BMCWEB_LOG_DEBUG << "Not a Valid JSON"; 373 asyncResp->res.result(boost::beast::http::status::bad_request); 374 return; 375 } 376 if (broadcastMsg.size() > maxBroadcastMsgSize) 377 { 378 BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]"; 379 asyncResp->res.result(boost::beast::http::status::bad_request); 380 return; 381 } 382 redfish::EventServiceManager::getInstance().sendBroadcastMsg(broadcastMsg); 383 } 384 385 inline void handleFileUrl(const crow::Request& req, 386 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 387 const std::string& fileID) 388 { 389 if (req.method() == boost::beast::http::verb::put) 390 { 391 handleFilePut(req, asyncResp, fileID); 392 return; 393 } 394 if (req.method() == boost::beast::http::verb::get) 395 { 396 handleFileGet(asyncResp, fileID); 397 return; 398 } 399 if (req.method() == boost::beast::http::verb::delete_) 400 { 401 handleFileDelete(asyncResp, fileID); 402 return; 403 } 404 } 405 406 inline void 407 handleAcquireLockAPI(const crow::Request& req, 408 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 409 std::vector<nlohmann::json> body) 410 { 411 LockRequests lockRequestStructure; 412 for (auto& element : body) 413 { 414 std::string lockType; 415 uint64_t resourceId = 0; 416 417 SegmentFlags segInfo; 418 std::vector<nlohmann::json> segmentFlags; 419 420 if (!redfish::json_util::readJson(element, asyncResp->res, "LockType", 421 lockType, "ResourceID", resourceId, 422 "SegmentFlags", segmentFlags)) 423 { 424 BMCWEB_LOG_DEBUG << "Not a Valid JSON"; 425 asyncResp->res.result(boost::beast::http::status::bad_request); 426 return; 427 } 428 BMCWEB_LOG_DEBUG << lockType; 429 BMCWEB_LOG_DEBUG << resourceId; 430 431 BMCWEB_LOG_DEBUG << "Segment Flags are present"; 432 433 for (auto& e : segmentFlags) 434 { 435 std::string lockFlags; 436 uint32_t segmentLength = 0; 437 438 if (!redfish::json_util::readJson(e, asyncResp->res, "LockFlag", 439 lockFlags, "SegmentLength", 440 segmentLength)) 441 { 442 asyncResp->res.result(boost::beast::http::status::bad_request); 443 return; 444 } 445 446 BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags; 447 BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength; 448 449 segInfo.push_back(std::make_pair(lockFlags, segmentLength)); 450 } 451 lockRequestStructure.push_back( 452 make_tuple(req.session->uniqueId, req.session->clientId, lockType, 453 resourceId, segInfo)); 454 } 455 456 // print lock request into journal 457 458 for (auto& i : lockRequestStructure) 459 { 460 BMCWEB_LOG_DEBUG << std::get<0>(i); 461 BMCWEB_LOG_DEBUG << std::get<1>(i); 462 BMCWEB_LOG_DEBUG << std::get<2>(i); 463 BMCWEB_LOG_DEBUG << std::get<3>(i); 464 465 for (const auto& p : std::get<4>(i)) 466 { 467 BMCWEB_LOG_DEBUG << p.first << ", " << p.second; 468 } 469 } 470 471 const LockRequests& t = lockRequestStructure; 472 473 auto varAcquireLock = crow::ibm_mc_lock::Lock::getInstance().acquireLock(t); 474 475 if (varAcquireLock.first) 476 { 477 // Either validity failure of there is a conflict with itself 478 479 auto validityStatus = 480 std::get<std::pair<bool, int>>(varAcquireLock.second); 481 482 if ((!validityStatus.first) && (validityStatus.second == 0)) 483 { 484 BMCWEB_LOG_DEBUG << "Not a Valid record"; 485 BMCWEB_LOG_DEBUG << "Bad json in request"; 486 asyncResp->res.result(boost::beast::http::status::bad_request); 487 return; 488 } 489 if (validityStatus.first && (validityStatus.second == 1)) 490 { 491 BMCWEB_LOG_DEBUG << "There is a conflict within itself"; 492 asyncResp->res.result(boost::beast::http::status::bad_request); 493 return; 494 } 495 } 496 else 497 { 498 auto conflictStatus = 499 std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second); 500 if (!conflictStatus.first) 501 { 502 BMCWEB_LOG_DEBUG << "There is no conflict with the locktable"; 503 asyncResp->res.result(boost::beast::http::status::ok); 504 505 auto var = std::get<uint32_t>(conflictStatus.second); 506 nlohmann::json returnJson; 507 returnJson["id"] = var; 508 asyncResp->res.jsonValue["TransactionID"] = var; 509 return; 510 } 511 BMCWEB_LOG_DEBUG << "There is a conflict with the lock table"; 512 asyncResp->res.result(boost::beast::http::status::conflict); 513 auto var = 514 std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second); 515 nlohmann::json returnJson; 516 nlohmann::json segments; 517 nlohmann::json myarray = nlohmann::json::array(); 518 returnJson["TransactionID"] = var.first; 519 returnJson["SessionID"] = std::get<0>(var.second); 520 returnJson["HMCID"] = std::get<1>(var.second); 521 returnJson["LockType"] = std::get<2>(var.second); 522 returnJson["ResourceID"] = std::get<3>(var.second); 523 524 for (auto& i : std::get<4>(var.second)) 525 { 526 segments["LockFlag"] = i.first; 527 segments["SegmentLength"] = i.second; 528 myarray.push_back(segments); 529 } 530 531 returnJson["SegmentFlags"] = myarray; 532 533 asyncResp->res.jsonValue["Record"] = returnJson; 534 return; 535 } 536 } 537 inline void 538 handleRelaseAllAPI(const crow::Request& req, 539 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 540 { 541 crow::ibm_mc_lock::Lock::getInstance().releaseLock(req.session->uniqueId); 542 asyncResp->res.result(boost::beast::http::status::ok); 543 } 544 545 inline void 546 handleReleaseLockAPI(const crow::Request& req, 547 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 548 const std::vector<uint32_t>& listTransactionIds) 549 { 550 BMCWEB_LOG_DEBUG << listTransactionIds.size(); 551 BMCWEB_LOG_DEBUG << "Data is present"; 552 for (unsigned int listTransactionId : listTransactionIds) 553 { 554 BMCWEB_LOG_DEBUG << listTransactionId; 555 } 556 557 // validate the request ids 558 559 auto varReleaselock = crow::ibm_mc_lock::Lock::getInstance().releaseLock( 560 listTransactionIds, 561 std::make_pair(req.session->clientId, req.session->uniqueId)); 562 563 if (!varReleaselock.first) 564 { 565 // validation Failed 566 asyncResp->res.result(boost::beast::http::status::bad_request); 567 return; 568 } 569 auto statusRelease = 570 std::get<crow::ibm_mc_lock::RcRelaseLock>(varReleaselock.second); 571 if (statusRelease.first) 572 { 573 // The current hmc owns all the locks, so we already released 574 // them 575 return; 576 } 577 578 // valid rid, but the current hmc does not own all the locks 579 BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks"; 580 asyncResp->res.result(boost::beast::http::status::unauthorized); 581 582 auto var = statusRelease.second; 583 nlohmann::json returnJson; 584 nlohmann::json segments; 585 nlohmann::json myArray = nlohmann::json::array(); 586 returnJson["TransactionID"] = var.first; 587 returnJson["SessionID"] = std::get<0>(var.second); 588 returnJson["HMCID"] = std::get<1>(var.second); 589 returnJson["LockType"] = std::get<2>(var.second); 590 returnJson["ResourceID"] = std::get<3>(var.second); 591 592 for (auto& i : std::get<4>(var.second)) 593 { 594 segments["LockFlag"] = i.first; 595 segments["SegmentLength"] = i.second; 596 myArray.push_back(segments); 597 } 598 599 returnJson["SegmentFlags"] = myArray; 600 asyncResp->res.jsonValue["Record"] = returnJson; 601 } 602 603 inline void 604 handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 605 const ListOfSessionIds& listSessionIds) 606 { 607 BMCWEB_LOG_DEBUG << listSessionIds.size(); 608 609 auto status = 610 crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds); 611 auto var = std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status); 612 613 nlohmann::json lockRecords = nlohmann::json::array(); 614 615 for (const auto& transactionId : var) 616 { 617 for (const auto& lockRecord : transactionId.second) 618 { 619 nlohmann::json returnJson; 620 621 returnJson["TransactionID"] = transactionId.first; 622 returnJson["SessionID"] = std::get<0>(lockRecord); 623 returnJson["HMCID"] = std::get<1>(lockRecord); 624 returnJson["LockType"] = std::get<2>(lockRecord); 625 returnJson["ResourceID"] = std::get<3>(lockRecord); 626 627 nlohmann::json segments; 628 nlohmann::json segmentInfoArray = nlohmann::json::array(); 629 630 for (const auto& segment : std::get<4>(lockRecord)) 631 { 632 segments["LockFlag"] = segment.first; 633 segments["SegmentLength"] = segment.second; 634 segmentInfoArray.push_back(segments); 635 } 636 637 returnJson["SegmentFlags"] = segmentInfoArray; 638 lockRecords.push_back(returnJson); 639 } 640 } 641 asyncResp->res.result(boost::beast::http::status::ok); 642 asyncResp->res.jsonValue["Records"] = lockRecords; 643 } 644 645 inline bool isValidConfigFileName(const std::string& fileName, 646 crow::Response& res) 647 { 648 if (fileName.empty()) 649 { 650 BMCWEB_LOG_ERROR << "Empty filename"; 651 res.jsonValue["Description"] = "Empty file path in the url"; 652 return false; 653 } 654 655 // ConfigFile name is allowed to take upper and lowercase letters, 656 // numbers and hyphen 657 std::size_t found = fileName.find_first_not_of( 658 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-"); 659 if (found != std::string::npos) 660 { 661 BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName; 662 res.jsonValue["Description"] = "Unsupported character in filename"; 663 return false; 664 } 665 666 // Check the filename length 667 if (fileName.length() > 20) 668 { 669 BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. " 670 "Input filename length is: " 671 << fileName.length(); 672 res.jsonValue["Description"] = "Filename must be maximum 20 characters"; 673 return false; 674 } 675 676 return true; 677 } 678 679 inline void requestRoutes(App& app) 680 { 681 682 // allowed only for admin 683 BMCWEB_ROUTE(app, "/ibm/v1/") 684 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 685 .methods(boost::beast::http::verb::get)( 686 [](const crow::Request&, 687 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 688 asyncResp->res.jsonValue["@odata.type"] = 689 "#ibmServiceRoot.v1_0_0.ibmServiceRoot"; 690 asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/"; 691 asyncResp->res.jsonValue["Id"] = "IBM Rest RootService"; 692 asyncResp->res.jsonValue["Name"] = "IBM Service Root"; 693 asyncResp->res.jsonValue["ConfigFiles"]["@odata.id"] = 694 "/ibm/v1/Host/ConfigFiles"; 695 asyncResp->res.jsonValue["LockService"]["@odata.id"] = 696 "/ibm/v1/HMC/LockService"; 697 asyncResp->res.jsonValue["BroadcastService"]["@odata.id"] = 698 "/ibm/v1/HMC/BroadcastService"; 699 }); 700 701 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles") 702 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 703 .methods(boost::beast::http::verb::get)( 704 [](const crow::Request&, 705 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 706 handleConfigFileList(asyncResp); 707 }); 708 709 BMCWEB_ROUTE(app, 710 "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll") 711 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 712 .methods(boost::beast::http::verb::post)( 713 [](const crow::Request&, 714 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 715 deleteConfigFiles(asyncResp); 716 }); 717 718 BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>") 719 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 720 .methods(boost::beast::http::verb::put, boost::beast::http::verb::get, 721 boost::beast::http::verb::delete_)( 722 [](const crow::Request& req, 723 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 724 const std::string& fileName) { 725 BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName; 726 // Validate the incoming fileName 727 if (!isValidConfigFileName(fileName, asyncResp->res)) 728 { 729 asyncResp->res.result( 730 boost::beast::http::status::bad_request); 731 return; 732 } 733 handleFileUrl(req, asyncResp, fileName); 734 }); 735 736 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService") 737 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 738 .methods(boost::beast::http::verb::get)( 739 [](const crow::Request&, 740 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 741 getLockServiceData(asyncResp); 742 }); 743 744 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.AcquireLock") 745 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 746 .methods(boost::beast::http::verb::post)( 747 [](const crow::Request& req, 748 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 749 std::vector<nlohmann::json> body; 750 if (!redfish::json_util::readJsonAction(req, asyncResp->res, 751 "Request", body)) 752 { 753 BMCWEB_LOG_DEBUG << "Not a Valid JSON"; 754 asyncResp->res.result( 755 boost::beast::http::status::bad_request); 756 return; 757 } 758 handleAcquireLockAPI(req, asyncResp, body); 759 }); 760 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock") 761 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 762 .methods(boost::beast::http::verb::post)( 763 [](const crow::Request& req, 764 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 765 std::string type; 766 std::vector<uint32_t> listTransactionIds; 767 768 if (!redfish::json_util::readJsonPatch( 769 req, asyncResp->res, "Type", type, "TransactionIDs", 770 listTransactionIds)) 771 { 772 asyncResp->res.result( 773 boost::beast::http::status::bad_request); 774 return; 775 } 776 if (type == "Transaction") 777 { 778 handleReleaseLockAPI(req, asyncResp, listTransactionIds); 779 } 780 else if (type == "Session") 781 { 782 handleRelaseAllAPI(req, asyncResp); 783 } 784 else 785 { 786 BMCWEB_LOG_DEBUG << " Value of Type : " << type 787 << "is Not a Valid key"; 788 redfish::messages::propertyValueNotInList(asyncResp->res, 789 type, "Type"); 790 } 791 }); 792 BMCWEB_ROUTE(app, "/ibm/v1/HMC/LockService/Actions/LockService.GetLockList") 793 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 794 .methods(boost::beast::http::verb::post)( 795 [](const crow::Request& req, 796 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 797 ListOfSessionIds listSessionIds; 798 799 if (!redfish::json_util::readJsonPatch( 800 req, asyncResp->res, "SessionIDs", listSessionIds)) 801 { 802 asyncResp->res.result( 803 boost::beast::http::status::bad_request); 804 return; 805 } 806 handleGetLockListAPI(asyncResp, listSessionIds); 807 }); 808 809 BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService") 810 .privileges({{"ConfigureComponents", "ConfigureManager"}}) 811 .methods(boost::beast::http::verb::post)( 812 [](const crow::Request& req, 813 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 814 handleBroadcastService(req, asyncResp); 815 }); 816 } 817 818 } // namespace ibm_mc 819 } // namespace crow 820