#!/bin/sh
# /usr/sbin/gnuplotconfig - configuration script for suid bit of gnuplot.
# author: tibor simko <simko@debian.org>.
# note: this script modifies `/etc/gnuplot.conf' configuration file.

if [ "root" != "`whoami`" ]
then 
   echo "Sorry, only root can run this script.  Exiting."
   exit 1
fi

echo "Gnuplot suid configuration:"
echo

if [ ! -e /etc/gnuplot.conf ] 
then 
   echo "Sorry, /etc/gnuplot.conf not found.  Exiting."
   exit 1
fi

grep "^is_suid=y.*$" /etc/gnuplot.conf > /dev/null
if [ $? -eq 0 ]; then old=y; else old=n; fi

while [ 1 = 1 ]; do

if [ "$old" = "y" ] 
then 
   echo "  Currently, gnuplot is set up as setuid root, beware!"
else 
   echo "  Currently, gnuplot is not set up as setuid root.  Good."
fi
echo -n "  Do you want to change it?  (y/n/?) [n] "
read yn
echo
test -n "$yn" || yn="n"
case "$yn" in
   [Nn]*)
          echo "Okay, keeping the old configuration."
          exit 0
          ;;
   [Yy]*)
          if [ "$old" = "n" ]
          then 
            sed -e "s/^is_suid=.*$/is_suid=yes/" < /etc/gnuplot.conf > /tmp/gp_$$
            mv -f /tmp/gp_$$ /etc/gnuplot.conf 
            if [ $? -ne 0 ]; then echo; echo "Exiting."; rm -f /tmp/gp_$$; exit 1; fi
            if [ -e /etc/suid.conf -a -x /usr/sbin/suidregister ]; then
               echo "Hmm, you seem to have suidmanager installed.  Will use it."
               suidregister -s gnuplot /usr/bin/gnuplot root root 4755
               echo "Okay, gnuplot is now set up and registered as setuid root."
            else
               echo "Hmm, you don't seem to have suidmanager installed."
               echo "Please consider installing suidmanager in the future."
               chown root.root /usr/bin/gnuplot
               chmod 4755 /usr/bin/gnuplot
               echo "Okay, gnuplot is now manually set up as setuid root."
            fi
            exit 0
          else
            sed -e "s/^is_suid=.*$/is_suid=no/" < /etc/gnuplot.conf > /tmp/gp_$$
            mv -f /tmp/gp_$$ /etc/gnuplot.conf
            if [ $? -ne 0 ]; then echo; echo "Exiting."; rm -f /tmp/gp_$$; exit 1; fi
            chmod u-s /usr/bin/gnuplot
            if [ -e /etc/suid.conf -a -x /usr/sbin/suidunregister ]; then
               echo "Unregistering gnuplot from suidmanager database."
               suidunregister -s gnuplot /usr/bin/gnuplot
            fi
            echo "Okay, gnuplot is not set up as setuid root anymore."
            exit 0
          fi
          ;;
      *)
          echo "    In order to enable ordinary users to use SVGA console graphics,"
          echo "    gnuplot needs to be set up as setuid root.  Please note that"
          echo "    this is usually considered to be a security hazard."
          echo
          ;;
esac

done
