DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
OksSystem::User Class Reference

User. More...

#include <User.hpp>

Collaboration diagram for OksSystem::User:
[legend]

Public Member Functions

 User () throw ()
 constructor for current user
 User (uid_t user_id) throw ()
 constructor from uid
 User (const std::string &name)
 constructor from name
 User (const User &other) throw ()
 copy constructor
 operator uid_t () const throw ()
 cast to user id
 operator std::string () const
 cast to string
 operator const char * () const
 cast to c-string
uid_t identity () const throw ()
 user-id
const std::string & name () const
 username
const std::string & name_safe () const throw ()
 username - no exception
const std::string & home () const
 home directory name
const std::string & real_name () const
 real name
void setuid () const
 tries to set the uid of current process

Static Public Attributes

static const User ROOT
 constant for root user

Protected Member Functions

void resolve () const
 resolve information from user-id
void resolve_safe () const throw ()
 resolve without throwing exceptions

Protected Attributes

uid_t m_user_id
 the actual user id
std::string m_user_name
 cached user name
std::string m_user_home
 cached user home directory
std::string m_user_real_name
 cached real user name

Detailed Description

User.

This class represents an user

Author
Matthias Wiesmann
Version
1.0

Definition at line 30 of file User.hpp.

Constructor & Destructor Documentation

◆ User() [1/4]

OksSystem::User::User ( )
throw ( )

constructor for current user

Definition at line 27 of file User.cpp.

27 {
28 m_user_id = ::getuid();
29} // User
uid_t m_user_id
the actual user id
Definition User.hpp:33

◆ User() [2/4]

OksSystem::User::User ( uid_t user_id)
throw ( )

constructor from uid

Definition at line 31 of file User.cpp.

31 {
32 m_user_id = user_id;
33} // User

◆ User() [3/4]

OksSystem::User::User ( const std::string & name)

constructor from name

Definition at line 35 of file User.cpp.

35 {
36
37 errno = 0;
38 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
39
40 if(bufSize == -1) {
41 if(errno == 0) {
42 bufSize = 1024;
43 } else {
44 std::string message = "with argument _SC_GETPW_R_SIZE_MAX while getting info about user " + s_name;
45 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "sysconf", message.c_str());
46 }
47 }
48
49 struct passwd pwd;
50 struct passwd *result;
51 char* buf = new char[bufSize];
52 errno = 0;
53 int res = ::getpwnam_r(s_name.c_str(), &pwd, buf, bufSize, &result);
54 if(res == 0) {
55 if(result != 0) {
56 m_user_id = result->pw_uid;
57 } else {
58 delete[] buf;
59 std::string eMsg = "User " + s_name + " not found";
60 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "getpwnam_r", eMsg.c_str());
61 }
62 } else {
63 delete[] buf;
64 std::string message = "while getting info about user " + s_name;
65 throw OksSystem::OksSystemCallIssue(ERS_HERE, res, "getpwnam_r", message.c_str());
66 }
67 delete[] buf;
68
69 m_user_name = s_name;
70} // User
#define ERS_HERE
std::string m_user_name
cached user name
Definition User.hpp:34
Only Configuration DB opened by rdbconfig or oksconflibs plug in can be message

◆ User() [4/4]

OksSystem::User::User ( const User & other)
throw ( )

copy constructor

Definition at line 72 of file User.cpp.

72 {
73 m_user_id = other.m_user_id;
74 m_user_name = other.m_user_name;
75} // User

Member Function Documentation

◆ home()

const std::string & OksSystem::User::home ( ) const

home directory name

Gets the home directory of the user

Returns
path to home directory

Definition at line 218 of file User.cpp.

218 {
219 if (m_user_home.empty()) { resolve();}
220 return m_user_home;
221} // home
void resolve() const
resolve information from user-id
Definition User.cpp:111
std::string m_user_home
cached user home directory
Definition User.hpp:35

◆ identity()

uid_t OksSystem::User::identity ( ) const
throw ( )

user-id

Returns the user_id for user

Returns
user-id

Definition at line 101 of file User.cpp.

101 {
102 return m_user_id;
103} // identity

◆ name()

const std::string & OksSystem::User::name ( ) const

username

Gets the (short) name of the user

Returns
the name of user

Definition at line 200 of file User.cpp.

200 {
201 if (m_user_name.empty()) { resolve();}
202 return m_user_name;
203} // name

◆ name_safe()

const std::string & OksSystem::User::name_safe ( ) const
throw ( )

username - no exception

Gets the (short) name of the user - no error is thrown in case of problem

Returns
the name of user

Definition at line 209 of file User.cpp.

209 {
210 if (m_user_name.empty()) { resolve_safe(); }
211 return m_user_name;
212} // name
void resolve_safe() const
resolve without throwing exceptions
Definition User.cpp:157

◆ operator const char *()

OksSystem::User::operator const char * ( ) const

cast to c-string

Definition at line 89 of file User.cpp.

89 {
90 if (m_user_name.empty()) { resolve();}
91 return m_user_name.c_str();
92} // char*

◆ operator std::string()

OksSystem::User::operator std::string ( ) const

cast to string

Definition at line 85 of file User.cpp.

