Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame^] | 1 | #include "certs_manager.hpp" |
| 2 | |
| 3 | #include <experimental/filesystem> |
| 4 | #include <phosphor-logging/elog-errors.hpp> |
| 5 | #include <phosphor-logging/elog.hpp> |
| 6 | #include <phosphor-logging/log.hpp> |
| 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <xyz/openbmc_project/Common/error.hpp> |
| 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace certs |
| 13 | { |
| 14 | |
| 15 | using namespace phosphor::logging; |
| 16 | using InternalFailure = |
| 17 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 18 | |
| 19 | void Manager::install(const std::string path) |
| 20 | { |
| 21 | // TODO Validate the certificate file |
| 22 | |
| 23 | // Copy the certificate file |
| 24 | copy(path, certPath); |
| 25 | |
| 26 | // Invoke type specific install function. |
| 27 | auto iter = typeFuncMap.find(type); |
| 28 | if (iter == typeFuncMap.end()) |
| 29 | { |
| 30 | log<level::ERR>("Unsupported Type", entry("TYPE=%s", type.c_str())); |
| 31 | elog<InternalFailure>(); |
| 32 | } |
| 33 | iter->second(); |
| 34 | } |
| 35 | |
| 36 | void Manager::serverInstall() |
| 37 | { |
| 38 | if (!unit.empty()) |
| 39 | { |
| 40 | reload(unit); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void Manager::clientInstall() |
| 45 | { |
| 46 | // Do nothing now |
| 47 | } |
| 48 | |
| 49 | void Manager::reload(const std::string& unit) |
| 50 | { |
| 51 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 52 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 53 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 54 | |
| 55 | try |
| 56 | { |
| 57 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 58 | SYSTEMD_INTERFACE, "ReloadUnit"); |
| 59 | |
| 60 | method.append(unit, "replace"); |
| 61 | |
| 62 | bus.call_noreply(method); |
| 63 | } |
| 64 | catch (const sdbusplus::exception::SdBusError& e) |
| 65 | { |
| 66 | log<level::ERR>("Failed to reload service", entry("ERR=%s", e.what()), |
| 67 | entry("UNIT=%s", unit.c_str())); |
| 68 | elog<InternalFailure>(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void Manager::copy(const std::string& src, const std::string& dst) |
| 73 | { |
| 74 | namespace fs = std::experimental::filesystem; |
| 75 | |
| 76 | try |
| 77 | { |
| 78 | auto path = fs::path(dst).parent_path(); |
| 79 | // create dst path folder by default |
| 80 | fs::create_directories(path); |
| 81 | fs::copy_file(src, dst, fs::copy_options::overwrite_existing); |
| 82 | } |
| 83 | catch (fs::filesystem_error& e) |
| 84 | { |
| 85 | log<level::ERR>("Failed to copy certificate", entry("ERR=%s", e.what()), |
| 86 | entry("SRC=%s", src.c_str()), |
| 87 | entry("DST=%s", dst.c_str())); |
| 88 | elog<InternalFailure>(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | } // namespace certs |
| 93 | } // namespace phosphor |