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
33 namespace 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///
44 class G::Identity
45{
46 public:
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
110 private:
111 Identity() noexcept ;
112 explicit Identity( SignalSafe ) noexcept ;
113 Identity( uid_t , gid_t ) ;
114 Identity( uid_t , gid_t , const std::string & ) ;
115
116 private:
117 uid_t m_uid ;
118 gid_t m_gid ;
119 std::string m_sid ; // windows
120} ;
121
122 namespace 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
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:45
G::Identity::groupid
gid_t groupid() const noexcept
Returns the group part (Unix).
Definition: gidentity_unix.cpp:111
G::Identity::sid
std::string sid() const
Returns the sid (Windows).
Definition: gidentity_win32.cpp:127
G::Identity::isRoot
bool isRoot() const noexcept
Returns true if the userid is zero.
Definition: gidentity_unix.cpp:116
G::Identity::userid
uid_t userid() const noexcept
Returns the user part (Unix).
Definition: gidentity_unix.cpp:106
G::Identity::invalid
static Identity invalid() noexcept
Returns an invalid identity.
Definition: gidentity_unix.cpp:80
G::Identity::operator==
bool operator==(const Identity &) const noexcept
Comparison operator.
Definition: gidentity_unix.cpp:121
G::Identity::match
bool match(std::pair< int, int > uid_range) const
Returns true if the user-id is in the given range or if not implemented.
Definition: gidentity_unix.cpp:157
G::Identity::root
static Identity root() noexcept
Returns the superuser identity.
Definition: gidentity_unix.cpp:92
G::Identity::lookupGroup
static gid_t lookupGroup(const std::string &group)
Does a groupname lookup.
Definition: gidentity_unix.cpp:150
G::Identity::lookup
static std::pair< Identity, std::string > lookup(std::string_view user)
Does a username lookup returning the identity and the canonical name.
Definition: gidentity_unix.cpp:132
G::Identity::str
std::string str() const
Returns a string representation.
Definition: gidentity_unix.cpp:98
G::Identity::effective
static Identity effective() noexcept
Returns the current effective identity.
Definition: gidentity_unix.cpp:70
G::Identity::operator!=
bool operator!=(const Identity &) const noexcept
Comparison operator.
Definition: gidentity_unix.cpp:126
G::Identity::real
static Identity real() noexcept
Returns the calling process's real identity.
Definition: gidentity_unix.cpp:75
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:37
gexception.h
gsignalsafe.h
gstringview.h
G
Low-level classes.
Definition: garg.h:36
G::tx
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84