blob: 8acd13b0ce57ed64e46c4821dd172a01ad32b32d [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;
tylerliu9ea1f762020-10-17 11:53:36 -070042#if BOOST_VERSION >= 106600
43 requestStream.expires_after(std::chrono::seconds(3));
44#else
45 requestStream.expires_at(std::chrono::seconds(3));
46#endif
Zhiyi Zhang74c61142020-10-07 21:00:49 -070047 requestStream.connect(repoHost, repoPort);
48 if (!requestStream) {
49 std::cerr << "ERROR: Cannot publish certificate to repo-ng"
50 << " (" << requestStream.error().message() << ")" << std::endl;
51 return false;
52 }
53 requestStream.write(reinterpret_cast<const char*>(data.wireEncode().wire()),
54 data.wireEncode().size());
55 return true;
56}
Zhiyi Zhang90c75782020-10-06 15:04:03 -070057
58static void
59handleSignal(const boost::system::error_code& error, int signalNum)
60{
61 if (error) {
62 return;
63 }
64 const char* signalName = ::strsignal(signalNum);
65 std::cerr << "Exiting on signal ";
66 if (signalName == nullptr) {
67 std::cerr << signalNum;
68 }
69 else {
70 std::cerr << signalName;
71 }
72 std::cerr << std::endl;
73 face.getIoService().stop();
74 exit(1);
75}
76
Davide Pesaventod4496f72019-01-19 20:23:50 -050077static int
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080078main(int argc, char* argv[])
79{
Zhiyi Zhang90c75782020-10-06 15:04:03 -070080 boost::asio::signal_set terminateSignals(face.getIoService());
81 terminateSignals.add(SIGINT);
82 terminateSignals.add(SIGTERM);
83 terminateSignals.async_wait(handleSignal);
84
Davide Pesaventod4496f72019-01-19 20:23:50 -050085 std::string configFilePath(SYSCONFDIR "/ndncert/ca.conf");
Davide Pesaventod4496f72019-01-19 20:23:50 -050086 bool wantRepoOut = false;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080087
88 namespace po = boost::program_options;
Davide Pesaventod4496f72019-01-19 20:23:50 -050089 po::options_description optsDesc("Options");
90 optsDesc.add_options()
Zhiyi Zhang74c61142020-10-07 21:00:49 -070091 ("help,h", "print this help message and exit")
92 ("config-file,c", po::value<std::string>(&configFilePath)->default_value(configFilePath), "path to configuration file")
93 ("repo-output,r", po::bool_switch(&wantRepoOut), "when enabled, all issued certificates will be published to repo-ng")
94 ("repo-host,H", po::value<std::string>(&repoHost)->default_value(repoHost), "repo-ng host")
95 ("repo-port,P", po::value<std::string>(&repoPort)->default_value(repoPort), "repo-ng port");
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080096
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080097 po::variables_map vm;
98 try {
Davide Pesaventod4496f72019-01-19 20:23:50 -050099 po::store(po::parse_command_line(argc, argv, optsDesc), vm);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800100 po::notify(vm);
101 }
Davide Pesaventod4496f72019-01-19 20:23:50 -0500102 catch (const po::error& e) {
103 std::cerr << "ERROR: " << e.what() << std::endl;
104 return 2;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800105 }
Davide Pesaventod4496f72019-01-19 20:23:50 -0500106 catch (const boost::bad_any_cast& e) {
107 std::cerr << "ERROR: " << e.what() << std::endl;
108 return 2;
109 }
110
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800111 if (vm.count("help") != 0) {
Davide Pesaventod4496f72019-01-19 20:23:50 -0500112 std::cout << "Usage: " << argv[0] << " [options]\n"
113 << "\n"
114 << optsDesc;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800115 return 0;
116 }
117
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800118 CaModule ca(face, keyChain, configFilePath);
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700119 std::map<Name, Data> cachedCertificates;
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700120 auto profileData = ca.getCaProfileData();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800121
Davide Pesaventod4496f72019-01-19 20:23:50 -0500122 if (wantRepoOut) {
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700123 writeDataToRepo(profileData);
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700124 ca.setStatusUpdateCallback([&](const CaState& request) {
tylerliu335e5222020-10-09 11:32:40 -0700125 if (request.m_status == Status::SUCCESS && request.m_requestType == RequestType::NEW) {
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700126 writeDataToRepo(request.m_cert);
127 }
128 });
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800129 }
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700130 else {
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700131 ca.setStatusUpdateCallback([&](const CaState& request) {
tylerliu335e5222020-10-09 11:32:40 -0700132 if (request.m_status == Status::SUCCESS && request.m_requestType == RequestType::NEW) {
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700133 cachedCertificates[request.m_cert.getName()] = request.m_cert;
134 }
135 });
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700136 cachedCertificates[profileData.getName()] = profileData;
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700137 face.setInterestFilter(
138 InterestFilter(ca.getCaConf().m_caItem.m_caPrefix),
139 [&](const InterestFilter&, const Interest& interest) {
140 auto search = cachedCertificates.find(interest.getName());
141 if (search != cachedCertificates.end()) {
142 face.put(search->second);
143 }
144 },
145 [](const Name&, const std::string& errorInfo) {
146 std::cerr << "ERROR: " << errorInfo << std::endl;
147 });
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700148 }
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800149
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800150 face.processEvents();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800151 return 0;
152}
153
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700154} // namespace ndncert
155} // namespace ndn
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800156
157int
158main(int argc, char* argv[])
159{
160 return ndn::ndncert::main(argc, argv);
161}