source: box/trunk/lib/common/UnixUser.cpp @ 1419

Revision 1419, 2.9 KB checked in by chris, 5 years ago (diff)

Remove #ifdefs, no longer required (refs #3, merges [1418])

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    UnixUser.cpp
5//              Purpose: Interface for managing the UNIX user of the current process
6//              Created: 21/1/04
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11
12#ifdef HAVE_PWD_H
13        #include <pwd.h>
14#endif
15
16#ifdef HAVE_UNISTD_H
17        #include <unistd.h>
18#endif
19
20#include "UnixUser.h"
21#include "CommonException.h"
22
23#include "MemLeakFindOn.h"
24
25
26// --------------------------------------------------------------------------
27//
28// Function
29//              Name:    UnixUser::UnixUser(const char *)
30//              Purpose: Constructor, initialises to info of given username
31//              Created: 21/1/04
32//
33// --------------------------------------------------------------------------
34UnixUser::UnixUser(const char *Username)
35        : mUID(0),
36          mGID(0),
37          mRevertOnDestruction(false)
38{
39        // Get password info
40        struct passwd *pwd = ::getpwnam(Username);
41        if(pwd == 0)
42        {
43                THROW_EXCEPTION(CommonException, CouldNotLookUpUsername)
44        }
45       
46        // Store UID and GID
47        mUID = pwd->pw_uid;
48        mGID = pwd->pw_gid;
49}
50
51
52// --------------------------------------------------------------------------
53//
54// Function
55//              Name:    UnixUser::UnixUser(uid_t, gid_t)
56//              Purpose: Construct from given UNIX user ID and group ID
57//              Created: 15/3/04
58//
59// --------------------------------------------------------------------------
60UnixUser::UnixUser(uid_t UID, gid_t GID)
61        : mUID(UID),
62          mGID(GID),
63          mRevertOnDestruction(false)
64{
65}
66
67
68// --------------------------------------------------------------------------
69//
70// Function
71//              Name:    UnixUser::~UnixUser()
72//              Purpose: Destructor -- reverts to previous user if the change wasn't perminant
73//              Created: 21/1/04
74//
75// --------------------------------------------------------------------------
76UnixUser::~UnixUser()
77{
78        if(mRevertOnDestruction)
79        {
80                // Revert to "real" user and group id of the process
81                if(::setegid(::getgid()) != 0 || ::seteuid(::getuid()) != 0)
82                {
83                        THROW_EXCEPTION(CommonException, CouldNotRestoreProcessUser)
84                }
85        }
86}
87
88
89// --------------------------------------------------------------------------
90//
91// Function
92//              Name:    UnixUser::ChangeProcessUser(bool)
93//              Purpose: Change the process user and group ID to the user. If Temporary == true
94//                               the process username will be changed back when the object is destructed.
95//              Created: 21/1/04
96//
97// --------------------------------------------------------------------------
98void UnixUser::ChangeProcessUser(bool Temporary)
99{
100        if(Temporary)
101        {
102                // Change temporarily (change effective only)
103                if(::setegid(mGID) != 0 || ::seteuid(mUID) != 0)
104                {
105                        THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser)
106                }
107               
108                // Mark for change on destruction
109                mRevertOnDestruction = true;
110        }
111        else
112        {
113                // Change permanently (change all UIDs and GIDs)
114                if(::setgid(mGID) != 0 || ::setuid(mUID) != 0)
115                {
116                        THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser)
117                }
118        }
119}
120
121
122
123
Note: See TracBrowser for help on using the repository browser.