Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 24 | #ifndef NDN_TOOLS_NDNSEC_UTIL_HPP |
| 25 | #define NDN_TOOLS_NDNSEC_UTIL_HPP |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 26 | |
| 27 | #include <iostream> |
| 28 | #include <fstream> |
| 29 | #include <string> |
| 30 | #include <cstring> |
| 31 | |
| 32 | #include <boost/program_options/options_description.hpp> |
| 33 | #include <boost/program_options/variables_map.hpp> |
| 34 | #include <boost/program_options/parsers.hpp> |
| 35 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 36 | #include <boost/tokenizer.hpp> |
| 37 | #include <boost/asio.hpp> |
| 38 | #include <boost/exception/all.hpp> |
| 39 | |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 40 | #include "encoding/buffer-stream.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 41 | #include "security/key-chain.hpp" |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 42 | #include "security/transform.hpp" |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 43 | #include "util/io.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 44 | |
| 45 | bool |
| 46 | getPassword(std::string& password, const std::string& prompt) |
| 47 | { |
Alexander Afanasyev | cf3a667 | 2015-02-01 20:33:22 -0800 | [diff] [blame] | 48 | #ifdef NDN_CXX_HAVE_GETPASS |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 49 | bool isReady = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 50 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 51 | char* pw0 = 0; |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 52 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 53 | pw0 = getpass(prompt.c_str()); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 54 | if (!pw0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 55 | return false; |
| 56 | std::string password1 = pw0; |
| 57 | memset(pw0, 0, strlen(pw0)); |
| 58 | |
| 59 | pw0 = getpass("Confirm:"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 60 | if (!pw0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 61 | { |
| 62 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 63 | memset(pw1, 0, password1.size()); |
| 64 | return false; |
| 65 | } |
| 66 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 67 | if (!password1.compare(pw0)) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 68 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 69 | isReady = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | password.swap(password1); |
| 71 | } |
| 72 | |
| 73 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 74 | memset(pw1, 0, password1.size()); |
| 75 | memset(pw0, 0, strlen(pw0)); |
| 76 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 77 | if (password.empty()) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 78 | return false; |
| 79 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 80 | return isReady; |
Alexander Afanasyev | a2ada22 | 2015-01-22 18:34:16 -0800 | [diff] [blame] | 81 | #else |
| 82 | return false; |
| 83 | #endif // NDN_CXX_HAVE_GETPASS |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 86 | ndn::shared_ptr<ndn::security::v1::IdentityCertificate> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 87 | getIdentityCertificate(const std::string& fileName) |
| 88 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 89 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 90 | if (fileName == "-") |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 91 | return ndn::io::load<ndn::security::v1::IdentityCertificate>(std::cin); |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 92 | else |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 93 | return ndn::io::load<ndn::security::v1::IdentityCertificate>(fileName); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * @brief An accumulating option value to handle multiple incrementing options. |
| 99 | * |
| 100 | * Based on https://gitorious.org/bwy/bwy/source/8753148c324ddfacb1f3cdc315650586bd7b75a4:use/accumulator.hpp |
| 101 | * @sa http://benjaminwolsey.de/node/103 |
| 102 | */ |
| 103 | template<typename T> |
| 104 | class AccumulatorType : public boost::program_options::value_semantic |
| 105 | { |
| 106 | public: |
| 107 | explicit |
| 108 | AccumulatorType(T* store) |
| 109 | : m_store(store) |
| 110 | , m_interval(1) |
| 111 | , m_default(0) |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | virtual |
| 116 | ~AccumulatorType() |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | /// @brief Set the default value for this option. |
| 121 | AccumulatorType* |
| 122 | setDefaultValue(const T& t) |
| 123 | { |
| 124 | m_default = t; |
| 125 | return this; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @brief Set the interval for this option. |
| 130 | * |
| 131 | * Unlike for program_options::value, this specifies a value |
| 132 | * to be applied on each occurrence of the option. |
| 133 | */ |
| 134 | AccumulatorType* |
| 135 | setInterval(const T& t) { |
| 136 | m_interval = t; |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | virtual std::string |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 141 | name() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 142 | { |
| 143 | return std::string(); |
| 144 | } |
| 145 | |
| 146 | // There are no tokens for an AccumulatorType |
| 147 | virtual unsigned |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 148 | min_tokens() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 149 | { |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | virtual unsigned |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 154 | max_tokens() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | // Accumulating from different sources is silly. |
| 160 | virtual bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 161 | is_composing() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 162 | { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | // Requiring one or more appearances is unlikely. |
| 167 | virtual bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 168 | is_required() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 169 | { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @brief Parse options |
| 175 | * |
| 176 | * Every appearance of the option simply increments the value |
| 177 | * There should never be any tokens. |
| 178 | */ |
| 179 | virtual void |
| 180 | parse(boost::any& value_store, |
| 181 | const std::vector<std::string>& new_tokens, |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 182 | bool utf8) const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 183 | { |
| 184 | if (value_store.empty()) |
| 185 | value_store = T(); |
| 186 | boost::any_cast<T&>(value_store) += m_interval; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @brief If the option doesn't appear, this is the default value. |
| 191 | */ |
| 192 | virtual bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 193 | apply_default(boost::any& value_store) const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 194 | { |
| 195 | value_store = m_default; |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @brief Notify the user function with the value of the value store. |
| 201 | */ |
| 202 | virtual void |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 203 | notify(const boost::any& value_store) const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 204 | { |
| 205 | const T* val = boost::any_cast<T>(&value_store); |
| 206 | if (m_store) |
| 207 | *m_store = *val; |
| 208 | } |
| 209 | |
Alexander Afanasyev | ae20525 | 2015-08-24 14:08:46 -0700 | [diff] [blame] | 210 | #if BOOST_VERSION >= 105900 |
| 211 | virtual bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 212 | adjacent_tokens_only() const final |
Alexander Afanasyev | ae20525 | 2015-08-24 14:08:46 -0700 | [diff] [blame] | 213 | { |
| 214 | return false; |
| 215 | } |
| 216 | #endif // BOOST_VERSION >= 105900 |
| 217 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 218 | private: |
| 219 | T* m_store; |
| 220 | T m_interval; |
| 221 | T m_default; |
| 222 | }; |
| 223 | |
| 224 | template<typename T> |
| 225 | AccumulatorType<T>* accumulator() |
| 226 | { |
| 227 | return new AccumulatorType<T>(0); |
| 228 | } |
| 229 | |
| 230 | template<typename T> |
| 231 | AccumulatorType<T>* accumulator(T* store) |
| 232 | { |
| 233 | return new AccumulatorType<T>(store); |
| 234 | } |
| 235 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 236 | #endif // NDN_TOOLS_NDNSEC_UTIL_HPP |