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