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 25 of file User.hpp.

Constructor & Destructor Documentation

◆ User() [1/4]

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

constructor for current user

Definition at line 22 of file User.cpp.

22 {
23 m_user_id = ::getuid();
24} // User
uid_t m_user_id
the actual user id
Definition User.hpp:28

◆ User() [2/4]

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

constructor from uid

Definition at line 26 of file User.cpp.

26 {
27 m_user_id = user_id;
28} // User

◆ User() [3/4]

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

constructor from name

Definition at line 30 of file User.cpp.

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

◆ User() [4/4]

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

copy constructor

Definition at line 67 of file User.cpp.

67 {
68 m_user_id = other.m_user_id;
69 m_user_name = other.m_user_name;
70} // 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 213 of file User.cpp.

213 {
214 if (m_user_home.empty()) { resolve();}
215 return m_user_home;
216} // home
void resolve() const
resolve information from user-id
Definition User.cpp:106
std::string m_user_home
cached user home directory
Definition User.hpp:30

◆ identity()

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

user-id

Returns the user_id for user

Returns
user-id

Definition at line 96 of file User.cpp.

96 {
97 return m_user_id;
98} // 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 195 of file User.cpp.

195 {
196 if (m_user_name.empty()) { resolve();}
197 return m_user_name;
198} // 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 204 of file User.cpp.

204 {
205 if (m_user_name.empty()) { resolve_safe(); }
206 return m_user_name;
207} // name
void resolve_safe() const
resolve without throwing exceptions
Definition User.cpp:152

◆ operator const char *()

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

cast to c-string

Definition at line 84 of file User.cpp.

84 {
85 if (m_user_name.empty()) { resolve();}
86 return m_user_name.c_str();
87} // char*

◆ operator std::string()

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

cast to string

Definition at line 80 of file User.cpp.

80 {
81 return name();
82} // string
const std::string & name() const
username
Definition User.cpp:195

◆ operator uid_t()

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

cast to user id

Definition at line 76 of file User.cpp.

76 {
77 return m_user_id;
78} // 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 222 of file User.cpp.

222 {
223 if (m_user_real_name.empty()) { resolve();}
224 return m_user_real_name;
225} // real_name
std::string m_user_real_name
cached real user name
Definition User.hpp:31

◆ 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 106 of file User.cpp.

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

◆ 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 152 of file User.cpp.

152 {
153
154 errno = 0;
155 long bufSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
156
157 if(bufSize == -1) {
158 if(errno == 0) {
159 bufSize = 1024;
160 } else {
161 m_user_name = "unknown";
162 m_user_home = "unknown";
163 m_user_real_name = "unknown";
164 return;
165 }
166 }
167
168 struct passwd pwd;
169 struct passwd *result;
170 char* buf = new char[bufSize];
171 int res = ::getpwuid_r(m_user_id, &pwd, buf, bufSize, &result);
172 if(res == 0) {
173 if(result != 0) {
174 m_user_name = std::string(result->pw_name);
175 m_user_home = std::string(result->pw_dir);
176 m_user_real_name = std::string(result->pw_gecos);
177 } else {
178 m_user_name = "unknown";
179 m_user_home = "unknown";
180 m_user_real_name = "unknown";
181 }
182 } else {
183 m_user_name = "unknown";
184 m_user_home = "unknown";
185 m_user_real_name = "unknown";
186 }
187 delete[] buf;
188
189} // 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 232 of file User.cpp.

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

Member Data Documentation

◆ m_user_home

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

cached user home directory

Definition at line 30 of file User.hpp.

◆ m_user_id

uid_t OksSystem::User::m_user_id
protected

the actual user id

Definition at line 28 of file User.hpp.

◆ m_user_name

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

cached user name

Definition at line 29 of file User.hpp.

◆ m_user_real_name

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

cached real user name

Definition at line 31 of file User.hpp.

◆ ROOT

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

constant for root user

Definition at line 35 of file User.hpp.


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