summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorScott Garman <scott.a.garman@intel.com>2011-05-17 18:13:49 (GMT)
committerRichard Purdie <richard.purdie@linuxfoundation.org>2011-07-01 16:17:34 (GMT)
commit14be7dbb159c43bdc5e2d128beb057d4b769a447 (patch)
tree14b134c24072f51f83b5313700311191b6814785
parenta965a615c130a05b9d8055f3d29aabe3d30df4f6 (diff)
downloadpoky-14be7dbb159c43bdc5e2d128beb057d4b769a447.tar.gz
useradd.bbclass: new class for managing user/group permissions
This class is to be used by recipes that need to set up specific user/group accounts and set custom file/directory permissions. (From OE-Core rev: a82885db00956734922291d8a17eb135461204fa) Signed-off-by: Scott Garman <scott.a.garman@intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--meta/classes/useradd.bbclass156
1 files changed, 156 insertions, 0 deletions
diff --git a/meta/classes/useradd.bbclass b/meta/classes/useradd.bbclass
new file mode 100644
index 0000000..ba8d8dc
--- /dev/null
+++ b/meta/classes/useradd.bbclass
@@ -0,0 +1,156 @@
1USERADDPN ?= "${PN}"
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 = " base-passwd shadow-native shadow-sysroot"
7RDEPENDS_${USERADDPN}_append = " base-passwd shadow"
8
9# This preinstall function will be run in two contexts: once for the
10# native sysroot (as invoked by the useradd_sysroot() wrapper), and
11# also as the preinst script in the target package.
12useradd_preinst () {
13OPT=""
14SYSROOT=""
15
16if test "x$D" != "x"; then
17 # Installing into a sysroot
18 SYSROOT="${STAGING_DIR_TARGET}"
19 OPT="--root ${STAGING_DIR_TARGET}"
20
21 # Add groups and users defined for all recipe packages
22 GROUPADD_PARAM="${@get_all_cmd_params(d, 'group')}"
23 USERADD_PARAM="${@get_all_cmd_params(d, 'user')}"
24else
25 # Installing onto a target
26 # Add groups and users defined only for this package
27 GROUPADD_PARAM="${GROUPADD_PARAM}"
28 USERADD_PARAM="${USERADD_PARAM}"
29fi
30
31# Perform group additions first, since user additions may depend
32# on these groups existing
33if test "x$GROUPADD_PARAM" != "x"; then
34 echo "Running groupadd commands..."
35 # Invoke multiple instances of groupadd for parameter lists
36 # separated by ';'
37 opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1`
38 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2-`
39 while test "x$opts" != "x"; do
40 eval $PSEUDO groupadd -f $OPT $opts
41
42 if test "x$opts" = "x$remaining"; then
43 break
44 fi
45 opts=`echo "$remaining" | cut -d ';' -f 1`
46 remaining=`echo "$remaining" | cut -d ';' -f 2-`
47 done
48fi
49
50if test "x$USERADD_PARAM" != "x"; then
51 echo "Running useradd commands..."
52 # Invoke multiple instances of useradd for parameter lists
53 # separated by ';'
54 opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1`
55 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2-`
56 while test "x$opts" != "x"; do
57 # useradd does not have a -f option, so we have to check if the
58 # username already exists manually
59 username=`echo "$opts" | awk '{ print $NF }'`
60 user_exists=`grep "^$username:" $SYSROOT/etc/passwd || true`
61 if test "x$user_exists" = "x"; then
62 eval $PSEUDO useradd $OPT $opts
63 else
64 echo "Note: username $username already exists, not re-creating it"
65 fi
66
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}
75
76useradd_sysroot () {
77 export PSEUDO="${STAGING_DIR_NATIVE}/usr/bin/pseudo"
78 export PSEUDO_LOCALSTATEDIR="${STAGING_DIR_TARGET}/var/pseudo"
79
80 # Explicitly set $D since it isn't set to anything
81 # before do_install
82 D=${D}
83 useradd_preinst
84}
85
86useradd_sysroot_sstate () {
87 if [ "${BB_CURRENTTASK}" = "populate_sysroot_setscene" ]
88 then
89 useradd_sysroot
90 fi
91}
92
93do_install[prefuncs] += "useradd_sysroot"
94SSTATEPOSTINSTFUNCS += "useradd_sysroot_sstate"
95
96# Recipe parse-time sanity checks
97def update_useradd_after_parse(d):
98 if not d.getVar('USERADD_PACKAGES', False):
99 if not d.getVar('USERADD_PARAM', False) and not d.getVar('GROUPADD_PARAM', False):
100 raise bb.build.FuncFailed, "%s inherits useradd but doesn't set USERADD_PARAM or GROUPADD_PARAM" % bb.data.getVar('FILE', d)
101
102python __anonymous() {
103 update_useradd_after_parse(d)
104}
105
106# Return a single [GROUP|USER]ADD_PARAM formatted string which includes the
107# [group|user]add parameters for all packages in this recipe
108def get_all_cmd_params(d, cmd_type):
109 import string
110
111 param_type = cmd_type.upper() + "ADD_PARAM_%s"
112 params = []
113
114 pkgs = d.getVar('USERADD_PACKAGES', True)
115 if not pkgs:
116 pkgs = d.getVar('USERADDPN', True)
117 packages = (d.getVar('PACKAGES', True) or "").split()
118 if packages and pkgs not in packages:
119 pkgs = packages[0]
120
121 for pkg in pkgs.split():
122 param = d.getVar(param_type % pkg, True)
123 if param:
124 params.append(param)
125
126 return string.join(params, "; ")
127
128# Adds the preinst script into generated packages
129fakeroot python populate_packages_prepend () {
130 def update_useradd_package(pkg):
131 bb.debug(1, 'adding user/group calls to preinst for %s' % pkg)
132
133 """
134 useradd preinst is appended here because pkg_preinst may be
135 required to execute on the target. Not doing so may cause
136 useradd preinst to be invoked twice, causing unwanted warnings.
137 """
138 preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True)
139 if not preinst:
140 preinst = '#!/bin/sh\n'
141 preinst += d.getVar('useradd_preinst', True)
142 bb.data.setVar('pkg_preinst_%s' % pkg, preinst, d)
143
144 # We add the user/group calls to all packages to allow any package
145 # to contain files owned by the users/groups defined in the recipe.
146 # The user/group addition code is careful not to create duplicate
147 # entries, so this is safe.
148 pkgs = d.getVar('USERADD_PACKAGES', True)
149 if not pkgs:
150 pkgs = d.getVar('USERADDPN', True)
151 packages = (d.getVar('PACKAGES', True) or "").split()
152 if packages and pkgs not in packages:
153 pkgs = packages[0]
154 for pkg in pkgs.split():
155 update_useradd_package(pkg)
156}