blob: 4f6cee41e11f7f92aa8b1d542798adddcf7e638c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyev80782e02017-01-04 13:16:54 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library 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 Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Yingdi Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080022#ifndef NDN_TOOLS_NDNSEC_UTIL_HPP
23#define NDN_TOOLS_NDNSEC_UTIL_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Junxiao Shi160701a2016-08-30 11:35:25 +000025#include "encoding/buffer-stream.hpp"
Junxiao Shi160701a2016-08-30 11:35:25 +000026#include "security/transform.hpp"
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080027#include "security/v1/key-chain.hpp"
Yingdi Yu64c3fb42014-02-26 17:30:04 -080028#include "util/io.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080030#include <fstream>
31#include <iostream>
32#include <string>
33
34#include <boost/asio.hpp>
35#include <boost/date_time/posix_time/posix_time.hpp>
36#include <boost/exception/all.hpp>
37#include <boost/program_options/options_description.hpp>
38#include <boost/program_options/parsers.hpp>
39#include <boost/program_options/variables_map.hpp>
40#include <boost/tokenizer.hpp>
41
42namespace ndn {
43namespace ndnsec {
44
Yingdi Yu8d7468f2014-02-21 14:49:45 -080045bool
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080046getPassword(std::string& password, const std::string& prompt);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080047
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080048shared_ptr<security::v1::IdentityCertificate>
49getIdentityCertificate(const std::string& fileName);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080050
51/**
52 * @brief An accumulating option value to handle multiple incrementing options.
53 *
54 * Based on https://gitorious.org/bwy/bwy/source/8753148c324ddfacb1f3cdc315650586bd7b75a4:use/accumulator.hpp
55 * @sa http://benjaminwolsey.de/node/103
56 */
57template<typename T>
58class AccumulatorType : public boost::program_options::value_semantic
59{
60public:
61 explicit
62 AccumulatorType(T* store)
63 : m_store(store)
64 , m_interval(1)
65 , m_default(0)
66 {
67 }
68
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080069 /// @brief Set the default value for this option.
70 AccumulatorType*
71 setDefaultValue(const T& t)
72 {
73 m_default = t;
74 return this;
75 }
76
77 /**
78 * @brief Set the interval for this option.
79 *
80 * Unlike for program_options::value, this specifies a value
81 * to be applied on each occurrence of the option.
82 */
83 AccumulatorType*
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080084 setInterval(const T& t)
85 {
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080086 m_interval = t;
87 return this;
88 }
89
Alexander Afanasyev80782e02017-01-04 13:16:54 -080090 std::string
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020091 name() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080092 {
93 return std::string();
94 }
95
96 // There are no tokens for an AccumulatorType
Alexander Afanasyev80782e02017-01-04 13:16:54 -080097 unsigned
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020098 min_tokens() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080099 {
100 return 0;
101 }
102
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800103 unsigned
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200104 max_tokens() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800105 {
106 return 0;
107 }
108
109 // Accumulating from different sources is silly.
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800110 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200111 is_composing() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800112 {
113 return false;
114 }
115
116 // Requiring one or more appearances is unlikely.
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800117 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200118 is_required() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800119 {
120 return false;
121 }
122
123 /**
124 * @brief Parse options
125 *
126 * Every appearance of the option simply increments the value
127 * There should never be any tokens.
128 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800129 void
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800130 parse(boost::any& value_store, const std::vector<std::string>& new_tokens, bool utf8) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800131 {
132 if (value_store.empty())
133 value_store = T();
134 boost::any_cast<T&>(value_store) += m_interval;
135 }
136
137 /**
138 * @brief If the option doesn't appear, this is the default value.
139 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800140 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200141 apply_default(boost::any& value_store) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800142 {
143 value_store = m_default;
144 return true;
145 }
146
147 /**
148 * @brief Notify the user function with the value of the value store.
149 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800150 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200151 notify(const boost::any& value_store) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800152 {
153 const T* val = boost::any_cast<T>(&value_store);
154 if (m_store)
155 *m_store = *val;
156 }
157
Alexander Afanasyevae205252015-08-24 14:08:46 -0700158#if BOOST_VERSION >= 105900
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800159 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200160 adjacent_tokens_only() const final
Alexander Afanasyevae205252015-08-24 14:08:46 -0700161 {
162 return false;
163 }
164#endif // BOOST_VERSION >= 105900
165
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800166private:
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800167 T* m_store;
168 T m_interval;
169 T m_default;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800170};
171
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800172template <typename T>
173AccumulatorType<T>*
174accumulator()
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800175{
176 return new AccumulatorType<T>(0);
177}
178
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800179template <typename T>
180AccumulatorType<T>*
181accumulator(T* store)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800182{
183 return new AccumulatorType<T>(store);
184}
185
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800186} // namespace ndnsec
187} // namespace ndn
188
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800189#endif // NDN_TOOLS_NDNSEC_UTIL_HPP