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 | |
| 13 | int |
| 14 | ndnsec_delete(int argc, char** argv) |
| 15 | { |
| 16 | using namespace ndn; |
| 17 | namespace po = boost::program_options; |
| 18 | |
| 19 | bool deleteId = true; |
| 20 | bool deleteKey = false; |
| 21 | bool deleteCert = false; |
| 22 | std::string name; |
| 23 | |
| 24 | po::options_description desc("General Usage\n ndnsec delete [-h] [-K|C] name\nGeneral options"); |
| 25 | desc.add_options() |
| 26 | ("help,h", "produce help message") |
| 27 | ("delete_key,K", "(Optional) delete a key if specified.") |
| 28 | ("delete_cert,C", "(Optional) delete a certificate if specified.") |
| 29 | ("name,n", po::value<std::string>(&name), "By default, it refers to an identity." |
| 30 | "If -K is specified, it refers to a key." |
| 31 | "If -C is specified, it refers to a certificate."); |
| 32 | ; |
| 33 | |
| 34 | po::positional_options_description p; |
| 35 | p.add("name", 1); |
| 36 | |
| 37 | po::variables_map vm; |
| 38 | try |
| 39 | { |
| 40 | po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
| 41 | po::notify(vm); |
| 42 | } |
| 43 | catch (std::exception &e) |
| 44 | { |
| 45 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | if (vm.count("help")) |
| 50 | { |
| 51 | std::cerr << desc << std::endl;; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | if(vm.count("delete_cert")) |
| 56 | { |
| 57 | deleteCert = true; |
| 58 | deleteId = false; |
| 59 | } |
| 60 | else if(vm.count("delete_key")) |
| 61 | { |
| 62 | deleteKey = true; |
| 63 | deleteId = false; |
| 64 | } |
| 65 | |
| 66 | try |
| 67 | { |
| 68 | KeyChain keyChain; |
| 69 | |
| 70 | if(deleteCert) |
| 71 | { |
| 72 | keyChain.deleteCertificate(name); |
| 73 | } |
| 74 | else if(deleteKey) |
| 75 | { |
| 76 | keyChain.deleteKey(name); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | keyChain.deleteIdentity(name); |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | |
| 85 | } |
| 86 | catch(SecPublicInfo::Error& e) |
| 87 | { |
| 88 | std::cerr << e.what() << std::endl; |
| 89 | return 1; |
| 90 | } |
| 91 | catch(SecTpm::Error& e) |
| 92 | { |
| 93 | std::cerr << e.what() << std::endl; |
| 94 | return 1; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #endif //NDNSEC_DELETE_HPP |