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 |
| 7 | # *) Invoke the actual command, make retries if necessary |
| 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" |
| 16 | local retries="$3" |
| 17 | bbnote "${PN}: Performing groupadd with [$opts] and $retries times of retry" |
| 18 | local groupname=`echo "$opts" | awk '{ print $NF }'` |
| 19 | local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" |
| 20 | if test "x$group_exists" = "x"; then |
| 21 | local count=0 |
| 22 | while true; do |
| 23 | eval $PSEUDO groupadd $opts || true |
| 24 | group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" |
| 25 | if test "x$group_exists" = "x"; then |
| 26 | bbwarn "${PN}: groupadd command did not succeed. Retrying..." |
| 27 | else |
| 28 | break |
| 29 | fi |
| 30 | count=`expr $count + 1` |
| 31 | if test $count = $retries; then |
| 32 | bbfatal "${PN}: Tried running groupadd command $retries times without success, giving up" |
| 33 | fi |
| 34 | sleep $count |
| 35 | done |
| 36 | else |
| 37 | bbnote "${PN}: group $groupname already exists, not re-creating it" |
| 38 | fi |
| 39 | } |
| 40 | |
| 41 | perform_useradd () { |
| 42 | local rootdir="$1" |
| 43 | local opts="$2" |
| 44 | local retries="$3" |
| 45 | bbnote "${PN}: Performing useradd with [$opts] and $retries times of retry" |
| 46 | local username=`echo "$opts" | awk '{ print $NF }'` |
| 47 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 48 | if test "x$user_exists" = "x"; then |
| 49 | local count=0 |
| 50 | while true; do |
| 51 | eval $PSEUDO useradd $opts || true |
| 52 | user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 53 | if test "x$user_exists" = "x"; then |
| 54 | bbwarn "${PN}: useradd command did not succeed. Retrying..." |
| 55 | else |
| 56 | break |
| 57 | fi |
| 58 | count=`expr $count + 1` |
| 59 | if test $count = $retries; then |
| 60 | bbfatal "${PN}: Tried running useradd command $retries times without success, giving up" |
| 61 | fi |
| 62 | sleep $count |
| 63 | done |
| 64 | else |
| 65 | bbnote "${PN}: user $username already exists, not re-creating it" |
| 66 | fi |
| 67 | } |
| 68 | |
| 69 | perform_groupmems () { |
| 70 | local rootdir="$1" |
| 71 | local opts="$2" |
| 72 | local retries="$3" |
| 73 | bbnote "${PN}: Performing groupmems with [$opts] and $retries times of retry" |
| 74 | local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'` |
| 75 | local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'` |
| 76 | bbnote "${PN}: Running groupmems command with group $groupname and user $username" |
| 77 | # groupmems fails if /etc/gshadow does not exist |
| 78 | local gshadow="" |
| 79 | if [ -f $rootdir${sysconfdir}/gshadow ]; then |
| 80 | gshadow="yes" |
| 81 | else |
| 82 | gshadow="no" |
| 83 | touch $rootdir${sysconfdir}/gshadow |
| 84 | fi |
| 85 | local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`" |
| 86 | if test "x$mem_exists" = "x"; then |
| 87 | local count=0 |
| 88 | while true; do |
| 89 | eval $PSEUDO groupmems $opts || true |
| 90 | mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`" |
| 91 | if test "x$mem_exists" = "x"; then |
| 92 | bbwarn "${PN}: groupmems command did not succeed. Retrying..." |
| 93 | else |
| 94 | break |
| 95 | fi |
| 96 | count=`expr $count + 1` |
| 97 | if test $count = $retries; then |
| 98 | if test "x$gshadow" = "xno"; then |
| 99 | rm -f $rootdir${sysconfdir}/gshadow |
| 100 | rm -f $rootdir${sysconfdir}/gshadow- |
| 101 | fi |
| 102 | bbfatal "${PN}: Tried running groupmems command $retries times without success, giving up" |
| 103 | fi |
| 104 | sleep $count |
| 105 | done |
| 106 | else |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 107 | bbnote "${PN}: group $groupname already contains $username, not re-adding it" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | fi |
| 109 | if test "x$gshadow" = "xno"; then |
| 110 | rm -f $rootdir${sysconfdir}/gshadow |
| 111 | rm -f $rootdir${sysconfdir}/gshadow- |
| 112 | fi |
| 113 | } |
| 114 | |
| 115 | perform_groupdel () { |
| 116 | local rootdir="$1" |
| 117 | local opts="$2" |
| 118 | local retries="$3" |
| 119 | bbnote "${PN}: Performing groupdel with [$opts] and $retries times of retry" |
| 120 | local groupname=`echo "$opts" | awk '{ print $NF }'` |
| 121 | local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" |
| 122 | if test "x$group_exists" != "x"; then |
| 123 | local count=0 |
| 124 | while true; do |
| 125 | eval $PSEUDO groupdel $opts || true |
| 126 | group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" |
| 127 | if test "x$group_exists" != "x"; then |
| 128 | bbwarn "${PN}: groupdel command did not succeed. Retrying..." |
| 129 | else |
| 130 | break |
| 131 | fi |
| 132 | count=`expr $count + 1` |
| 133 | if test $count = $retries; then |
| 134 | bbfatal "${PN}: Tried running groupdel command $retries times without success, giving up" |
| 135 | fi |
| 136 | sleep $count |
| 137 | done |
| 138 | else |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 139 | bbnote "${PN}: group $groupname doesn't exist, not removing it" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 140 | fi |
| 141 | } |
| 142 | |
| 143 | perform_userdel () { |
| 144 | local rootdir="$1" |
| 145 | local opts="$2" |
| 146 | local retries="$3" |
| 147 | bbnote "${PN}: Performing userdel with [$opts] and $retries times of retry" |
| 148 | local username=`echo "$opts" | awk '{ print $NF }'` |
| 149 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 150 | if test "x$user_exists" != "x"; then |
| 151 | local count=0 |
| 152 | while true; do |
| 153 | eval $PSEUDO userdel $opts || true |
| 154 | user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 155 | if test "x$user_exists" != "x"; then |
| 156 | bbwarn "${PN}: userdel command did not succeed. Retrying..." |
| 157 | else |
| 158 | break |
| 159 | fi |
| 160 | count=`expr $count + 1` |
| 161 | if test $count = $retries; then |
| 162 | bbfatal "${PN}: Tried running userdel command $retries times without success, giving up" |
| 163 | fi |
| 164 | sleep $count |
| 165 | done |
| 166 | else |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 167 | bbnote "${PN}: user $username doesn't exist, not removing it" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | fi |
| 169 | } |
| 170 | |
| 171 | perform_groupmod () { |
| 172 | # Other than the return value of groupmod, there's no simple way to judge whether the command |
| 173 | # succeeds, so we disable -e option temporarily |
| 174 | set +e |
| 175 | local rootdir="$1" |
| 176 | local opts="$2" |
| 177 | local retries="$3" |
| 178 | bbnote "${PN}: Performing groupmod with [$opts] and $retries times of retry" |
| 179 | local groupname=`echo "$opts" | awk '{ print $NF }'` |
| 180 | local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" |
| 181 | if test "x$group_exists" != "x"; then |
| 182 | local count=0 |
| 183 | while true; do |
| 184 | eval $PSEUDO groupmod $opts |
| 185 | if test $? != 0; then |
| 186 | bbwarn "${PN}: groupmod command did not succeed. Retrying..." |
| 187 | else |
| 188 | break |
| 189 | fi |
| 190 | count=`expr $count + 1` |
| 191 | if test $count = $retries; then |
| 192 | bbfatal "${PN}: Tried running groupmod command $retries times without success, giving up" |
| 193 | fi |
| 194 | sleep $count |
| 195 | done |
| 196 | else |
| 197 | bbwarn "${PN}: group $groupname doesn't exist, unable to modify it" |
| 198 | fi |
| 199 | set -e |
| 200 | } |
| 201 | |
| 202 | perform_usermod () { |
| 203 | # Same reason with groupmod, temporarily disable -e option |
| 204 | set +e |
| 205 | local rootdir="$1" |
| 206 | local opts="$2" |
| 207 | local retries="$3" |
| 208 | bbnote "${PN}: Performing usermod with [$opts] and $retries times of retry" |
| 209 | local username=`echo "$opts" | awk '{ print $NF }'` |
| 210 | local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" |
| 211 | if test "x$user_exists" != "x"; then |
| 212 | local count=0 |
| 213 | while true; do |
| 214 | eval $PSEUDO usermod $opts |
| 215 | if test $? != 0; then |
| 216 | bbwarn "${PN}: usermod command did not succeed. Retrying..." |
| 217 | else |
| 218 | break |
| 219 | fi |
| 220 | count=`expr $count + 1` |
| 221 | if test $count = $retries; then |
| 222 | bbfatal "${PN}: Tried running usermod command $retries times without success, giving up" |
| 223 | fi |
| 224 | sleep $count |
| 225 | done |
| 226 | else |
| 227 | bbwarn "${PN}: user $username doesn't exist, unable to modify it" |
| 228 | fi |
| 229 | set -e |
| 230 | } |