Bump DISTRO_VERSION to 2.0.0 for blowfish
[AGL/meta-agl.git] / meta-agl / classes / useradd_base.bbclass
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 with flock
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
13 perform_groupadd () {
14         local rootdir="$1"
15         local opts="$2"
16         bbnote "${PN}: Performing groupadd with [$opts]"
17         local groupname=`echo "$opts" | awk '{ print $NF }'`
18         local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
19         if test "x$group_exists" = "x"; then
20                 opts=`echo $opts | sed s/\'/\"/g`
21                 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO groupadd $opts\' || true
22                 group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
23                 if test "x$group_exists" = "x"; then
24                         bbfatal "${PN}: groupadd command did not succeed."
25                 fi
26         else
27                 bbnote "${PN}: group $groupname already exists, not re-creating it"
28         fi
29 }
30
31 perform_useradd () {
32         local rootdir="$1"
33         local opts="$2"
34         bbnote "${PN}: Performing useradd with [$opts]"
35         local username=`echo "$opts" | awk '{ print $NF }'`
36         local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
37         if test "x$user_exists" = "x"; then
38                 opts=`echo $opts | sed s/\'/\"/g`
39                 eval flock -x -w 100 $rootdir${sysconfdir} -c  \'$PSEUDO useradd $opts\' || true
40                 user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
41                 if test "x$user_exists" = "x"; then
42                         bbfatal "${PN}: useradd command did not succeed."
43                 fi
44         else
45                 bbnote "${PN}: user $username already exists, not re-creating it"
46         fi
47 }
48
49 perform_groupmems () {
50         local rootdir="$1"
51         local opts="$2"
52         bbnote "${PN}: Performing groupmems with [$opts]"
53         local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'`
54         local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'`
55         bbnote "${PN}: Running groupmems command with group $groupname and user $username"
56         # groupmems fails if /etc/gshadow does not exist
57         local gshadow=""
58         if [ -f $rootdir${sysconfdir}/gshadow ]; then
59                 gshadow="yes"
60         else
61                 gshadow="no"
62                 touch $rootdir${sysconfdir}/gshadow
63         fi
64         local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
65         if test "x$mem_exists" = "x"; then
66                 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO groupmems $opts\' || true
67                 mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
68                 if test "x$mem_exists" = "x"; then
69                         bbfatal "${PN}: groupmems command did not succeed."
70                 fi
71         else
72                 bbnote "${PN}: group $groupname already contains $username, not re-adding it"
73         fi
74         if test "x$gshadow" = "xno"; then
75                 rm -f $rootdir${sysconfdir}/gshadow
76                 rm -f $rootdir${sysconfdir}/gshadow-
77         fi
78 }
79
80 perform_groupdel () {
81         local rootdir="$1"
82         local opts="$2"
83         bbnote "${PN}: Performing groupdel with [$opts]"
84         local groupname=`echo "$opts" | awk '{ print $NF }'`
85         local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
86         if test "x$group_exists" != "x"; then
87                 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO groupdel $opts\' || true
88                 group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
89                 if test "x$group_exists" != "x"; then
90                         bbfatal "${PN}: groupdel command did not succeed."
91                 fi
92         else
93                 bbnote "${PN}: group $groupname doesn't exist, not removing it"
94         fi
95 }
96
97 perform_userdel () {
98         local rootdir="$1"
99         local opts="$2"
100         bbnote "${PN}: Performing userdel with [$opts]"
101         local username=`echo "$opts" | awk '{ print $NF }'`
102         local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
103         if test "x$user_exists" != "x"; then
104                 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO userdel $opts\' || true
105                 user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
106                 if test "x$user_exists" != "x"; then
107                         bbfatal "${PN}: userdel command did not succeed."
108                 fi
109         else
110                 bbnote "${PN}: user $username doesn't exist, not removing it"
111         fi
112 }
113
114 perform_groupmod () {
115         # Other than the return value of groupmod, there's no simple way to judge whether the command
116         # succeeds, so we disable -e option temporarily
117         set +e
118         local rootdir="$1"
119         local opts="$2"
120         bbnote "${PN}: Performing groupmod with [$opts]"
121         local groupname=`echo "$opts" | awk '{ print $NF }'`
122         local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
123         if test "x$group_exists" != "x"; then
124                 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO groupmod $opts\'
125                 if test $? != 0; then
126                         bbwarn "${PN}: groupmod command did not succeed."
127                 fi
128         else
129                 bbwarn "${PN}: group $groupname doesn't exist, unable to modify it"
130         fi
131         set -e
132 }
133
134 perform_usermod () {
135         # Same reason with groupmod, temporarily disable -e option
136         set +e
137         local rootdir="$1"
138         local opts="$2"
139         bbnote "${PN}: Performing usermod with [$opts]"
140         local username=`echo "$opts" | awk '{ print $NF }'`
141         local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
142         if test "x$user_exists" != "x"; then
143                 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO usermod $opts\'
144                 if test $? != 0; then
145                         bbfatal "${PN}: usermod command did not succeed."
146                 fi
147         else
148                 bbwarn "${PN}: user $username doesn't exist, unable to modify it"
149         fi
150         set -e
151 }