xref: /minix3/external/bsd/kyua-cli/dist/utils/passwd.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "utils/passwd.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <sys/types.h>
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc #include <pwd.h>
35*11be35a1SLionel Sambuc #include <unistd.h>
36*11be35a1SLionel Sambuc }
37*11be35a1SLionel Sambuc 
38*11be35a1SLionel Sambuc #include <stdexcept>
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
41*11be35a1SLionel Sambuc #include "utils/logging/macros.hpp"
42*11be35a1SLionel Sambuc #include "utils/optional.ipp"
43*11be35a1SLionel Sambuc #include "utils/sanity.hpp"
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc namespace passwd_ns = utils::passwd;
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc namespace {
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc /// If defined, replaces the value returned by current_user().
52*11be35a1SLionel Sambuc static utils::optional< passwd_ns::user > fake_current_user;
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc /// If not empty, defines the current set of mock users.
56*11be35a1SLionel Sambuc static std::vector< passwd_ns::user > mock_users;
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc /// Formats a user for logging purposes.
60*11be35a1SLionel Sambuc ///
61*11be35a1SLionel Sambuc /// \param user The user to format.
62*11be35a1SLionel Sambuc ///
63*11be35a1SLionel Sambuc /// \return The user as a string.
64*11be35a1SLionel Sambuc static std::string
format_user(const passwd_ns::user & user)65*11be35a1SLionel Sambuc format_user(const passwd_ns::user& user)
66*11be35a1SLionel Sambuc {
67*11be35a1SLionel Sambuc     return F("name=%s, uid=%s, gid=%s") % user.name % user.uid % user.gid;
68*11be35a1SLionel Sambuc }
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc 
71*11be35a1SLionel Sambuc }  // anonymous namespace
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc /// Constructs a new user.
75*11be35a1SLionel Sambuc ///
76*11be35a1SLionel Sambuc /// \param name_ The name of the user.
77*11be35a1SLionel Sambuc /// \param uid_ The user identifier.
78*11be35a1SLionel Sambuc /// \param gid_ The login group identifier.
user(const std::string & name_,const unsigned int uid_,const unsigned int gid_)79*11be35a1SLionel Sambuc passwd_ns::user::user(const std::string& name_, const unsigned int uid_,
80*11be35a1SLionel Sambuc                       const unsigned int gid_) :
81*11be35a1SLionel Sambuc     name(name_),
82*11be35a1SLionel Sambuc     uid(uid_),
83*11be35a1SLionel Sambuc     gid(gid_)
84*11be35a1SLionel Sambuc {
85*11be35a1SLionel Sambuc }
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc /// Checks if the user has superpowers or not.
89*11be35a1SLionel Sambuc ///
90*11be35a1SLionel Sambuc /// \return True if the user is root, false otherwise.
91*11be35a1SLionel Sambuc bool
is_root(void) const92*11be35a1SLionel Sambuc passwd_ns::user::is_root(void) const
93*11be35a1SLionel Sambuc {
94*11be35a1SLionel Sambuc     return uid == 0;
95*11be35a1SLionel Sambuc }
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc 
98*11be35a1SLionel Sambuc /// Gets the current user.
99*11be35a1SLionel Sambuc ///
100*11be35a1SLionel Sambuc /// \return The current user.
101*11be35a1SLionel Sambuc passwd_ns::user
current_user(void)102*11be35a1SLionel Sambuc passwd_ns::current_user(void)
103*11be35a1SLionel Sambuc {
104*11be35a1SLionel Sambuc     if (fake_current_user) {
105*11be35a1SLionel Sambuc         const user u = fake_current_user.get();
106*11be35a1SLionel Sambuc         LD(F("Current user is fake: %s") % format_user(u));
107*11be35a1SLionel Sambuc         return u;
108*11be35a1SLionel Sambuc     } else {
109*11be35a1SLionel Sambuc         const user u = find_user_by_uid(::getuid());
110*11be35a1SLionel Sambuc         LD(F("Current user is: %s") % format_user(u));
111*11be35a1SLionel Sambuc         return u;
112*11be35a1SLionel Sambuc     }
113*11be35a1SLionel Sambuc }
114*11be35a1SLionel Sambuc 
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc /// Gets information about a user by its name.
117*11be35a1SLionel Sambuc ///
118*11be35a1SLionel Sambuc /// \param name The name of the user to query.
119*11be35a1SLionel Sambuc ///
120*11be35a1SLionel Sambuc /// \return The information about the user.
121*11be35a1SLionel Sambuc ///
122*11be35a1SLionel Sambuc /// \throw std::runtime_error If the user does not exist.
123*11be35a1SLionel Sambuc passwd_ns::user
find_user_by_name(const std::string & name)124*11be35a1SLionel Sambuc passwd_ns::find_user_by_name(const std::string& name)
125*11be35a1SLionel Sambuc {
126*11be35a1SLionel Sambuc     if (mock_users.empty()) {
127*11be35a1SLionel Sambuc         const struct ::passwd* pw = ::getpwnam(name.c_str());
128*11be35a1SLionel Sambuc         if (pw == NULL)
129*11be35a1SLionel Sambuc             throw std::runtime_error(F("Failed to get information about the "
130*11be35a1SLionel Sambuc                                        "user '%s'") % name);
131*11be35a1SLionel Sambuc         INV(pw->pw_name == name);
132*11be35a1SLionel Sambuc         return user(pw->pw_name, pw->pw_uid, pw->pw_gid);
133*11be35a1SLionel Sambuc     } else {
134*11be35a1SLionel Sambuc         for (std::vector< user >::const_iterator iter = mock_users.begin();
135*11be35a1SLionel Sambuc              iter != mock_users.end(); iter++) {
136*11be35a1SLionel Sambuc             if ((*iter).name == name)
137*11be35a1SLionel Sambuc                 return *iter;
138*11be35a1SLionel Sambuc         }
139*11be35a1SLionel Sambuc         throw std::runtime_error(F("Failed to get information about the "
140*11be35a1SLionel Sambuc                                    "user '%s'") % name);
141*11be35a1SLionel Sambuc     }
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc /// Gets information about a user by its identifier.
146*11be35a1SLionel Sambuc ///
147*11be35a1SLionel Sambuc /// \param uid The identifier of the user to query.
148*11be35a1SLionel Sambuc ///
149*11be35a1SLionel Sambuc /// \return The information about the user.
150*11be35a1SLionel Sambuc ///
151*11be35a1SLionel Sambuc /// \throw std::runtime_error If the user does not exist.
152*11be35a1SLionel Sambuc passwd_ns::user
find_user_by_uid(const unsigned int uid)153*11be35a1SLionel Sambuc passwd_ns::find_user_by_uid(const unsigned int uid)
154*11be35a1SLionel Sambuc {
155*11be35a1SLionel Sambuc     if (mock_users.empty()) {
156*11be35a1SLionel Sambuc         const struct ::passwd* pw = ::getpwuid(uid);
157*11be35a1SLionel Sambuc         if (pw == NULL)
158*11be35a1SLionel Sambuc             throw std::runtime_error(F("Failed to get information about the "
159*11be35a1SLionel Sambuc                                        "user with UID %s") % uid);
160*11be35a1SLionel Sambuc         INV(pw->pw_uid == uid);
161*11be35a1SLionel Sambuc         return user(pw->pw_name, pw->pw_uid, pw->pw_gid);
162*11be35a1SLionel Sambuc     } else {
163*11be35a1SLionel Sambuc         for (std::vector< user >::const_iterator iter = mock_users.begin();
164*11be35a1SLionel Sambuc              iter != mock_users.end(); iter++) {
165*11be35a1SLionel Sambuc             if ((*iter).uid == uid)
166*11be35a1SLionel Sambuc                 return *iter;
167*11be35a1SLionel Sambuc         }
168*11be35a1SLionel Sambuc         throw std::runtime_error(F("Failed to get information about the "
169*11be35a1SLionel Sambuc                                    "user with UID %s") % uid);
170*11be35a1SLionel Sambuc     }
171*11be35a1SLionel Sambuc }
172*11be35a1SLionel Sambuc 
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc /// Overrides the current user for testing purposes.
175*11be35a1SLionel Sambuc ///
176*11be35a1SLionel Sambuc /// This DOES NOT change the current privileges!
177*11be35a1SLionel Sambuc ///
178*11be35a1SLionel Sambuc /// \param new_current_user The new current user.
179*11be35a1SLionel Sambuc void
set_current_user_for_testing(const user & new_current_user)180*11be35a1SLionel Sambuc passwd_ns::set_current_user_for_testing(const user& new_current_user)
181*11be35a1SLionel Sambuc {
182*11be35a1SLionel Sambuc     fake_current_user = new_current_user;
183*11be35a1SLionel Sambuc }
184*11be35a1SLionel Sambuc 
185*11be35a1SLionel Sambuc 
186*11be35a1SLionel Sambuc /// Overrides the current set of users for testing purposes.
187*11be35a1SLionel Sambuc ///
188*11be35a1SLionel Sambuc /// \param users The new users set.  Cannot be empty.
189*11be35a1SLionel Sambuc void
set_mock_users_for_testing(const std::vector<user> & users)190*11be35a1SLionel Sambuc passwd_ns::set_mock_users_for_testing(const std::vector< user >& users)
191*11be35a1SLionel Sambuc {
192*11be35a1SLionel Sambuc     PRE(!users.empty());
193*11be35a1SLionel Sambuc     mock_users = users;
194*11be35a1SLionel Sambuc }
195