1 // distribution boxbackup-0.11_trunk_2979 (svn version: 2979)
2 // Box Backup, http://www.boxbackup.org/
3 //
4 // Copyright (c) 2003-2010, Ben Summers and contributors.
5 // All rights reserved.
6 //
7 // Note that this project uses mixed licensing. Any file with this license
8 // attached, or where the code LICENSE-DUAL appears on the first line, falls
9 // under this license. See the file COPYING.txt for more information.
10 //
11 // This file is dual licensed. You may use and distribute it providing that you
12 // comply EITHER with the terms of the BSD license, OR the GPL license. It is
13 // not necessary to comply with both licenses, only one.
14 //
15 // The BSD license option follows:
16 //
17 // Redistribution and use in source and binary forms, with or without
18 // modification, are permitted provided that the following conditions are met:
19 //
20 // 1. Redistributions of source code must retain the above copyright
21 //    notice, this list of conditions and the following disclaimer.
22 //
23 // 2. Redistributions in binary form must reproduce the above copyright
24 //    notice, this list of conditions and the following disclaimer in the
25 //    documentation and/or other materials provided with the distribution.
26 //
27 // 3. Neither the name of the Box Backup nor the names of its contributors may
28 //    be used to endorse or promote products derived from this software without
29 //    specific prior written permission.
30 //
31 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
35 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 //
42 // [http://en.wikipedia.org/wiki/BSD_licenses#3-clause_license_.28.22New_BSD_License.22.29]
43 //
44 // The GPL license option follows:
45 //
46 // This program is free software; you can redistribute it and/or
47 // modify it under the terms of the GNU General Public License
48 // as published by the Free Software Foundation; either version 2
49 // of the License, or (at your option) any later version.
50 //
51 // This program is distributed in the hope that it will be useful,
52 // but WITHOUT ANY WARRANTY; without even the implied warranty of
53 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54 // GNU General Public License for more details.
55 //
56 // You should have received a copy of the GNU General Public License
57 // along with this program; if not, write to the Free Software
58 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
59 //
60 // [http://www.gnu.org/licenses/old-licenses/gpl-2.0.html#SEC4]
61 // --------------------------------------------------------------------------
62 //
63 // File
64 //		Name:    UnixUser.cpp
65 //		Purpose: Interface for managing the UNIX user of the current process
66 //		Created: 21/1/04
67 //
68 // --------------------------------------------------------------------------
69 
70 #include "Box.h"
71 
72 #ifdef HAVE_PWD_H
73 	#include <pwd.h>
74 #endif
75 
76 #ifdef HAVE_UNISTD_H
77 	#include <unistd.h>
78 #endif
79 
80 #include "UnixUser.h"
81 #include "CommonException.h"
82 
83 #include "MemLeakFindOn.h"
84 
85 
86 // --------------------------------------------------------------------------
87 //
88 // Function
89 //		Name:    UnixUser::UnixUser(const char *)
90 //		Purpose: Constructor, initialises to info of given username
91 //		Created: 21/1/04
92 //
93 // --------------------------------------------------------------------------
UnixUser(const char * Username)94 UnixUser::UnixUser(const char *Username)
95 	: mUID(0),
96 	  mGID(0),
97 	  mRevertOnDestruction(false)
98 {
99 	// Get password info
100 	struct passwd *pwd = ::getpwnam(Username);
101 	if(pwd == 0)
102 	{
103 		THROW_EXCEPTION(CommonException, CouldNotLookUpUsername)
104 	}
105 
106 	// Store UID and GID
107 	mUID = pwd->pw_uid;
108 	mGID = pwd->pw_gid;
109 }
110 
111 
112 // --------------------------------------------------------------------------
113 //
114 // Function
115 //		Name:    UnixUser::UnixUser(uid_t, gid_t)
116 //		Purpose: Construct from given UNIX user ID and group ID
117 //		Created: 15/3/04
118 //
119 // --------------------------------------------------------------------------
UnixUser(uid_t UID,gid_t GID)120 UnixUser::UnixUser(uid_t UID, gid_t GID)
121 	: mUID(UID),
122 	  mGID(GID),
123 	  mRevertOnDestruction(false)
124 {
125 }
126 
127 
128 // --------------------------------------------------------------------------
129 //
130 // Function
131 //		Name:    UnixUser::~UnixUser()
132 //		Purpose: Destructor -- reverts to previous user if the change wasn't perminant
133 //		Created: 21/1/04
134 //
135 // --------------------------------------------------------------------------
~UnixUser()136 UnixUser::~UnixUser()
137 {
138 	if(mRevertOnDestruction)
139 	{
140 		// Revert to "real" user and group id of the process
141 		if(::setegid(::getgid()) != 0 || ::seteuid(::getuid()) != 0)
142 		{
143 			THROW_EXCEPTION(CommonException, CouldNotRestoreProcessUser)
144 		}
145 	}
146 }
147 
148 
149 // --------------------------------------------------------------------------
150 //
151 // Function
152 //		Name:    UnixUser::ChangeProcessUser(bool)
153 //		Purpose: Change the process user and group ID to the user. If Temporary == true
154 //				 the process username will be changed back when the object is destructed.
155 //		Created: 21/1/04
156 //
157 // --------------------------------------------------------------------------
ChangeProcessUser(bool Temporary)158 void UnixUser::ChangeProcessUser(bool Temporary)
159 {
160 	if(Temporary)
161 	{
162 		// Change temporarily (change effective only)
163 		if(::setegid(mGID) != 0 || ::seteuid(mUID) != 0)
164 		{
165 			THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser)
166 		}
167 
168 		// Mark for change on destruction
169 		mRevertOnDestruction = true;
170 	}
171 	else
172 	{
173 		// Change permanently (change all UIDs and GIDs)
174 		if(::setgid(mGID) != 0 || ::setuid(mUID) != 0)
175 		{
176 			THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser)
177 		}
178 	}
179 }
180 
181 
182 
183 
184