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 | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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. |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 22 | #include "ndnsec.hpp" |
| 23 | #include "util.hpp" |
Alexander Afanasyev | cfe0b06 | 2014-05-08 18:26:50 -0700 | [diff] [blame] | 24 | #include "version.hpp" |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 25 | #include "util/logger.hpp" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 27 | #include <boost/exception/get_error_info.hpp> |
| 28 | |
| 29 | NDN_LOG_INIT(ndnsec); |
| 30 | |
| 31 | std::string ndnsec_helper = R"STR(\ |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 32 | help Show all commands |
| 33 | version Show version and exit |
| 34 | list Display information in PublicInfo |
| 35 | get-default Get default setting info |
| 36 | set-default Configure default setting |
| 37 | key-gen Generate a Key-Signing-Key for an identity |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 38 | sign-req Generate a certificate signing request |
| 39 | cert-gen Generate an identity certificate |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 40 | cert-dump Dump a certificate from PublicInfo |
| 41 | cert-install Install a certificate into PublicInfo |
| 42 | delete Delete identity/key/certificate |
| 43 | export Export an identity package |
| 44 | import Import an identity package |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 45 | unlock-tpm Unlock Tpm |
| 46 | )STR"; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 47 | |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 48 | int |
Yingdi Yu | 5c1f841 | 2014-03-25 11:49:47 -0700 | [diff] [blame] | 49 | main(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 50 | { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 51 | if (argc < 2) { |
| 52 | std::cerr << ndnsec_helper << std::endl; |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | using namespace ndn::ndnsec; |
| 57 | |
| 58 | std::string command(argv[1]); |
| 59 | try { |
| 60 | if (command == "help") { std::cout << ndnsec_helper << std::endl; } |
| 61 | else if (command == "version") { std::cout << NDN_CXX_VERSION_BUILD_STRING << std::endl; } |
| 62 | else if (command == "list") { return ndnsec_list(argc - 1, argv + 1); } |
| 63 | else if (command == "get-default") { return ndnsec_get_default(argc - 1, argv + 1); } |
| 64 | else if (command == "set-default") { return ndnsec_set_default(argc - 1, argv + 1); } |
| 65 | else if (command == "key-gen") { return ndnsec_key_gen(argc - 1, argv + 1); } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 66 | else if (command == "sign-req") { return ndnsec_sign_req(argc - 1, argv + 1); } |
| 67 | else if (command == "cert-gen") { return ndnsec_cert_gen(argc - 1, argv + 1); } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 68 | else if (command == "cert-dump") { return ndnsec_cert_dump(argc - 1, argv + 1); } |
| 69 | else if (command == "cert-install") { return ndnsec_cert_install(argc - 1, argv + 1); } |
| 70 | else if (command == "delete") { return ndnsec_delete(argc - 1, argv + 1); } |
| 71 | else if (command == "export") { return ndnsec_export(argc - 1, argv + 1); } |
| 72 | else if (command == "import") { return ndnsec_import(argc - 1, argv + 1); } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 73 | else if (command == "unlock-tpm") { return ndnsec_unlock_tpm(argc - 1, argv + 1); } |
| 74 | else { |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 75 | std::cerr << ndnsec_helper << std::endl; |
| 76 | return 1; |
| 77 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 78 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 79 | catch (const std::exception& e) { |
| 80 | |
| 81 | std::cerr << "ERROR: " << e.what(); |
| 82 | |
| 83 | std::ostringstream extendedError; |
| 84 | const char* const* file = boost::get_error_info<boost::throw_file>(e); |
| 85 | const int* line = boost::get_error_info<boost::throw_line>(e); |
| 86 | const char* const* func = boost::get_error_info<boost::throw_function>(e); |
| 87 | if (file && line) { |
| 88 | extendedError << " [from " << *file << ":" << *line; |
| 89 | if (func) { |
| 90 | extendedError << " in " << *func; |
| 91 | } |
| 92 | extendedError << "]"; |
| 93 | } |
| 94 | NDN_LOG_ERROR(e.what() << extendedError.str()); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 95 | return 1; |
| 96 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |