#!/bin/bash
# (C) 2008 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
# License: GPL v2 or later
# modified by David D Lowe and Thomas Detoux
# (C) 2016 Andrey Cherepanov <cas@altlinux.org>
# Adapt for ALT Linux, get list and enable/disable guest session support

# Debian 7 support by pixline <pixline@gmail.com>
# It NEEDS /bin/bash, dash won't work (sed issues).
#
# Setup user and temporary home directory for guest session.
# If this succeeds, this script needs to print the username as the last line to
# stdout.

. shell-ini-config

conf="/etc/lightdm/lightdm.conf"
prefs="/etc/guest-session/prefs.sh"
shell_ini_config_prefix=''

add_account ()
{
  HOME=`mktemp -p /home -td guest-XXXXXX`
  USER=`echo $HOME | sed 's/\(.*\)guest/guest/' | tr 'A-Z' 'a-z'`

  # if $USER already exists, it must be a locked system account with no existing
  # home directory
  if PWSTAT=`passwd -S "$USER" 2>/dev/null`; then
    if [ "`echo \"$PWSTAT\" | cut -f2 -d\ `" != "L" ]; then
      echo "User account $USER already exists and is not locked"
      exit 1
    fi
    PWENT=`getent passwd "$USER"` || {
      echo "getent passwd $USER failed"
      exit 1
    }
    GUEST_UID=`echo "$PWENT" | cut -f3 -d:`
    if [ "$GUEST_UID" -ge 500 ]; then
      echo "Account $USER is not a system user"
      exit 1
    fi
    HOME=`echo "$PWENT" | cut -f6 -d:`
    if [ "$HOME" != / ] && [ "${HOME#/home}" = "$HOME" ] && [ -d "$HOME" ]; then
      echo "Home directory of $USER already exists"
      exit 1
    fi
  else
    # does not exist, so create it
    useradd --system --no-create-home --home / -c "guest" --shell /bin/bash $USER || {
        umount "$HOME"
        rm -rf "$HOME"
        exit 1
    }
  fi

  gpasswd -a $USER audio

  # create temporary home directory
  mount -t tmpfs -o mode=700 none "$HOME" || { rm -rf "$HOME"; exit 1; }
  chown $USER:$USER "$HOME"
  gs_skel=/etc/guest-session/skel/
  if [ -d "$gs_skel" ] && [ -n "`find $gs_skel -type f`" ]; then
    cp -rT $gs_skel "$HOME"
  else
    cp -rT /etc/skel/ "$HOME"
  fi
  chown -R $USER:$USER "$HOME"
  usermod -d "$HOME" "$USER"

  #
  # setup session
  #

  # disable screensaver, to avoid locking guest out of itself (no password)
#  su $USER <<EOF
#  gconftool-2 --set --type bool /desktop/gnome/lockdown/disable_lock_screen True
#EOF

  # Set empty password to avoid locking guest out of itself
  usermod -p "" $USER

  # disable some services that are unnecessary for the guest session
  mkdir --parents "$HOME"/.config/autostart
  cd /etc/xdg/autostart/
  services="jockey-gtk.desktop update-notifier.desktop user-dirs-update-gtk.desktop"
  for service in $services
  do
    if [ -e /etc/xdg/autostart/"$service" ] ; then
        cp "$service" "$HOME"/.config/autostart
        echo "X-GNOME-Autostart-enabled=false" >> "$HOME"/.config/autostart/"$service"
    fi
  done

  # Load restricted session
  #dmrc='[Desktop]\nSession=guest-restricted'
  #/bin/echo -e "$dmrc" > "$HOME"/.dmrc

  chown -R $USER:$USER "$HOME"

  # set possible local guest session preferences
  if [ -f "$prefs" ]; then
      . "$prefs"
  fi

  echo $USER  
}

remove_account ()
{
  USER=$1
  
  PWENT=`getent passwd "$USER"` || {
    echo "Error: invalid user $USER"
    exit 1
  }
  GUID=`echo "$PWENT" | cut -f3 -d:`
  HOME=`echo "$PWENT" | cut -f6 -d:`

  if [ "$GUID" -ge 500 ]; then
    echo "Error: user $USER is not a system user."
    exit 1
  fi

  # kill all remaining processes
  while ps h -u "$USER" >/dev/null; do 
    killall -9 -u "$USER" || true
    sleep 0.2; 
  done

  umount "$HOME" || umount -l "$HOME" || true
  rm -rf "$HOME"

  userdel -r "$USER" 2>/dev/null
  
}

enable_guest_support()
{
    ini_config_set "$conf" LightDM  guest-account-script guest-account
    ini_config_set "$conf" 'Seat:*' allow-guest          true
    ini_config_set "$conf" 'Seat:*' greeter-allow-guest  true
}

case "$1" in
  add)
    add_account
    ;;
  remove)
    if [ -z $2 ] ; then
      echo "Usage: $0 remove [account]"
      exit 1
    fi
    remove_account $2
    ;;
  list)
    getent passwd |cut -f1 -d:|grep '^guest-'
    ;;
  enable)
    enable_guest_support
    ;;
  autologin)
    enable_guest_support
    ini_config_set "$conf" 'Seat:*' allow-user-switching false
    ini_config_set "$conf" 'Seat:*' autologin-guest      true
    ;;
  disable)
    ini_config_set "$conf" LightDM  guest-account-script ''
    ini_config_set "$conf" 'Seat:*' allow-guest          false
    ini_config_set "$conf" 'Seat:*' greeter-allow-guest  false
    ini_config_set "$conf" 'Seat:*' allow-user-switching true
    ini_config_set "$conf" 'Seat:*' autologin-guest      false
    ;;    
  *)
    cat << END.
Usage: $0 <command>

Available commands:

add        Add guest user
remove     Remove guest user
list       List all guest users
enable     Enable guest session in LightDM
autologin  Enable autologin in guest session in LightDM
disable    Disable guest session in LightDM
END.
    exit 1
esac
