Update useradd overlays in meta-agl
[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_class-target = " base-files shadow-native shadow-sysroot shadow"
7
8 # This preinstall function can be run in four different contexts:
9 #
10 # a) Before do_install
11 # b) At do_populate_sysroot_setscene when installing from sstate packages
12 # c) As the preinst script in the target package at do_rootfs time
13 # d) As the preinst script in the target package on device as a package upgrade
14 #
15 useradd_preinst () {
16 OPT=""
17 SYSROOT=""
18
19 if test "x$D" != "x"; then
20         # Installing into a sysroot
21         SYSROOT="$D"
22         OPT="--root $D"
23
24         # Make sure login.defs is there, this is to make debian package backend work
25         # correctly while doing rootfs.
26         # The problem here is that if /etc/login.defs is treated as a config file for
27         # shadow package, then while performing preinsts for packages that depend on
28         # shadow, there might only be /etc/login.def.dpkg-new there in root filesystem.
29         if [ ! -e $D${sysconfdir}/login.defs -a -e $D${sysconfdir}/login.defs.dpkg-new ]; then
30             cp $D${sysconfdir}/login.defs.dpkg-new $D${sysconfdir}/login.defs
31         fi
32
33         # user/group lookups should match useradd/groupadd --root
34         export PSEUDO_PASSWD="$SYSROOT:${STAGING_DIR_NATIVE}"
35 fi
36
37 # If we're not doing a special SSTATE/SYSROOT install
38 # then set the values, otherwise use the environment
39 if test "x$UA_SYSROOT" = "x"; then
40         # Installing onto a target
41         # Add groups and users defined only for this package
42         GROUPADD_PARAM="${GROUPADD_PARAM}"
43         USERADD_PARAM="${USERADD_PARAM}"
44         GROUPMEMS_PARAM="${GROUPMEMS_PARAM}"
45 fi
46
47 # Perform group additions first, since user additions may depend
48 # on these groups existing
49 if test "x`echo $GROUPADD_PARAM | tr -d '[:space:]'`" != "x"; then
50         echo "Running groupadd commands..."
51         # Invoke multiple instances of groupadd for parameter lists
52         # separated by ';'
53         opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
54         remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
55         while test "x$opts" != "x"; do
56                 perform_groupadd "$SYSROOT" "$OPT $opts"
57                 if test "x$opts" = "x$remaining"; then
58                         break
59                 fi
60                 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
61                 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
62         done
63 fi 
64
65 if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then
66         echo "Running useradd commands..."
67         # Invoke multiple instances of useradd for parameter lists
68         # separated by ';'
69         opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
70         remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
71         while test "x$opts" != "x"; do
72                 perform_useradd "$SYSROOT" "$OPT $opts"
73                 if test "x$opts" = "x$remaining"; then
74                         break
75                 fi
76                 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
77                 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
78         done
79 fi
80
81 if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then
82         echo "Running groupmems commands..."
83         # Invoke multiple instances of groupmems for parameter lists
84         # separated by ';'
85         opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
86         remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
87         while test "x$opts" != "x"; do
88                 perform_groupmems "$SYSROOT" "$OPT $opts"
89                 if test "x$opts" = "x$remaining"; then
90                         break
91                 fi
92                 opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
93                 remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
94         done
95 fi
96 }
97
98 useradd_sysroot () {
99         # Pseudo may (do_install) or may not (do_populate_sysroot_setscene) be running 
100         # at this point so we're explicit about the environment so pseudo can load if 
101         # not already present.
102         export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir_native}/pseudo"
103
104         # Explicitly set $D since it isn't set to anything
105         # before do_install
106         D=${STAGING_DIR_TARGET}
107
108         # Add groups and users defined for all recipe packages
109         GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
110         USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
111         GROUPMEMS_PARAM="${@get_all_cmd_params(d, 'groupmems')}"
112
113         # Tell the system to use the environment vars
114         UA_SYSROOT=1
115
116         useradd_preinst
117 }
118
119 useradd_sysroot_sstate () {
120         if [ "${BB_CURRENTTASK}" = "package_setscene" -o "${BB_CURRENTTASK}" = "populate_sysroot_setscene" ]
121         then
122                 useradd_sysroot
123         fi
124 }
125
126 userdel_sysroot_sstate () {
127 if test "x${STAGING_DIR_TARGET}" != "x"; then
128     if [ "${BB_CURRENTTASK}" = "clean" ]; then
129         export PSEUDO="${FAKEROOTENV} PSEUDO_LOCALSTATEDIR=${STAGING_DIR_TARGET}${localstatedir}/pseudo ${STAGING_DIR_NATIVE}${bindir_native}/pseudo"
130         OPT="--root ${STAGING_DIR_TARGET}"
131
132         # Remove groups and users defined for package
133         GROUPADD_PARAM="${@get_all_cmd_params(d, 'groupadd')}"
134         USERADD_PARAM="${@get_all_cmd_params(d, 'useradd')}"
135
136         user=`echo "$USERADD_PARAM" | cut -d ';' -f 1 | awk '{ print $NF }'`
137         remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
138         while test "x$user" != "x"; do
139             perform_userdel "${STAGING_DIR_TARGET}" "$OPT $user"
140             user=`echo "$remaining" | cut -d ';' -f 1 | awk '{ print $NF }'`
141             remaining=`echo "$remaining" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
142         done
143
144         user=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1 | awk '{ print $NF }'`
145         remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
146         while test "x$user" != "x"; do
147             perform_groupdel "${STAGING_DIR_TARGET}" "$OPT $user"
148             user=`echo "$remaining" | cut -d ';' -f 1 | awk '{ print $NF }'`
149             remaining=`echo "$remaining" | cut -d ';' -f 2- -s | sed -e 's#[ \t]*$##'`
150         done
151
152     fi
153 fi
154 }
155
156 SSTATECLEANFUNCS_append_class-target = " userdel_sysroot_sstate"
157
158 do_install[prefuncs] += "${SYSROOTFUNC}"
159 SYSROOTFUNC_class-target = "useradd_sysroot"
160 SYSROOTFUNC = ""
161
162 SSTATEPREINSTFUNCS_append_class-target = " useradd_sysroot_sstate"
163
164 do_package_setscene[depends] += "${USERADDSETSCENEDEPS}"
165 do_populate_sysroot_setscene[depends] += "${USERADDSETSCENEDEPS}"
166 USERADDSETSCENEDEPS_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"
167 USERADDSETSCENEDEPS = ""
168
169 # Recipe parse-time sanity checks
170 def update_useradd_after_parse(d):
171     useradd_packages = d.getVar('USERADD_PACKAGES', True)
172
173     if not useradd_packages:
174         raise bb.build.FuncFailed("%s inherits useradd but doesn't set USERADD_PACKAGES" % d.getVar('FILE', False))
175
176     for pkg in useradd_packages.split():
177         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):
178             bb.fatal("%s inherits useradd but doesn't set USERADD_PARAM, GROUPADD_PARAM or GROUPMEMS_PARAM for package %s" % (d.getVar('FILE', False), pkg))
179
180 python __anonymous() {
181     if not bb.data.inherits_class('nativesdk', d) \
182         and not bb.data.inherits_class('native', d):
183         update_useradd_after_parse(d)
184 }
185
186 # Return a single [GROUP|USER]ADD_PARAM formatted string which includes the
187 # [group|user]add parameters for all USERADD_PACKAGES in this recipe
188 def get_all_cmd_params(d, cmd_type):
189     import string
190     
191     param_type = cmd_type.upper() + "_PARAM_%s"
192     params = []
193
194     useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
195     for pkg in useradd_packages.split():
196         param = d.getVar(param_type % pkg, True)
197         if param:
198             params.append(param.rstrip(" ;"))
199
200     return "; ".join(params)
201
202 # Adds the preinst script into generated packages
203 fakeroot python populate_packages_prepend () {
204     def update_useradd_package(pkg):
205         bb.debug(1, 'adding user/group calls to preinst for %s' % pkg)
206
207         """
208         useradd preinst is appended here because pkg_preinst may be
209         required to execute on the target. Not doing so may cause
210         useradd preinst to be invoked twice, causing unwanted warnings.
211         """
212         preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True)
213         if not preinst:
214             preinst = '#!/bin/sh\n'
215         preinst += 'bbnote () {\n\techo "NOTE: $*"\n}\n'
216         preinst += 'bbwarn () {\n\techo "WARNING: $*"\n}\n'
217         preinst += 'bbfatal () {\n\techo "ERROR: $*"\n\texit 1\n}\n'
218         preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd', True)
219         preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd', True)
220         preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems', True)
221         preinst += d.getVar('useradd_preinst', True)
222         d.setVar('pkg_preinst_%s' % pkg, preinst)
223
224         # RDEPENDS setup
225         rdepends = d.getVar("RDEPENDS_%s" % pkg, True) or ""
226         rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-passwd'
227         rdepends += ' ' + d.getVar('MLPREFIX', False) + 'shadow'
228         # base-files is where the default /etc/skel is packaged
229         rdepends += ' ' + d.getVar('MLPREFIX', False) + 'base-files'
230         d.setVar("RDEPENDS_%s" % pkg, rdepends)
231
232     # Add the user/group preinstall scripts and RDEPENDS requirements
233     # to packages specified by USERADD_PACKAGES
234     if not bb.data.inherits_class('nativesdk', d) \
235         and not bb.data.inherits_class('native', d):
236         useradd_packages = d.getVar('USERADD_PACKAGES', True) or ""
237         for pkg in useradd_packages.split():
238             update_useradd_package(pkg)
239 }
240
241 # Use the following to extend the useradd with custom functions
242 USERADDEXTENSION ?= ""
243
244 inherit ${USERADDEXTENSION}