Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame^] | 24 | */ |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 25 | |
| 26 | #include "general-config-section.hpp" |
| 27 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
| 29 | #include "core/privilege-helper.hpp" |
| 30 | #include "core/config-file.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | namespace general { |
| 35 | |
| 36 | NFD_LOG_INIT("GeneralConfigSection"); |
| 37 | |
| 38 | static void |
| 39 | onConfig(const ConfigSection& configSection, |
| 40 | bool isDryRun, |
| 41 | const std::string& filename) |
| 42 | { |
| 43 | // general |
| 44 | // { |
| 45 | // ; user "ndn-user" |
| 46 | // ; group "ndn-user" |
| 47 | // } |
| 48 | |
| 49 | std::string user; |
| 50 | std::string group; |
| 51 | |
| 52 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 53 | i != configSection.end(); |
| 54 | ++i) |
| 55 | { |
| 56 | if (i->first == "user") |
| 57 | { |
| 58 | try |
| 59 | { |
| 60 | user = i->second.get_value<std::string>("user"); |
| 61 | |
| 62 | if (user.empty()) |
| 63 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 64 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\"" |
| 65 | " in \"general\" section")); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | catch (const boost::property_tree::ptree_error& error) |
| 69 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 70 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\"" |
| 71 | " in \"general\" section")); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | else if (i->first == "group") |
| 75 | { |
| 76 | try |
| 77 | { |
| 78 | group = i->second.get_value<std::string>("group"); |
| 79 | |
| 80 | if (group.empty()) |
| 81 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 82 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\"" |
| 83 | " in \"general\" section")); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | catch (const boost::property_tree::ptree_error& error) |
| 87 | { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 88 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\"" |
| 89 | " in \"general\" section")); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | NFD_LOG_TRACE("using user \"" << user << "\" group \"" << group << "\""); |
| 94 | |
| 95 | PrivilegeHelper::initialize(user, group); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | setConfigFile(ConfigFile& configFile) |
| 100 | { |
| 101 | configFile.addSectionHandler("general", &onConfig); |
| 102 | } |
| 103 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 104 | } // namespace general |
| 105 | |
| 106 | } // namespace nfd |