summaryrefslogtreecommitdiffstats
path: root/meta/classes/useradd_base.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes/useradd_base.bbclass')
-rw-r--r--meta/classes/useradd_base.bbclass230
1 files changed, 230 insertions, 0 deletions
diff --git a/meta/classes/useradd_base.bbclass b/meta/classes/useradd_base.bbclass
new file mode 100644
index 0000000..c47b1eb
--- /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 else
28 break
29 fi
30 count=`expr $count + 1`
31 if test $count = $retries; then
32 bbfatal "Tried running groupadd command $retries times without scucess, giving up"
33 fi
34 sleep $count
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 else
56 break
57 fi
58 count=`expr $count + 1`
59 if test $count = $retries; then
60 bbfatal "Tried running useradd command $retries times without scucess, giving up"
61 fi
62 sleep $count
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 else
94 break
95 fi
96 count=`expr $count + 1`
97 if test $count = $retries; then
98 if test "x$gshadow" = "xno"; then
99 rm -f $rootdir${sysconfdir}/gshadow
100 rm -f $rootdir${sysconfdir}/gshadow-
101 fi
102 bbfatal "Tried running groupmems command $retries times without scucess, giving up"
103 fi
104 sleep $count
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 else
130 break
131 fi
132 count=`expr $count + 1`
133 if test $count = $retries; then
134 bbfatal "Tried running groupdel command $retries times without scucess, giving up"
135 fi
136 sleep $count
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 else
158 break
159 fi
160 count=`expr $count + 1`
161 if test $count = $retries; then
162 bbfatal "Tried running userdel command $retries times without scucess, giving up"
163 fi
164 sleep $count
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 else
188 break
189 fi
190 count=`expr $count + 1`
191 if test $count = $retries; then
192 bbfatal "Tried running groupmod command $retries times without scucess, giving up"
193 fi
194 sleep $count
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 else
218 break
219 fi
220 count=`expr $count + 1`
221 if test $count = $retries; then
222 bbfatal "Tried running usermod command $retries times without scucess, giving up"
223 fi
224 sleep $count
225 done
226 else
227 bbwarn "user $username doesn't exist, unable to modify it"
228 fi
229 set -e
230}