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 17 #include "config.h" 18 19 #include "argument.hpp" 20 #include "certificate.hpp" 21 #include "certs_manager.hpp" 22 23 #include <stdlib.h> 24 #include <systemd/sd-event.h> 25 26 #include <cctype> 27 #include <iostream> 28 #include <sdbusplus/bus.hpp> 29 #include <sdbusplus/server/manager.hpp> 30 #include <sdeventplus/event.hpp> 31 #include <string> 32 #include <utility> 33 34 static void exitWithError(const char* err, char** argv) 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 42 inline std::string capitalize(const std::string& s) 43 { 44 std::string res = s; 45 if (!res.empty()) 46 { 47 res[0] = std::toupper(res[0]); 48 } 49 return res; 50 } 51 52 int main(int argc, char** argv) 53 { 54 // Read arguments. 55 auto options = phosphor::certs::util::ArgumentParser(argc, argv); 56 57 // Parse arguments 58 const std::string& typeStr = (options)["type"]; 59 phosphor::certs::CertificateType type = 60 phosphor::certs::stringToCertificateType(typeStr); 61 if (type == phosphor::certs::CertificateType::Unsupported) 62 { 63 exitWithError("type not specified or invalid.", argv); 64 } 65 66 const std::string& endpoint = (options)["endpoint"]; 67 if (endpoint == phosphor::certs::util::ArgumentParser::empty_string) 68 { 69 exitWithError("endpoint not specified.", argv); 70 } 71 72 const std::string& path = (options)["path"]; 73 if (path == phosphor::certs::util::ArgumentParser::empty_string) 74 { 75 exitWithError("path not specified.", argv); 76 } 77 78 // unit is an optional parameter 79 const std::string& unit = (options)["unit"]; 80 auto bus = sdbusplus::bus::new_default(); 81 auto objPath = 82 std::string(objectNamePrefix) + '/' + typeStr + '/' + endpoint; 83 84 // Add sdbusplus ObjectManager 85 sdbusplus::server::manager::manager objManager(bus, objPath.c_str()); 86 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 93 phosphor::certs::Manager manager(bus, event, objPath.c_str(), type, unit, 94 path); 95 96 // Adjusting Interface name as per std convention 97 auto busName = std::string(busNamePrefix) + '.' + capitalize(typeStr) + 98 '.' + capitalize(endpoint); 99 bus.request_name(busName.c_str()); 100 event.loop(); 101 return 0; 102 } 103