1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/passwd.hpp"
32 #include <sys/types.h>
40 #include "utils/format/macros.hpp"
41 #include "utils/logging/macros.hpp"
42 #include "utils/optional.ipp"
43 #include "utils/sanity.hpp"
45 namespace passwd_ns
= utils::passwd
;
51 /// If defined, replaces the value returned by current_user().
52 static utils::optional
< passwd_ns::user
> fake_current_user
;
55 /// If not empty, defines the current set of mock users.
56 static std::vector
< passwd_ns::user
> mock_users
;
59 /// Formats a user for logging purposes.
61 /// \param user The user to format.
63 /// \return The user as a string.
65 format_user(const passwd_ns::user
& user
)
67 return F("name=%s, uid=%s, gid=%s") % user
.name
% user
.uid
% user
.gid
;
71 } // anonymous namespace
74 /// Constructs a new user.
76 /// \param name_ The name of the user.
77 /// \param uid_ The user identifier.
78 /// \param gid_ The login group identifier.
79 passwd_ns::user::user(const std::string
& name_
, const unsigned int uid_
,
80 const unsigned int gid_
) :
88 /// Checks if the user has superpowers or not.
90 /// \return True if the user is root, false otherwise.
92 passwd_ns::user::is_root(void) const
98 /// Gets the current user.
100 /// \return The current user.
102 passwd_ns::current_user(void)
104 if (fake_current_user
) {
105 const user u
= fake_current_user
.get();
106 LD(F("Current user is fake: %s") % format_user(u
));
109 const user u
= find_user_by_uid(::getuid());
110 LD(F("Current user is: %s") % format_user(u
));
116 /// Gets information about a user by its name.
118 /// \param name The name of the user to query.
120 /// \return The information about the user.
122 /// \throw std::runtime_error If the user does not exist.
124 passwd_ns::find_user_by_name(const std::string
& name
)
126 if (mock_users
.empty()) {
127 const struct ::passwd
* pw
= ::getpwnam(name
.c_str());
129 throw std::runtime_error(F("Failed to get information about the "
130 "user '%s'") % name
);
131 INV(pw
->pw_name
== name
);
132 return user(pw
->pw_name
, pw
->pw_uid
, pw
->pw_gid
);
134 for (std::vector
< user
>::const_iterator iter
= mock_users
.begin();
135 iter
!= mock_users
.end(); iter
++) {
136 if ((*iter
).name
== name
)
139 throw std::runtime_error(F("Failed to get information about the "
140 "user '%s'") % name
);
145 /// Gets information about a user by its identifier.
147 /// \param uid The identifier of the user to query.
149 /// \return The information about the user.
151 /// \throw std::runtime_error If the user does not exist.
153 passwd_ns::find_user_by_uid(const unsigned int uid
)
155 if (mock_users
.empty()) {
156 const struct ::passwd
* pw
= ::getpwuid(uid
);
158 throw std::runtime_error(F("Failed to get information about the "
159 "user with UID %s") % uid
);
160 INV(pw
->pw_uid
== uid
);
161 return user(pw
->pw_name
, pw
->pw_uid
, pw
->pw_gid
);
163 for (std::vector
< user
>::const_iterator iter
= mock_users
.begin();
164 iter
!= mock_users
.end(); iter
++) {
165 if ((*iter
).uid
== uid
)
168 throw std::runtime_error(F("Failed to get information about the "
169 "user with UID %s") % uid
);
174 /// Overrides the current user for testing purposes.
176 /// This DOES NOT change the current privileges!
178 /// \param new_current_user The new current user.
180 passwd_ns::set_current_user_for_testing(const user
& new_current_user
)
182 fake_current_user
= new_current_user
;
186 /// Overrides the current set of users for testing purposes.
188 /// \param users The new users set. Cannot be empty.
190 passwd_ns::set_mock_users_for_testing(const std::vector
< user
>& users
)