Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [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 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NDNSEC_DELETE_HPP |
| 16 | #define NDNSEC_DELETE_HPP |
| 17 | |
| 18 | #include "ndnsec-util.hpp" |
| 19 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 20 | int |
| 21 | ndnsec_delete(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | { |
| 23 | using namespace ndn; |
| 24 | namespace po = boost::program_options; |
| 25 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 26 | // bool deleteId = true; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 27 | bool isDeleteKey = false; |
| 28 | bool isDeleteCert = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | std::string name; |
| 30 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 31 | po::options_description description("General Usage\n ndnsec delete [-h] [-k|c] name\nGeneral options"); |
| 32 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 33 | ("help,h", "produce help message") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 34 | ("delete-key,k", "(Optional) delete a key if specified.") |
| 35 | ("delete-key2,K", "(Optional) delete a key if specified.") |
| 36 | ("delete-cert,c", "(Optional) delete a certificate if specified.") |
| 37 | ("delete-cert2,C", "(Optional) delete a certificate if specified.") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 38 | ("name,n", po::value<std::string>(&name), "By default, it refers to an identity." |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 39 | "If -k is specified, it refers to a key." |
| 40 | "If -c is specified, it refers to a certificate."); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 41 | ; |
| 42 | |
| 43 | po::positional_options_description p; |
| 44 | p.add("name", 1); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 45 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 46 | po::variables_map vm; |
| 47 | try |
| 48 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 49 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 50 | vm); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 51 | po::notify(vm); |
| 52 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 53 | catch (const std::exception& e) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 54 | { |
| 55 | std::cerr << "ERROR: " << e.what() << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 56 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 57 | return 1; |
| 58 | } |
| 59 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 60 | if (vm.count("help") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 61 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 62 | std::cerr << description << std::endl;; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 66 | if (vm.count("name") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 67 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 68 | std::cerr << "ERROR: name must be specified" << std::endl; |
| 69 | std::cerr << description << std::endl; |
| 70 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 71 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 72 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 73 | if (vm.count("delete-cert") != 0 || vm.count("delete-cert2") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 74 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 75 | isDeleteCert = true; |
| 76 | // deleteId = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 77 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 78 | else if (vm.count("delete-key") != 0 || vm.count("delete-key2") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 79 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 80 | isDeleteKey = true; |
| 81 | // deleteId = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 82 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 83 | |
| 84 | KeyChain keyChain; |
| 85 | |
| 86 | if (isDeleteCert) |
| 87 | { |
| 88 | keyChain.deleteCertificate(name); |
| 89 | } |
| 90 | else if (isDeleteKey) |
| 91 | { |
| 92 | keyChain.deleteKey(name); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | keyChain.deleteIdentity(name); |
| 97 | } |
| 98 | |
| 99 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | #endif //NDNSEC_DELETE_HPP |