E-MailRelay
gidentity.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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 <utility>
30#include <new>
31
32namespace G
33{
34 class Identity ;
35}
36
37//| \class G::Identity
38/// A combination of user-id and group-id, with a very low-level interface
39/// to the get/set/e/uid/gid functions. Uses getpwnam() to do username
40/// lookups.
41/// \see G::Process, G::Root
42///
44{
45public:
46 G_EXCEPTION( NoSuchUser , tx("no such user") ) ;
47 G_EXCEPTION( NoSuchGroup , tx("no such group") ) ;
48 G_EXCEPTION( Error , tx("cannot read user database") ) ;
49
50 explicit Identity( const std::string & username ,
51 const std::string & group_name_override = {} ) ;
52 ///< Constructor for the named identity.
53 ///< Throws NoSuchUser on error.
54
55 static Identity effective() noexcept ;
56 ///< Returns the current effective identity.
57
58 static Identity real() noexcept ;
59 ///< Returns the calling process's real identity.
60
61 static Identity root() noexcept ;
62 ///< Returns the superuser identity.
63
64 static Identity invalid() noexcept ;
65 ///< Returns an invalid identity.
66
67 static Identity invalid( SignalSafe ) noexcept ;
68 ///< Returns an invalid identity, with a
69 ///< signal-safe guarantee.
70
71 bool isRoot() const noexcept ;
72 ///< Returns true if the userid is zero.
73
74 std::string str() const ;
75 ///< Returns a string representation.
76
77 uid_t userid() const noexcept ;
78 ///< Returns the user part (Unix).
79
80 gid_t groupid() const noexcept ;
81 ///< Returns the group part (Unix).
82
83 std::string sid() const ;
84 ///< Returns the sid (Windows).
85
86 bool operator==( const Identity & ) const noexcept ;
87 ///< Comparison operator.
88
89 bool operator!=( const Identity & ) const noexcept ;
90 ///< Comparison operator.
91
92 static std::pair<Identity,std::string> lookup( const std::string & user ) ;
93 ///< Does a username lookup returning the identity and the
94 ///< canonical name. Throws if no such user or on error.
95
96 static std::pair<Identity,std::string> lookup( const std::string & user , std::nothrow_t ) ;
97 ///< Does a username lookup returning the identity and the
98 ///< canonical name. Returns with Identitiy::invalid() if
99 ///< no such user. Throws on error.
100
101 static gid_t lookupGroup( const std::string & group ) ;
102 ///< Does a groupname lookup. Returns -1 if no
103 ///< such group. Throws on error.
104
105 bool match( std::pair<int,int> uid_range ) const ;
106 ///< Returns true if the user-id is in the given range
107 ///< or if not implemented.
108
109private:
110 Identity() noexcept ;
111 explicit Identity( SignalSafe ) noexcept ;
112 Identity( uid_t , gid_t ) ;
113 Identity( uid_t , gid_t , const std::string & ) ;
114
115private:
116 uid_t m_uid ;
117 gid_t m_gid ;
118 std::string m_sid ; // windows
119} ;
120
121namespace G
122{
123 inline
124 std::ostream & operator<<( std::ostream & stream , const Identity & identity )
125 {
126 return stream << identity.str() ;
127 }
128}
129
130#endif
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:44
gid_t groupid() const noexcept
Returns the group part (Unix).
std::string sid() const
Returns the sid (Windows).
bool isRoot() const noexcept
Returns true if the userid is zero.
uid_t userid() const noexcept
Returns the user part (Unix).
static Identity invalid() noexcept
Returns an invalid identity.
bool operator==(const Identity &) const noexcept
Comparison operator.
bool match(std::pair< int, int > uid_range) const
Returns true if the user-id is in the given range or if not implemented.
static Identity root() noexcept
Returns the superuser identity.
static gid_t lookupGroup(const std::string &group)
Does a groupname lookup.
std::string str() const
Returns a string representation.
static Identity effective() noexcept
Returns the current effective identity.
bool operator!=(const Identity &) const noexcept
Comparison operator.
static std::pair< Identity, std::string > lookup(const std::string &user)
Does a username lookup returning the identity and the canonical name.
static Identity real() noexcept
Returns the calling process's real identity.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:37
Low-level classes.
Definition: garg.h:30
constexpr const char * tx(const char *p)
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84