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