E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gidentity.h
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 /// \file gidentity.h
19 ///
20
21 #ifndef G_IDENTITY_H
22 #define G_IDENTITY_H
23
24 #include "gdef.h"
25 #include "gexception.h"
26 #include "gsignalsafe.h"
27 #include <string>
28 #include <iostream>
29
30 namespace G
31 {
32 class Identity ;
33 class IdentityUser ;
34 }
35
36 /// \class G::Identity
37 /// A combination of user-id and group-id, with a very low-level interface
38 /// to the get/set/e/uid/gid functions. Uses getpwnam() to do username
39 /// lookups.
40 /// \see G::Process, G::Root
41 ///
42 class G::Identity
43 {
44 public:
45 G_EXCEPTION( NoSuchUser , "no such user" ) ;
46 G_EXCEPTION( UidError , "cannot set uid" ) ;
47 G_EXCEPTION( GidError , "cannot set gid" ) ;
48
49 explicit Identity( const std::string & login_name ) ;
50 ///< Constructor for the named identity.
51 ///< Throws if NoSuchUser.
52
53 static Identity effective() ;
54 ///< Returns the current effective identity.
55
56 static Identity real() ;
57 ///< Returns the calling process's real identity.
58
59 static Identity root() ;
60 ///< Returns the superuser identity.
61
62 static Identity invalid() ;
63 ///< Returns an invalid identity.
64
65 static Identity invalid( SignalSafe ) ;
66 ///< Returns an invalid identity, with a
67 ///< signal-safe guarantee.
68
69 bool isRoot() const ;
70 ///< Returns true if the userid is zero.
71
72 std::string str() const ;
73 ///< Returns a string representation.
74
75 void setRealUser( bool do_throw = true ) ;
76 ///< Sets the real userid.
77
78 void setEffectiveUser( bool do_throw = true ) ;
79 ///< Sets the effective userid.
80
81 void setEffectiveUser( SignalSafe ) ;
82 ///< Sets the effective userid.
83 ///< A signal-safe, reentrant overload.
84
85 void setRealGroup( bool do_throw = true ) ;
86 ///< Sets the real group id.
87
88 void setEffectiveGroup( bool do_throw = true ) ;
89 ///< Sets the effective group id.
90
91 void setEffectiveGroup( SignalSafe ) ;
92 ///< Sets the effective group id.
93 ///< A signal-safe, reentrant overload.
94
95 bool operator==( const Identity & ) const ;
96 ///< Comparison operator.
97
98 bool operator!=( const Identity & ) const ;
99 ///< Comparison operator.
100
101 private:
102 Identity() ; // no throw
103 explicit Identity( SignalSafe ) ; // no throw
104
105 private:
106 uid_t m_uid ;
107 gid_t m_gid ;
108 HANDLE m_h ; // windows
109 } ;
110
111 /// \class G::IdentityUser
112 /// A convenience class which, when used as a private base, can improve
113 /// readability when calling Identity 'set' methods.
114 ///
115 class G::IdentityUser
116 {
117 protected:
118 static void setRealUserTo( Identity , bool do_throw = true ) ;
119 ///< Sets the real userid.
120
121 static void setEffectiveUserTo( Identity , bool do_throw = true ) ;
122 ///< Sets the effective userid.
123
124 static void setEffectiveUserTo( SignalSafe , Identity ) ;
125 ///< Sets the effective userid.
126
127 static void setRealGroupTo( Identity , bool do_throw = true ) ;
128 ///< Sets the real group id.
129
130 static void setEffectiveGroupTo( Identity , bool do_throw = true ) ;
131 ///< Sets the effective group id.
132
133 static void setEffectiveGroupTo( SignalSafe , Identity ) ;
134 ///< Sets the effective group id.
135
136 private:
137 IdentityUser() ; // not implemented
138 } ;
139
140 namespace G
141 {
142 inline
143 std::ostream & operator<<( std::ostream & stream , const G::Identity & identity )
144 {
145 return stream << identity.str() ;
146 }
147 }
148
149 #endif
150
G::Identity::isRoot
bool isRoot() const
Returns true if the userid is zero.
Definition: gidentity_unix.cpp:132
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
G::Identity::root
static Identity root()
Returns the superuser identity.
Definition: gidentity_unix.cpp:117
G::Identity::real
static Identity real()
Returns the calling process's real identity.
Definition: gidentity_unix.cpp:99
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::IdentityUser::setRealGroupTo
static void setRealGroupTo(Identity, bool do_throw=true)
Sets the real group id.
Definition: gidentity_unix.cpp:194
G::Identity::effective
static Identity effective()
Returns the current effective identity.
Definition: gidentity_unix.cpp:91
gsignalsafe.h
G::Identity::setRealUser
void setRealUser(bool do_throw=true)
Sets the real userid.
Definition: gidentity_unix.cpp:157
G::Identity::setRealGroup
void setRealGroup(bool do_throw=true)
Sets the real group id.
Definition: gidentity_unix.cpp:172
G
Low-level classes.
Definition: garg.h:29
G::IdentityUser
A convenience class which, when used as a private base, can improve readability when calling Identity...
Definition: gidentity.h:115
G::IdentityUser::setEffectiveUserTo
static void setEffectiveUserTo(Identity, bool do_throw=true)
Sets the effective userid.
Definition: gidentity_unix.cpp:184
G::Identity::setEffectiveGroup
void setEffectiveGroup(bool do_throw=true)
Sets the effective group id.
Definition: gidentity_unix.cpp:162
G::Identity::operator==
bool operator==(const Identity &) const
Comparison operator.
Definition: gidentity_unix.cpp:137
gexception.h
G::IdentityUser::setEffectiveGroupTo
static void setEffectiveGroupTo(Identity, bool do_throw=true)
Sets the effective group id.
Definition: gidentity_unix.cpp:199
G::Identity::str
std::string str() const
Returns a string representation.
Definition: gidentity_unix.cpp:125
G::IdentityUser::setRealUserTo
static void setRealUserTo(Identity, bool do_throw=true)
Sets the real userid.
Definition: gidentity_unix.cpp:179
G::Identity::operator!=
bool operator!=(const Identity &) const
Comparison operator.
Definition: gidentity_unix.cpp:142
G::Identity::setEffectiveUser
void setEffectiveUser(bool do_throw=true)
Sets the effective userid.
Definition: gidentity_unix.cpp:152