blob: c633c83bedb10764557758f77ca5597f99ace619 [file] [log] [blame]
Zhiyi Zhang08e0e982017-03-01 10:10:42 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventod4496f72019-01-19 20:23:50 -05002/*
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -04003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang08e0e982017-03-01 10:10:42 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#include "ca-module.hpp"
Zhiyi Zhang90c75782020-10-06 15:04:03 -070022#include <boost/asio.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050023#include <boost/asio/ip/tcp.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050024#include <boost/program_options/options_description.hpp>
25#include <boost/program_options/parsers.hpp>
26#include <boost/program_options/variables_map.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050027#include <iostream>
Zhiyi Zhang74c61142020-10-07 21:00:49 -070028#include <ndn-cxx/face.hpp>
29#include <ndn-cxx/security/key-chain.hpp>
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080030
31namespace ndn {
32namespace ndncert {
33
Zhiyi Zhang90c75782020-10-06 15:04:03 -070034Face face;
tylerliua7bea662020-10-08 18:51:02 -070035security::KeyChain keyChain;
Zhiyi Zhang74c61142020-10-07 21:00:49 -070036std::string repoHost = "localhost";
37std::string repoPort = "7376";
38
39static bool
40writeDataToRepo(const Data& data) {
41 boost::asio::ip::tcp::iostream requestStream;
42 requestStream.expires_after(std::chrono::seconds(3));
43 requestStream.connect(repoHost, repoPort);
44 if (!requestStream) {
45 std::cerr << "ERROR: Cannot publish certificate to repo-ng"
46 << " (" << requestStream.error().message() << ")" << std::endl;
47 return false;
48 }
49 requestStream.write(reinterpret_cast<const char*>(data.wireEncode().wire()),
50 data.wireEncode().size());
51 return true;
52}
Zhiyi Zhang90c75782020-10-06 15:04:03 -070053
54static void
55handleSignal(const boost::system::error_code& error, int signalNum)
56{
57 if (error) {
58 return;
59 }
60 const char* signalName = ::strsignal(signalNum);
61 std::cerr << "Exiting on signal ";
62 if (signalName == nullptr) {
63 std::cerr << signalNum;
64 }
65 else {
66 std::cerr << signalName;
67 }
68 std::cerr << std::endl;
69 face.getIoService().stop();
70 exit(1);
71}
72
Davide Pesaventod4496f72019-01-19 20:23:50 -050073static int
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080074main(int argc, char* argv[])
75{
Zhiyi Zhang90c75782020-10-06 15:04:03 -070076 boost::asio::signal_set terminateSignals(face.getIoService());
77 terminateSignals.add(SIGINT);
78 terminateSignals.add(SIGTERM);
79 terminateSignals.async_wait(handleSignal);
80
Davide Pesaventod4496f72019-01-19 20:23:50 -050081 std::string configFilePath(SYSCONFDIR "/ndncert/ca.conf");
Davide Pesaventod4496f72019-01-19 20:23:50 -050082 bool wantRepoOut = false;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080083
84 namespace po = boost::program_options;
Davide Pesaventod4496f72019-01-19 20:23:50 -050085 po::options_description optsDesc("Options");
86 optsDesc.add_options()
Zhiyi Zhang74c61142020-10-07 21:00:49 -070087 ("help,h", "print this help message and exit")
88 ("config-file,c", po::value<std::string>(&configFilePath)->default_value(configFilePath), "path to configuration file")
89 ("repo-output,r", po::bool_switch(&wantRepoOut), "when enabled, all issued certificates will be published to repo-ng")
90 ("repo-host,H", po::value<std::string>(&repoHost)->default_value(repoHost), "repo-ng host")
91 ("repo-port,P", po::value<std::string>(&repoPort)->default_value(repoPort), "repo-ng port");
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080092
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080093 po::variables_map vm;
94 try {
Davide Pesaventod4496f72019-01-19 20:23:50 -050095 po::store(po::parse_command_line(argc, argv, optsDesc), vm);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080096 po::notify(vm);
97 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050098 catch (const po::error& e) {
99 std::cerr << "ERROR: " << e.what() << std::endl;
100 return 2;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800101 }
Davide Pesaventod4496f72019-01-19 20:23:50 -0500102 catch (const boost::bad_any_cast& e) {
103 std::cerr << "ERROR: " << e.what() << std::endl;
104 return 2;
105 }
106
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800107 if (vm.count("help") != 0) {
Davide Pesaventod4496f72019-01-19 20:23:50 -0500108 std::cout << "Usage: " << argv[0] << " [options]\n"
109 << "\n"
110 << optsDesc;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800111 return 0;
112 }
113
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800114 CaModule ca(face, keyChain, configFilePath);
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700115 std::map<Name, Data> cachedCertificates;
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700116 auto profileData = ca.getCaProfileData();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800117
Davide Pesaventod4496f72019-01-19 20:23:50 -0500118 if (wantRepoOut) {
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700119 writeDataToRepo(profileData);
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700120 ca.setStatusUpdateCallback([&](const CaState& request) {
tylerliu335e5222020-10-09 11:32:40 -0700121 if (request.m_status == Status::SUCCESS && request.m_requestType == RequestType::NEW) {
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700122 writeDataToRepo(request.m_cert);
123 }
124 });
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800125 }
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700126 else {
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700127 ca.setStatusUpdateCallback([&](const CaState& request) {
tylerliu335e5222020-10-09 11:32:40 -0700128 if (request.m_status == Status::SUCCESS && request.m_requestType == RequestType::NEW) {
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700129 cachedCertificates[request.m_cert.getName()] = request.m_cert;
130 }
131 });
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700132 cachedCertificates[profileData.getName()] = profileData;
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700133 face.setInterestFilter(
134 InterestFilter(ca.getCaConf().m_caItem.m_caPrefix),
135 [&](const InterestFilter&, const Interest& interest) {
136 auto search = cachedCertificates.find(interest.getName());
137 if (search != cachedCertificates.end()) {
138 face.put(search->second);
139 }
140 },
141 [](const Name&, const std::string& errorInfo) {
142 std::cerr << "ERROR: " << errorInfo << std::endl;
143 });
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700144 }
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800145
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800146 face.processEvents();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800147 return 0;
148}
149
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700150} // namespace ndncert
151} // namespace ndn
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800152
153int
154main(int argc, char* argv[])
155{
156 return ndn::ndncert::main(argc, argv);
157}