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 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."
26 bbnote "${PN}: group $groupname already exists, not re-creating it"
33 bbnote "${PN}: Performing useradd with [$opts]"
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
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."
43 bbnote "${PN}: user $username already exists, not re-creating it"
47 perform_groupmems () {
50 bbnote "${PN}: Performing groupmems with [$opts]"
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"
54 local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
55 if test "x$mem_exists" = "x"; then
56 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmems \$opts\" || true
57 mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
58 if test "x$mem_exists" = "x"; then
59 bbfatal "${PN}: groupmems command did not succeed."
62 bbnote "${PN}: group $groupname already contains $username, not re-adding it"
69 bbnote "${PN}: Performing groupdel with [$opts]"
70 local groupname=`echo "$opts" | awk '{ print $NF }'`
71 local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
72 if test "x$group_exists" != "x"; then
73 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupdel \$opts\" || true
74 group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
75 if test "x$group_exists" != "x"; then
76 bbfatal "${PN}: groupdel command did not succeed."
79 bbnote "${PN}: group $groupname doesn't exist, not removing it"
86 bbnote "${PN}: Performing userdel with [$opts]"
87 local username=`echo "$opts" | awk '{ print $NF }'`
88 local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
89 if test "x$user_exists" != "x"; then
90 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO userdel \$opts\" || true
91 user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
92 if test "x$user_exists" != "x"; then
93 bbfatal "${PN}: userdel command did not succeed."
96 bbnote "${PN}: user $username doesn't exist, not removing it"
100 perform_groupmod () {
101 # Other than the return value of groupmod, there's no simple way to judge whether the command
102 # succeeds, so we disable -e option temporarily
106 bbnote "${PN}: Performing groupmod with [$opts]"
107 local groupname=`echo "$opts" | awk '{ print $NF }'`
108 local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
109 if test "x$group_exists" != "x"; then
110 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmod \$opts\"
111 if test $? != 0; then
112 bbwarn "${PN}: groupmod command did not succeed."
115 bbwarn "${PN}: group $groupname doesn't exist, unable to modify it"
121 # Same reason with groupmod, temporarily disable -e option
125 bbnote "${PN}: Performing usermod with [$opts]"
126 local username=`echo "$opts" | awk '{ print $NF }'`
127 local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
128 if test "x$user_exists" != "x"; then
129 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO usermod \$opts\"
130 if test $? != 0; then
131 bbfatal "${PN}: usermod command did not succeed."
134 bbwarn "${PN}: user $username doesn't exist, unable to modify it"