85 {
86 return name();
87} // string
const std::string & name() const
username
Definition User.cpp:200

◆ operator uid_t()

OksSystem::User::operator uid_t ( ) const
throw ( )

cast to user id

Definition at line 81 of file User.cpp.

81 {
82 return m_user_id;
83} // uid_t

◆ real_name()

const std::string & OksSystem::User::real_name ( ) const

real name

Gets the 'real name' of the user

Returns
real name

Definition at line 227 of file User.cpp.

227 {
228 if (m_user_real_name.empty()) { resolve();}
229 return m_user_real_name;
230} // real_name
std::string m_user_real_name
cached real user name
Definition User.hpp:36

◆ resolve()

void OksSystem::User::resolve ( ) const
protected

resolve information from user-id

This method is responsible for filling in the mutable fields of the class when needed, this is done by calling the getpwuid function, all fields of the object are then filled in.

Exceptions
OksSystem::OksSystemCallIssueif the user information cannot be found

Definition at line 111 of file User.cpp.

111 {
112
113 errno = 0;
114 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
115
116 if(bufSize == -1) {
117 if(errno == 0) {
118 bufSize = 1024;
119 } else {
120 std::ostringstream message;
121 message << "with argument _SC_GETPW_R_SIZE_MAX while getting info about user with id " << m_user_id;
122 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "sysconf", message.str().c_str());
123 }
124 }
125
126 struct passwd pwd;
127 struct passwd *result;
128 char* buf = new char[bufSize];
129 errno = 0;
130 int res = ::getpwuid_r(m_user_id, &pwd, buf, bufSize, &result);
131 if(res == 0) {
132 if(result != 0) {
133 m_user_name = std::string(result->pw_name);
134 m_user_home = std::string(result->pw_dir);
135 m_user_real_name = std::string(result->pw_gecos);
136 } else {
137 delete[] buf;
138 std::ostringstream eMsg;
139 eMsg << "User " << m_user_id << " not found";
140 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "getpwuid_r", eMsg.str().c_str());
141 }
142 } else {
143 delete[] buf;
144 std::ostringstream message;
145 message << "while getting info about user with id " << m_user_id;
146 throw OksSystem::OksSystemCallIssue(ERS_HERE, res, "getpwnam_r", message.str().c_str());
147 }
148 delete[] buf;
149
150} //

◆ resolve_safe()

void OksSystem::User::resolve_safe ( ) const
throw ( )
protected

resolve without throwing exceptions

This method is the same as resolve() but does not throw exceptions. Instead the method silently fails

See also
OksSystem::User::resolve()

Definition at line 157 of file User.cpp.

157 {
158
159 errno = 0;
160 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
161
162 if(bufSize == -1) {
163 if(errno == 0) {
164 bufSize = 1024;
165 } else {
166 m_user_name = "unknown";
167 m_user_home = "unknown";
168 m_user_real_name = "unknown";
169 return;
170 }
171 }
172
173 struct passwd pwd;
174 struct passwd *result;
175 char* buf = new char[bufSize];
176 int res = ::getpwuid_r(m_user_id, &pwd, buf, bufSize, &result);
177 if(res == 0) {
178 if(result != 0) {
179 m_user_name = std::string(result->pw_name);
180 m_user_home = std::string(result->pw_dir);
181 m_user_real_name = std::string(result->pw_gecos);
182 } else {
183 m_user_name = "unknown";
184 m_user_home = "unknown";
185 m_user_real_name = "unknown";
186 }
187 } else {
188 m_user_name = "unknown";
189 m_user_home = "unknown";
190 m_user_real_name = "unknown";
191 }
192 delete[] buf;
193
194} // resolve_safe

◆ setuid()

void OksSystem::User::setuid ( ) const

tries to set the uid of current process

Sets the user-identity of the current process to this user. This method will only succeed if the current user has sufficient privileges to changed uids (typically because the current user is root).

Definition at line 237 of file User.cpp.

237 {
238 const int status = ::setuid(m_user_id);
239 if (status<0) {
240 std::ostringstream message;
241 message << "while setting the effective user ID to " << m_user_id << "(" << this->name_safe() << ")";
242 throw OksSystem::OksSystemCallIssue( ERS_HERE, errno, "setuid", message.str().c_str() );
243 }
244} // setuid
const std::string & name_safe() const
username - no exception
Definition User.cpp:209
void setuid() const
tries to set the uid of current process
Definition User.cpp:237

Member Data Documentation

◆ m_user_home

std::string OksSystem::User::m_user_home
mutableprotected

cached user home directory

Definition at line 35 of file User.hpp.

◆ m_user_id

uid_t OksSystem::User::m_user_id
protected

the actual user id

Definition at line 33 of file User.hpp.

◆ m_user_name

std::string OksSystem::User::m_user_name
mutableprotected

cached user name

Definition at line 34 of file User.hpp.

◆ m_user_real_name

std::string OksSystem::User::m_user_real_name
mutableprotected

cached real user name

Definition at line 36 of file User.hpp.

◆ ROOT

const OksSystem::User OksSystem::User::ROOT
static

constant for root user

Definition at line 40 of file User.hpp.


The documentation for this class was generated from the following files: