#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description:
#  setup autologin mode for DRBL Clients

imagehost="/var/lib/diskless/default"
default_display_manager_file="/etc/X11/default-display-manager"

# must be root
ID=`id -u`
if [ "$ID" != "0" ]; then
  echo "You must be root to use this script."
  echo "Use sudo to enable it for users."
  exit 1
fi

### check if gdm or kdm is installed..
if [ "$(chroot $imagehost/root dpkg -l gdm | grep "gdm" | cut -d' ' -f1)" != "ii" -a \
     "$(chroot $imagehost/root dpkg -l kdm | grep "kdm" | cut -d' ' -f1)" != "ii" ]; then 
  echo "Either kdm nor gdm is installed..Abort!!"
  exit 0
fi

## warning ...
if [ "$1" = "start" ]; then
  if [ "$FORCE_YES" = "" ]; then 
    echo "This program will generate some users for auto login"
    echo "Please make sure you know what you are doing before you start!!"
    echo -n "Continue [Y|n]? "
    read ANS
  else
    ANS="Y"
  fi

  if [ "$ANS" = "n" -o "$ANS" = "N" ]; then exit; fi
fi

### create all hosts before setup autologin ..
if [ -f /etc/default/drbl ]; then
  . /etc/default/drbl
fi

if [ "$NO_DRBLMGRD" = "true" -o "$NO_DRBLMGRD" = "" ]; then
  ## no drblmgrd
  if [ ! -f /opt/drbl/sbin/drbl_deploy.sh ]; then
    echo "DRBL Configuration is not correct!"
    echo "Please use '/opt/drbl/sbin/drblpush-desktop' to setup!!"
    exit
  else
    for ip in `cat /etc/dhcp3/dhcpd.conf | grep "fixed-address" | cut -d\; -f1 | awk '{ print $2; }'`
    do
      ## if one host lost in $imagehost, regenerate all ..
      if [ ! -d $imagehost/$ip ]; then
        /opt/drbl/sbin/drbl_deploy.sh
        break
      fi
    done
  fi
else
  ## drblmgrd
  drblmgrd=`/sbin/ifconfig eth0 | grep "inet addr" | cut -d: -f2 | awk '{ print $1; }'`
  drblmgrd_ps=`ps -e 2> /dev/null | grep "drblmgrd"`
  if [ "$drblmgrd" = "" -o "$drblmgrd_ps" = "" ]; then
    echo "DRBL Configuration is not correct!"
    echo "Please use '/opt/drbl/sbin/drblpush-desktop' to setup!!"
    exit
  fi

  for ip in `cat /etc/dhcp3/dhcpd.conf | grep "fixed-address" | cut -d\; -f1 | awk '{ print $2; }'`
  do
    if [ ! -d $imagehost/$ip ]; then
      echo "create host: $ip"
      echo "$ip" | /usr/bin/socket $drblmgrd 6460 
    fi
  done
fi

