-
Notifications
You must be signed in to change notification settings - Fork 346
/
user_create_test.py
360 lines (280 loc) · 14.8 KB
/
user_create_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# vim:set fileencoding=utf-8
#
# Copyright (C) 2015 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): David Shea <dshea@redhat.com>
#
# Ignore any interruptible calls
# pylint: disable=interruptible-system-call
from pyanaconda import users
import unittest
import tempfile
import shutil
import os
import crypt
import platform
import glob
@unittest.skipIf(os.geteuid() != 0, "user creation must be run as root")
class UserCreateTest(unittest.TestCase):
def setUp(self):
self.users = users.Users()
# Create a temporary directory with empty passwd and group files
self.tmpdir = tempfile.mkdtemp()
os.mkdir(self.tmpdir + "/etc")
open(self.tmpdir + "/etc/passwd", "w").close()
open(self.tmpdir + "/etc/group", "w").close()
open(self.tmpdir + "/etc/shadow", "w").close()
open(self.tmpdir + "/etc/gshadow", "w").close()
# Copy over enough of libnss for UID and GID lookups to work
with open(self.tmpdir + "/etc/nsswitch.conf", "w") as f:
f.write("passwd: files\n")
f.write("shadow: files\n")
f.write("group: files\n")
f.write("initgroups: files\n")
if platform.architecture()[0].startswith("64"):
libdir = "/lib64"
else:
libdir = "/lib"
os.mkdir(self.tmpdir + libdir)
for lib in glob.glob(libdir + "/libnss_files*"):
shutil.copy(lib, self.tmpdir + lib)
def tearDown(self):
shutil.rmtree(self.tmpdir)
def _readFields(self, filename, key):
"""Look for a line in a password or group file where the first field
matches key, and return the record as a list of fields.
"""
with open(self.tmpdir + filename) as f:
for line in f:
fields = line.strip().split(':')
if fields[0] == key:
return fields
return None
def create_group_test(self):
"""Create a group."""
self.users.createGroup("test_group", root=self.tmpdir)
fields = self._readFields("/etc/group", "test_group")
self.assertIsNotNone(fields)
self.assertEqual(fields[0], "test_group")
fields = self._readFields("/etc/gshadow", "test_group")
self.assertIsNotNone(fields)
self.assertEqual(fields[0], "test_group")
def create_group_gid_test(self):
"""Create a group with a specific GID."""
self.users.createGroup("test_group", gid=47, root=self.tmpdir)
fields = self._readFields("/etc/group", "test_group")
self.assertIsNotNone(fields)
self.assertEqual(fields[0], "test_group")
self.assertEqual(fields[2], "47")
def create_group_exists_test(self):
"""Create a group that already exists."""
with open(self.tmpdir + "/etc/group", "w") as f:
f.write("test_group:x:47:\n")
self.assertRaises(ValueError, self.users.createGroup, "test_group", root=self.tmpdir)
def create_group_gid_exists_test(self):
"""Create a group with a GID that already exists."""
with open(self.tmpdir + "/etc/group", "w") as f:
f.write("gid_used:x:47:\n")
self.assertRaises(ValueError, self.users.createGroup, "test_group", gid=47, root=self.tmpdir)
def create_user_test(self):
"""Create a user."""
self.users.createUser("test_user", root=self.tmpdir)
pwd_fields = self._readFields("/etc/passwd", "test_user")
self.assertIsNotNone(pwd_fields)
self.assertEqual(pwd_fields[0], "test_user")
# Check that the fields got the right default values
# UID + GID set to some sort of int
self.assertTrue(isinstance(int(pwd_fields[2]), int))
self.assertTrue(isinstance(int(pwd_fields[3]), int))
# home is /home/username
self.assertEqual(pwd_fields[5], "/home/test_user")
# shell set to something
self.assertTrue(pwd_fields[6])
shadow_fields = self._readFields("/etc/shadow", "test_user")
self.assertIsNotNone(shadow_fields)
self.assertEqual(shadow_fields[0], "test_user")
# Ensure the password is locked
self.assertTrue(shadow_fields[1].startswith("!"))
# Ensure the date of last password change is empty
self.assertEqual(shadow_fields[2], "")
# Check that the user group was created
grp_fields = self._readFields("/etc/group", "test_user")
self.assertIsNotNone(grp_fields)
self.assertEqual(grp_fields[0], "test_user")
# Check that user group's GID matches the user's GID
self.assertEqual(grp_fields[2], pwd_fields[3])
gshadow_fields = self._readFields("/etc/gshadow", "test_user")
self.assertIsNotNone(gshadow_fields)
self.assertEqual(gshadow_fields[0], "test_user")
def create_user_text_options_test(self):
"""Create a user with the text fields set."""
self.users.createUser("test_user", gecos="Test User", homedir="/home/users/testuser", shell="/bin/test", root=self.tmpdir)
pwd_fields = self._readFields("/etc/passwd", "test_user")
self.assertIsNotNone(pwd_fields)
self.assertEqual(pwd_fields[0], "test_user")
self.assertEqual(pwd_fields[4], "Test User")
self.assertEqual(pwd_fields[5], "/home/users/testuser")
self.assertEqual(pwd_fields[6], "/bin/test")
# Check that the home directory was created
self.assertTrue(os.path.isdir(self.tmpdir + "/home/users/testuser"))
def create_user_groups_test(self):
"""Create a user with a list of groups."""
# Create one of the groups
self.users.createGroup("test3", root=self.tmpdir)
# Create a user and add it three groups, two of which do not exist,
# and one which specifies a GID.
self.users.createUser("test_user", groups=["test1", "test2(5001)", "test3"], root=self.tmpdir)
grp_fields1 = self._readFields("/etc/group", "test1")
self.assertEqual(grp_fields1[3], "test_user")
grp_fields2 = self._readFields("/etc/group", "test2")
self.assertEqual(grp_fields2[3], "test_user")
self.assertEqual(grp_fields2[2], "5001")
grp_fields3 = self._readFields("/etc/group", "test3")
self.assertEqual(grp_fields3[3], "test_user")
def create_user_groups_gid_conflict_test(self):
"""Create a user with a bad list of groups."""
# Create one of the groups
self.users.createGroup("test3", gid=5000, root=self.tmpdir)
# Add test3 to the group list with a different GID.
self.assertRaises(ValueError, self.users.createUser,
"test_user", groups=["test3(5002)"], root=self.tmpdir)
def create_user_password_test(self):
"""Create a user with a password."""
self.users.createUser("test_user1", password="password", root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "test_user1")
self.assertIsNotNone(shadow_fields)
# Make sure the password works
self.assertEqual(crypt.crypt("password", shadow_fields[1]), shadow_fields[1])
# Set the encrypted password for another user with isCrypted
cryptpw = shadow_fields[1]
self.users.createUser("test_user2", password=cryptpw, isCrypted=True, root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "test_user2")
self.assertIsNotNone(shadow_fields)
self.assertEqual(cryptpw, shadow_fields[1])
# Set an empty password
self.users.createUser("test_user3", password="", root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "test_user3")
self.assertIsNotNone(shadow_fields)
self.assertEqual("", shadow_fields[1])
def create_user_lock_test(self):
"""Create a locked user account."""
# Create an empty, locked password
self.users.createUser("test_user1", lock=True, password="", root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "test_user1")
self.assertIsNotNone(shadow_fields)
self.assertEqual("!", shadow_fields[1])
# Create a locked password and ensure it can be unlocked (by removing the ! at the front)
self.users.createUser("test_user2", lock=True, password="password", root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "test_user2")
self.assertIsNotNone(shadow_fields)
self.assertTrue(shadow_fields[1].startswith("!"))
self.assertEqual(crypt.crypt("password", shadow_fields[1][1:]), shadow_fields[1][1:])
def create_user_uid_test(self):
"""Create a user with a specific UID."""
self.users.createUser("test_user", uid=1047, root=self.tmpdir)
pwd_fields = self._readFields("/etc/passwd", "test_user")
self.assertIsNotNone(pwd_fields)
self.assertEqual(pwd_fields[2], "1047")
def create_user_gid_test(self):
"""Create a user with a specific GID."""
self.users.createUser("test_user", gid=1047, root=self.tmpdir)
pwd_fields = self._readFields("/etc/passwd", "test_user")
self.assertIsNotNone(pwd_fields)
self.assertEqual(pwd_fields[3], "1047")
grp_fields = self._readFields("/etc/group", "test_user")
self.assertIsNotNone(grp_fields)
self.assertEqual(grp_fields[2], "1047")
def create_user_algo_test(self):
"""Create a user with a specific password algorithm."""
self.users.createUser("test_user1", password="password", algo="md5", root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "test_user1")
self.assertIsNotNone(shadow_fields)
self.assertTrue(shadow_fields[1].startswith("$1$"))
self.users.createUser("test_user2", password="password", algo="sha512", root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "test_user2")
self.assertIsNotNone(shadow_fields)
self.assertTrue(shadow_fields[1].startswith("$6$"))
def create_user_exists_test(self):
"""Create a user that already exists."""
with open(self.tmpdir + "/etc/passwd", "w") as f:
f.write("test_user:x:1000:1000::/:/bin/sh\n")
self.assertRaises(ValueError, self.users.createUser, "test_user", root=self.tmpdir)
def create_user_uid_exists_test(self):
"""Create a user with a UID that already exists."""
with open(self.tmpdir + "/etc/passwd", "w") as f:
f.write("conflict:x:1000:1000::/:/bin/sh\n")
self.assertRaises(ValueError, self.users.createUser, "test_user", uid=1000, root=self.tmpdir)
def create_user_gid_exists_test(self):
"""Create a user with a GID of an existing group."""
self.users.createGroup("test_group", gid=5000, root=self.tmpdir)
self.users.createUser("test_user", gid=5000, root=self.tmpdir)
passwd_fields = self._readFields("/etc/passwd", "test_user")
self.assertIsNotNone(passwd_fields)
self.assertEqual(passwd_fields[3], "5000")
def set_user_ssh_key_test(self):
keydata = "THIS IS TOTALLY A SSH KEY"
self.users.createUser("test_user", homedir="/home/test_user", root=self.tmpdir)
self.users.setUserSshKey("test_user", keydata, root=self.tmpdir)
keyfile = self.tmpdir + "/home/test_user/.ssh/authorized_keys"
self.assertTrue(os.path.isfile(keyfile))
with open(keyfile) as f:
output_keydata = f.read()
self.assertEqual(keydata, output_keydata.strip())
def set_root_password_test(self):
password = "password1"
# Initialize a root user with an empty password, like the setup package would have
with open(self.tmpdir + "/etc/passwd", "w") as f:
f.write("root:x:0:0:root:/root:/bin/bash\n")
with open(self.tmpdir + "/etc/shadow", "w") as f:
f.write("root:*:16489:0:99999:7:::\n")
self.users.setRootPassword(password, root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "root")
self.assertEqual(crypt.crypt(password, shadow_fields[1]), shadow_fields[1])
# Try a different password with isLocked=True
password = "password2"
self.users.setRootPassword(password, isLocked=True, root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "root")
self.assertTrue(shadow_fields[1].startswith("!"))
self.assertEqual(crypt.crypt(password, shadow_fields[1][1:]), shadow_fields[1][1:])
# Try an encrypted password
password = "$1$asdf$password"
self.users.setRootPassword(password, isCrypted=True, root=self.tmpdir)
shadow_fields = self._readFields("/etc/shadow", "root")
self.assertEqual(password, shadow_fields[1])
def create_user_reuse_home_test(self):
# Create a user, reusing an old home directory
os.makedirs(self.tmpdir + "/home/test_user")
os.chown(self.tmpdir + "/home/test_user", 500, 500)
self.users.createUser("test_user", homedir="/home/test_user", uid=1000, gid=1000, root=self.tmpdir)
passwd_fields = self._readFields("/etc/passwd", "test_user")
self.assertIsNotNone(passwd_fields)
self.assertEqual(passwd_fields[2], "1000")
self.assertEqual(passwd_fields[3], "1000")
stat_fields = os.stat(self.tmpdir + "/home/test_user")
self.assertEqual(stat_fields.st_uid, 1000)
self.assertEqual(stat_fields.st_gid, 1000)
def create_user_gid_in_group_list_test(self):
"""Create a user with a GID equal to that of one of the requested groups"""
self.users.createUser("test_user", gid=1047, groups=["test_group(1047)"], root=self.tmpdir)
# Ensure that the user's GID is equal to the GID requested
pwd_fields = self._readFields("/etc/passwd", "test_user")
self.assertIsNotNone(pwd_fields)
self.assertEqual(pwd_fields[3], "1047")
# and that the requested group has the right GID
grp_fields = self._readFields("/etc/group", "test_group")
self.assertIsNotNone(grp_fields)
self.assertEqual(grp_fields[2], "1047")