#!/bin/sh
# /usr/sbin/fdutilsconfig - configuration script for suid bit of fdmount.
# Originally written by Tibor Simko <simko@debian.org> for gnuplot.
# Adapted by Anthony Fok <foka@debian.org> for fdmount in fdutils.
# note: this script modifies `/etc/fdmount.conf' configuration file.

set -e
set -C

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

echo "Fdmount suid configuration:"
echo

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

TMPFILE=`mktemp -q /tmp/fdmount.conf.tmp.XXXXXX`
if [ $? -ne 0 ]; then
    echo "$0: Can't create temp file, exiting..."
    exit 1
fi

if grep -q "^is_suid=y.*$" /etc/fdmount.conf; then old=y; else old=n; fi

while true; do

if [ "$old" = "y" ] 
then 
   echo "  Currently, fdmount is set up as setuid root, beware!"
else 
   echo "  Currently, fdmount 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/fdmount.conf >>$TMPFILE
	    chmod 644 $TMPFILE
            mv $TMPFILE /etc/fdmount.conf
            if [ -e /etc/suid.conf -a -x /usr/sbin/suidregister ]; then
               echo "Hmm, you seem to have suidmanager installed.  Will use it."
               suidregister -s fdutils /usr/bin/fdmount root root 4755
               echo "Okay, fdmount 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/fdmount
               chmod 4755 /usr/bin/fdmount
               echo "Okay, fdmount is now manually set up as setuid root."
            fi
            exit 0
          else
            sed -e "s/^is_suid=.*$/is_suid=no/" </etc/fdmount.conf >>$TMPFILE
	    chmod 644 $TMPFILE
            mv $TMPFILE /etc/fdmount.conf
            chmod u-s /usr/bin/fdmount
            if [ -e /etc/suid.conf -a -x /usr/sbin/suidunregister ]; then
               echo "Unregistering fdmount from suidmanager database."
               suidunregister -s fdutils /usr/bin/fdmount
            fi
            echo "Okay, fdmount is not set up as setuid root anymore."
            exit 0
          fi
          ;;
      *)
          echo "    In order to enable ordinary users to mount a floppy to mount"
          echo "    a floppy disk using fdmount, fdmount needs to be set up as"
          echo "    setuid root.  Please note that this is usually considered to"
          echo "    be a security hazard."
          echo
          ;;
esac

done
