blob: 0d81accd1aebbd540d47a1ad718cbfadd3be826e [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# This bbclass provides basic functionality for user/group settings.
2# This bbclass is intended to be inherited by useradd.bbclass and
3# extrausers.bbclass.
4
5# The following functions basically have similar logic.
6# *) Perform necessary checks before invoking the actual command
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05007# *) Invoke the actual command with flock
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008# *) Error out if an error occurs.
9
10# Note that before invoking these functions, make sure the global variable
11# PSEUDO is set up correctly.
12
13perform_groupadd () {
14 local rootdir="$1"
15 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050016 bbnote "${PN}: Performing groupadd with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017 local groupname=`echo "$opts" | awk '{ print $NF }'`
18 local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
19 if test "x$group_exists" = "x"; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020 opts=`echo $opts | sed s/\'/\"/g`
21 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupadd \$opts\" || true
22 group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
23 if test "x$group_exists" = "x"; then
24 bbfatal "${PN}: groupadd command did not succeed."
25 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 else
27 bbnote "${PN}: group $groupname already exists, not re-creating it"
28 fi
29}
30
31perform_useradd () {
32 local rootdir="$1"
33 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050034 bbnote "${PN}: Performing useradd with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035 local username=`echo "$opts" | awk '{ print $NF }'`
36 local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
37 if test "x$user_exists" = "x"; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050038 opts=`echo $opts | sed s/\'/\"/g`
39 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO useradd \$opts\" || true
40 user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
41 if test "x$user_exists" = "x"; then
42 bbfatal "${PN}: useradd command did not succeed."
43 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 else
45 bbnote "${PN}: user $username already exists, not re-creating it"
46 fi
47}
48
49perform_groupmems () {
50 local rootdir="$1"
51 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050052 bbnote "${PN}: Performing groupmems with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053 local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'`
54 local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'`
55 bbnote "${PN}: Running groupmems command with group $groupname and user $username"
56 # groupmems fails if /etc/gshadow does not exist
57 local gshadow=""
58 if [ -f $rootdir${sysconfdir}/gshadow ]; then
59 gshadow="yes"
60 else
61 gshadow="no"
62 touch $rootdir${sysconfdir}/gshadow
63 fi
64 local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
65 if test "x$mem_exists" = "x"; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050066 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmems \$opts\" || true
67 mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
68 if test "x$mem_exists" = "x"; then
69 bbfatal "${PN}: groupmems command did not succeed."
70 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 else
Patrick Williamsf1e5d692016-03-30 15:21:19 -050072 bbnote "${PN}: group $groupname already contains $username, not re-adding it"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 fi
74 if test "x$gshadow" = "xno"; then
75 rm -f $rootdir${sysconfdir}/gshadow
76 rm -f $rootdir${sysconfdir}/gshadow-
77 fi
78}
79
80perform_groupdel () {
81 local rootdir="$1"
82 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050083 bbnote "${PN}: Performing groupdel with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 local groupname=`echo "$opts" | awk '{ print $NF }'`
85 local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
86 if test "x$group_exists" != "x"; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050087 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupdel \$opts\" || true
88 group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
89 if test "x$group_exists" != "x"; then
90 bbfatal "${PN}: groupdel command did not succeed."
91 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 else
Patrick Williamsf1e5d692016-03-30 15:21:19 -050093 bbnote "${PN}: group $groupname doesn't exist, not removing it"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 fi
95}
96
97perform_userdel () {
98 local rootdir="$1"
99 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500100 bbnote "${PN}: Performing userdel with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 local username=`echo "$opts" | awk '{ print $NF }'`
102 local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
103 if test "x$user_exists" != "x"; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500104 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO userdel \$opts\" || true
105 user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
106 if test "x$user_exists" != "x"; then
107 bbfatal "${PN}: userdel command did not succeed."
108 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 else
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500110 bbnote "${PN}: user $username doesn't exist, not removing it"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 fi
112}
113
114perform_groupmod () {
115 # Other than the return value of groupmod, there's no simple way to judge whether the command
116 # succeeds, so we disable -e option temporarily
117 set +e
118 local rootdir="$1"
119 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500120 bbnote "${PN}: Performing groupmod with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 local groupname=`echo "$opts" | awk '{ print $NF }'`
122 local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
123 if test "x$group_exists" != "x"; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500124 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmod \$opts\"
125 if test $? != 0; then
126 bbwarn "${PN}: groupmod command did not succeed."
127 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128 else
129 bbwarn "${PN}: group $groupname doesn't exist, unable to modify it"
130 fi
131 set -e
132}
133
134perform_usermod () {
135 # Same reason with groupmod, temporarily disable -e option
136 set +e
137 local rootdir="$1"
138 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500139 bbnote "${PN}: Performing usermod with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140 local username=`echo "$opts" | awk '{ print $NF }'`
141 local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
142 if test "x$user_exists" != "x"; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO usermod \$opts\"
144 if test $? != 0; then
145 bbfatal "${PN}: usermod command did not succeed."
146 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147 else
148 bbwarn "${PN}: user $username doesn't exist, unable to modify it"
149 fi
150 set -e
151}