1 # This bbclass provides basic functionality for user/group settings.
2 # This bbclass is intended to be inherited by useradd.bbclass and
5 # The following functions basically have similar logic.
6 # *) Perform necessary checks before invoking the actual command
7 # *) Invoke the actual command with flock
8 # *) Error out if an error occurs.
10 # Note that before invoking these functions, make sure the global variable
11 # PSEUDO is set up correctly.
16 bbnote "${PN}: Performing groupadd with [$opts]"
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
20 opts=`echo $opts | sed s/\'/\"/g`
21 eval flock -x -w 100 $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."
27 bbnote "${PN}: group $groupname already exists, not re-creating it"
34 bbnote "${PN}: Performing useradd with [$opts]"
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
38 opts=`echo $opts | sed s/\'/\"/g`
39 eval flock -x -w 100 $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."
45 bbnote "${PN}: user $username already exists, not re-creating it"
49 perform_groupmems () {
52 bbnote "${PN}: Performing groupmems with [$opts]"
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
58 if [ -f $rootdir${sysconfdir}/gshadow ]; then
62 touch $rootdir${sysconfdir}/gshadow
64 local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
65 if test "x$mem_exists" = "x"; then
66 eval flock -x -w 100 $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."
72 bbnote "${PN}: group $groupname already contains $username, not re-adding it"
74 if test "x$gshadow" = "xno"; then
75 rm -f $rootdir${sysconfdir}/gshadow
76 rm -f $rootdir${sysconfdir}/gshadow-
83 bbnote "${PN}: Performing groupdel with [$opts]"
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
87 eval flock -x -w 100 $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."
93 bbnote "${PN}: group $groupname doesn't exist, not removing it"
100 bbnote "${PN}: Performing userdel with [$opts]"
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
104 eval flock -x -w 100 $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."
110 bbnote "${PN}: user $username doesn't exist, not removing it"
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
120 bbnote "${PN}: Performing groupmod with [$opts]"
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
124 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO groupmod $opts\'
125 if test $? != 0; then
126 bbwarn "${PN}: groupmod command did not succeed."
129 bbwarn "${PN}: group $groupname doesn't exist, unable to modify it"
135 # Same reason with groupmod, temporarily disable -e option
139 bbnote "${PN}: Performing usermod with [$opts]"
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
143 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO usermod $opts\'
144 if test $? != 0; then
145 bbfatal "${PN}: usermod command did not succeed."
148 bbwarn "${PN}: user $username doesn't exist, unable to modify it"