Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * BSD license, See the LICENSE file for more information |
| 5 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDNSEC_DELETE_HPP |
| 9 | #define NDNSEC_DELETE_HPP |
| 10 | |
| 11 | #include "ndnsec-util.hpp" |
| 12 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 13 | int |
| 14 | ndnsec_delete(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 15 | { |
| 16 | using namespace ndn; |
| 17 | namespace po = boost::program_options; |
| 18 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 19 | // bool deleteId = true; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 20 | bool isDeleteKey = false; |
| 21 | bool isDeleteCert = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | std::string name; |
| 23 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 24 | po::options_description description("General Usage\n ndnsec delete [-h] [-k|c] name\nGeneral options"); |
| 25 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 26 | ("help,h", "produce help message") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 27 | ("delete-key,k", "(Optional) delete a key if specified.") |
| 28 | ("delete-key2,K", "(Optional) delete a key if specified.") |
| 29 | ("delete-cert,c", "(Optional) delete a certificate if specified.") |
| 30 | ("delete-cert2,C", "(Optional) delete a certificate if specified.") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 31 | ("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^] | 32 | "If -k is specified, it refers to a key." |
| 33 | "If -c is specified, it refers to a certificate."); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 34 | ; |
| 35 | |
| 36 | po::positional_options_description p; |
| 37 | p.add("name", 1); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 38 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 39 | po::variables_map vm; |
| 40 | try |
| 41 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 42 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 43 | vm); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 44 | po::notify(vm); |
| 45 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 46 | catch (const std::exception& e) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 47 | { |
| 48 | std::cerr << "ERROR: " << e.what() << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 49 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 50 | return 1; |
| 51 | } |
| 52 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 53 | if (vm.count("help") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 54 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 55 | std::cerr << description << std::endl;; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 59 | if (vm.count("name") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 60 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 61 | std::cerr << "ERROR: name must be specified" << std::endl; |
| 62 | std::cerr << description << std::endl; |
| 63 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 64 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 65 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 66 | if (vm.count("delete-cert") != 0 || vm.count("delete-cert2") != 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 | isDeleteCert = true; |
| 69 | // deleteId = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 71 | else if (vm.count("delete-key") != 0 || vm.count("delete-key2") != 0) |
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 | isDeleteKey = true; |
| 74 | // deleteId = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 75 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 76 | |
| 77 | KeyChain keyChain; |
| 78 | |
| 79 | if (isDeleteCert) |
| 80 | { |
| 81 | keyChain.deleteCertificate(name); |
| 82 | } |
| 83 | else if (isDeleteKey) |
| 84 | { |
| 85 | keyChain.deleteKey(name); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | keyChain.deleteIdentity(name); |
| 90 | } |
| 91 | |
| 92 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | #endif //NDNSEC_DELETE_HPP |