blob: aafda243259c13627ae0bc8290f2832d8976ce97 [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
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 Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_DELETE_HPP
16#define NDNSEC_DELETE_HPP
17
18#include "ndnsec-util.hpp"
19
Yingdi Yub61f5402014-02-26 17:46:11 -080020int
21ndnsec_delete(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022{
23 using namespace ndn;
24 namespace po = boost::program_options;
25
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070026 // bool deleteId = true;
Yingdi Yub61f5402014-02-26 17:46:11 -080027 bool isDeleteKey = false;
28 bool isDeleteCert = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029 std::string name;
30
Yingdi Yub61f5402014-02-26 17:46:11 -080031 po::options_description description("General Usage\n ndnsec delete [-h] [-k|c] name\nGeneral options");
32 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080034 ("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 Yu8d7468f2014-02-21 14:49:45 -080038 ("name,n", po::value<std::string>(&name), "By default, it refers to an identity."
Yingdi Yub61f5402014-02-26 17:46:11 -080039 "If -k is specified, it refers to a key."
40 "If -c is specified, it refers to a certificate.");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 ;
42
43 po::positional_options_description p;
44 p.add("name", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080045
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046 po::variables_map vm;
47 try
48 {
Yingdi Yub61f5402014-02-26 17:46:11 -080049 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
50 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080051 po::notify(vm);
52 }
Yingdi Yub61f5402014-02-26 17:46:11 -080053 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 {
55 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080056 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057 return 1;
58 }
59
Yingdi Yub61f5402014-02-26 17:46:11 -080060 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080061 {
Yingdi Yub61f5402014-02-26 17:46:11 -080062 std::cerr << description << std::endl;;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063 return 0;
64 }
65
Yingdi Yub61f5402014-02-26 17:46:11 -080066 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067 {
Yingdi Yub61f5402014-02-26 17:46:11 -080068 std::cerr << "ERROR: name must be specified" << std::endl;
69 std::cerr << description << std::endl;
70 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072
Yingdi Yub61f5402014-02-26 17:46:11 -080073 if (vm.count("delete-cert") != 0 || vm.count("delete-cert2") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080074 {
Yingdi Yub61f5402014-02-26 17:46:11 -080075 isDeleteCert = true;
76 // deleteId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080077 }
Yingdi Yub61f5402014-02-26 17:46:11 -080078 else if (vm.count("delete-key") != 0 || vm.count("delete-key2") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080079 {
Yingdi Yub61f5402014-02-26 17:46:11 -080080 isDeleteKey = true;
81 // deleteId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080082 }
Yingdi Yub61f5402014-02-26 17:46:11 -080083
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 Yu8d7468f2014-02-21 14:49:45 -0800100}
101
102#endif //NDNSEC_DELETE_HPP