Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 53 | if test "x`echo $GROUPADD_PARAM | tr -d '[:space:]'`" != "x"; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 60 | perform_groupadd "$SYSROOT" "$OPT $opts" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 69 | if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 76 | perform_useradd "$SYSROOT" "$OPT $opts" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 85 | if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 92 | perform_groupmems "$SYSROOT" "$OPT $opts" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 130 | userdel_sysroot_sstate () { |
| 131 | if 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 |
| 151 | fi |
| 152 | } |
| 153 | |
| 154 | SSTATECLEANFUNCS = "userdel_sysroot_sstate" |
| 155 | SSTATECLEANFUNCS_class-cross = "" |
| 156 | SSTATECLEANFUNCS_class-native = "" |
| 157 | SSTATECLEANFUNCS_class-nativesdk = "" |
| 158 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 159 | do_install[prefuncs] += "${SYSROOTFUNC}" |
| 160 | SYSROOTFUNC = "useradd_sysroot" |
| 161 | SYSROOTFUNC_class-cross = "" |
| 162 | SYSROOTFUNC_class-native = "" |
| 163 | SYSROOTFUNC_class-nativesdk = "" |
| 164 | SSTATEPREINSTFUNCS += "${SYSROOTPOSTFUNC}" |
| 165 | SYSROOTPOSTFUNC = "useradd_sysroot_sstate" |
| 166 | SYSROOTPOSTFUNC_class-cross = "" |
| 167 | SYSROOTPOSTFUNC_class-native = "" |
| 168 | SYSROOTPOSTFUNC_class-nativesdk = "" |
| 169 | |
| 170 | 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" |
| 171 | USERADDSETSCENEDEPS_class-cross = "" |
| 172 | USERADDSETSCENEDEPS_class-native = "" |
| 173 | USERADDSETSCENEDEPS_class-nativesdk = "" |
| 174 | do_package_setscene[depends] += "${USERADDSETSCENEDEPS}" |
| 175 | do_populate_sysroot_setscene[depends] += "${USERADDSETSCENEDEPS}" |
| 176 | |
| 177 | # Recipe parse-time sanity checks |
| 178 | def 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 | |
| 188 | python __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 |
| 196 | def 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 |
| 211 | fakeroot 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 |
| 250 | USERADDEXTENSION ?= "" |
| 251 | |
| 252 | inherit ${USERADDEXTENSION} |