]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/pw/tests/pw_useradd_test.sh
MFC r322678: pw useradd: Validate the user name before creating the entry
[FreeBSD/stable/10.git] / usr.sbin / pw / tests / pw_useradd_test.sh
1 # $FreeBSD$
2
3 # Import helper functions
4 . $(atf_get_srcdir)/helper_functions.shin
5
6 # Test add user
7 atf_test_case user_add
8 user_add_body() {
9         populate_etc_skel
10
11         atf_check -s exit:0 ${PW} useradd test
12         atf_check -s exit:0 -o match:"^test:.*" \
13                 grep "^test:.*" $HOME/master.passwd
14 }
15
16 # Test add user with option -N
17 atf_test_case user_add_noupdate
18 user_add_noupdate_body() {
19         populate_etc_skel
20
21         atf_check -s exit:0 -o match:"^test:.*" ${PW} useradd test -N
22         atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
23 }
24
25 # Test add user with comments
26 atf_test_case user_add_comments
27 user_add_comments_body() {
28         populate_etc_skel
29
30         atf_check -s exit:0 ${PW} useradd test -c "Test User,work,123,456"
31         atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
32                 grep "^test:.*:Test User,work,123,456:" $HOME/master.passwd
33 }
34
35 # Test add user with comments and option -N
36 atf_test_case user_add_comments_noupdate
37 user_add_comments_noupdate_body() {
38         populate_etc_skel
39
40         atf_check -s exit:0 -o match:"^test:.*:Test User,work,123,456:" \
41                 ${PW} useradd test -c "Test User,work,123,456" -N
42         atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
43 }
44
45 # Test add user with invalid comments
46 atf_test_case user_add_comments_invalid
47 user_add_comments_invalid_body() {
48         populate_etc_skel
49
50         atf_check -s exit:65 -e match:"invalid character" \
51                 ${PW} useradd test -c "Test User,work,123:456,456"
52         atf_check -s exit:1 -o empty \
53                 grep "^test:.*:Test User,work,123:456,456:" $HOME/master.passwd
54 }
55
56 # Test add user with invalid comments and option -N
57 atf_test_case user_add_comments_invalid_noupdate
58 user_add_comments_invalid_noupdate_body() {
59         populate_etc_skel
60
61         atf_check -s exit:65 -e match:"invalid character" \
62                 ${PW} useradd test -c "Test User,work,123:456,456" -N
63         atf_check -s exit:1 -o empty grep "^test:.*" $HOME/master.passwd
64 }
65
66 # Test add user with alternate homedir
67 atf_test_case user_add_homedir
68 user_add_homedir_body() {
69         populate_etc_skel
70
71         atf_check -s exit:0 ${PW} useradd test -d /foo/bar
72         atf_check -s exit:0 -o match:"^test:\*:.*::0:0:User &:/foo/bar:.*" \
73                 ${PW} usershow test
74 }
75
76 # Test add user with account expiration as an epoch date
77 atf_test_case user_add_account_expiration_epoch
78 user_add_account_expiration_epoch_body() {
79         populate_etc_skel
80
81         DATE=`date -j -v+1d "+%s"`
82         atf_check -s exit:0 ${PW} useradd test -e ${DATE}
83         atf_check -s exit:0 -o match:"^test:\*:.*::0:${DATE}:.*" \
84                 ${PW} usershow test
85 }
86
87 # Test add user with account expiration as a DD-MM-YYYY date
88 atf_test_case user_add_account_expiration_date_numeric
89 user_add_account_expiration_date_numeric_body() {
90         populate_etc_skel
91
92         DATE=`date -j -v+1d "+%d-%m-%Y"`
93         EPOCH=`date -j -f "%d-%m-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
94         atf_check -s exit:0 ${PW} useradd test -e ${DATE}
95         atf_check -s exit:0 -o match:"^test:\*:.*::0:${EPOCH}:User &:.*" \
96                 ${PW} usershow test
97 }
98
99 # Test add user with account expiration as a DD-MM-YYYY date
100 atf_test_case user_add_account_expiration_date_month
101 user_add_account_expiration_date_month_body() {
102         populate_etc_skel
103
104         DATE=`date -j -v+1d "+%d-%b-%Y"`
105         EPOCH=`date -j -f "%d-%b-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
106         atf_check -s exit:0 ${PW} useradd test -e ${DATE}
107         atf_check -s exit:0 -o match:"^test:\*:.*::0:${EPOCH}:User &:.*" \
108                 ${PW} usershow test
109 }
110
111 # Test add user with account expiration as a relative date
112 atf_test_case user_add_account_expiration_date_relative
113 user_add_account_expiration_date_relative_body() {
114         populate_etc_skel
115
116         EPOCH=`date -j -v+13m "+%s"`
117         BUF=`expr $EPOCH + 5`
118         atf_check -s exit:0 ${PW} useradd test -e +13o
119         TIME=`${PW} usershow test | awk -F ':' '{print $7}'`
120         [ ! -z $TIME -a $TIME -ge $EPOCH -a $TIME -lt $BUF ] || \
121                 atf_fail "Expiration time($TIME) was not within $EPOCH - $BUF seconds."
122 }
123
124 # Test add user with password expiration as an epoch date
125 atf_test_case user_add_password_expiration_epoch
126 user_add_password_expiration_epoch_body() {
127         populate_etc_skel
128
129         DATE=`date -j -v+1d "+%s"`
130         atf_check -s exit:0 ${PW} useradd test -p ${DATE}
131         atf_check -s exit:0 -o match:"^test:\*:.*::${DATE}:0:.*" \
132                 ${PW} usershow test
133 }
134
135 # Test add user with password expiration as a DD-MM-YYYY date
136 atf_test_case user_add_password_expiration_date_numeric
137 user_add_password_expiration_date_numeric_body() {
138         populate_etc_skel
139
140         DATE=`date -j -v+1d "+%d-%m-%Y"`
141         EPOCH=`date -j -f "%d-%m-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
142         atf_check -s exit:0 ${PW} useradd test -p ${DATE}
143         atf_check -s exit:0 -o match:"^test:\*:.*::${EPOCH}:0:User &:.*" \
144                 ${PW} usershow test
145 }
146
147 # Test add user with password expiration as a DD-MMM-YYYY date
148 atf_test_case user_add_password_expiration_date_month
149 user_add_password_expiration_date_month_body() {
150         populate_etc_skel
151
152         DATE=`date -j -v+1d "+%d-%b-%Y"`
153         EPOCH=`date -j -f "%d-%b-%Y %H:%M:%S" "${DATE} 00:00:00" "+%s"`
154         atf_check -s exit:0 ${PW} useradd test -p ${DATE}
155         atf_check -s exit:0 -o match:"^test:\*:.*::${EPOCH}:0:User &:.*" \
156                 ${PW} usershow test
157 }
158
159 # Test add user with password expiration as a relative date
160 atf_test_case user_add_password_expiration_date_relative
161 user_add_password_expiration_date_relative_body() {
162         populate_etc_skel
163
164         EPOCH=`date -j -v+13m "+%s"`
165         BUF=`expr $EPOCH + 5`
166         atf_check -s exit:0 ${PW} useradd test -p +13o
167         TIME=`${PW} usershow test | awk -F ':' '{print $6}'`
168         [ ! -z $TIME -a $TIME -ge $EPOCH -a $TIME -lt $BUF ] || \
169                 atf_fail "Expiration time($TIME) was not within $EPOCH - $BUF seconds."
170 }
171
172 atf_test_case user_add_name_too_long
173 user_add_name_too_long_body() {
174         populate_etc_skel
175         atf_check -e match:"too long" -s exit:64 \
176                 ${PW} useradd name_very_vert_very_very_very_long
177 }
178
179 atf_test_case user_add_name_with_spaces
180 user_add_name_with_spaces_body() {
181         populate_etc_skel
182         atf_check -s exit:65 -e match:"invalid character" \
183                   ${PW} useradd 'test user'
184         atf_check -s exit:1 -o empty grep "^test user:.*" $HOME/master.passwd
185         # Try again with -n which uses a slightly different code path.
186         atf_check -s exit:65 -e match:"invalid character" \
187                   ${PW} useradd -n 'test user'
188         atf_check -s exit:1 -o empty grep "^test user:.*" $HOME/master.passwd
189 }
190
191 atf_test_case user_add_name_with_spaces_and_gid_specified
192 user_add_name_with_spaces_and_gid_specified_body() {
193         populate_etc_skel
194         gid=12345
195         user_name="test user"
196         # pw useradd should fail because of the space in the user
197         # name, not because the group doesn't exist.
198         atf_check -s exit:65 -e match:"invalid character" \
199                   ${PW} useradd "${user_name}" -g ${gid}
200         atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
201         # Try again with -n which uses a slightly different code path.
202         atf_check -s exit:65 -e match:"invalid character" \
203                   ${PW} useradd -n "${user_name}" -g ${gid}
204         atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
205         # Make sure the user isn't added even if the group exists
206         atf_check -s exit:0 ${PW} groupadd blafasel -g ${gid}
207         atf_check -s exit:65 -e match:"invalid character" \
208                   ${PW} useradd "${user_name}" -g ${gid}
209         atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
210         # Try again with the -n option.
211         atf_check -s exit:65 -e match:"invalid character" \
212                   ${PW} useradd -n "${user_name}" -g ${gid}
213         atf_check -s exit:1 -o empty grep "^${user_name}:.*" $HOME/master.passwd
214 }
215
216 atf_test_case user_add_expiration
217 user_add_expiration_body() {
218         populate_etc_skel
219
220         atf_check -s exit:0 \
221                 ${PW} useradd foo -e 20-03-2037
222         atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
223                 -s exit:0 grep "^foo" ${HOME}/master.passwd
224         atf_check -s exit:0 ${PW} userdel foo
225         atf_check -s exit:0 \
226                 ${PW} useradd foo -e 20-03-37
227         atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
228                 -s exit:0 grep "^foo" ${HOME}/master.passwd
229         atf_check -s exit:0 ${PW} userdel foo
230         atf_check -s exit:0 \
231                 ${PW} useradd foo -e 20-Mar-2037
232         atf_check -o inline:"foo:*:1001:1001::0:2121120000:User &:/home/foo:/bin/sh\n" \
233                 -s exit:0 grep "^foo" ${HOME}/master.passwd
234         atf_check -s exit:0 ${PW} userdel foo
235         atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
236                 ${PW} useradd foo -e 20-Foo-2037
237         atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
238                 ${PW} useradd foo -e 20-13-2037
239         atf_check -s exit:0 ${PW} useradd foo -e "12:00 20-03-2037"
240         atf_check -s exit:0 ${PW} userdel foo
241         atf_check -e inline:"pw: Invalid date\n" -s exit:1 \
242                 ${PW} useradd foo -e "12 20-03-2037"
243         atf_check -s exit:0 ${PW} useradd foo -e "20-03-2037    12:00"
244         atf_check -s exit:0 ${PW} userdel foo
245 }
246
247 atf_test_case user_add_invalid_user_entry
248 user_add_invalid_user_entry_body() {
249         touch ${HOME}/master.passwd
250         touch ${HOME}/group
251
252         pwd_mkdb -p -d ${HOME} ${HOME}/master.passwd || \
253                 atf_fail "generate passwd from master.passwd"
254         atf_check -s exit:0 ${PW} useradd foo
255         echo "foo1:*:1002" >> ${HOME}/master.passwd
256         atf_check -s exit:1 -e match:"Invalid user entry" ${PW} useradd foo2
257 }
258
259 atf_test_case user_add_invalid_group_entry
260 user_add_invalid_group_entry_body() {
261         touch ${HOME}/master.passwd
262         touch ${HOME}/group
263
264         pwd_mkdb -p -d ${HOME} ${HOME}/master.passwd || \
265                 atf_fail "generate passwd from master.passwd"
266         atf_check -s exit:0 ${PW} useradd foo
267         echo 'foo1:*:1002' >> group
268         atf_check -s exit:1 -e match:"Invalid group entry" ${PW} useradd foo2
269 }
270
271 atf_test_case user_add_password_from_h
272 user_add_password_from_h_body() {
273         populate_etc_skel
274
275         atf_check -s exit:0 ${PW} useradd foo -h 0 <<-EOF
276         $(echo mypassword)
277         EOF
278         passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
279         atf_check -s exit:0 -o inline:$passhash \
280                 $(atf_get_srcdir)/crypt $passhash "mypassword"
281 }
282
283 atf_test_case user_add_R
284 user_add_R_body() {
285         populate_root_etc_skel
286
287         atf_check -s exit:0 ${RPW} useradd foo
288         atf_check -s exit:0 ${RPW} useradd bar -m
289         test -d ${HOME}/home || atf_fail "Home parent directory not created"
290         test -d ${HOME}/home/bar || atf_fail "Directory not created"
291         atf_check -s exit:0 ${RPW} userdel bar
292         test -d ${HOME}/home/bar || atf_fail "Directory removed"
293         atf_check -s exit:0 ${RPW} useradd bar
294         atf_check -s exit:0 ${RPW} userdel bar -r
295         [ ! -d ${HOME}/home/bar ] || atf_fail "Directory not removed"
296 }
297
298 atf_test_case user_add_R_symlink
299 user_add_R_symlink_body() {
300         populate_root_etc_skel
301
302         mkdir ${HOME}/usr
303         atf_check -s exit:0 ${RPW} useradd foo -m
304         test -d ${HOME}/usr/home || atf_fail "Home parent directory not created"
305         test -h ${HOME}/home || atf_fail "/home directory is not a symlink"
306         atf_check -s exit:0 -o inline:"usr/home\n" readlink ${HOME}/home
307 }
308
309 atf_test_case user_add_skel
310 user_add_skel_body() {
311         populate_root_etc_skel
312
313         mkdir ${HOME}/skel
314         echo "a" > ${HOME}/skel/.a
315         echo "b" > ${HOME}/skel/b
316         mkdir ${HOME}/skel/c
317         mkdir ${HOME}/skel/c/d
318         mkdir ${HOME}/skel/dot.plop
319         echo "c" > ${HOME}/skel/c/d/dot.c
320         mkdir ${HOME}/home
321         ln -sf /nonexistent ${HOME}/skel/c/foo
322         atf_check -s exit:0 ${RPW} useradd foo -k /skel -m
323         test -d ${HOME}/home/foo || atf_fail "Directory not created"
324         test -f ${HOME}/home/foo/.a || atf_fail "File not created"
325         atf_check -o file:${HOME}/skel/.a -s exit:0 cat ${HOME}/home/foo/.a
326         atf_check -o file:${HOME}/skel/b -s exit:0 cat ${HOME}/home/foo/b
327         test -d ${HOME}/home/foo/c || atf_fail "Dotted directory in skel not copied"
328         test -d ${HOME}/home/foo/.plop || atf_fail "Directory in skell not created"
329         atf_check -o inline:"/nonexistent\n" -s ignore readlink -f ${HOME}/home/foo/c/foo
330         atf_check -o file:${HOME}/skel/c/d/dot.c -s exit:0 cat ${HOME}/home/foo/c/d/.c
331 }
332
333 atf_test_case user_add_uid0
334 user_add_uid0_body() {
335         populate_etc_skel
336         atf_check -e inline:"pw: WARNING: new account \`foo' has a uid of 0 (superuser access!)\n" \
337                 -s exit:0 ${PW} useradd foo -u 0 -g 0 -d /root -s /bin/sh -c "Bourne-again Superuser" -o
338         atf_check \
339                 -o inline:"foo:*:0:0::0:0:Bourne-again Superuser:/root:/bin/sh\n" \
340                 -s exit:0 ${PW} usershow foo
341 }
342
343 atf_test_case user_add_uid_too_large
344 user_add_uid_too_large_body() {
345         populate_etc_skel
346         atf_check -s exit:64 -e inline:"pw: Bad id '9999999999999': too large\n" \
347                 ${PW} useradd -n test1 -u 9999999999999
348 }
349
350 atf_test_case user_add_bad_shell
351 user_add_bad_shell_body() {
352         populate_etc_skel
353
354         atf_check -s exit:0 ${PW} useradd foo -s sh
355         atf_check -s exit:78 -e ignore ${PW} useradd bar -s badshell
356 }
357
358 atf_test_case user_add_already_exists
359 user_add_already_exists_body() {
360         populate_etc_skel
361
362         atf_check -s exit:0 ${PW} useradd foo
363         atf_check -s exit:65 \
364                 -e inline:"pw: login name \`foo' already exists\n" \
365                 ${PW} useradd foo
366 }
367
368 atf_test_case user_add_w_error
369 user_add_w_error_body() {
370         populate_etc_skel
371
372         atf_check -s exit:1 -e match:"pw: Invalid value for default password" \
373                 ${PW} useradd foo -w invalid_value
374 }
375
376 atf_test_case user_add_w_no
377 user_add_w_no_body() {
378         populate_etc_skel
379
380         atf_check -s exit:0 ${PW} useradd foo -w no
381         atf_check -s exit:0 -o match:"^foo:\*" grep "^foo:" $HOME/master.passwd
382 }
383
384 atf_test_case user_add_w_none
385 user_add_w_none_body() {
386         populate_etc_skel
387
388         atf_check -s exit:0 ${PW} useradd foo -w none
389         atf_check -s exit:0 -o match:"^foo::" grep "^foo:" $HOME/master.passwd
390 }
391
392 atf_test_case user_add_w_random
393 user_add_w_random_body() {
394         populate_etc_skel
395
396         password=`${PW} useradd foo -w random | cat`
397         passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
398         atf_check -s exit:0 -o inline:$passhash \
399                 $(atf_get_srcdir)/crypt $passhash "$password"
400 }
401
402 atf_test_case user_add_w_yes
403 user_add_w_yes_body() {
404         populate_etc_skel
405         password=`${PW} useradd foo -w random | cat`
406         passhash=`awk -F ':' '/^foo:/ {print $2}' $HOME/master.passwd`
407         atf_check -s exit:0 -o inline:$passhash \
408                 $(atf_get_srcdir)/crypt $passhash "$password"
409 }
410
411 atf_test_case user_add_with_pw_conf
412 user_add_with_pw_conf_body()
413 {
414         populate_etc_skel
415         atf_check -s exit:0 \
416                 ${PW} useradd -D -C ${HOME}/pw.conf \
417                 -u 2000,32767 -i 2000,32767
418         atf_check -s exit:0 \
419                 -o inline:"minuid = 2000\nmaxuid = 32767\nmingid = 2000\nmaxgid = 32767\n" \
420                 grep "^m.*id =" ${HOME}/pw.conf
421         atf_check -s exit:0 \
422                 ${PW} useradd foo -C ${HOME}/pw.conf
423 }
424 atf_test_case user_add_defaultgroup
425 user_add_defaultgroup_body()
426 {
427         populate_etc_skel
428         echo 'defaultgroup = "plop"' > ${HOME}/pw.conf
429         atf_check -s exit:0 \
430                 ${PW} groupadd plop -g 442
431         atf_check -s exit:0 \
432                 ${PW} useradd foo -C ${HOME}/pw.conf
433         atf_check -s exit:0 \
434                 -o inline:"foo:*:1001:442::0:0:User &:/home/foo:/bin/sh\n" \
435                 ${PW} usershow foo
436 }
437
438 atf_init_test_cases() {
439         atf_add_test_case user_add
440         atf_add_test_case user_add_noupdate
441         atf_add_test_case user_add_comments
442         atf_add_test_case user_add_comments_noupdate
443         atf_add_test_case user_add_comments_invalid
444         atf_add_test_case user_add_comments_invalid_noupdate
445         atf_add_test_case user_add_homedir
446         atf_add_test_case user_add_account_expiration_epoch
447         atf_add_test_case user_add_account_expiration_date_numeric
448         atf_add_test_case user_add_account_expiration_date_month
449         atf_add_test_case user_add_account_expiration_date_relative
450         atf_add_test_case user_add_password_expiration_epoch
451         atf_add_test_case user_add_password_expiration_date_numeric
452         atf_add_test_case user_add_password_expiration_date_month
453         atf_add_test_case user_add_password_expiration_date_relative
454         atf_add_test_case user_add_name_too_long
455         atf_add_test_case user_add_name_with_spaces
456         atf_add_test_case user_add_name_with_spaces_and_gid_specified
457         atf_add_test_case user_add_expiration
458         atf_add_test_case user_add_invalid_user_entry
459         atf_add_test_case user_add_invalid_group_entry
460         atf_add_test_case user_add_password_from_h
461         atf_add_test_case user_add_R
462         atf_add_test_case user_add_R_symlink
463         atf_add_test_case user_add_skel
464         atf_add_test_case user_add_uid0
465         atf_add_test_case user_add_uid_too_large
466         atf_add_test_case user_add_bad_shell
467         atf_add_test_case user_add_already_exists
468         atf_add_test_case user_add_w_error
469         atf_add_test_case user_add_w_no
470         atf_add_test_case user_add_w_none
471         atf_add_test_case user_add_w_random
472         atf_add_test_case user_add_w_yes
473         atf_add_test_case user_add_with_pw_conf
474         atf_add_test_case user_add_defaultgroup
475 }