blob: a23f044b79b67563c550df7c70df1c21ea61166c [file] [log] [blame]
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05001#pragma once
2#include <cstring>
3#include <sdbusplus/bus.hpp>
4#include <sdbusplus/server/object.hpp>
5#include <unordered_map>
6#include <xyz/openbmc_project/Certs/Install/server.hpp>
7
8namespace phosphor
9{
10namespace certs
11{
12
13// Supported Types.
14static constexpr auto SERVER = "server";
15static constexpr auto CLIENT = "client";
16
17using CreateIface = sdbusplus::server::object::object<
18 sdbusplus::xyz::openbmc_project::Certs::server::Install>;
19using InstallFunc = std::function<void()>;
20using InputType = std::string;
21
22class Manager : public CreateIface
23{
24 public:
25 /* Define all of the basic class operations:
26 * Not allowed:
27 * - Default constructor is not possible due to member
28 * reference
29 * - Move operations due to 'this' being registered as the
30 * 'context' with sdbus.
31 * Allowed:
32 * - copy
33 * - Destructor.
34 */
35 Manager() = delete;
36 Manager(const Manager&) = default;
37 Manager& operator=(const Manager&) = delete;
38 Manager(Manager&&) = delete;
39 Manager& operator=(Manager&&) = delete;
40 virtual ~Manager() = default;
41
42 /** @brief Constructor to put object onto bus at a dbus path.
43 * @param[in] bus - Bus to attach to.
44 * @param[in] path - Path to attach at.
45 * @param[in] type - Type of the certificate.
46 * @param[in] unit - Unit consumed by this certificate.
47 * @param[in] certpath - Certificate installation path.
48 */
49 Manager(sdbusplus::bus::bus& bus, const char* path, const std::string& type,
50 std::string&& unit, std::string&& certPath) :
51 CreateIface(bus, path),
52 bus(bus), path(path), type(type), unit(std::move(unit)),
53 certPath(std::move(certPath))
54 {
55 typeFuncMap[SERVER] =
56 std::bind(&phosphor::certs::Manager::serverInstall, this);
57 typeFuncMap[CLIENT] =
58 std::bind(&phosphor::certs::Manager::clientInstall, this);
59 }
60
61 /** @brief Implementation for Install
62 * Replace the existing certificate key file with another
63 * (possibly CA signed) Certificate key file.
64 *
65 * @param[in] path - Certificate key file path.
66 */
67 void install(const std::string path) override;
68
69 private:
70 /** @brief Client certificate Installation helper function **/
71 void clientInstall();
72
73 /** @brief Server certificate Installation helper function **/
74 void serverInstall();
75
76 /** @brief systemd unit reload helper function
77 * @param[in] unit - service need to reload.
78 */
79 void reload(const std::string& unit);
80
81 /** @brief helper function to copy the file.
82 * @param[in] src - Source file path to copy
83 * @param[in] dst - Destination path to copy
84 */
85 void copy(const std::string& src, const std::string& dst);
86
87 /** @brief sdbusplus handler */
88 sdbusplus::bus::bus& bus;
89
90 /** @brief object path */
91 std::string path;
92
93 /** @brief Type of the certificate **/
94 InputType type;
95
96 /** @brief Unit name associated to the service **/
97 std::string unit;
98
99 /** @brief Certificate file installation path **/
100 std::string certPath;
101
102 /** @brief Type specific function pointer map **/
103 std::unordered_map<InputType, InstallFunc> typeFuncMap;
104};
105
106} // namespace certs
107} // namespace phosphor