blob: 124becd082420ebff09cdf27f2c36512f5227dc9 [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
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006DEPENDS_append_class-target = " base-files shadow-native shadow-sysroot shadow base-passwd"
7PACKAGE_WRITE_DEPS += "shadow-native"
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008
9# This preinstall function can be run in four different contexts:
10#
11# a) Before do_install
12# b) At do_populate_sysroot_setscene when installing from sstate packages
13# c) As the preinst script in the target package at do_rootfs time
14# d) As the preinst script in the target package on device as a package upgrade
15#
16useradd_preinst () {
17OPT=""
18SYSROOT=""
19
20if test "x$D" != "x"; then
21 # Installing into a sysroot
22 SYSROOT="$D"
23 OPT="--root $D"
24
25 # Make sure login.defs is there, this is to make debian package backend work
26 # correctly while doing rootfs.
27 # The problem here is that if /etc/login.defs is treated as a config file for
28 # shadow package, then while performing preinsts for packages that depend on
29 # shadow, there might only be /etc/login.def.dpkg-new there in root filesystem.
30 if [ ! -e $D${sysconfdir}/login.defs -a -e $D${sysconfdir}/login.defs.dpkg-new ]; then
31 cp $D${sysconfdir}/login.defs.dpkg-new $D${sysconfdir}/login.defs
32 fi
33
34 # user/group lookups should match useradd/groupadd --root
Brad Bishop6e60e8b2018-02-01 10:27:11 -050035 export PSEUDO_PASSWD="$SYSROOT"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036fi
37
38# If we're not doing a special SSTATE/SYSROOT install
39# then set the values, otherwise use the environment
40if test "x$UA_SYSROOT" = "x"; then
41 # Installing onto a target
42 # Add groups and users defined only for this package
43 GROUPADD_PARAM="${GROUPADD_PARAM}"
44 USERADD_PARAM="${USERADD_PARAM}"
45 GROUPMEMS_PARAM="${GROUPMEMS_PARAM}"
46fi
47
48# Perform group additions first, since user additions may depend
49# on these groups existing
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050050if test "x`echo $GROUPADD_PARAM | tr -d '[:space:]'`" != "x"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 echo "Running groupadd commands..."
52 # Invoke multiple instances of groupadd for parameter lists
53 # separated by ';'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060054 opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
55 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 while test "x$opts" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050057 perform_groupadd "$SYSROOT" "$OPT $opts"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 if test "x$opts" = "x$remaining"; then
59 break
60 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -060061 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
62 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 done
64fi
65
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050066if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067 echo "Running useradd commands..."
68 # Invoke multiple instances of useradd for parameter lists
69 # separated by ';'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060070 opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
71 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072 while test "x$opts" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050073 perform_useradd "$SYSROOT" "$OPT $opts"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 if test "x$opts" = "x$remaining"; then
75 break
76 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -060077 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
78 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 done
80fi
81
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050082if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 echo "Running groupmems commands..."
84 # Invoke multiple instances of groupmems for parameter lists
85 # separated by ';'
Patrick Williamsc0f7c042017-02-23 20:41:17 -060086 opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
87 remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088 while test "x$opts" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050089 perform_groupmems "$SYSROOT" "$OPT $opts"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090 if test "x$opts" = "x$remaining"; then
91 break
92 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -060093 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
94 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 done
96fi
97}
98
99useradd_sysroot () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 # Pseudo may (do_prepare_recipe_sysroot) or may not (do_populate_sysroot_setscene) be running
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 # at this point so we're explicit about the environment so pseudo can load if
102 # not already present.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 export PSEUDO="${FAKEROOTENV} ${PSEUDO_SYSROOT}${bindir_native}/pseudo"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
105 # Explicitly set $D since it isn't set to anything
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 # before do_prepare_recipe_sysroot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 D=${STAGING_DIR_TARGET}
108
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 # base-passwd's postinst may not have run yet in which case we'll get called later, just exit.
110 # Beware that in some cases we might see the fake pseudo passwd here, in which case we also must
111 # exit.
112 if [ ! -f $D${sysconfdir}/passwd ] ||
113 grep -q this-is-the-pseudo-passwd $D${sysconfdir}/passwd; then
114 exit 0
115 fi
116
117 # It is also possible we may be in a recipe which doesn't have useradd dependencies and hence the
118 # useradd/groupadd tools are unavailable. If there is no dependency, we assume we don't want to
119 # create users in the sysroot
120 if ! command -v useradd; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500121 bbwarn "command useradd not found!"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 exit 0
123 fi
124
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 # Add groups and users defined for all recipe packages
126 GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
127 USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
128 GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}"
129
130 # Tell the system to use the environment vars
131 UA_SYSROOT=1
132
133 useradd_preinst
134}
135
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500136# The export of PSEUDO in useradd_sysroot() above contains references to
137# ${COMPONENTS_DIR} and ${PSEUDO_LOCALSTATEDIR}. Additionally, the logging
138# shell functions use ${LOGFIFO}. These need to be handled when restoring
139# postinst-useradd-${PN} from the sstate cache.
140EXTRA_STAGING_FIXMES += "COMPONENTS_DIR PSEUDO_LOCALSTATEDIR LOGFIFO"
141
142python useradd_sysroot_sstate () {
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143 scriptfile = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500144 task = d.getVar("BB_CURRENTTASK")
145 if task == "package_setscene":
146 bb.build.exec_func("useradd_sysroot", d)
147 elif task == "prepare_recipe_sysroot":
148 # Used to update this recipe's own sysroot so the user/groups are available to do_install
149 scriptfile = d.expand("${RECIPE_SYSROOT}${bindir}/postinst-useradd-${PN}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150 bb.build.exec_func("useradd_sysroot", d)
151 elif task == "populate_sysroot":
152 # Used when installed in dependent task sysroots
153 scriptfile = d.expand("${SYSROOT_DESTDIR}${bindir}/postinst-useradd-${PN}")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500154
155 if scriptfile:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156 bb.utils.mkdirhier(os.path.dirname(scriptfile))
157 with open(scriptfile, 'w') as script:
158 script.write("#!/bin/sh\n")
159 bb.data.emit_func("useradd_sysroot", script, d)
160 script.write("useradd_sysroot\n")
161 os.chmod(scriptfile, 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162}
163
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164do_prepare_recipe_sysroot[postfuncs] += "${SYSROOTFUNC}"
165SYSROOTFUNC_class-target = "useradd_sysroot_sstate"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600166SYSROOTFUNC = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500168SYSROOT_PREPROCESS_FUNCS += "${SYSROOTFUNC}"
169
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600170SSTATEPREINSTFUNCS_append_class-target = " useradd_sysroot_sstate"
171
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172do_package_setscene[depends] += "${USERADDSETSCENEDEPS}"
173do_populate_sysroot_setscene[depends] += "${USERADDSETSCENEDEPS}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600174USERADDSETSCENEDEPS_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"
175USERADDSETSCENEDEPS = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176
177# Recipe parse-time sanity checks
178def update_useradd_after_parse(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500179 useradd_packages = d.getVar('USERADD_PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180
181 if not useradd_packages:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600182 bb.fatal("%s inherits useradd but doesn't set USERADD_PACKAGES" % d.getVar('FILE', False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183
184 for pkg in useradd_packages.split():
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500185 if not d.getVar('USERADD_PARAM_%s' % pkg) and not d.getVar('GROUPADD_PARAM_%s' % pkg) and not d.getVar('GROUPMEMS_PARAM_%s' % pkg):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186 bb.fatal("%s inherits useradd but doesn't set USERADD_PARAM, GROUPADD_PARAM or GROUPMEMS_PARAM for package %s" % (d.getVar('FILE', False), pkg))
187
188python __anonymous() {
189 if not bb.data.inherits_class('nativesdk', d) \
190 and not bb.data.inherits_class('native', d):
191 update_useradd_after_parse(d)
192}
193
194# Return a single [GROUP|USER]ADD_PARAM formatted string which includes the
195# [group|user]add parameters for all USERADD_PACKAGES in this recipe
196def get_all_cmd_params(d, cmd_type):
197 import string
198
199 param_type = cmd_type.upper() + "_PARAM_%s"
200 params = []
201
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500202 useradd_packages = d.getVar('USERADD_PACKAGES') or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 for pkg in useradd_packages.split():
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500204 param = d.getVar(param_type % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 if param:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600206 params.append(param.rstrip(" ;"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500207
208 return "; ".join(params)
209
210# Adds the preinst script into generated packages
211fakeroot python populate_packages_prepend () {
212 def update_useradd_package(pkg):
213 bb.debug(1, 'adding user/group calls to preinst for %s' % pkg)
214
215 """
216 useradd preinst is appended here because pkg_preinst may be
217 required to execute on the target. Not doing so may cause
218 useradd preinst to be invoked twice, causing unwanted warnings.
219 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500220 preinst = d.getVar('pkg_preinst_%s' % pkg) or d.getVar('pkg_preinst')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 if not preinst:
222 preinst = '#!/bin/sh\n'
223 preinst += 'bbnote () {\n\techo "NOTE: $*"\n}\n'
224 preinst += 'bbwarn () {\n\techo "WARNING: $*"\n}\n'
225 preinst += 'bbfatal () {\n\techo "ERROR: $*"\n\texit 1\n}\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500226 preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd')
227 preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd')
228 preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems')
229 preinst += d.getVar('useradd_preinst')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 d.setVar('pkg_preinst_%s' % pkg, preinst)
231
232 # RDEPENDS setup
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500233 rdepends = d.getVar("RDEPENDS_%s" % pkg) or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234 rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-passwd'
235 rdepends += ' ' + d.getVar('MLPREFIX', False) + 'shadow'
236 # base-files is where the default /etc/skel is packaged
237 rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-files'
238 d.setVar("RDEPENDS_%s" % pkg, rdepends)
239
240 # Add the user/group preinstall scripts and RDEPENDS requirements
241 # to packages specified by USERADD_PACKAGES
242 if not bb.data.inherits_class('nativesdk', d) \
243 and not bb.data.inherits_class('native', d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500244 useradd_packages = d.getVar('USERADD_PACKAGES') or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500245 for pkg in useradd_packages.split():
246 update_useradd_package(pkg)
247}
248
249# Use the following to extend the useradd with custom functions
250USERADDEXTENSION ?= ""
251
252inherit ${USERADDEXTENSION}