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