blob: bdce78f4fc598339961f50417c00676fbb51df40 [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- 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 Yub61f5402014-02-26 17:46:11 -080013int
14ndnsec_delete(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080015{
16 using namespace ndn;
17 namespace po = boost::program_options;
18
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070019 // bool deleteId = true;
Yingdi Yub61f5402014-02-26 17:46:11 -080020 bool isDeleteKey = false;
21 bool isDeleteCert = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 std::string name;
23
Yingdi Yub61f5402014-02-26 17:46:11 -080024 po::options_description description("General Usage\n ndnsec delete [-h] [-k|c] name\nGeneral options");
25 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080027 ("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 Yu8d7468f2014-02-21 14:49:45 -080031 ("name,n", po::value<std::string>(&name), "By default, it refers to an identity."
Yingdi Yub61f5402014-02-26 17:46:11 -080032 "If -k is specified, it refers to a key."
33 "If -c is specified, it refers to a certificate.");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034 ;
35
36 po::positional_options_description p;
37 p.add("name", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080038
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039 po::variables_map vm;
40 try
41 {
Yingdi Yub61f5402014-02-26 17:46:11 -080042 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
43 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 po::notify(vm);
45 }
Yingdi Yub61f5402014-02-26 17:46:11 -080046 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080047 {
48 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080049 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 return 1;
51 }
52
Yingdi Yub61f5402014-02-26 17:46:11 -080053 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 {
Yingdi Yub61f5402014-02-26 17:46:11 -080055 std::cerr << description << std::endl;;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 return 0;
57 }
58
Yingdi Yub61f5402014-02-26 17:46:11 -080059 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080060 {
Yingdi Yub61f5402014-02-26 17:46:11 -080061 std::cerr << "ERROR: name must be specified" << std::endl;
62 std::cerr << description << std::endl;
63 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080064 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065
Yingdi Yub61f5402014-02-26 17:46:11 -080066 if (vm.count("delete-cert") != 0 || vm.count("delete-cert2") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067 {
Yingdi Yub61f5402014-02-26 17:46:11 -080068 isDeleteCert = true;
69 // deleteId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070 }
Yingdi Yub61f5402014-02-26 17:46:11 -080071 else if (vm.count("delete-key") != 0 || vm.count("delete-key2") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072 {
Yingdi Yub61f5402014-02-26 17:46:11 -080073 isDeleteKey = true;
74 // deleteId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075 }
Yingdi Yub61f5402014-02-26 17:46:11 -080076
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 Yu8d7468f2014-02-21 14:49:45 -080093}
94
95#endif //NDNSEC_DELETE_HPP