Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 IBM 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 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 17 | #include "config.h" |
| 18 | |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 19 | #include "argument.hpp" |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 20 | #include "certificate.hpp" |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 21 | #include "certs_manager.hpp" |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 22 | |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame^] | 23 | #include <stdlib.h> |
| 24 | #include <systemd/sd-event.h> |
| 25 | |
| 26 | #include <cctype> |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 27 | #include <iostream> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame^] | 28 | #include <sdbusplus/bus.hpp> |
| 29 | #include <sdbusplus/server/manager.hpp> |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 30 | #include <sdeventplus/event.hpp> |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 31 | #include <string> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame^] | 32 | #include <utility> |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 33 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 34 | static void exitWithError(const char* err, char** argv) |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 35 | { |
| 36 | phosphor::certs::util::ArgumentParser::usage(argv); |
| 37 | std::cerr << std::endl; |
| 38 | std::cerr << "ERROR: " << err << std::endl; |
| 39 | exit(EXIT_FAILURE); |
| 40 | } |
| 41 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 42 | inline std::string capitalize(const std::string& s) |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 43 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 44 | std::string res = s; |
| 45 | if (!res.empty()) |
| 46 | { |
| 47 | res[0] = std::toupper(res[0]); |
| 48 | } |
| 49 | return res; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 52 | int main(int argc, char** argv) |
| 53 | { |
| 54 | // Read arguments. |
| 55 | auto options = phosphor::certs::util::ArgumentParser(argc, argv); |
| 56 | |
| 57 | // Parse arguments |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 58 | const std::string& typeStr = (options)["typeStr"]; |
| 59 | phosphor::certs::CertificateType type = |
| 60 | phosphor::certs::stringToCertificateType(typeStr); |
| 61 | if (type == phosphor::certs::CertificateType::Unsupported) |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 62 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 63 | exitWithError("type not specified or invalid.", argv); |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 66 | const std::string& endpoint = (options)["endpoint"]; |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 67 | if (endpoint == phosphor::certs::util::ArgumentParser::empty_string) |
| 68 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 69 | exitWithError("endpoint not specified.", argv); |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 72 | const std::string& path = (options)["path"]; |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 73 | if (path == phosphor::certs::util::ArgumentParser::empty_string) |
| 74 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 75 | exitWithError("path not specified.", argv); |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 78 | // unit is an optional parameter |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 79 | const std::string& unit = (options)["unit"]; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 80 | auto bus = sdbusplus::bus::new_default(); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 81 | auto objPath = |
| 82 | std::string(objectNamePrefix) + '/' + typeStr + '/' + endpoint; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 83 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 84 | // Add sdbusplus ObjectManager |
| 85 | sdbusplus::server::manager::manager objManager(bus, objPath.c_str()); |
| 86 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 87 | // Get default event loop |
| 88 | auto event = sdeventplus::Event::get_default(); |
| 89 | |
| 90 | // Attach the bus to sd_event to service user requests |
| 91 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 92 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 93 | phosphor::certs::Manager manager(bus, event, objPath.c_str(), type, unit, |
| 94 | path); |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 95 | |
| 96 | // Adjusting Interface name as per std convention |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 97 | auto busName = std::string(busNamePrefix) + '.' + capitalize(typeStr) + |
| 98 | '.' + capitalize(endpoint); |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 99 | bus.request_name(busName.c_str()); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 100 | event.loop(); |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 101 | return 0; |
| 102 | } |