Improve the process for GNU tools
[minix3.git] / external / bsd / kyua-cli / dist / utils / passwd.cpp
blobb884f7e7ec7cb091da4f1f59ad7d72b55a11a06e
1 // Copyright 2010 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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"
31 extern "C" {
32 #include <sys/types.h>
34 #include <pwd.h>
35 #include <unistd.h>
38 #include <stdexcept>
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;
48 namespace {
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.
60 ///
61 /// \param user The user to format.
62 ///
63 /// \return The user as a string.
64 static std::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.
75 ///
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_) :
81 name(name_),
82 uid(uid_),
83 gid(gid_)
88 /// Checks if the user has superpowers or not.
89 ///
90 /// \return True if the user is root, false otherwise.
91 bool
92 passwd_ns::user::is_root(void) const
94 return uid == 0;
98 /// Gets the current user.
99 ///
100 /// \return The current user.
101 passwd_ns::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));
107 return u;
108 } else {
109 const user u = find_user_by_uid(::getuid());
110 LD(F("Current user is: %s") % format_user(u));
111 return 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.
123 passwd_ns::user
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());
128 if (pw == NULL)
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);
133 } else {
134 for (std::vector< user >::const_iterator iter = mock_users.begin();
135 iter != mock_users.end(); iter++) {
136 if ((*iter).name == name)
137 return *iter;
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.
152 passwd_ns::user
153 passwd_ns::find_user_by_uid(const unsigned int uid)
155 if (mock_users.empty()) {
156 const struct ::passwd* pw = ::getpwuid(uid);
157 if (pw == NULL)
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);
162 } else {
163 for (std::vector< user >::const_iterator iter = mock_users.begin();
164 iter != mock_users.end(); iter++) {
165 if ((*iter).uid == uid)
166 return *iter;
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.
179 void
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.
189 void
190 passwd_ns::set_mock_users_for_testing(const std::vector< user >& users)
192 PRE(!users.empty());
193 mock_users = users;