Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 7 | # *) Invoke the actual command with flock |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | # *) 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 | |
| 13 | perform_groupadd () { |
| 14 | local rootdir="$1" |
| 15 | local opts="$2" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 16 | bbnote "${PN}: Performing groupadd with [$opts]" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 20 | eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupadd \$opts\" || true |
| 21 | group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" |
| 22 | if test "x$group_exists" = "x"; then |
| 23 | bbfatal "${PN}: groupadd command did not succeed." |
| 24 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | else |
| 26 | bbnote "${PN}: group $groupname already exists, not re-creating it" |
| 27 | fi |
| 28 | } |
| 29 | |
| 30 | perform_useradd () { |
| 31 | local rootdir="$1" |
| 32 | local opts="$2" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 33 | bbnote "${PN}: Performing useradd with [$opts]" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | local username=`echo "$opts" | awk '{ print $NF }'` |
| 35 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 36 | if test "x$user_exists" = "x"; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 37 | eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO useradd \$opts\" || true |
| 38 | user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 39 | if test "x$user_exists" = "x"; then |
| 40 | bbfatal "${PN}: useradd command did not succeed." |
| 41 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | else |
| 43 | bbnote "${PN}: user $username already exists, not re-creating it" |
| 44 | fi |
| 45 | } |
| 46 | |
| 47 | perform_groupmems () { |
| 48 | local rootdir="$1" |
| 49 | local opts="$2" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 50 | bbnote "${PN}: Performing groupmems with [$opts]" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'` |
| 52 | local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'` |
| 53 | bbnote "${PN}: Running groupmems command with group $groupname and user $username" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 54 | local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*$" $rootdir/etc/group || true`" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | if test "x$mem_exists" = "x"; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 56 | eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmems \$opts\" || true |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 57 | mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*$" $rootdir/etc/group || true`" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 58 | if test "x$mem_exists" = "x"; then |
| 59 | bbfatal "${PN}: groupmems command did not succeed." |
| 60 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | else |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 62 | bbnote "${PN}: group $groupname already contains $username, not re-adding it" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | perform_groupdel () { |
| 67 | local rootdir="$1" |
| 68 | local opts="$2" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 69 | bbnote "${PN}: Performing groupdel with [$opts]" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 70 | local groupname=`echo "$opts" | awk '{ print $NF }'` |
| 71 | local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 72 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | if test "x$group_exists" != "x"; then |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 74 | local awk_input='BEGIN {FS=":"}; $1=="'$groupname'" { print $3 }' |
| 75 | local groupid=`echo "$awk_input" | awk -f- $rootdir/etc/group` |
| 76 | local awk_check_users='BEGIN {FS=":"}; $4=="'$groupid'" {print $1}' |
| 77 | local other_users=`echo "$awk_check_users" | awk -f- $rootdir/etc/passwd` |
| 78 | |
| 79 | if test "x$other_users" = "x"; then |
| 80 | eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupdel \$opts\" || true |
| 81 | group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" |
| 82 | if test "x$group_exists" != "x"; then |
| 83 | bbfatal "${PN}: groupdel command did not succeed." |
| 84 | fi |
| 85 | else |
| 86 | bbnote "${PN}: '$groupname' is primary group for users '$other_users', not removing it" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 87 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | else |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 89 | bbnote "${PN}: group $groupname doesn't exist, not removing it" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | fi |
| 91 | } |
| 92 | |
| 93 | perform_userdel () { |
| 94 | local rootdir="$1" |
| 95 | local opts="$2" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 96 | bbnote "${PN}: Performing userdel with [$opts]" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | local username=`echo "$opts" | awk '{ print $NF }'` |
| 98 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 99 | if test "x$user_exists" != "x"; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 100 | eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO userdel \$opts\" || true |
| 101 | user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 102 | if test "x$user_exists" != "x"; then |
| 103 | bbfatal "${PN}: userdel command did not succeed." |
| 104 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | else |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 106 | bbnote "${PN}: user $username doesn't exist, not removing it" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | fi |
| 108 | } |
| 109 | |
| 110 | perform_groupmod () { |
| 111 | # Other than the return value of groupmod, there's no simple way to judge whether the command |
| 112 | # succeeds, so we disable -e option temporarily |
| 113 | set +e |
| 114 | local rootdir="$1" |
| 115 | local opts="$2" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 116 | bbnote "${PN}: Performing groupmod with [$opts]" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | local groupname=`echo "$opts" | awk '{ print $NF }'` |
| 118 | local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" |
| 119 | if test "x$group_exists" != "x"; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 120 | eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmod \$opts\" |
| 121 | if test $? != 0; then |
| 122 | bbwarn "${PN}: groupmod command did not succeed." |
| 123 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | else |
| 125 | bbwarn "${PN}: group $groupname doesn't exist, unable to modify it" |
| 126 | fi |
| 127 | set -e |
| 128 | } |
| 129 | |
| 130 | perform_usermod () { |
| 131 | # Same reason with groupmod, temporarily disable -e option |
| 132 | set +e |
| 133 | local rootdir="$1" |
| 134 | local opts="$2" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 135 | bbnote "${PN}: Performing usermod with [$opts]" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 136 | local username=`echo "$opts" | awk '{ print $NF }'` |
| 137 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 138 | if test "x$user_exists" != "x"; then |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 139 | eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO usermod \$opts\" |
| 140 | if test $? != 0; then |
| 141 | bbfatal "${PN}: usermod command did not succeed." |
| 142 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 143 | else |
| 144 | bbwarn "${PN}: user $username doesn't exist, unable to modify it" |
| 145 | fi |
| 146 | set -e |
| 147 | } |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 148 | |
| 149 | perform_passwd_expire () { |
| 150 | local rootdir="$1" |
| 151 | local opts="$2" |
| 152 | bbnote "${PN}: Performing equivalent of passwd --expire with [$opts]" |
| 153 | # Directly set sp_lstchg to 0 without using the passwd command: Only root can do that |
| 154 | local username=`echo "$opts" | awk '{ print $NF }'` |
| 155 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 156 | if test "x$user_exists" != "x"; then |
| 157 | eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO sed -i \''s/^\('$username':[^:]*\):[^:]*:/\1:0:/'\' $rootdir/etc/shadow \" || true |
| 158 | local passwd_lastchanged="`grep "^$username:" $rootdir/etc/shadow | cut -d: -f3`" |
| 159 | if test "x$passwd_lastchanged" != "x0"; then |
| 160 | bbfatal "${PN}: passwd --expire operation did not succeed." |
| 161 | fi |
| 162 | else |
| 163 | bbnote "${PN}: user $username doesn't exist, not expiring its password" |
| 164 | fi |
| 165 | } |