xref: /freebsd/contrib/kyua/utils/passwd.cpp (revision dc318a4ffabcbfa23bb56a33403aad36e6de30af)
1 // Copyright 2010 The Kyua Authors.
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.
16 //
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.
28 
29 #include "utils/passwd.hpp"
30 
31 extern "C" {
32 #include <sys/types.h>
33 
34 #include <pwd.h>
35 #include <unistd.h>
36 }
37 
38 #include <stdexcept>
39 
40 #include "utils/format/macros.hpp"
41 #include "utils/logging/macros.hpp"
42 #include "utils/optional.ipp"
43 #include "utils/sanity.hpp"
44 
45 namespace passwd_ns = utils::passwd;
46 
47 
48 namespace {
49 
50 
51 /// If defined, replaces the value returned by current_user().
52 static utils::optional< passwd_ns::user > fake_current_user;
53 
54 
55 /// If not empty, defines the current set of mock users.
56 static std::vector< passwd_ns::user > mock_users;
57 
58 
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)
66 {
67     return F("name=%s, uid=%s, gid=%s") % user.name % user.uid % user.gid;
68 }
69 
70 
71 }  // anonymous namespace
72 
73 
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_)
84 {
85 }
86 
87 
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
93 {
94     return uid == 0;
95 }
96 
97 
98 /// Gets the current user.
99 ///
100 /// \return The current user.
101 passwd_ns::user
102 passwd_ns::current_user(void)
103 {
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;
112     }
113 }
114 
115 
116 /// Gets information about a user by its name.
117 ///
118 /// \param name The name of the user to query.
119 ///
120 /// \return The information about the user.
121 ///
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)
125 {
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;
138         }
139         throw std::runtime_error(F("Failed to get information about the "
140                                    "user '%s'") % name);
141     }
142 }
143 
144 
145 /// Gets information about a user by its identifier.
146 ///
147 /// \param uid The identifier of the user to query.
148 ///
149 /// \return The information about the user.
150 ///
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)
154 {
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;
167         }
168         throw std::runtime_error(F("Failed to get information about the "
169                                    "user with UID %s") % uid);
170     }
171 }
172 
173 
174 /// Overrides the current user for testing purposes.
175 ///
176 /// This DOES NOT change the current privileges!
177 ///
178 /// \param new_current_user The new current user.
179 void
180 passwd_ns::set_current_user_for_testing(const user& new_current_user)
181 {
182     fake_current_user = new_current_user;
183 }
184 
185 
186 /// Overrides the current set of users for testing purposes.
187 ///
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)
191 {
192     PRE(!users.empty());
193     mock_users = users;
194 }
195