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 
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 
79 
80  void setRealGroup( bool do_throw = true ) ;
82 
83  void setEffectiveGroup( bool do_throw = true ) ;
85 
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 
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 
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:35
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 very low-level interface to getpwnam() and the get/set/e/uid/gid functions.
Definition: gidentity.h:41
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.
Low-level classes.
A convenience class which, when used as a private base, can improve readability when calling Identity...
Definition: gidentity.h:109
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.
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
static void setEffectiveGroupTo(Identity, bool do_throw=true)
Sets the effective group id.
std::ostream & operator<<(std::ostream &stream, const G::Identity &identity)
Definition: gidentity.h:138
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.