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