blob: ba87edc57ac87049512c5b4412a499a875976f03 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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 Williamsd8c66bc2016-06-20 12:57:21 -05007# *) Invoke the actual command with flock
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008# *) 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
13perform_groupadd () {
14 local rootdir="$1"
15 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050016 bbnote "${PN}: Performing groupadd with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017 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 Williamsd8c66bc2016-06-20 12:57:21 -050020 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 Williamsc124f4f2015-09-15 14:41:29 -050025 else
26 bbnote "${PN}: group $groupname already exists, not re-creating it"
27 fi
28}
29
30perform_useradd () {
31 local rootdir="$1"
32 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050033 bbnote "${PN}: Performing useradd with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 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 Williamsd8c66bc2016-06-20 12:57:21 -050037 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 Williamsc124f4f2015-09-15 14:41:29 -050042 else
43 bbnote "${PN}: user $username already exists, not re-creating it"
44 fi
45}
46
47perform_groupmems () {
48 local rootdir="$1"
49 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050050 bbnote "${PN}: Performing groupmems with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 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"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054 local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
55 if test "x$mem_exists" = "x"; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050056 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."
60 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 else
Patrick Williamsf1e5d692016-03-30 15:21:19 -050062 bbnote "${PN}: group $groupname already contains $username, not re-adding it"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064}
65
66perform_groupdel () {
67 local rootdir="$1"
68 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050069 bbnote "${PN}: Performing groupdel with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050073 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."
77 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 else
Patrick Williamsf1e5d692016-03-30 15:21:19 -050079 bbnote "${PN}: group $groupname doesn't exist, not removing it"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 fi
81}
82
83perform_userdel () {
84 local rootdir="$1"
85 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050086 bbnote "${PN}: Performing userdel with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087 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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050090 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."
94 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 else
Patrick Williamsf1e5d692016-03-30 15:21:19 -050096 bbnote "${PN}: user $username doesn't exist, not removing it"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 fi
98}
99
100perform_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
103 set +e
104 local rootdir="$1"
105 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500106 bbnote "${PN}: Performing groupmod with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500110 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmod \$opts\"
111 if test $? != 0; then
112 bbwarn "${PN}: groupmod command did not succeed."
113 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 else
115 bbwarn "${PN}: group $groupname doesn't exist, unable to modify it"
116 fi
117 set -e
118}
119
120perform_usermod () {
121 # Same reason with groupmod, temporarily disable -e option
122 set +e
123 local rootdir="$1"
124 local opts="$2"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500125 bbnote "${PN}: Performing usermod with [$opts]"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500129 eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO usermod \$opts\"
130 if test $? != 0; then
131 bbfatal "${PN}: usermod command did not succeed."
132 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133 else
134 bbwarn "${PN}: user $username doesn't exist, unable to modify it"
135 fi
136 set -e
137}