### main
apt-get -y install makepasswd
case $1 in
  'start')
    if [ -f $imagehost/root/etc/diskless-image/autologin ]; then
      echo "autologin is enable. abort!!"
      exit
    fi
    if [ "$2" != "" ]; then
      echo "autologin account will sync its desktop with the account '$2'" > $imagehost/root/etc/diskless-image/autologin
    fi

    TMP=`mktemp /tmp/autologin.XXXXXX`
    for host in `ls $imagehost`
    do
      ## skip these $host
      if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then continue; fi
      skip=0
      for netdev in `cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}'`
      do
        ip=`/sbin/ifconfig $netdev | grep "inet addr" | cut -d: -f2 | awk '{ print $1; }'`
        if [ "$host" = "$ip" ]; then skip=1; fi
      done
      if [ $skip -eq 1 ]; then continue; fi

      ## username
      user_exists="not_yet"
      while [ "$user_exists" != "" ]; do
        username=`/usr/bin/makepasswd --chars 5`
        username="u$username"
        user_exists=`grep -e "^$username:" /etc/passwd`
      done
      echo "$username 8" >> $TMP

      echo "create the account '$username' for host '$host'" >> $imagehost/root/diskless-image/autologin

      ## enable auto login
      echo "enable auto login for $host"
      if [ "$(cat $imagehost/$host/$default_display_manager_file)" = "/usr/bin/gdm" ]; then
        /usr/bin/perl -p -i -e "s/AutomaticLoginEnable=.*/AutomaticLoginEnable=true/" $imagehost/$host/etc/gdm/gdm.conf
        /usr/bin/perl -p -i -e "s/AutomaticLogin=.*/AutomaticLogin=$username/" $imagehost/$host/etc/gdm/gdm.conf
      elif [ "$(cat $imagehost/$host/$default_display_manager_file)" = "/usr/bin/kdm" ]; then
        /usr/bin/perl -p -i -e "s/AutoLoginEnable=.*/AutoLoginEnable=true/" $imagehost/$host/etc/kde3/kdm/kdmrc
        /usr/bin/perl -p -i -e "s/AutoLoginUser=.*/AutoLoginUser=$username/" $imagehost/$host/etc/kde3/kdm/kdmrc
      fi

      ## sync
      if [ "$2" != "" ]; then
        tail +181 $0 > $imagehost/$host/etc/rc2.d/S95drbl-autologin 
        perl -p -i -e "s/SYNC_USER=.*/SYNC_USER=$2/" $imagehost/$host/etc/rc2.d/S95drbl-autologin
        perl -p -i -e "s/AUTOLOGIN_USER=.*/AUTOLOGIN_USER=$username/" $imagehost/$host/etc/rc2.d/S95drbl-autologin
        chmod 755 $imagehost/$host/etc/rc2.d/S95drbl-autologin
      fi
    done
    /opt/drbl/sbin/drbl-useradd --list $TMP
    rm -f $TMP

    touch $imagehost/root/etc/diskless-image/autologin
    ;;

  'stop')
    if [ ! -f $imagehost/root/etc/diskless-image/autologin ]; then
      echo "autologin is not enable. abort!!"
      exit
    fi

    TMP=`mktemp /tmp/autologin.XXXXXX`
    for host in `ls $imagehost`
    do
      ## skip these $host
      if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then continue; fi
      skip=0
      for netdev in `cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}'`
      do
        ip=`/sbin/ifconfig $netdev | grep "inet addr" | cut -d: -f2 | awk '{ print $1; }'`
        if [ "$ip" != "" -a "$host" = "$ip" ]; then skip=1; fi
      done
      if [ $skip -eq 1 ]; then continue; fi

      ## disable autologin configuration
      echo "disable autologin at host $host"
      username=`grep -e "AutoLoginUser=.*" $imagehost/$host/etc/kde3/kdm/kdmrc | cut -d= -f2`
      if [ "$username" != "" -a "$username" != "root" ]; then echo "$username" >> $TMP; fi

      if [ "$(cat $imagehost/$host/$default_display_manager_file)" = "/usr/bin/gdm" ]; then
        /usr/bin/perl -p -i -e "s/AutomaticLoginEnable=.*/AutomaticLoginEnable=false/" $imagehost/$host/etc/gdm/gdm.conf
      elif [ "$(cat $imagehost/$host/$default_display_manager_file)" = "/usr/bin/kdm" ]; then
        /usr/bin/perl -p -i -e "s/AutoLoginEnable=.*/AutoLoginEnable=false/" $imagehost/$host/etc/kde3/kdm/kdmrc
      fi
      rm -f $imagehost/$host/etc/rc2.d/S95drbl-autologin
    done
    /opt/drbl/sbin/drbl-userdel --list $TMP
    rm -f $TMP
    rm -f $imagehost/root/etc/diskless-image/autologin
    ;;
  *)
    echo "Usage: "
    echo "  $0 start [sync_username]"
    echo "  $0 stop"
    ;;
esac
exit 0
#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: sync autologin account's Desktop

SYNC_USER=""
AUTOLOGIN_USER=""

case $1 in
  "start")
    if [ "$SYNC_USER" = "$AUTOLOGIN_USER" ]; then exit; fi
    if [ ! -d /home/$SYNC_USER ]; then exit; fi
    if [ ! -d /home/$AUTOLOGIN_USER ]; then mkdir -p /home/$AUTOLOGIN_USER; fi

    # .kde
    rm -rf /home/$AUTOLOGIN_USER/.kde
    mkdir -p /home/$AUTOLOGIN_USER/.kde
    cp -r /home/$SYNC_USER/.kde/Autostart /home/$AUTOLOGIN_USER/.kde
    cp -r /home/$SYNC_USER/.kde/share /home/$AUTOLOGIN_USER/.kde
    cp /etc/openoffice/openoffice.desktop /home/$AUTOLOGIN_USER/.kde/Autostart

    # Desktop
    rm -rf /home/$AUTOLOGIN_USER/Desktop
    cp -r /home/$SYNC_USER/Desktop /home/$AUTOLOGIN_USER

    chown -R $AUTOLOGIN_USER.users /home/$AUTOLOGIN_USER
    ;;
  "stop")
    ;;
esac
exit 0
