| 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 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | else | 
|  | 27 | bbnote "${PN}: group $groupname already exists, not re-creating it" | 
|  | 28 | fi | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | perform_useradd () { | 
|  | 32 | local rootdir="$1" | 
|  | 33 | local opts="$2" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 34 | bbnote "${PN}: Performing useradd with [$opts]" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 38 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | else | 
|  | 45 | bbnote "${PN}: user $username already exists, not re-creating it" | 
|  | 46 | fi | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | perform_groupmems () { | 
|  | 50 | local rootdir="$1" | 
|  | 51 | local opts="$2" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 52 | bbnote "${PN}: Performing groupmems with [$opts]" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 66 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | else | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 72 | bbnote "${PN}: group $groupname already contains $username, not re-adding it" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | fi | 
|  | 74 | if test "x$gshadow" = "xno"; then | 
|  | 75 | rm -f $rootdir${sysconfdir}/gshadow | 
|  | 76 | rm -f $rootdir${sysconfdir}/gshadow- | 
|  | 77 | fi | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | perform_groupdel () { | 
|  | 81 | local rootdir="$1" | 
|  | 82 | local opts="$2" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 83 | bbnote "${PN}: Performing groupdel with [$opts]" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 87 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 92 | else | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 93 | bbnote "${PN}: group $groupname doesn't exist, not removing it" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | fi | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | perform_userdel () { | 
|  | 98 | local rootdir="$1" | 
|  | 99 | local opts="$2" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 100 | bbnote "${PN}: Performing userdel with [$opts]" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 104 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | else | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 110 | bbnote "${PN}: user $username doesn't exist, not removing it" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | fi | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | perform_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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 120 | bbnote "${PN}: Performing groupmod with [$opts]" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 124 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | else | 
|  | 129 | bbwarn "${PN}: group $groupname doesn't exist, unable to modify it" | 
|  | 130 | fi | 
|  | 131 | set -e | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | perform_usermod () { | 
|  | 135 | # Same reason with groupmod, temporarily disable -e option | 
|  | 136 | set +e | 
|  | 137 | local rootdir="$1" | 
|  | 138 | local opts="$2" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 139 | bbnote "${PN}: Performing usermod with [$opts]" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 140 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 143 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 147 | else | 
|  | 148 | bbwarn "${PN}: user $username doesn't exist, unable to modify it" | 
|  | 149 | fi | 
|  | 150 | set -e | 
|  | 151 | } |