blob: 3cff08e00dfabd8ac9a90e45fe9c5f31beac96bc [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001inherit useradd_base
2
3# base-passwd-cross provides the default passwd and group files in the
4# target sysroot, and shadow -native and -sysroot provide the utilities
5# and support files needed to add and modify user and group accounts
Patrick Williamsc0f7c042017-02-23 20:41:17 -06006DEPENDS_append_class-target = " base-files shadow-native shadow-sysroot shadow"
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
8# This preinstall function can be run in four different contexts:
9#
10# a) Before do_install
11# b) At do_populate_sysroot_setscene when installing from sstate packages
12# c) As the preinst script in the target package at do_rootfs time
13# d) As the preinst script in the target package on device as a package upgrade
14#
15useradd_preinst () {
16OPT=""
17SYSROOT=""
18
19if test "x$D" != "x"; then
20 # Installing into a sysroot
21 SYSROOT="$D"
22 OPT="--root $D"
23
24 # Make sure login.defs is there, this is to make debian package backend work
25 # correctly while doing rootfs.
26 # The problem here is that if /etc/login.defs is treated as a config file for
27 # shadow package, then while performing preinsts for packages that depend on
28 # shadow, there might only be /etc/login.def.dpkg-new there in root filesystem.
29 if [ ! -e $D${sysconfdir}/login.defs -a -e $D${sysconfdir}/login.defs.dpkg-new ]; then
30 cp $D${sysconfdir}/login.defs.dpkg-new $D${sysconfdir}/login.defs
31 fi
32
33 # user/group lookups should match useradd/groupadd --root
34 export PSEUDO_PASSWD="$SYSROOT:${STAGING_DIR_NATIVE}"
35fi
36
37# If we're not doing a special SSTATE/SYSROOT install
38# then set the values, otherwise use the environment
39if test "x$UA_SYSROOT" = "x"; then
40 # Installing onto a target
41 # Add groups and users defined only for this package
42 GROUPADD_PARAM="${GROUPADD_PARAM}"
43 USERADD_PARAM="${USERADD_PARAM}"
44 GROUPMEMS_PARAM="${GROUPMEMS_PARAM}"
45fi
46
47# Perform group additions first, since user additions may depend
48# on these groups existing
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050049if test "x`echo $GROUPADD_PARAM | tr -d '[:space:]'`" != "x"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 echo "Running groupadd commands..."
51 # Invoke multiple instances of groupadd for parameter lists
52 # separated by ';'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053 opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
54 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 while test "x$opts" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050056 perform_groupadd "$SYSROOT" "$OPT $opts"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 if test "x$opts" = "x$remaining"; then
58 break
59 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -060060 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
61 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 done
63fi
64
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050065if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066 echo "Running useradd commands..."
67 # Invoke multiple instances of useradd for parameter lists
68 # separated by ';'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060069 opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
70 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 while test "x$opts" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050072 perform_useradd "$SYSROOT" "$OPT $opts"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 if test "x$opts" = "x$remaining"; then
74 break
75 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -060076 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
77 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 done
79fi
80
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050081if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 echo "Running groupmems commands..."
83 # Invoke multiple instances of groupmems for parameter lists
84 # separated by ';'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060085 opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
86 remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087 while test "x$opts" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088 perform_groupmems "$SYSROOT" "$OPT $opts"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 if test "x$opts" = "x$remaining"; then
90 break
91 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -060092 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
93 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 done
95fi
96}
97
98useradd_sysroot () {
99 # Pseudo may (do_install) or may not (do_populate_sysroot_setscene) be running
100 # at this point so we're explicit about the environment so pseudo can load if
101 # not already present.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600102 export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir_native}/pseudo"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103
104 # Explicitly set $D since it isn't set to anything
105 # before do_install
106 D=${STAGING_DIR_TARGET}
107
108 # Add groups and users defined for all recipe packages
109 GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
110 USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
111 GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}"
112
113 # Tell the system to use the environment vars
114 UA_SYSROOT=1
115
116 useradd_preinst
117}
118
119useradd_sysroot_sstate () {
120 if [ "${BB_CURRENTTASK}" = "package_setscene" -o "${BB_CURRENTTASK}" = "populate_sysroot_setscene" ]
121 then
122 useradd_sysroot
123 fi
124}
125
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500126userdel_sysroot_sstate () {
127if test "x${STAGING_DIR_TARGET}" != "x"; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600128 if [ "${BB_CURRENTTASK}" = "clean" ]; then
129 export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir_native}/pseudo"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500130 OPT="--root ${STAGING_DIR_TARGET}"
131
132 # Remove groups and users defined for package
133 GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
134 USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
135
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600136 user=`echo "$USERADD_PARAM" | cut -d ';' -f 1 | awk '{ print $NF }'`
137 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
138 while test "x$user" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500139 perform_userdel "${STAGING_DIR_TARGET}" "$OPT $user"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600140 user=`echo "$remaining" | cut -d ';' -f 1 | awk '{ print $NF }'`
141 remaining=`echo "$remaining" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
142 done
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600144 user=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1 | awk '{ print $NF }'`
145 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
146 while test "x$user" != "x"; do
147 perform_groupdel "${STAGING_DIR_TARGET}" "$OPT $user"
148 user=`echo "$remaining" | cut -d ';' -f 1 | awk '{ print $NF }'`
149 remaining=`echo "$remaining" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
150 done
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500151
152 fi
153fi
154}
155
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156SSTATECLEANFUNCS_append_class-target = " userdel_sysroot_sstate"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500157
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158do_install[prefuncs] += "${SYSROOTFUNC}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600159SYSROOTFUNC_class-target = "useradd_sysroot"
160SYSROOTFUNC = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600162SSTATEPREINSTFUNCS_append_class-target = " useradd_sysroot_sstate"
163
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164do_package_setscene[depends] += "${USERADDSETSCENEDEPS}"
165do_populate_sysroot_setscene[depends] += "${USERADDSETSCENEDEPS}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600166USERADDSETSCENEDEPS_class-target = "${MLPREFIX}base-passwd:do_populate_sysroot_setscene pseudo-native:do_populate_sysroot_setscene shadow-native:do_populate_sysroot_setscene ${MLPREFIX}shadow-sysroot:do_populate_sysroot_setscene"
167USERADDSETSCENEDEPS = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168
169# Recipe parse-time sanity checks
170def update_useradd_after_parse(d):
171 useradd_packages = d.getVar('USERADD_PACKAGES', True)
172
173 if not useradd_packages:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600174 bb.fatal("%s inherits useradd but doesn't set USERADD_PACKAGES" % d.getVar('FILE', False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500175
176 for pkg in useradd_packages.split():
177 if not d.getVar('USERADD_PARAM_%s' % pkg, True) and not d.getVar('GROUPADD_PARAM_%s' % pkg, True) and not d.getVar('GROUPMEMS_PARAM_%s' % pkg, True):
178 bb.fatal("%s inherits useradd but doesn't set USERADD_PARAM, GROUPADD_PARAM or GROUPMEMS_PARAM for package %s" % (d.getVar('FILE', False), pkg))
179
180python __anonymous() {
181 if not bb.data.inherits_class('nativesdk', d) \
182 and not bb.data.inherits_class('native', d):
183 update_useradd_after_parse(d)
184}
185
186# Return a single [GROUP|USER]ADD_PARAM formatted string which includes the
187# [group|user]add parameters for all USERADD_PACKAGES in this recipe
188def get_all_cmd_params(d, cmd_type):
189 import string
190
191 param_type = cmd_type.upper() + "_PARAM_%s"
192 params = []
193
194 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
195 for pkg in useradd_packages.split():
196 param = d.getVar(param_type % pkg, True)
197 if param:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600198 params.append(param.rstrip(" ;"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199
200 return "; ".join(params)
201
202# Adds the preinst script into generated packages
203fakeroot python populate_packages_prepend () {
204 def update_useradd_package(pkg):
205 bb.debug(1, 'adding user/group calls to preinst for %s' % pkg)
206
207 """
208 useradd preinst is appended here because pkg_preinst may be
209 required to execute on the target. Not doing so may cause
210 useradd preinst to be invoked twice, causing unwanted warnings.
211 """
212 preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True)
213 if not preinst:
214 preinst = '#!/bin/sh\n'
215 preinst += 'bbnote () {\n\techo "NOTE: $*"\n}\n'
216 preinst += 'bbwarn () {\n\techo "WARNING: $*"\n}\n'
217 preinst += 'bbfatal () {\n\techo "ERROR: $*"\n\texit 1\n}\n'
218 preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd', True)
219 preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd', True)
220 preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems', True)
221 preinst += d.getVar('useradd_preinst', True)
222 d.setVar('pkg_preinst_%s' % pkg, preinst)
223
224 # RDEPENDS setup
225 rdepends = d.getVar("RDEPENDS_%s" % pkg, True) or ""
226 rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-passwd'
227 rdepends += ' ' + d.getVar('MLPREFIX', False) + 'shadow'
228 # base-files is where the default /etc/skel is packaged
229 rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-files'
230 d.setVar("RDEPENDS_%s" % pkg, rdepends)
231
232 # Add the user/group preinstall scripts and RDEPENDS requirements
233 # to packages specified by USERADD_PACKAGES
234 if not bb.data.inherits_class('nativesdk', d) \
235 and not bb.data.inherits_class('native', d):
236 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
237 for pkg in useradd_packages.split():
238 update_useradd_package(pkg)
239}
240
241# Use the following to extend the useradd with custom functions
242USERADDEXTENSION ?= ""
243
244inherit ${USERADDEXTENSION}