[jethro] Fix yocto bug in useradd calls
[AGL/meta-agl.git] / meta-agl / classes / useradd.bbclass
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-files shadow-native shadow-sysroot shadow"
8 USERADDDEPENDS_class-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         # 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}"
39 fi
40
41 # If we're not doing a special SSTATE/SYSROOT install
42 # then set the values, otherwise use the environment
43 if 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}"
49 fi
50
51 # Perform group additions first, since user additions may depend
52 # on these groups existing
53 if test "x$GROUPADD_PARAM" != "x"; then
54         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
60                 perform_groupadd "$SYSROOT" "$OPT $opts"
61                 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
67 fi 
68
69 if test "x$USERADD_PARAM" != "x"; then
70         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
76                 perform_useradd "$SYSROOT" "$OPT $opts"
77                 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
83 fi
84
85 if test "x$GROUPMEMS_PARAM" != "x"; then
86         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
92                 perform_groupmems "$SYSROOT" "$OPT $opts"
93                 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
99 fi
100 }
101
102 useradd_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
123 useradd_sysroot_sstate () {
124         if [ "${BB_CURRENTTASK}" = "package_setscene" -o "${BB_CURRENTTASK}" = "populate_sysroot_setscene" ]
125         then
126                 useradd_sysroot
127         fi
128 }
129
130 do_install[prefuncs] += "${SYSROOTFUNC}"
131 SYSROOTFUNC = "useradd_sysroot"
132 SYSROOTFUNC_class-cross = ""
133 SYSROOTFUNC_class-native = ""
134 SYSROOTFUNC_class-nativesdk = ""
135 SSTATEPREINSTFUNCS += "${SYSROOTPOSTFUNC}"
136 SYSROOTPOSTFUNC = "useradd_sysroot_sstate"
137 SYSROOTPOSTFUNC_class-cross = ""
138 SYSROOTPOSTFUNC_class-native = ""
139 SYSROOTPOSTFUNC_class-nativesdk = ""
140
141 USERADDSETSCENEDEPS = "${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"
142 USERADDSETSCENEDEPS_class-cross = ""
143 USERADDSETSCENEDEPS_class-native = ""
144 USERADDSETSCENEDEPS_class-nativesdk = ""
145 do_package_setscene[depends] += "${USERADDSETSCENEDEPS}"
146 do_populate_sysroot_setscene[depends] += "${USERADDSETSCENEDEPS}"
147
148 # Recipe parse-time sanity checks
149 def update_useradd_after_parse(d):
150     useradd_packages = d.getVar('USERADD_PACKAGES', True)
151
152     if not useradd_packages:
153         raise bb.build.FuncFailed("%s inherits useradd but doesn't set USERADD_PACKAGES" % d.getVar('FILE', False))
154
155     for pkg in useradd_packages.split():
156         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):
157             bb.fatal("%s inherits useradd but doesn't set USERADD_PARAM, GROUPADD_PARAM or GROUPMEMS_PARAM for package %s" % (d.getVar('FILE', False), pkg))
158
159 python __anonymous() {
160     if not bb.data.inherits_class('nativesdk', d) \
161         and not bb.data.inherits_class('native', d):
162         update_useradd_after_parse(d)
163 }
164
165 # Return a single [GROUP|USER]ADD_PARAM formatted string which includes the
166 # [group|user]add parameters for all USERADD_PACKAGES in this recipe
167 def get_all_cmd_params(d, cmd_type):
168     import string
169     
170     param_type = cmd_type.upper() + "_PARAM_%s"
171     params = []
172
173     useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
174     for pkg in useradd_packages.split():
175         param = d.getVar(param_type % pkg, True)
176         if param:
177             params.append(param)
178
179     return "; ".join(params)
180
181 # Adds the preinst script into generated packages
182 fakeroot python populate_packages_prepend () {
183     def update_useradd_package(pkg):
184         bb.debug(1, 'adding user/group calls to preinst for %s' % pkg)
185
186         """
187         useradd preinst is appended here because pkg_preinst may be
188         required to execute on the target. Not doing so may cause
189         useradd preinst to be invoked twice, causing unwanted warnings.
190         """
191         preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True)
192         if not preinst:
193             preinst = '#!/bin/sh\n'
194         preinst += 'bbnote () {\n\techo "NOTE: $*"\n}\n'
195         preinst += 'bbwarn () {\n\techo "WARNING: $*"\n}\n'
196         preinst += 'bbfatal () {\n\techo "ERROR: $*"\n\texit 1\n}\n'
197         preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd', True)
198         preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd', True)
199         preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems', True)
200         preinst += d.getVar('useradd_preinst', True)
201         d.setVar('pkg_preinst_%s' % pkg, preinst)
202
203         # RDEPENDS setup
204         rdepends = d.getVar("RDEPENDS_%s" % pkg, True) or ""
205         rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-passwd'
206         rdepends += ' ' + d.getVar('MLPREFIX', False) + 'shadow'
207         # base-files is where the default /etc/skel is packaged
208         rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-files'
209         d.setVar("RDEPENDS_%s" % pkg, rdepends)
210
211     # Add the user/group preinstall scripts and RDEPENDS requirements
212     # to packages specified by USERADD_PACKAGES
213     if not bb.data.inherits_class('nativesdk', d) \
214         and not bb.data.inherits_class('native', d):
215         useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
216         for pkg in useradd_packages.split():
217             update_useradd_package(pkg)
218 }
219
220 # Use the following to extend the useradd with custom functions
221 USERADDEXTENSION ?= ""
222
223 inherit ${USERADDEXTENSION}