From 99f8ce096bc5569adbfea1911dbcda24c28d8d8b Mon Sep 17 00:00:00 2001 From: Ben Summers Date: Fri, 14 Oct 2005 08:50:54 +0000 Subject: Box Backup 0.09 with a few tweeks --- lib/common/UnixUser.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100755 lib/common/UnixUser.cpp (limited to 'lib/common/UnixUser.cpp') diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp new file mode 100755 index 00000000..7a60b263 --- /dev/null +++ b/lib/common/UnixUser.cpp @@ -0,0 +1,121 @@ +// -------------------------------------------------------------------------- +// +// File +// Name: UnixUser.cpp +// Purpose: Interface for managing the UNIX user of the current process +// Created: 21/1/04 +// +// -------------------------------------------------------------------------- + +#include "Box.h" + +#include +#include + +#include "UnixUser.h" +#include "CommonException.h" + +#include "MemLeakFindOn.h" + + +// -------------------------------------------------------------------------- +// +// Function +// Name: UnixUser::UnixUser(const char *) +// Purpose: Constructor, initialises to info of given username +// Created: 21/1/04 +// +// -------------------------------------------------------------------------- +UnixUser::UnixUser(const char *Username) + : mUID(0), + mGID(0), + mRevertOnDestruction(false) +{ + // Get password info + struct passwd *pwd = ::getpwnam(Username); + if(pwd == 0) + { + THROW_EXCEPTION(CommonException, CouldNotLookUpUsername) + } + + // Store UID and GID + mUID = pwd->pw_uid; + mGID = pwd->pw_gid; +} + + +// -------------------------------------------------------------------------- +// +// Function +// Name: UnixUser::UnixUser(uid_t, gid_t) +// Purpose: Construct from given UNIX user ID and group ID +// Created: 15/3/04 +// +// -------------------------------------------------------------------------- +UnixUser::UnixUser(uid_t UID, gid_t GID) + : mUID(UID), + mGID(GID), + mRevertOnDestruction(false) +{ +} + + +// -------------------------------------------------------------------------- +// +// Function +// Name: UnixUser::~UnixUser() +// Purpose: Destructor -- reverts to previous user if the change wasn't perminant +// Created: 21/1/04 +// +// -------------------------------------------------------------------------- +UnixUser::~UnixUser() +{ + if(mRevertOnDestruction) + { + // Revert to "real" user and group id of the process + if(::setegid(::getgid()) != 0 + || ::seteuid(::getuid()) != 0) + { + THROW_EXCEPTION(CommonException, CouldNotRestoreProcessUser) + } + } +} + + +// -------------------------------------------------------------------------- +// +// Function +// Name: UnixUser::ChangeProcessUser(bool) +// Purpose: Change the process user and group ID to the user. If Temporary == true +// the process username will be changed back when the object is destructed. +// Created: 21/1/04 +// +// -------------------------------------------------------------------------- +void UnixUser::ChangeProcessUser(bool Temporary) +{ + if(Temporary) + { + // Change temporarily (change effective only) + if(::setegid(mGID) != 0 + || ::seteuid(mUID) != 0) + { + THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser) + } + + // Mark for change on destruction + mRevertOnDestruction = true; + } + else + { + // Change perminantely (change all UIDs and GIDs) + if(::setgid(mGID) != 0 + || ::setuid(mUID) != 0) + { + THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser) + } + } +} + + + + -- cgit v1.2.3 From 3bedf8846f4d7a5cb38276b274662d62a36dcd52 Mon Sep 17 00:00:00 2001 From: Martin Ebourne Date: Mon, 12 Dec 2005 20:50:00 +0000 Subject: Marged chris/win32/merge/07-win32-fixes at r210 to trunk --- lib/common/UnixUser.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/common/UnixUser.cpp') diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp index 7a60b263..8b85c3e1 100755 --- a/lib/common/UnixUser.cpp +++ b/lib/common/UnixUser.cpp @@ -9,7 +9,10 @@ #include "Box.h" +#ifndef WIN32 #include +#endif + #include #include "UnixUser.h" -- cgit v1.2.3 From 8b99bea2180508f1a8720074fa043828583ce98b Mon Sep 17 00:00:00 2001 From: Martin Ebourne Date: Mon, 12 Dec 2005 21:03:38 +0000 Subject: Fixing up svn:executable properties. You may need a new checkout to see this. --- lib/common/UnixUser.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 lib/common/UnixUser.cpp (limited to 'lib/common/UnixUser.cpp') diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp old mode 100755 new mode 100644 -- cgit v1.2.3 From 830aa82e44381c85d8486e46de7ae0e26830457e Mon Sep 17 00:00:00 2001 From: Ben Summers Date: Mon, 13 Feb 2006 13:30:21 +0000 Subject: Merge chris/win32/vc2005-compile-fixes @ r455, add infrastructure/msvc to distribution --- lib/common/UnixUser.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'lib/common/UnixUser.cpp') diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp index 8b85c3e1..df2d0ddd 100644 --- a/lib/common/UnixUser.cpp +++ b/lib/common/UnixUser.cpp @@ -9,11 +9,13 @@ #include "Box.h" -#ifndef WIN32 -#include +#ifdef HAVE_PWD_H + #include #endif -#include +#ifdef HAVE_UNISTD_H + #include +#endif #include "UnixUser.h" #include "CommonException.h" -- cgit v1.2.3 From c7662795f519d2b6797e4b1ac7fa4a22afa26310 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Thu, 27 Jul 2006 23:18:35 +0000 Subject: * merge - This is my current patch queue. I think that all of these are safe to apply. This is just under half of the pending changes in chris/general (the easy half). --- lib/common/UnixUser.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/common/UnixUser.cpp') diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp index df2d0ddd..d0fd337b 100644 --- a/lib/common/UnixUser.cpp +++ b/lib/common/UnixUser.cpp @@ -75,6 +75,7 @@ UnixUser::UnixUser(uid_t UID, gid_t GID) // -------------------------------------------------------------------------- UnixUser::~UnixUser() { +#ifndef WIN32 if(mRevertOnDestruction) { // Revert to "real" user and group id of the process @@ -84,6 +85,7 @@ UnixUser::~UnixUser() THROW_EXCEPTION(CommonException, CouldNotRestoreProcessUser) } } +#endif } @@ -98,6 +100,7 @@ UnixUser::~UnixUser() // -------------------------------------------------------------------------- void UnixUser::ChangeProcessUser(bool Temporary) { +#ifndef WIN32 if(Temporary) { // Change temporarily (change effective only) @@ -119,6 +122,7 @@ void UnixUser::ChangeProcessUser(bool Temporary) THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser) } } +#endif } -- cgit v1.2.3 From 85d5ccab78c156a8113cfef02748facb91aa6393 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Thu, 31 Aug 2006 22:33:05 +0000 Subject: Revert to trunk --- lib/common/UnixUser.cpp | 4 ---- 1 file changed, 4 deletions(-) (limited to 'lib/common/UnixUser.cpp') diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp index d0fd337b..df2d0ddd 100644 --- a/lib/common/UnixUser.cpp +++ b/lib/common/UnixUser.cpp @@ -75,7 +75,6 @@ UnixUser::UnixUser(uid_t UID, gid_t GID) // -------------------------------------------------------------------------- UnixUser::~UnixUser() { -#ifndef WIN32 if(mRevertOnDestruction) { // Revert to "real" user and group id of the process @@ -85,7 +84,6 @@ UnixUser::~UnixUser() THROW_EXCEPTION(CommonException, CouldNotRestoreProcessUser) } } -#endif } @@ -100,7 +98,6 @@ UnixUser::~UnixUser() // -------------------------------------------------------------------------- void UnixUser::ChangeProcessUser(bool Temporary) { -#ifndef WIN32 if(Temporary) { // Change temporarily (change effective only) @@ -122,7 +119,6 @@ void UnixUser::ChangeProcessUser(bool Temporary) THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser) } } -#endif } -- cgit v1.2.3 From 186fbcc5fa57fa5899262d877f10be5cb4f87654 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Thu, 31 Aug 2006 22:35:51 +0000 Subject: Disable all calls to set*id() on Win32 (doesn't work) (refs #3) --- lib/common/UnixUser.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'lib/common/UnixUser.cpp') diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp index df2d0ddd..1ec9608d 100644 --- a/lib/common/UnixUser.cpp +++ b/lib/common/UnixUser.cpp @@ -78,8 +78,12 @@ UnixUser::~UnixUser() if(mRevertOnDestruction) { // Revert to "real" user and group id of the process + #ifdef WIN32 + if(0) + #else if(::setegid(::getgid()) != 0 || ::seteuid(::getuid()) != 0) + #endif { THROW_EXCEPTION(CommonException, CouldNotRestoreProcessUser) } @@ -101,8 +105,12 @@ void UnixUser::ChangeProcessUser(bool Temporary) if(Temporary) { // Change temporarily (change effective only) + #ifdef WIN32 + if(0) + #else if(::setegid(mGID) != 0 || ::seteuid(mUID) != 0) + #endif { THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser) } @@ -112,9 +120,13 @@ void UnixUser::ChangeProcessUser(bool Temporary) } else { - // Change perminantely (change all UIDs and GIDs) + // Change permanently (change all UIDs and GIDs) + #ifdef WIN32 + if(0) + #else if(::setgid(mGID) != 0 || ::setuid(mUID) != 0) + #endif { THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser) } -- cgit v1.2.3 From f6cbd5bfc97fb701685dc17b0cfcbbbdb0ff1bcd Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 10 Mar 2007 19:26:02 +0000 Subject: Remove #ifdefs, no longer required (refs #3, merges [1418]) --- lib/common/UnixUser.cpp | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) (limited to 'lib/common/UnixUser.cpp') diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp index 1ec9608d..f81b474c 100644 --- a/lib/common/UnixUser.cpp +++ b/lib/common/UnixUser.cpp @@ -78,12 +78,7 @@ UnixUser::~UnixUser() if(mRevertOnDestruction) { // Revert to "real" user and group id of the process - #ifdef WIN32 - if(0) - #else - if(::setegid(::getgid()) != 0 - || ::seteuid(::getuid()) != 0) - #endif + if(::setegid(::getgid()) != 0 || ::seteuid(::getuid()) != 0) { THROW_EXCEPTION(CommonException, CouldNotRestoreProcessUser) } @@ -105,12 +100,7 @@ void UnixUser::ChangeProcessUser(bool Temporary) if(Temporary) { // Change temporarily (change effective only) - #ifdef WIN32 - if(0) - #else - if(::setegid(mGID) != 0 - || ::seteuid(mUID) != 0) - #endif + if(::setegid(mGID) != 0 || ::seteuid(mUID) != 0) { THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser) } @@ -121,12 +111,7 @@ void UnixUser::ChangeProcessUser(bool Temporary) else { // Change permanently (change all UIDs and GIDs) - #ifdef WIN32 - if(0) - #else - if(::setgid(mGID) != 0 - || ::setuid(mUID) != 0) - #endif + if(::setgid(mGID) != 0 || ::setuid(mUID) != 0) { THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser) } -- cgit v1.2.3 From 46bb5c32a5273b4c7c936569a4da3ca1ecc05ec8 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 29 Jun 2012 22:18:36 +0000 Subject: Allow UnixUser to be created with a std::string for C++ style. --- lib/common/UnixUser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/common/UnixUser.cpp') diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp index f81b474c..32acacfa 100644 --- a/lib/common/UnixUser.cpp +++ b/lib/common/UnixUser.cpp @@ -31,13 +31,13 @@ // Created: 21/1/04 // // -------------------------------------------------------------------------- -UnixUser::UnixUser(const char *Username) +UnixUser::UnixUser(const std::string& Username) : mUID(0), mGID(0), mRevertOnDestruction(false) { // Get password info - struct passwd *pwd = ::getpwnam(Username); + struct passwd *pwd = ::getpwnam(Username.c_str()); if(pwd == 0) { THROW_EXCEPTION(CommonException, CouldNotLookUpUsername) -- cgit v1.2.3