blob: ee402acef1baad5824e6b8fd43cf46a271bc19de [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
6DEPENDS_append = "${USERADDDEPENDS}"
7USERADDDEPENDS = " base-files shadow-native shadow-sysroot shadow"
8USERADDDEPENDS_class-cross = ""
9USERADDDEPENDS_class-native = ""
10USERADDDEPENDS_class-nativesdk = ""
11
12# This preinstall function can be run in four different contexts:
13#
14# a) Before do_install
15# b) At do_populate_sysroot_setscene when installing from sstate packages
16# c) As the preinst script in the target package at do_rootfs time
17# d) As the preinst script in the target package on device as a package upgrade
18#
19useradd_preinst () {
20OPT=""
21SYSROOT=""
22
23if test "x$D" != "x"; then
24 # Installing into a sysroot
25 SYSROOT="$D"
26 OPT="--root $D"
27
28 # Make sure login.defs is there, this is to make debian package backend work
29 # correctly while doing rootfs.
30 # The problem here is that if /etc/login.defs is treated as a config file for
31 # shadow package, then while performing preinsts for packages that depend on
32 # shadow, there might only be /etc/login.def.dpkg-new there in root filesystem.
33 if [ ! -e $D${sysconfdir}/login.defs -a -e $D${sysconfdir}/login.defs.dpkg-new ]; then
34 cp $D${sysconfdir}/login.defs.dpkg-new $D${sysconfdir}/login.defs
35 fi
36
37 # user/group lookups should match useradd/groupadd --root
38 export PSEUDO_PASSWD="$SYSROOT:${STAGING_DIR_NATIVE}"
39fi
40
41# If we're not doing a special SSTATE/SYSROOT install
42# then set the values, otherwise use the environment
43if test "x$UA_SYSROOT" = "x"; then
44 # Installing onto a target
45 # Add groups and users defined only for this package
46 GROUPADD_PARAM="${GROUPADD_PARAM}"
47 USERADD_PARAM="${USERADD_PARAM}"
48 GROUPMEMS_PARAM="${GROUPMEMS_PARAM}"
49fi
50
51# Perform group additions first, since user additions may depend
52# on these groups existing
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053if test "x`echo $GROUPADD_PARAM | tr -d '[:space:]'`" != "x"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054 echo "Running groupadd commands..."
55 # Invoke multiple instances of groupadd for parameter lists
56 # separated by ';'
57 opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1`
58 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2-`
59 while test "x$opts" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050060 perform_groupadd "$SYSROOT" "$OPT $opts"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 if test "x$opts" = "x$remaining"; then
62 break
63 fi
64 opts=`echo "$remaining" | cut -d ';' -f 1`
65 remaining=`echo "$remaining" | cut -d ';' -f 2-`
66 done
67fi
68
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050069if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 echo "Running useradd commands..."
71 # Invoke multiple instances of useradd for parameter lists
72 # separated by ';'
73 opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1`
74 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2-`
75 while test "x$opts" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050076 perform_useradd "$SYSROOT" "$OPT $opts"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 if test "x$opts" = "x$remaining"; then
78 break
79 fi
80 opts=`echo "$remaining" | cut -d ';' -f 1`
81 remaining=`echo "$remaining" | cut -d ';' -f 2-`
82 done
83fi
84
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050085if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086 echo "Running groupmems commands..."
87 # Invoke multiple instances of groupmems for parameter lists
88 # separated by ';'
89 opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1`
90 remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2-`
91 while test "x$opts" != "x"; do
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050092 perform_groupmems "$SYSROOT" "$OPT $opts"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 if test "x$opts" = "x$remaining"; then
94 break
95 fi
96 opts=`echo "$remaining" | cut -d ';' -f 1`
97 remaining=`echo "$remaining" | cut -d ';' -f 2-`
98 done
99fi
100}
101
102useradd_sysroot () {
103 # Pseudo may (do_install) or may not (do_populate_sysroot_setscene) be running
104 # at this point so we're explicit about the environment so pseudo can load if
105 # not already present.
106 export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir}/pseudo"
107
108 # Explicitly set $D since it isn't set to anything
109 # before do_install
110 D=${STAGING_DIR_TARGET}
111
112 # Add groups and users defined for all recipe packages
113 GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
114 USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
115 GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}"
116
117 # Tell the system to use the environment vars
118 UA_SYSROOT=1
119
120 useradd_preinst
121}
122
123useradd_sysroot_sstate () {
124 if [ "${BB_CURRENTTASK}" = "package_setscene" -o "${BB_CURRENTTASK}" = "populate_sysroot_setscene" ]
125 then
126 useradd_sysroot
127 fi
128}
129
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500130userdel_sysroot_sstate () {
131if test "x${STAGING_DIR_TARGET}" != "x"; then
132 if [ "${BB_CURRENTTASK}" = "configure" -o "${BB_CURRENTTASK}" = "clean" ]; then
133 export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir}/pseudo"
134 OPT="--root ${STAGING_DIR_TARGET}"
135
136 # Remove groups and users defined for package
137 GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
138 USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
139
140 if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then
141 user=`echo "$USERADD_PARAM" | cut -d ';' -f 1 | awk '{ print $NF }'`
142 perform_userdel "${STAGING_DIR_TARGET}" "$OPT $user"
143 fi
144
145 if test "x`echo $GROUPADD_PARAM | tr -d '[:space:]'`" != "x"; then
146 group=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1 | awk '{ print $NF }'`
147 perform_groupdel "${STAGING_DIR_TARGET}" "$OPT $group"
148 fi
149
150 fi
151fi
152}
153
154SSTATECLEANFUNCS = "userdel_sysroot_sstate"
155SSTATECLEANFUNCS_class-cross = ""
156SSTATECLEANFUNCS_class-native = ""
157SSTATECLEANFUNCS_class-nativesdk = ""
158
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159do_install[prefuncs] += "${SYSROOTFUNC}"
160SYSROOTFUNC = "useradd_sysroot"
161SYSROOTFUNC_class-cross = ""
162SYSROOTFUNC_class-native = ""
163SYSROOTFUNC_class-nativesdk = ""
164SSTATEPREINSTFUNCS += "${SYSROOTPOSTFUNC}"
165SYSROOTPOSTFUNC = "useradd_sysroot_sstate"
166SYSROOTPOSTFUNC_class-cross = ""
167SYSROOTPOSTFUNC_class-native = ""
168SYSROOTPOSTFUNC_class-nativesdk = ""
169
170USERADDSETSCENEDEPS = "${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"
171USERADDSETSCENEDEPS_class-cross = ""
172USERADDSETSCENEDEPS_class-native = ""
173USERADDSETSCENEDEPS_class-nativesdk = ""
174do_package_setscene[depends] += "${USERADDSETSCENEDEPS}"
175do_populate_sysroot_setscene[depends] += "${USERADDSETSCENEDEPS}"
176
177# Recipe parse-time sanity checks
178def update_useradd_after_parse(d):
179 useradd_packages = d.getVar('USERADD_PACKAGES', True)
180
181 if not useradd_packages:
182 raise bb.build.FuncFailed("%s inherits useradd but doesn't set USERADD_PACKAGES" % d.getVar('FILE', False))
183
184 for pkg in useradd_packages.split():
185 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):
186 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
202 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
203 for pkg in useradd_packages.split():
204 param = d.getVar(param_type % pkg, True)
205 if param:
206 params.append(param)
207
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 """
220 preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True)
221 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'
226 preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd', True)
227 preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd', True)
228 preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems', True)
229 preinst += d.getVar('useradd_preinst', True)
230 d.setVar('pkg_preinst_%s' % pkg, preinst)
231
232 # RDEPENDS setup
233 rdepends = d.getVar("RDEPENDS_%s" % pkg, True) or ""
234 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):
244 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
245 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}