summaryrefslogtreecommitdiffstats
path: root/meta/classes/useradd.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes/useradd.bbclass')
-rw-r--r--meta/classes/useradd.bbclass213
1 files changed, 213 insertions, 0 deletions
diff --git a/meta/classes/useradd.bbclass b/meta/classes/useradd.bbclass
new file mode 100644
index 0000000000..0b9a843b24
--- /dev/null
+++ b/meta/classes/useradd.bbclass
@@ -0,0 +1,213 @@
1inherit 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 base-passwd 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 # user/group lookups should match useradd/groupadd --root
28 export PSEUDO_PASSWD="$SYSROOT:${STAGING_DIR_NATIVE}"
29fi
30
31# If we're not doing a special SSTATE/SYSROOT install
32# then set the values, otherwise use the environment
33if test "x$UA_SYSROOT" = "x"; then
34 # Installing onto a target
35 # Add groups and users defined only for this package
36 GROUPADD_PARAM="${GROUPADD_PARAM}"
37 USERADD_PARAM="${USERADD_PARAM}"
38 GROUPMEMS_PARAM="${GROUPMEMS_PARAM}"
39fi
40
41# Perform group additions first, since user additions may depend
42# on these groups existing
43if test "x$GROUPADD_PARAM" != "x"; then
44 echo "Running groupadd commands..."
45 # Invoke multiple instances of groupadd for parameter lists
46 # separated by ';'
47 opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1`
48 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2-`
49 while test "x$opts" != "x"; do
50 perform_groupadd "$SYSROOT" "$OPT $opts" 10
51 if test "x$opts" = "x$remaining"; then
52 break
53 fi
54 opts=`echo "$remaining" | cut -d ';' -f 1`
55 remaining=`echo "$remaining" | cut -d ';' -f 2-`
56 done
57fi
58
59if test "x$USERADD_PARAM" != "x"; then
60 echo "Running useradd commands..."
61 # Invoke multiple instances of useradd for parameter lists
62 # separated by ';'
63 opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1`
64 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2-`
65 while test "x$opts" != "x"; do
66 perform_useradd "$SYSROOT" "$OPT $opts" 10
67 if test "x$opts" = "x$remaining"; then
68 break
69 fi
70 opts=`echo "$remaining" | cut -d ';' -f 1`
71 remaining=`echo "$remaining" | cut -d ';' -f 2-`
72 done
73fi
74
75if test "x$GROUPMEMS_PARAM" != "x"; then
76 echo "Running groupmems commands..."
77 # Invoke multiple instances of groupmems for parameter lists
78 # separated by ';'
79 opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1`
80 remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2-`
81 while test "x$opts" != "x"; do
82 perform_groupmems "$SYSROOT" "$OPT $opts" 10
83 if test "x$opts" = "x$remaining"; then
84 break
85 fi
86 opts=`echo "$remaining" | cut -d ';' -f 1`
87 remaining=`echo "$remaining" | cut -d ';' -f 2-`
88 done
89fi
90}
91
92useradd_sysroot () {
93 # Pseudo may (do_install) or may not (do_populate_sysroot_setscene) be running
94 # at this point so we're explicit about the environment so pseudo can load if
95 # not already present.
96 export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir}/pseudo"
97
98 # Explicitly set $D since it isn't set to anything
99 # before do_install
100 D=${STAGING_DIR_TARGET}
101
102 # Add groups and users defined for all recipe packages
103 GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
104 USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
105 GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}"
106
107 # Tell the system to use the environment vars
108 UA_SYSROOT=1
109
110 useradd_preinst
111}
112
113useradd_sysroot_sstate () {
114 if [ "${BB_CURRENTTASK}" = "package_setscene" -o "${BB_CURRENTTASK}" = "populate_sysroot_setscene" ]
115 then
116 useradd_sysroot
117 fi
118}
119
120do_install[prefuncs] += "${SYSROOTFUNC}"
121SYSROOTFUNC = "useradd_sysroot"
122SYSROOTFUNC_class-cross = ""
123SYSROOTFUNC_class-native = ""
124SYSROOTFUNC_class-nativesdk = ""
125SSTATEPREINSTFUNCS += "${SYSROOTPOSTFUNC}"
126SYSROOTPOSTFUNC = "useradd_sysroot_sstate"
127SYSROOTPOSTFUNC_class-cross = ""
128SYSROOTPOSTFUNC_class-native = ""
129SYSROOTPOSTFUNC_class-nativesdk = ""
130
131USERADDSETSCENEDEPS = "${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"
132USERADDSETSCENEDEPS_class-cross = ""
133USERADDSETSCENEDEPS_class-native = ""
134USERADDSETSCENEDEPS_class-nativesdk = ""
135do_package_setscene[depends] += "${USERADDSETSCENEDEPS}"
136do_populate_sysroot_setscene[depends] += "${USERADDSETSCENEDEPS}"
137
138# Recipe parse-time sanity checks
139def update_useradd_after_parse(d):
140 useradd_packages = d.getVar('USERADD_PACKAGES', True)
141
142 if not useradd_packages:
143 raise bb.build.FuncFailed("%s inherits useradd but doesn't set USERADD_PACKAGES" % d.getVar('FILE'))
144
145 for pkg in useradd_packages.split():
146 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):
147 bb.fatal("%s inherits useradd but doesn't set USERADD_PARAM, GROUPADD_PARAM or GROUPMEMS_PARAM for package %s" % (d.getVar('FILE'), pkg))
148
149python __anonymous() {
150 if not bb.data.inherits_class('nativesdk', d) \
151 and not bb.data.inherits_class('native', d):
152 update_useradd_after_parse(d)
153}
154
155# Return a single [GROUP|USER]ADD_PARAM formatted string which includes the
156# [group|user]add parameters for all USERADD_PACKAGES in this recipe
157def get_all_cmd_params(d, cmd_type):
158 import string
159
160 param_type = cmd_type.upper() + "_PARAM_%s"
161 params = []
162
163 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
164 for pkg in useradd_packages.split():
165 param = d.getVar(param_type % pkg, True)
166 if param:
167 params.append(param)
168
169 return "; ".join(params)
170
171# Adds the preinst script into generated packages
172fakeroot python populate_packages_prepend () {
173 def update_useradd_package(pkg):
174 bb.debug(1, 'adding user/group calls to preinst for %s' % pkg)
175
176 """
177 useradd preinst is appended here because pkg_preinst may be
178 required to execute on the target. Not doing so may cause
179 useradd preinst to be invoked twice, causing unwanted warnings.
180 """
181 preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True)
182 if not preinst:
183 preinst = '#!/bin/sh\n'
184 preinst += 'bbnote () {\n%s}\n' % d.getVar('bbnote', True)
185 preinst += 'bbwarn () {\n%s}\n' % d.getVar('bbwarn', True)
186 preinst += 'bbfatal () {\n%s}\n' % d.getVar('bbfatal', True)
187 preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd', True)
188 preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd', True)
189 preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems', True)
190 preinst += d.getVar('useradd_preinst', True)
191 d.setVar('pkg_preinst_%s' % pkg, preinst)
192
193 # RDEPENDS setup
194 rdepends = d.getVar("RDEPENDS_%s" % pkg, True) or ""
195 rdepends += ' ' + d.getVar('MLPREFIX') + 'base-passwd'
196 rdepends += ' ' + d.getVar('MLPREFIX') + 'shadow'
197 # base-files is where the default /etc/skel is packaged
198 rdepends += ' ' + d.getVar('MLPREFIX') + 'base-files'
199 d.setVar("RDEPENDS_%s" % pkg, rdepends)
200
201 # Add the user/group preinstall scripts and RDEPENDS requirements
202 # to packages specified by USERADD_PACKAGES
203 if not bb.data.inherits_class('nativesdk', d) \
204 and not bb.data.inherits_class('native', d):
205 useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
206 for pkg in useradd_packages.split():
207 update_useradd_package(pkg)
208}
209
210# Use the following to extend the useradd with custom functions
211USERADDEXTENSION ?= ""
212
213inherit ${USERADDEXTENSION}