E-MailRelay

gidentity.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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#include <new>
30
31 namespace G
32{
33 class Identity ;
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( NoSuchGroup , "no such group" ) ;
47 G_EXCEPTION( Error , "cannot read user database" ) ;
48
49 explicit Identity( const std::string & username ,
50 const std::string & group_name_override = std::string() ) ;
51 ///< Constructor for the named identity.
52 ///< Throws NoSuchUser on error.
53
54 static Identity effective() noexcept ;
55 ///< Returns the current effective identity.
56
57 static Identity real( bool with_cache = true ) noexcept ;
58 ///< Returns the calling process's real identity.
59
60 static Identity root() noexcept ;
61 ///< Returns the superuser identity.
62
63 static Identity invalid() noexcept ;
64 ///< Returns an invalid identity.
65
66 static Identity invalid( SignalSafe ) noexcept ;
67 ///< Returns an invalid identity, with a
68 ///< signal-safe guarantee.
69
70 bool isRoot() const noexcept ;
71 ///< Returns true if the userid is zero.
72
73 std::string str() const ;
74 ///< Returns a string representation.
75
76 uid_t userid() const noexcept ;
77 ///< Returns the user part.
78
79 gid_t groupid() const noexcept ;
80 ///< Returns the group part.
81
82 bool operator==( const Identity & ) const noexcept ;
83 ///< Comparison operator.
84
85 bool operator!=( const Identity & ) const noexcept ;
86 ///< Comparison operator.
87
88 static std::pair<uid_t,gid_t> lookupUser( const std::string & user ) ;
89 ///< Does a username lookup. Throws on error.
90
91 static gid_t lookupGroup( const std::string & group ) ;
92 ///< Does a groupname lookup. Throws on error.
93
94 private:
95 Identity() noexcept ;
96 explicit Identity( SignalSafe ) noexcept ;
97
98 private:
99 uid_t m_uid ;
100 gid_t m_gid ;
101 HANDLE m_h{0} ; // windows
102} ;
103
104 namespace G
105{
106 inline
107 std::ostream & operator<<( std::ostream & stream , const Identity & identity )
108 {
109 return stream << identity.str() ;
110 }
111}
112
113#endif
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:43
G::Identity::lookupUser
static std::pair< uid_t, gid_t > lookupUser(const std::string &user)
Does a username lookup. Throws on error.
Definition: gidentity_unix.cpp:67
G::Identity::groupid
gid_t groupid() const noexcept
Returns the group part.
Definition: gidentity_unix.cpp:189
G::Identity::isRoot
bool isRoot() const noexcept
Returns true if the userid is zero.
Definition: gidentity_unix.cpp:194
G::Identity::userid
uid_t userid() const noexcept
Returns the user part.
Definition: gidentity_unix.cpp:184
G::Identity::invalid
static Identity invalid() noexcept
Returns an invalid identity.
Definition: gidentity_unix.cpp:159
G::Identity::operator==
bool operator==(const Identity &) const noexcept
Comparison operator.
Definition: gidentity_unix.cpp:199
G::Identity::root
static Identity root() noexcept
Returns the superuser identity.
Definition: gidentity_unix.cpp:169
G::Identity::lookupGroup
static gid_t lookupGroup(const std::string &group)
Does a groupname lookup. Throws on error.
Definition: gidentity_unix.cpp:106
G::Identity::str
std::string str() const
Returns a string representation.
Definition: gidentity_unix.cpp:177
G::Identity::effective
static Identity effective() noexcept
Returns the current effective identity.
Definition: gidentity_unix.cpp:133
G::Identity::operator!=
bool operator!=(const Identity &) const noexcept
Comparison operator.
Definition: gidentity_unix.cpp:204
G::Identity::real
static Identity real(bool with_cache=true) noexcept
Returns the calling process's real identity.
Definition: gidentity_unix.cpp:142
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:37
gexception.h
gsignalsafe.h
G
Low-level classes.
Definition: galign.h:28