1#!/bin/sh
2# exercise chcon
3
4# Copyright (C) 2007-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ chcon
21require_root_
22require_selinux_
23skip_if_mcstransd_is_running_
24mls_enabled_ || skip_ 'MLS is disabled'
25
26mkdir -p d/sub/s2 || framework_failure_
27touch f g d/sub/1 d/sub/2 || framework_failure_
28
29
30# Set to a specified context.
31# Use root:object_r:tmp_t:s0.  It is special in that
32# it works even when mcstransd isn't running.
33u1=root
34r1=object_r
35t1=tmp_t
36range=s0
37ctx=$u1:$r1:$t1:$range
38chcon $ctx f || skip_ "Failed to set context: $ctx"
39stat --printf='f|%C\n' f > out || fail=1
40
41# Use --reference.
42chcon --ref=f g || fail=1
43stat --printf='g|%C\n' g >> out || fail=1
44
45# Change the individual parts of the context, one by one.
46u2=user_u
47r2=object_r
48t2=unlabeled_t
49for i in --user=$u2 --role=$r2 --type=$t2 --range=$range; do
50  chcon $i f || fail=1
51  stat --printf="f|$i|"'%C\n' f >> out || fail=1
52done
53
54# Same, but change back using the short-named options.
55for i in -u$u1 -r$r1 -t$t1; do
56  chcon $i f || fail=1
57  stat --printf="f|$i|"'%C\n' f >> out || fail=1
58done
59
60cat <<EOF > exp || framework_failure_
61f|$ctx
62g|$ctx
63f|--user=$u2|$u2:$r1:$t1:$range
64f|--role=$r2|$u2:$r2:$t1:$range
65f|--type=$t2|$u2:$r2:$t2:$range
66f|--range=$range|$u2:$r2:$t2:$range
67f|-uroot|root:object_r:$t2:$range
68f|-robject_r|root:object_r:$t2:$range
69f|-ttmp_t|root:object_r:tmp_t:$range
70EOF
71
72compare exp out || fail=1
73
74chcon --verbose -u$u1 f > out || fail=1
75echo "changing security context of 'f'" > exp || framework_failure_
76compare exp out || fail=1
77
78Exit $fail
79