blob: cdd4d057d1ed2d0abdb211409be3ef301420c6b4 [file] [log] [blame]
Steve DiBenedetto24b9a642014-04-07 15:45:39 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoacd00872018-02-15 18:24:06 -05002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08004 * 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 DiBenedetto24b9a642014-04-07 15:45:39 -060010 *
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/>.
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -080024 */
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060025
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040026#ifndef NFD_DAEMON_COMMON_PRIVILEGE_HELPER_HPP
27#define NFD_DAEMON_COMMON_PRIVILEGE_HELPER_HPP
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060028
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "core/common.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060030
31#include <unistd.h>
32
33namespace nfd {
34
35class PrivilegeHelper
36{
37public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040038 /**
39 * \brief Indicates a serious seteuid/setegid failure.
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050040 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040041 * This should only be caught by main as part of a graceful program termination.
42 *
43 * \note This is not an std::exception and NDN_THROW should not be used.
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070044 */
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060045 class Error
46 {
47 public:
48 explicit
49 Error(const std::string& what)
50 : m_whatMessage(what)
51 {
52 }
53
54 const char*
55 what() const
56 {
Davide Pesaventoacd00872018-02-15 18:24:06 -050057 return m_whatMessage.data();
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060058 }
59
60 private:
61 const std::string m_whatMessage;
62 };
63
64 static void
65 initialize(const std::string& userName, const std::string& groupName);
66
67 static void
68 drop();
69
Davide Pesaventoacd00872018-02-15 18:24:06 -050070 template<class F>
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060071 static void
Davide Pesaventoacd00872018-02-15 18:24:06 -050072 runElevated(F&& f)
73 {
74 raise();
75 try {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040076 std::invoke(std::forward<F>(f));
Davide Pesaventoacd00872018-02-15 18:24:06 -050077 }
78 catch (...) {
79 drop();
80 throw;
81 }
82 drop();
83 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060084
Davide Pesavento264af772021-02-09 21:48:24 -050085NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060086 static void
87 raise();
88
Davide Pesavento264af772021-02-09 21:48:24 -050089NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
90#ifdef NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060091 static uid_t s_normalUid;
92 static gid_t s_normalGid;
93
94 static uid_t s_privilegedUid;
95 static gid_t s_privilegedGid;
Davide Pesavento264af772021-02-09 21:48:24 -050096#endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060097};
98
99} // namespace nfd
100
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400101#endif // NFD_DAEMON_COMMON_PRIVILEGE_HELPER_HPP