Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "client-config.hpp" |
| 22 | |
| 23 | namespace ndn { |
| 24 | namespace ndncert { |
| 25 | |
| 26 | void |
| 27 | ClientConfig::load(const std::string& fileName) |
| 28 | { |
| 29 | try { |
| 30 | boost::property_tree::read_json(fileName, m_config); |
| 31 | } |
| 32 | catch (const boost::property_tree::info_parser_error& error) { |
| 33 | BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName + |
| 34 | " " + error.message() + " line " + std::to_string(error.line()))); |
| 35 | } |
| 36 | |
| 37 | if (m_config.begin() == m_config.end()) { |
| 38 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + " no data")); |
| 39 | } |
| 40 | |
| 41 | parse(); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | ClientConfig::parse() |
| 46 | { |
| 47 | m_caItems.clear(); |
| 48 | auto caList = m_config.get_child("ca-list"); |
| 49 | auto it = caList.begin(); |
| 50 | for (; it != caList.end(); it++) { |
| 51 | CaItem item; |
| 52 | item.m_caName = Name(it->second.get<std::string>("ca-prefix")); |
| 53 | item.m_caInfo = it->second.get<std::string>("ca-info"); |
| 54 | item.m_probe = it->second.get("probe", ""); |
| 55 | |
| 56 | auto challengeList = it->second.get_child("supported-challenges"); |
| 57 | item.m_supportedChallenges = parseChallengeList(challengeList); |
| 58 | |
| 59 | m_caItems.push_back(item); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | std::list<std::string> |
| 64 | ClientConfig::parseChallengeList(const ConfigSection& section) |
| 65 | { |
| 66 | std::list<std::string> result; |
| 67 | auto it = section.begin(); |
| 68 | for (; it != section.end(); it++) { |
| 69 | result.push_back(it->second.get<std::string>("type")); |
| 70 | } |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | ClientConfig::addNewCaItem(const CaItem& item) |
| 76 | { |
| 77 | auto& caList = m_config.get_child("ca-list"); |
| 78 | |
| 79 | ConfigSection newCaItem; |
| 80 | ConfigSection newCaChallengeList; |
| 81 | newCaItem.put("ca-prefix", item.m_caName.toUri()); |
| 82 | newCaItem.put("ca-info", item.m_caInfo); |
| 83 | if (item.m_probe != "") { |
| 84 | newCaItem.put("probe", item.m_probe); |
| 85 | } |
| 86 | for (const auto& challengeType : item.m_supportedChallenges) { |
| 87 | ConfigSection challengeSection; |
| 88 | challengeSection.put("type", challengeType); |
| 89 | newCaChallengeList.push_back(std::make_pair("", challengeSection)); |
| 90 | } |
| 91 | newCaItem.add_child("supported-challenges", newCaChallengeList); |
| 92 | caList.push_back(std::make_pair("", newCaItem)); |
| 93 | |
| 94 | parse(); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | ClientConfig::removeCaItem(const Name& caName) |
| 99 | { |
| 100 | auto& caList = m_config.get_child("ca-list"); |
| 101 | auto it = caList.begin(); |
| 102 | while (it != caList.end()) { |
| 103 | if (it->second.get<std::string>("ca-prefix") == caName.toUri()) { |
| 104 | it = caList.erase(it); |
| 105 | break; |
| 106 | } |
| 107 | else { |
| 108 | it++; |
| 109 | } |
| 110 | } |
| 111 | parse(); |
| 112 | } |
| 113 | |
| 114 | } // namespace ndncert |
| 115 | } // namespace ndn |