- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gidentity.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 // ===
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
31 namespace G
32 {
33 class Identity ;
34 class IdentityUser ;
35 }
36
41 class G::Identity
42 {
43 public:
44 G_EXCEPTION( NoSuchUser , "no such user" ) ;
45 G_EXCEPTION( UidError , "cannot set uid" ) ;
46 G_EXCEPTION( GidError , "cannot set gid" ) ;
47
48 explicit Identity( const std::string & login_name ) ;
51
52 static Identity effective() ;
54
55 static Identity real() ;
57
58 static Identity root() ;
60
61 static Identity invalid() ;
63
64 bool isRoot() const ;
66
67 std::string str() const ;
69
70 void setRealUser( bool do_throw = true ) ;
72
73 void setEffectiveUser( bool do_throw = true ) ;
75
76 void setEffectiveUser( SignalSafe ) ;
79
80 void setRealGroup( bool do_throw = true ) ;
82
83 void setEffectiveGroup( bool do_throw = true ) ;
85
86 void setEffectiveGroup( SignalSafe ) ;
89
90 bool operator==( const Identity & ) const ;
92
93 bool operator!=( const Identity & ) const ;
95
96 private:
97 Identity() ; // no throw
98
99 private:
100 uid_t m_uid ;
101 gid_t m_gid ;
102 HANDLE m_h ; // windows
103 } ;
104
109 class G::IdentityUser
110 {
111 protected:
112 static void setRealUserTo( Identity , bool do_throw = true ) ;
114
115 static void setEffectiveUserTo( Identity , bool do_throw = true ) ;
117
118 static void setEffectiveUserTo( SignalSafe , Identity ) ;
120
121 static void setRealGroupTo( Identity , bool do_throw = true ) ;
123
124 static void setEffectiveGroupTo( Identity , bool do_throw = true ) ;
126
127 static void setEffectiveGroupTo( SignalSafe , Identity ) ;
129
130 private:
131 IdentityUser() ; // not implemented
132 } ;
133
135 namespace G
136 {
137 inline
138 std::ostream & operator<<( std::ostream & stream , const G::Identity & identity )
139 {
140 return stream << identity.str() ;
141 }
142 }
143
144 #endif
145
G::Identity::isRoot
bool isRoot() const
Returns true if the userid is zero.
Definition: gidentity_unix.cpp:122
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:35
G::Identity::root
static Identity root()
Returns the superuser identity.
Definition: gidentity_unix.cpp:107
G::Identity::real
static Identity real()
Returns the calling process's real identity.
Definition: gidentity_unix.cpp:94
G::Identity::invalid
static Identity invalid()
Returns an invalid identity.
Definition: gidentity_unix.cpp:102
G::Identity
A very low-level interface to getpwnam() and the get/set/e/uid/gid functions.
Definition: gidentity.h:41
G::IdentityUser::setRealGroupTo
static void setRealGroupTo(Identity, bool do_throw=true)
Sets the real group id.
Definition: gidentity_unix.cpp:184
G::Identity::effective
static Identity effective()
Returns the current effective identity.
Definition: gidentity_unix.cpp:86
gsignalsafe.h
G::Identity::setRealUser
void setRealUser(bool do_throw=true)
Sets the real userid.
Definition: gidentity_unix.cpp:147
G::Identity::setRealGroup
void setRealGroup(bool do_throw=true)
Sets the real group id.
Definition: gidentity_unix.cpp:162
G
Low-level classes.
G::IdentityUser
A convenience class which, when used as a private base, can improve readability when calling Identity...
Definition: gidentity.h:109
G::IdentityUser::setEffectiveUserTo
static void setEffectiveUserTo(Identity, bool do_throw=true)
Sets the effective userid.
Definition: gidentity_unix.cpp:174
G::Identity::setEffectiveGroup
void setEffectiveGroup(bool do_throw=true)
Sets the effective group id.
Definition: gidentity_unix.cpp:152
G::Identity::operator==
bool operator==(const Identity &) const
Comparison operator.
Definition: gidentity_unix.cpp:127
gexception.h
G_EXCEPTION
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
G::IdentityUser::setEffectiveGroupTo
static void setEffectiveGroupTo(Identity, bool do_throw=true)
Sets the effective group id.
Definition: gidentity_unix.cpp:189
G::operator<<
std::ostream & operator<<(std::ostream &stream, const G::Identity &identity)
Definition: gidentity.h:138
G::Identity::str
std::string str() const
Returns a string representation.
Definition: gidentity_unix.cpp:115
G::IdentityUser::setRealUserTo
static void setRealUserTo(Identity, bool do_throw=true)
Sets the real userid.
Definition: gidentity_unix.cpp:169
G::Identity::operator!=
bool operator!=(const Identity &) const
Comparison operator.
Definition: gidentity_unix.cpp:132
G::Identity::setEffectiveUser
void setEffectiveUser(bool do_throw=true)
Sets the effective userid.
Definition: gidentity_unix.cpp:142