]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - debian/guest-account.sh
Drop local keyword, as it may break guest-account.sh. See LP #139097.
[sojka/lightdm.git] / debian / guest-account.sh
1 #!/bin/sh -e
2 # (C) 2008 Canonical Ltd.
3 # Author: Martin Pitt <martin.pitt@ubuntu.com>
4 # License: GPL v2 or later
5 # modified by David D Lowe and Thomas Detoux
6 #
7 # Setup user and temporary home directory for guest session.
8 # If this succeeds, this script needs to print the username as the last line to
9 # stdout.
10
11 export TEXTDOMAINDIR=/usr/share/locale-langpack
12 export TEXTDOMAIN=lightdm
13
14 # set the system wide locale for gettext calls
15 if [ -f /etc/default/locale ]; then
16   . /etc/default/locale
17   LANGUAGE=
18   export LANG LANGUAGE
19 fi
20
21 is_system_user ()
22 {
23   UID_MIN=$(cat /etc/login.defs | grep UID_MIN | awk '{print $2}')
24   SYS_UID_MIN=$(cat /etc/login.defs | grep SYS_UID_MIN | awk '{print $2}')
25   SYS_UID_MAX=$(cat /etc/login.defs | grep SYS_UID_MAX | awk '{print $2}')
26
27   SYS_UID_MIN=${SYS_UID_MIN:-101}
28   SYS_UID_MAX=${SYS_UID_MAX:-$(( UID_MIN - 1 ))}
29
30   [ ${1} -ge ${SYS_UID_MIN} ] && [ ${1} -le ${SYS_UID_MAX} ]
31 }
32
33 add_account ()
34 {
35   temp_home=$(mktemp -td guest-XXXXXX)
36   HOME=$(echo ${temp_home} | tr '[:upper:]' '[:lower:]')
37   USER=$(echo ${HOME} | sed 's/\(.*\)guest/guest/')
38   [ ${HOME} != ${temp_home} ] && mv ${temp_home} ${HOME}
39
40   # if ${USER} already exists, it must be a locked system account with no existing
41   # home directory
42   if PWSTAT=$(passwd -S ${USER}) 2>/dev/null; then
43     if [ $(echo ${PWSTAT} | cut -f2 -d' ') != L ]; then
44       echo "User account ${USER} already exists and is not locked"
45       exit 1
46     fi
47
48     PWENT=$(getent passwd ${USER}) || {
49       echo "getent passwd ${USER} failed"
50       exit 1
51     }
52
53     GUEST_UID=$(echo ${PWENT} | cut -f3 -d:)
54
55     if ! is_system_user ${GUEST_UID}; then
56       echo "Account ${USER} is not a system user"
57       exit 1
58     fi
59
60     HOME=$(echo ${PWENT} | cut -f6 -d:)
61
62     if [ ${HOME} != / ] && [ ${HOME#/tmp} = ${HOME} ] && [ -d ${HOME} ]; then
63       echo "Home directory of ${USER} already exists"
64       exit 1
65     fi
66   else
67     # does not exist, so create it
68     useradd --system --home-dir / --comment $(gettext "Guest") --user-group --shell /bin/bash ${USER} || {
69       rm -rf ${HOME}
70       exit 1
71     }
72   fi
73
74   dist_gs=/usr/share/lightdm/guest-session
75   site_gs=/etc/guest-session
76
77   # create temporary home directory
78   mount -t tmpfs -o mode=700,uid=${USER} none ${HOME} || {
79     rm -rf ${HOME}
80     exit 1
81   }
82
83   if [ -d ${site_gs}/skel ] && [ -n $(find ${site_gs}/skel -type f) ]; then
84     # Only perform union-mounting if BindFS is available
85     if [ -x /usr/bin/bindfs ]; then
86       bindfs_mount=true
87
88       # Try OverlayFS first
89       if modinfo -n overlay >/dev/null 2>&1; then
90         mkdir ${HOME}/upper ${HOME}/work
91         chown ${USER}:${USER} ${HOME}/upper ${HOME}/work
92
93         mount -t overlay -o lowerdir=${dist_gs}/skel:${site_gs}/skel,upperdir=${HOME}/upper,workdir=${HOME}/work overlay ${HOME} || {
94           umount ${HOME}
95           rm -rf ${HOME}
96           exit 1
97         }
98       # If OverlayFS is not available, try AuFS
99       elif [ -x /sbin/mount.aufs ]; then
100         mount -t aufs -o br=${HOME}:${dist_gs}/skel:${site_gs}/skel none ${HOME} || {
101           umount ${HOME}
102           rm -rf ${HOME}
103           exit 1
104         }
105       # If none of them is available, fall back to copy over
106       else
107         cp -rT ${site_gs}/skel/ ${HOME}
108         cp -rT ${dist_gs}/skel/ ${HOME}
109         chown -R ${USER}:${USER} ${HOME}
110         bindfs_mount=false
111       fi
112
113       if ${bindfs_mount}; then
114         # Wrap ${HOME} in a BindFS mount, so that
115         # ${USER} will be seen as the owner of ${HOME}'s contents.
116         bindfs -u ${USER} -g ${USER} ${HOME} ${HOME} || {
117           umount ${HOME} # union mount
118           umount ${HOME} # tmpfs mount
119           rm -rf ${HOME}
120           exit 1
121         }
122       fi
123     # If BindFS is not available, just fall back to copy over
124     else
125       cp -rT ${site_gs}/skel/ ${HOME}
126       cp -rT ${dist_gs}/skel/ ${HOME}
127       chown -R ${USER}:${USER} ${HOME}
128     fi
129   else
130     cp -rT /etc/skel/ ${HOME}
131     cp -rT ${dist_gs}/skel/ ${HOME}
132     chown -R ${USER}:${USER} ${HOME}
133   fi
134
135   usermod -d ${HOME} ${USER}
136
137   # setup session
138   su ${USER} -c "env HOME=${HOME} site_gs=${site_gs} ${dist_gs}/setup.sh"
139
140   echo ${USER}
141 }
142
143 remove_account ()
144 {
145   GUEST_USER=${1}
146   
147   PWENT=$(getent passwd ${GUEST_USER}) || {
148     echo "Error: invalid user ${GUEST_USER}"
149     exit 1
150   }
151
152   GUEST_UID=$(echo ${PWENT} | cut -f3 -d:)
153
154   if ! is_system_user ${GUEST_UID}; then
155     echo "Error: user ${GUEST_USER} is not a system user."
156     exit 1
157   fi
158
159   GUEST_HOME=$(echo ${PWENT} | cut -f6 -d:)
160
161   if [ ${GUEST_HOME} = ${GUEST_HOME#/tmp/} ]; then
162     echo "Error: home directory ${GUEST_HOME} is not in /tmp/."
163     exit 1
164   fi
165
166   # kill all remaining processes
167   if [ -x /bin/loginctl ] || [ -x /usr/bin/loginctl ]; then
168     loginctl terminate-user ${GUEST_USER} >/dev/null || true
169   else
170     while ps h -u ${GUEST_USER} >/dev/null; do 
171       killall -9 -u ${GUEST_USER} || true
172       sleep 0.2; 
173     done
174   fi
175
176   umount ${GUEST_HOME} || umount -l ${GUEST_HOME} || true # BindFS mount
177   umount ${GUEST_HOME} || umount -l ${GUEST_HOME} || true # union mount
178   umount ${GUEST_HOME} || umount -l ${GUEST_HOME} || true # tmpfs mount
179   rm -rf ${GUEST_HOME}
180
181   # remove leftovers in /tmp
182   find /tmp -mindepth 1 -maxdepth 1 -uid ${GUEST_UID} -print0 | xargs -0 rm -rf || true
183
184   # remove possible {/run,}/media/guest-XXXXXX folder
185   for media_dir in /run/media/${GUEST_USER} /media/${GUEST_USER}; do
186     if [ -d ${media_dir} ]; then
187       for dir in $(find ${media_dir} -mindepth 1 -maxdepth 1); do
188         umount ${dir} || true
189       done
190
191       rmdir ${media_dir} || true
192     fi
193   done
194
195   userdel ${GUEST_USER}
196 }
197
198 case ${1} in
199   add)
200     add_account
201     ;;
202   remove)
203     if [ -z ${2} ] ; then
204       echo "Usage: ${0} remove [account]"
205       exit 1
206     fi
207
208     remove_account ${2}
209     ;;
210   *)
211     echo "Usage: ${0} add"
212     echo "       ${0} remove [account]"
213     exit 1
214 esac