summaryrefslogtreecommitdiffstats
path: root/meta/classes
diff options
context:
space:
mode:
authorChen Qi <Qi.Chen@windriver.com>2013-07-19 09:52:25 +0800
committerRichard Purdie <richard.purdie@linuxfoundation.org>2013-07-24 11:35:35 +0100
commit50d2548f74322c130129cb2e7bb0b763c8774968 (patch)
tree6d4b2dfc7a6a7008f72287af08e0e6791ca639be /meta/classes
parent18f39e545674d7405cbdf4486a911c577177706f (diff)
downloadpoky-50d2548f74322c130129cb2e7bb0b763c8774968.tar.gz
useradd.bbclass: add a new base class and code refactor
Add a new base class, useradd_base.bbclass, which is mainly a collection of basic functions for user/group settings. The useradd_base.bbclass is intended to be inherited by useradd.bbclass and the extrausers.bbclass to avoid code cuplication. [YOCTO #4074] (From OE-Core rev: 2a57bb7e9a7e154578aa7cb9aeebdf398a54ec00) Signed-off-by: Chen Qi <Qi.Chen@windriver.com> Signed-off-by: Saul Wold <sgw@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/classes')
-rw-r--r--meta/classes/useradd.bbclass99
-rw-r--r--meta/classes/useradd_base.bbclass230
2 files changed, 238 insertions, 91 deletions
diff --git a/meta/classes/useradd.bbclass b/meta/classes/useradd.bbclass
index 3fe011d976..9b3ac258ae 100644
--- a/meta/classes/useradd.bbclass
+++ b/meta/classes/useradd.bbclass
@@ -1,3 +1,5 @@
1inherit useradd_base
2
1# base-passwd-cross provides the default passwd and group files in the 3# base-passwd-cross provides the default passwd and group files in the
2# target sysroot, and shadow -native and -sysroot provide the utilities 4# target sysroot, and shadow -native and -sysroot provide the utilities
3# and support files needed to add and modify user and group accounts 5# and support files needed to add and modify user and group accounts
@@ -44,30 +46,7 @@ if test "x$GROUPADD_PARAM" != "x"; then
44 opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1` 46 opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1`
45 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2-` 47 remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2-`
46 while test "x$opts" != "x"; do 48 while test "x$opts" != "x"; do
47 groupname=`echo "$opts" | awk '{ print $NF }'` 49 perform_groupadd "$SYSROOT" "$OPT $opts" 10
48 group_exists=`grep "^$groupname:" $SYSROOT/etc/group || true`
49 if test "x$group_exists" = "x"; then
50 count=1
51 while true; do
52 eval $PSEUDO groupadd $OPT $opts || true
53 group_exists=`grep "^$groupname:" $SYSROOT/etc/group || true`
54 if test "x$group_exists" = "x"; then
55 # File locking issues can require us to retry the command
56 echo "WARNING: groupadd command did not succeed. Retrying..."
57 sleep 1
58 else
59 break
60 fi
61 count=`expr $count + 1`
62 if test $count = 11; then
63 echo "ERROR: tried running groupadd command 10 times without success, giving up"
64 exit 1
65 fi
66 done
67 else
68 echo "Note: group $groupname already exists, not re-creating it"
69 fi
70
71 if test "x$opts" = "x$remaining"; then 50 if test "x$opts" = "x$remaining"; then
72 break 51 break
73 fi 52 fi
@@ -83,32 +62,7 @@ if test "x$USERADD_PARAM" != "x"; then
83 opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1` 62 opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1`
84 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2-` 63 remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2-`
85 while test "x$opts" != "x"; do 64 while test "x$opts" != "x"; do
86 # useradd does not have a -f option, so we have to check if the 65 perform_useradd "$SYSROOT" "$OPT $opts" 10
87 # username already exists manually
88 username=`echo "$opts" | awk '{ print $NF }'`
89 user_exists=`grep "^$username:" $SYSROOT/etc/passwd || true`
90 if test "x$user_exists" = "x"; then
91 count=1
92 while true; do
93 eval $PSEUDO useradd $OPT $opts || true
94 user_exists=`grep "^$username:" $SYSROOT/etc/passwd || true`
95 if test "x$user_exists" = "x"; then
96 # File locking issues can require us to retry the command
97 echo "WARNING: useradd command did not succeed. Retrying..."
98 sleep 1
99 else
100 break
101 fi
102 count=`expr $count + 1`
103 if test $count = 11; then
104 echo "ERROR: tried running useradd command 10 times without success, giving up"
105 exit 1
106 fi
107 done
108 else
109 echo "Note: username $username already exists, not re-creating it"
110 fi
111
112 if test "x$opts" = "x$remaining"; then 66 if test "x$opts" = "x$remaining"; then
113 break 67 break
114 fi 68 fi
@@ -119,58 +73,18 @@ fi
119 73
120if test "x$GROUPMEMS_PARAM" != "x"; then 74if test "x$GROUPMEMS_PARAM" != "x"; then
121 echo "Running groupmems commands..." 75 echo "Running groupmems commands..."
122 # groupmems fails if /etc/gshadow does not exist
123 if [ -f $SYSROOT${sysconfdir}/gshadow ]; then
124 gshadow="yes"
125 else
126 gshadow="no"
127 touch $SYSROOT${sysconfdir}/gshadow
128 fi
129 # Invoke multiple instances of groupmems for parameter lists 76 # Invoke multiple instances of groupmems for parameter lists
130 # separated by ';' 77 # separated by ';'
131 opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1` 78 opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1`
132 remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2-` 79 remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2-`
133 while test "x$opts" != "x"; do 80 while test "x$opts" != "x"; do
134 groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'` 81 perform_groupmems "$SYSROOT" "$OPT $opts" 10
135 username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'`
136 echo "$groupname $username"
137 mem_exists=`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $SYSROOT/etc/group || true`
138 if test "x$mem_exists" = "x"; then
139 count=1
140 while true; do
141 eval $PSEUDO groupmems $OPT $opts || true
142 mem_exists=`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $SYSROOT/etc/group || true`
143 if test "x$mem_exists" = "x"; then
144 # File locking issues can require us to retry the command
145 echo "WARNING: groupmems command did not succeed. Retrying..."
146 sleep 1
147 else
148 break
149 fi
150 count=`expr $count + 1`
151 if test $count = 11; then
152 echo "ERROR: tried running groupmems command 10 times without success, giving up"
153 if test "x$gshadow" = "xno"; then
154 rm -f $SYSROOT${sysconfdir}/gshadow
155 rm -f $SYSROOT${sysconfdir}/gshadow-
156 fi
157 exit 1
158 fi
159 done
160 else
161 echo "Note: group $groupname already contains $username, not re-adding it"
162 fi
163
164 if test "x$opts" = "x$remaining"; then 82 if test "x$opts" = "x$remaining"; then
165 break 83 break
166 fi 84 fi
167 opts=`echo "$remaining" | cut -d ';' -f 1` 85 opts=`echo "$remaining" | cut -d ';' -f 1`
168 remaining=`echo "$remaining" | cut -d ';' -f 2-` 86 remaining=`echo "$remaining" | cut -d ';' -f 2-`
169 done 87 done
170 if test "x$gshadow" = "xno"; then
171 rm -f $SYSROOT${sysconfdir}/gshadow
172 rm -f $SYSROOT${sysconfdir}/gshadow-
173 fi
174fi 88fi
175} 89}
176 90
@@ -254,6 +168,9 @@ fakeroot python populate_packages_prepend () {
254 preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True) 168 preinst = d.getVar('pkg_preinst_%s' % pkg, True) or d.getVar('pkg_preinst', True)
255 if not preinst: 169 if not preinst:
256 preinst = '#!/bin/sh\n' 170 preinst = '#!/bin/sh\n'
171 preinst += 'perform_groupadd () {\n%s}\n' % d.getVar('perform_groupadd', True)
172 preinst += 'perform_useradd () {\n%s}\n' % d.getVar('perform_useradd', True)
173 preinst += 'perform_groupmems () {\n%s}\n' % d.getVar('perform_groupmems', True)
257 preinst += d.getVar('useradd_preinst', True) 174 preinst += d.getVar('useradd_preinst', True)
258 d.setVar('pkg_preinst_%s' % pkg, preinst) 175 d.setVar('pkg_preinst_%s' % pkg, preinst)
259 176
diff --git a/meta/classes/useradd_base.bbclass b/meta/classes/useradd_base.bbclass
new file mode 100644
index 0000000000..7aafe29a4a
--- /dev/null
+++ b/meta/classes/useradd_base.bbclass
@@ -0,0 +1,230 @@
1# This bbclass provides basic functionality for user/group settings.
2# This bbclass is intended to be inherited by useradd.bbclass and
3# extrausers.bbclass.
4
5# The following functions basically have similar logic.
6# *) Perform necessary checks before invoking the actual command
7# *) Invoke the actual command, make retries if necessary
8# *) Error out if an error occurs.
9
10# Note that before invoking these functions, make sure the global variable
11# PSEUDO is set up correctly.
12
13perform_groupadd () {
14 local rootdir="$1"
15 local opts="$2"
16 local retries="$3"
17 bbnote "Performing groupadd with [$opts] and $retries times of retry"
18 local groupname=`echo "$opts" | awk '{ print $NF }'`
19 local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
20 if test "x$group_exists" = "x"; then
21 local count=0
22 while true; do
23 eval $PSEUDO groupadd $opts || true
24 group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
25 if test "x$group_exists" = "x"; then
26 bbwarn "groupadd command did not succeed. Retrying..."
27 sleep 1
28 else
29 break
30 fi
31 count=`expr $count + 1`
32 if test $count = $retries; then
33 bbfatal "Tried running groupadd command $retries times without scucess, giving up"
34 fi
35 done
36 else
37 bbwarn "group $groupname already exists, not re-creating it"
38 fi
39}
40
41perform_useradd () {
42 local rootdir="$1"
43 local opts="$2"
44 local retries="$3"
45 bbnote "Performing useradd with [$opts] and $retries times of retry"
46 local username=`echo "$opts" | awk '{ print $NF }'`
47 local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
48 if test "x$user_exists" = "x"; then
49 local count=0
50 while true; do
51 eval $PSEUDO useradd $opts || true
52 user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
53 if test "x$user_exists" = "x"; then
54 bbwarn "useradd command did not succeed. Retrying..."
55 sleep 1
56 else
57 break
58 fi
59 count=`expr $count + 1`
60 if test $count = $retries; then
61 bbfatal "Tried running useradd command $retries times without scucess, giving up"
62 fi
63 done
64 else
65 bbwarn "user $username already exists, not re-creating it"
66 fi
67}
68
69perform_groupmems () {
70 local rootdir="$1"
71 local opts="$2"
72 local retries="$3"
73 bbnote "Performing groupmems with [$opts] and $retries times of retry"
74 local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'`
75 local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'`
76 bbnote "Running groupmems command with group $groupname and user $username"
77 # groupmems fails if /etc/gshadow does not exist
78 local gshadow=""
79 if [ -f $rootdir${sysconfdir}/gshadow ]; then
80 gshadow="yes"
81 else
82 gshadow="no"
83 touch $rootdir${sysconfdir}/gshadow
84 fi
85 local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
86 if test "x$mem_exists" = "x"; then
87 local count=0
88 while true; do
89 eval $PSEUDO groupmems $opts || true
90 mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
91 if test "x$mem_exists" = "x"; then
92 bbwarn "groupmems command did not succeed. Retrying..."
93 sleep 1
94 else
95 break
96 fi
97 count=`expr $count + 1`
98 if test $count = $retries; then
99 if test "x$gshadow" = "xno"; then
100 rm -f $rootdir${sysconfdir}/gshadow
101 rm -f $rootdir${sysconfdir}/gshadow-
102 fi
103 bbfatal "Tried running groupmems command $retries times without scucess, giving up"
104 fi
105 done
106 else
107 bbwarn "group $groupname already contains $username, not re-adding it"
108 fi
109 if test "x$gshadow" = "xno"; then
110 rm -f $rootdir${sysconfdir}/gshadow
111 rm -f $rootdir${sysconfdir}/gshadow-
112 fi
113}
114
115perform_groupdel () {
116 local rootdir="$1"
117 local opts="$2"
118 local retries="$3"
119 bbnote "Performing groupdel with [$opts] and $retries times of retry"
120 local groupname=`echo "$opts" | awk '{ print $NF }'`
121 local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
122 if test "x$group_exists" != "x"; then
123 local count=0
124 while true; do
125 eval $PSEUDO groupdel $opts || true
126 group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
127 if test "x$group_exists" != "x"; then
128 bbwarn "groupdel command did not succeed. Retrying..."
129 sleep 1
130 else
131 break
132 fi
133 count=`expr $count + 1`
134 if test $count = $retries; then
135 bbfatal "Tried running groupdel command $retries times without scucess, giving up"
136 fi
137 done
138 else
139 bbwarn "group $groupname doesn't exist, not removing it"
140 fi
141}
142
143perform_userdel () {
144 local rootdir="$1"
145 local opts="$2"
146 local retries="$3"
147 bbnote "Performing userdel with [$opts] and $retries times of retry"
148 local username=`echo "$opts" | awk '{ print $NF }'`
149 local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
150 if test "x$user_exists" != "x"; then
151 local count=0
152 while true; do
153 eval $PSEUDO userdel $opts || true
154 user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
155 if test "x$user_exists" != "x"; then
156 bbwarn "userdel command did not succeed. Retrying..."
157 sleep 1
158 else
159 break
160 fi
161 count=`expr $count + 1`
162 if test $count = $retries; then
163 bbfatal "Tried running userdel command $retries times without scucess, giving up"
164 fi
165 done
166 else
167 bbwarn "user $username doesn't exist, not removing it"
168 fi
169}
170
171perform_groupmod () {
172 # Other than the return value of groupmod, there's no simple way to judge whether the command
173 # succeeds, so we disable -e option temporarily
174 set +e
175 local rootdir="$1"
176 local opts="$2"
177 local retries="$3"
178 bbnote "Performing groupmod with [$opts] and $retries times of retry"
179 local groupname=`echo "$opts" | awk '{ print $NF }'`
180 local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
181 if test "x$group_exists" != "x"; then
182 local count=0
183 while true; do
184 eval $PSEUDO groupmod $opts
185 if test $? != 0; then
186 bbwarn "groupmod command did not succeed. Retrying..."
187 sleep 1
188 else
189 break
190 fi
191 count=`expr $count + 1`
192 if test $count = $retries; then
193 bbfatal "Tried running groupmod command $retries times without scucess, giving up"
194 fi
195 done
196 else
197 bbwarn "group $groupname doesn't exist, unable to modify it"
198 fi
199 set -e
200}
201
202perform_usermod () {
203 # Same reason with groupmod, temporarily disable -e option
204 set +e
205 local rootdir="$1"
206 local opts="$2"
207 local retries="$3"
208 bbnote "Performing usermod with [$opts] and $retries times of retry"
209 local username=`echo "$opts" | awk '{ print $NF }'`
210 local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
211 if test "x$user_exists" != "x"; then
212 local count=0
213 while true; do
214 eval $PSEUDO usermod $opts
215 if test $? != 0; then
216 bbwarn "usermod command did not succeed. Retrying..."
217 sleep 1
218 else
219 break
220 fi
221 count=`expr $count + 1`
222 if test $count = $retries; then
223 bbfatal "Tried running usermod command $retries times without scucess, giving up"
224 fi
225 done
226 else
227 bbwarn "user $username doesn't exist, unable to modify it"
228 fi
229 set -e
230}