E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

groot.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // groot.cpp
19 //
20
21 #include "gdef.h"
22 #include "groot.h"
23 #include "gtest.h"
24 #include "gassert.h"
25 #include "gprocess.h"
26 #include "gdebug.h"
27
28 G::Root * G::Root::m_this = nullptr ;
29 bool G::Root::m_initialised = false ;
30 bool G::Root::m_default_change_group = true ;
31 G::Identity G::Root::m_special( G::Identity::invalid() ) ;
32 G::Identity G::Root::m_ordinary( G::Identity::invalid() ) ;
33
34 G::Root::Root() :
35 m_change_group(m_default_change_group)
36 {
37 if( G::Test::enabled("root-scope") && m_this != nullptr )
38 G_WARNING( "G::Root::ctor: root control object exists at outer scope" ) ;
39
40 if( m_this == nullptr && m_initialised )
41 {
42 Process::beSpecial( m_special , m_change_group ) ;
43 m_this = this ;
44 }
45 }
46
47 G::Root::Root( bool change_group ) :
48 m_change_group(change_group)
49 {
50 if( G::Test::enabled("root-scope") && m_this != nullptr )
51 G_WARNING( "G::Root::ctor: root control object exists at outer scope" ) ;
52
53 if( m_this == nullptr && m_initialised )
54 {
55 Process::beSpecial( m_special , m_change_group ) ;
56 m_this = this ;
57 }
58 }
59
60 G::Root::~Root()
61 {
62 try
63 {
64 if( m_this == this && m_initialised )
65 {
66 m_this = nullptr ;
67 Process::beOrdinary( m_ordinary , m_change_group ) ;
68 }
69 }
70 catch( std::exception & e )
71 {
72 G_ERROR( "G::Root: cannot release root privileges: " << e.what() ) ;
73 }
74 catch(...)
75 {
76 G_ERROR( "G::Root: cannot release root privileges" ) ;
77 }
78 }
79
80 G::Identity G::Root::start( SignalSafe safe )
81 {
82 if( !m_initialised ) return Identity::invalid( safe ) ;
83 return Process::beSpecial( safe , m_special , m_default_change_group ) ;
84 }
85
86 void G::Root::stop( SignalSafe safe , Identity identity )
87 {
88 if( identity == Identity::invalid(safe) ) return ;
89 Process::beOrdinary( safe , identity , m_default_change_group ) ;
90 }
91
92 void G::Root::init( const std::string & non_root , bool default_change_group )
93 {
94 G_ASSERT( !non_root.empty() ) ;
95 Process::revokeExtraGroups() ;
96 m_ordinary = Identity( non_root ) ;
97 m_special = Process::beOrdinary( SignalSafe() , m_ordinary , default_change_group ) ;
98 m_initialised = true ;
99 m_default_change_group = default_change_group ;
100 }
101
102 G::Identity G::Root::nobody()
103 {
104 return m_ordinary ;
105 }
106
107 /// \file groot.cpp
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
G::Root::nobody
static Identity nobody()
Returns the 'nobody' identity corresponding to the init() user name.
Definition: groot.cpp:102
G::Identity::invalid
static Identity invalid()
Returns an invalid identity.
Definition: gidentity_unix.cpp:107
G::Identity
A combination of user-id and group-id, with a very low-level interface to the get/set/e/uid/gid funct...
Definition: gidentity.h:42
G::Root
A class which acquires the process's special privileges on construction and releases them on destruct...
Definition: groot.h:49
G::Root::start
static Identity start(SignalSafe)
A signal-safe alternative to construction.
Definition: groot.cpp:80
groot.h
gtest.h
G::Root::init
static void init(const std::string &non_root, bool default_change_group=true)
Initialises this class on process start-up by releasing root (or suid) privileges.
Definition: groot.cpp:92
G::Root::stop
static void stop(SignalSafe, Identity)
A signal-safe alternative to destruction.
Definition: groot.cpp:86
G::Root::Root
Root()
Default constructor.
Definition: groot.cpp:34
gassert.h
G::Test::enabled
static bool enabled()
Returns true if test features are enabled.
Definition: gtest.cpp:69
G::Root::~Root
~Root()
Desctructor.
Definition: groot.cpp:60
gprocess.h
gdebug.h