]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - debian/guest-account.sh
Make the real name of a guest account translatable. Fixes: https://launchpad.net...
[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 add_account ()
22 {
23   HOME=`mktemp -td guest-XXXXXX`
24   USER=`echo $HOME | sed 's/\(.*\)guest/guest/'`
25
26   # if $USER already exists, it must be a locked system account with no existing
27   # home directory
28   if PWSTAT=`passwd -S "$USER"` 2>/dev/null; then
29     if [ "`echo \"$PWSTAT\" | cut -f2 -d\ `" != "L" ]; then
30       echo "User account $USER already exists and is not locked"
31       exit 1
32     fi
33     PWENT=`getent passwd "$USER"` || {
34       echo "getent passwd $USER failed"
35       exit 1
36     }
37     GUEST_UID=`echo "$PWENT" | cut -f3 -d:`
38     if [ "$GUEST_UID" -ge 500 ]; then
39       echo "Account $USER is not a system user"
40       exit 1
41     fi
42     HOME=`echo "$PWENT" | cut -f6 -d:`
43     if [ "$HOME" != / ] && [ "${HOME#/tmp}" = "$HOME" ] && [ -d "$HOME" ]; then
44       echo "Home directory of $USER already exists"
45       exit 1
46     fi
47   else
48     # does not exist, so create it
49     adduser --system --no-create-home --home / --gecos $(gettext "Guest") --group --shell /bin/bash $USER || {
50         umount "$HOME"
51         rm -rf "$HOME"
52         exit 1
53     }
54   fi
55
56   # create temporary home directory
57   mount -t tmpfs -o mode=700 none "$HOME" || { rm -rf "$HOME"; exit 1; }
58   chown $USER:$USER "$HOME"
59   gs_skel=/etc/guest-session/skel/
60   if [ -d "$gs_skel" ] && [ -n "`find $gs_skel -type f`" ]; then
61     cp -rT $gs_skel "$HOME"
62   else
63     cp -rT /etc/skel/ "$HOME"
64   fi
65   chown -R $USER:$USER "$HOME"
66   usermod -d "$HOME" "$USER"
67
68   #
69   # setup session
70   #
71
72   # disable some services that are unnecessary for the guest session
73   mkdir --parents "$HOME"/.config/autostart
74   cd /etc/xdg/autostart/
75   services="jockey-kde.desktop jockey-gtk.desktop update-notifier.desktop user-dirs-update-gtk.desktop"
76   for service in $services
77   do
78     if [ -e /etc/xdg/autostart/"$service" ] ; then
79         cp "$service" "$HOME"/.config/autostart
80         echo "X-GNOME-Autostart-enabled=false" >> "$HOME"/.config/autostart/"$service"
81     fi
82   done
83
84   # disable Unity shortcut hint
85   mkdir -p "$HOME"/.cache/unity
86   touch "$HOME"/.cache/unity/first_run.stamp
87
88   STARTUP="$HOME"/.config/autostart/startup-commands.desktop
89   echo "[Desktop Entry]" > $STARTUP
90   echo "Name=Startup commands" >> $STARTUP
91   echo "Type=Application" >> $STARTUP
92   echo "NoDisplay=true" >> $STARTUP
93   echo "Exec=/usr/lib/lightdm/guest-session-auto.sh" >> $STARTUP
94
95   echo "export DIALOG_SLEEP=4" >> "$HOME"/.profile
96
97   mkdir -p "$HOME"/.kde/share/config
98   echo "[Basic Settings]" >> "$HOME"/.kde/share/config/nepomukserverrc
99   echo "Start Nepomuk=false" >> "$HOME"/.kde/share/config/nepomukserverrc
100
101   echo "[Event]" >> "$HOME"/.kde/share/config/notificationhelper
102   echo "hideHookNotifier=true" >> "$HOME"/.kde/share/config/notificationhelper
103   echo "hideInstallNotifier=true" >> "$HOME"/.kde/share/config/notificationhelper
104   echo "hideRestartNotifier=true" >> "$HOME"/.kde/share/config/notificationhelper
105
106   # Load restricted session
107   #dmrc='[Desktop]\nSession=guest-restricted'
108   #/bin/echo -e "$dmrc" > "$HOME"/.dmrc
109
110   # set possible local guest session preferences
111   if [ -f /etc/guest-session/prefs.sh ]; then
112       . /etc/guest-session/prefs.sh
113   fi
114
115   chown -R $USER:$USER "$HOME"
116
117   echo $USER  
118 }
119
120 remove_account ()
121 {
122   USER=$1
123   
124   PWENT=`getent passwd "$USER"` || {
125     echo "Error: invalid user $USER"
126     exit 1
127   }
128   UID=`echo "$PWENT" | cut -f3 -d:`
129   HOME=`echo "$PWENT" | cut -f6 -d:`
130
131   if [ "$UID" -ge 500 ]; then
132     echo "Error: user $USER is not a system user."
133     exit 1
134   fi
135
136   if [ "${HOME}" = "${HOME#/tmp/}" ]; then
137     echo "Error: home directory $HOME is not in /tmp/."
138     exit 1
139   fi
140
141   # kill all remaining processes
142   while ps h -u "$USER" >/dev/null; do 
143     killall -9 -u "$USER" || true
144     sleep 0.2; 
145   done
146
147   umount "$HOME" || umount -l "$HOME" || true
148   rm -rf "$HOME"
149
150   # remove leftovers in /tmp
151   find /tmp -mindepth 1 -maxdepth 1 -uid "$UID" -print0 | xargs -0 rm -rf || true
152
153   # remove possible /media/guest-XXXXXX folder
154   if [ -d /media/"$USER" ]; then
155     for dir in $( find /media/"$USER" -mindepth 1 -maxdepth 1 ); do
156       umount "$dir" || true
157     done
158     rmdir /media/"$USER" || true
159   fi
160
161   deluser --system "$USER"
162 }
163
164 case "$1" in
165   add)
166     add_account
167     ;;
168   remove)
169     if [ -z $2 ] ; then
170       echo "Usage: $0 remove [account]"
171       exit 1
172     fi
173     remove_account $2
174     ;;
175   *)
176     echo "Usage: $0 add|remove"
177     exit 1
178 esac