VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gidentity.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 ///
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 
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 
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 ///
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 
bool isRoot() const
Returns true if the userid is zero.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
static Identity root()
Returns the superuser identity.
static Identity real()
Returns the calling process's real identity.
static Identity invalid()
Returns an invalid 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
static void setRealGroupTo(Identity, bool do_throw=true)
Sets the real group id.
static Identity effective()
Returns the current effective identity.
void setRealUser(bool do_throw=true)
Sets the real userid.
void setRealGroup(bool do_throw=true)
Sets the real group id.
A convenience class which, when used as a private base, can improve readability when calling Identity...
Definition: gidentity.h:115
static void setEffectiveUserTo(Identity, bool do_throw=true)
Sets the effective userid.
void setEffectiveGroup(bool do_throw=true)
Sets the effective group id.
bool operator==(const Identity &) const
Comparison operator.
static void setEffectiveGroupTo(Identity, bool do_throw=true)
Sets the effective group id.
std::string str() const
Returns a string representation.
static void setRealUserTo(Identity, bool do_throw=true)
Sets the real userid.
bool operator!=(const Identity &) const
Comparison operator.
void setEffectiveUser(bool do_throw=true)
Sets the effective userid.