xref: /OK3568_Linux_fs/yocto/poky/meta/classes/useradd_base.bbclass (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# This bbclass provides basic functionality for user/group settings.
2*4882a593Smuzhiyun# This bbclass is intended to be inherited by useradd.bbclass and
3*4882a593Smuzhiyun# extrausers.bbclass.
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun# The following functions basically have similar logic.
6*4882a593Smuzhiyun# *) Perform necessary checks before invoking the actual command
7*4882a593Smuzhiyun# *) Invoke the actual command with flock
8*4882a593Smuzhiyun# *) Error out if an error occurs.
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun# Note that before invoking these functions, make sure the global variable
11*4882a593Smuzhiyun# PSEUDO is set up correctly.
12*4882a593Smuzhiyun
13*4882a593Smuzhiyunperform_groupadd () {
14*4882a593Smuzhiyun	local rootdir="$1"
15*4882a593Smuzhiyun	local opts="$2"
16*4882a593Smuzhiyun	bbnote "${PN}: Performing groupadd with [$opts]"
17*4882a593Smuzhiyun	local groupname=`echo "$opts" | awk '{ print $NF }'`
18*4882a593Smuzhiyun	local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
19*4882a593Smuzhiyun	if test "x$group_exists" = "x"; then
20*4882a593Smuzhiyun		eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupadd \$opts\" || true
21*4882a593Smuzhiyun		group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
22*4882a593Smuzhiyun		if test "x$group_exists" = "x"; then
23*4882a593Smuzhiyun			bbfatal "${PN}: groupadd command did not succeed."
24*4882a593Smuzhiyun		fi
25*4882a593Smuzhiyun	else
26*4882a593Smuzhiyun		bbnote "${PN}: group $groupname already exists, not re-creating it"
27*4882a593Smuzhiyun	fi
28*4882a593Smuzhiyun}
29*4882a593Smuzhiyun
30*4882a593Smuzhiyunperform_useradd () {
31*4882a593Smuzhiyun	local rootdir="$1"
32*4882a593Smuzhiyun	local opts="$2"
33*4882a593Smuzhiyun	bbnote "${PN}: Performing useradd with [$opts]"
34*4882a593Smuzhiyun	local username=`echo "$opts" | awk '{ print $NF }'`
35*4882a593Smuzhiyun	local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
36*4882a593Smuzhiyun	if test "x$user_exists" = "x"; then
37*4882a593Smuzhiyun		eval flock -x $rootdir${sysconfdir} -c  \"$PSEUDO useradd \$opts\" || true
38*4882a593Smuzhiyun		user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
39*4882a593Smuzhiyun		if test "x$user_exists" = "x"; then
40*4882a593Smuzhiyun			bbfatal "${PN}: useradd command did not succeed."
41*4882a593Smuzhiyun		fi
42*4882a593Smuzhiyun	else
43*4882a593Smuzhiyun		bbnote "${PN}: user $username already exists, not re-creating it"
44*4882a593Smuzhiyun	fi
45*4882a593Smuzhiyun}
46*4882a593Smuzhiyun
47*4882a593Smuzhiyunperform_groupmems () {
48*4882a593Smuzhiyun	local rootdir="$1"
49*4882a593Smuzhiyun	local opts="$2"
50*4882a593Smuzhiyun	bbnote "${PN}: Performing groupmems with [$opts]"
51*4882a593Smuzhiyun	local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'`
52*4882a593Smuzhiyun	local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'`
53*4882a593Smuzhiyun	bbnote "${PN}: Running groupmems command with group $groupname and user $username"
54*4882a593Smuzhiyun	local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*$" $rootdir/etc/group || true`"
55*4882a593Smuzhiyun	if test "x$mem_exists" = "x"; then
56*4882a593Smuzhiyun		eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmems \$opts\" || true
57*4882a593Smuzhiyun		mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*$" $rootdir/etc/group || true`"
58*4882a593Smuzhiyun		if test "x$mem_exists" = "x"; then
59*4882a593Smuzhiyun			bbfatal "${PN}: groupmems command did not succeed."
60*4882a593Smuzhiyun		fi
61*4882a593Smuzhiyun	else
62*4882a593Smuzhiyun		bbnote "${PN}: group $groupname already contains $username, not re-adding it"
63*4882a593Smuzhiyun	fi
64*4882a593Smuzhiyun}
65*4882a593Smuzhiyun
66*4882a593Smuzhiyunperform_groupdel () {
67*4882a593Smuzhiyun	local rootdir="$1"
68*4882a593Smuzhiyun	local opts="$2"
69*4882a593Smuzhiyun	bbnote "${PN}: Performing groupdel with [$opts]"
70*4882a593Smuzhiyun	local groupname=`echo "$opts" | awk '{ print $NF }'`
71*4882a593Smuzhiyun	local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun	if test "x$group_exists" != "x"; then
74*4882a593Smuzhiyun		local awk_input='BEGIN {FS=":"}; $1=="'$groupname'" { print $3 }'
75*4882a593Smuzhiyun		local groupid=`echo "$awk_input" | awk -f- $rootdir/etc/group`
76*4882a593Smuzhiyun		local awk_check_users='BEGIN {FS=":"}; $4=="'$groupid'" {print $1}'
77*4882a593Smuzhiyun		local other_users=`echo "$awk_check_users" | awk -f- $rootdir/etc/passwd`
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun		if test "x$other_users" = "x"; then
80*4882a593Smuzhiyun			eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupdel \$opts\" || true
81*4882a593Smuzhiyun			group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
82*4882a593Smuzhiyun			if test "x$group_exists" != "x"; then
83*4882a593Smuzhiyun				bbfatal "${PN}: groupdel command did not succeed."
84*4882a593Smuzhiyun			fi
85*4882a593Smuzhiyun		else
86*4882a593Smuzhiyun			bbnote "${PN}: '$groupname' is primary group for users '$other_users', not removing it"
87*4882a593Smuzhiyun		fi
88*4882a593Smuzhiyun	else
89*4882a593Smuzhiyun		bbnote "${PN}: group $groupname doesn't exist, not removing it"
90*4882a593Smuzhiyun	fi
91*4882a593Smuzhiyun}
92*4882a593Smuzhiyun
93*4882a593Smuzhiyunperform_userdel () {
94*4882a593Smuzhiyun	local rootdir="$1"
95*4882a593Smuzhiyun	local opts="$2"
96*4882a593Smuzhiyun	bbnote "${PN}: Performing userdel with [$opts]"
97*4882a593Smuzhiyun	local username=`echo "$opts" | awk '{ print $NF }'`
98*4882a593Smuzhiyun	local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
99*4882a593Smuzhiyun	if test "x$user_exists" != "x"; then
100*4882a593Smuzhiyun		eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO userdel \$opts\" || true
101*4882a593Smuzhiyun		user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
102*4882a593Smuzhiyun		if test "x$user_exists" != "x"; then
103*4882a593Smuzhiyun			bbfatal "${PN}: userdel command did not succeed."
104*4882a593Smuzhiyun		fi
105*4882a593Smuzhiyun	else
106*4882a593Smuzhiyun		bbnote "${PN}: user $username doesn't exist, not removing it"
107*4882a593Smuzhiyun	fi
108*4882a593Smuzhiyun}
109*4882a593Smuzhiyun
110*4882a593Smuzhiyunperform_groupmod () {
111*4882a593Smuzhiyun	# Other than the return value of groupmod, there's no simple way to judge whether the command
112*4882a593Smuzhiyun	# succeeds, so we disable -e option temporarily
113*4882a593Smuzhiyun	set +e
114*4882a593Smuzhiyun	local rootdir="$1"
115*4882a593Smuzhiyun	local opts="$2"
116*4882a593Smuzhiyun	bbnote "${PN}: Performing groupmod with [$opts]"
117*4882a593Smuzhiyun	local groupname=`echo "$opts" | awk '{ print $NF }'`
118*4882a593Smuzhiyun	local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
119*4882a593Smuzhiyun	if test "x$group_exists" != "x"; then
120*4882a593Smuzhiyun		eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmod \$opts\"
121*4882a593Smuzhiyun		if test $? != 0; then
122*4882a593Smuzhiyun			bbwarn "${PN}: groupmod command did not succeed."
123*4882a593Smuzhiyun		fi
124*4882a593Smuzhiyun	else
125*4882a593Smuzhiyun		bbwarn "${PN}: group $groupname doesn't exist, unable to modify it"
126*4882a593Smuzhiyun	fi
127*4882a593Smuzhiyun	set -e
128*4882a593Smuzhiyun}
129*4882a593Smuzhiyun
130*4882a593Smuzhiyunperform_usermod () {
131*4882a593Smuzhiyun	# Same reason with groupmod, temporarily disable -e option
132*4882a593Smuzhiyun	set +e
133*4882a593Smuzhiyun	local rootdir="$1"
134*4882a593Smuzhiyun	local opts="$2"
135*4882a593Smuzhiyun	bbnote "${PN}: Performing usermod with [$opts]"
136*4882a593Smuzhiyun	local username=`echo "$opts" | awk '{ print $NF }'`
137*4882a593Smuzhiyun	local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
138*4882a593Smuzhiyun	if test "x$user_exists" != "x"; then
139*4882a593Smuzhiyun		eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO usermod \$opts\"
140*4882a593Smuzhiyun		if test $? != 0; then
141*4882a593Smuzhiyun			bbfatal "${PN}: usermod command did not succeed."
142*4882a593Smuzhiyun		fi
143*4882a593Smuzhiyun	else
144*4882a593Smuzhiyun		bbwarn "${PN}: user $username doesn't exist, unable to modify it"
145*4882a593Smuzhiyun	fi
146*4882a593Smuzhiyun	set -e
147*4882a593Smuzhiyun}
148*4882a593Smuzhiyun
149*4882a593Smuzhiyunperform_passwd_expire () {
150*4882a593Smuzhiyun	local rootdir="$1"
151*4882a593Smuzhiyun	local opts="$2"
152*4882a593Smuzhiyun	bbnote "${PN}: Performing equivalent of passwd --expire with [$opts]"
153*4882a593Smuzhiyun	# Directly set sp_lstchg to 0 without using the passwd command: Only root can do that
154*4882a593Smuzhiyun	local username=`echo "$opts" | awk '{ print $NF }'`
155*4882a593Smuzhiyun	local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
156*4882a593Smuzhiyun	if test "x$user_exists" != "x"; then
157*4882a593Smuzhiyun		eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO sed -i \''s/^\('$username':[^:]*\):[^:]*:/\1:0:/'\' $rootdir/etc/shadow \" || true
158*4882a593Smuzhiyun		local passwd_lastchanged="`grep "^$username:" $rootdir/etc/shadow | cut -d: -f3`"
159*4882a593Smuzhiyun		if test "x$passwd_lastchanged" != "x0"; then
160*4882a593Smuzhiyun			bbfatal "${PN}: passwd --expire operation did not succeed."
161*4882a593Smuzhiyun		fi
162*4882a593Smuzhiyun	else
163*4882a593Smuzhiyun		bbnote "${PN}: user $username doesn't exist, not expiring its password"
164*4882a593Smuzhiyun	fi
165*4882a593Smuzhiyun}
166