#!/bin/sh

# File:        sfdconf
#
# Author:      Ulli Horlacher (framstag@rus.uni-stuttgart.de)
#
# History:
#
#   1997-11-23	 Framstag	initial version
#   1998-01-04	 Framstag	added -r option
#
# The daemon configuration helper program for the sendfile package.
#
# Copyright  1997,1998 Ulli Horlacher
# This file is covered by the GNU General Public License

#SPOOL=`sendfile -qW=spool`
SPOOL=/var/spool/sendfile
INLOG=/var/spool/sendfile/LOG/in
OUTLOG=/var/spool/sendfile/LOG/out
CONFIG=/usr/pkg/etc/sendfile.cf
ALIASES=/usr/pkg/etc/sendfile.aliases
DENY=/usr/pkg/etc/sendfile.deny
ALLOW=/usr/pkg/etc/sendfile.allow
EDITOR=${EDITOR:=vi}
PAGER=${PAGER:=more}
PRG=`basename $0`
FILE=


usage() {
  echo "$PRG is the sendfiled configuration helper"
  echo "usage: $PRG OPTION ARGUMENT"
  echo "options:  -l  -- list"
  echo "          -e  -- edit"
  echo "          -i  -- initialize (= edit with default values)"
  echo "          -r  -- receiving mode"
  echo "arguments for options -l -e -i :"
  echo "          config   -- sendfiled configuration file"
  echo "          redirect -- system alias (redirection) file"
  echo "          allow    -- users allow-only file"
  echo "          deny     -- users deny file"
  echo "arguments for option -l :"
  echo "          inlog    -- input log file"
  echo "          outlog   -- output log file"
  echo "arguments for option -r :"
  echo "          enable   -- enable receiving"
  echo "          disable  -- disable receiving"
  echo "examples:"
  echo "          $PRG -l deny    # list the deny file"
  echo "          $PRG -e config  # edit the configuration file"
  exit
}


testfile() {
  error="`(cat $1 >/dev/null) 2>&1 |  sed 's/cat: //'`"
  if [ "$error" ]; then
    echo "%$PRG-Error: $error" >&2
    exit 1
  fi
}


init_append() {
  if [ -r $FILE ]; then
    cat <<EOD>> $FILE.tmp

## The next lines are from your old `basename $FILE` file. 
## You may want to delete them (if they are garbage).

EOD
    cat $FILE >> $FILE.tmp
  fi
  mv $FILE.tmp $FILE
}


init_aliases() {
  cat <<EOD> $FILE.tmp || exit 1
## This is the global aliases file for sendfiled. 
## It is used only for redirection of incoming files or messages!
## Text after a # is a comment and will be ignored.
## The syntax is: ALIAS USER[@HOST], examples:
# zrxh0370	framstag 
# root		admin@bigvax.saft.net

EOD
  init_append
}


init_config() {
  cat <<EOD> $FILE.tmp || exit 1
## This is the sendfiled configuration file.
## Text after a # is a comment and will be ignored.

## If your sendfile-spool is on a NFS volume, you have to distinguish between
## the NFS-clients and the NFS-server !

# accept only messages or files 
# (NFS-clients must set "acceptonly = messages" !)
# acceptonly = messages

# forward address to your generic SAFT-server (e.g. for NFS)
# saftserver = saft.banana.net

### the following section is only relevant if you have a local sendfile spool
###  ( = if the sendfile spool is not an a remote NFS volume)  ###

# you can set here your domain name if necessary
# domain = my.host.org

# where shall new user configuration directories be created (SPOOL/HOME)
userconfig = SPOOL

# allow automatic forwarding of files by the local user (on/off)
forwarding = on

# allow automatic postprocessing of files by the local user (on/off)
piping = on

# allow remote sender to delete his files afterwards (on/off)
deleting = on

# maximum allowed files to receive per user
# setting this option too high will slow down your machine!
maxfiles = 200

# minimum free disk space for your spool partition in MB 
minfree = 5

# maximum total disk space usage for spool in MB 
# (WARNING! Defining this option will sendfile slow down! Better use minfree)
# maxspool = 100

# dontcompress is a list of file extension, which indicate that the
# correspondening file is not compressible.
dontcompress = .zip,.zoo,.arj,.z,.gz,.bz,.bz2,.tgz,.mp3,.gif,.jpg,.tif,.tiff,.png,.avi,.mpeg,.pgp,.rpm,.rar,.deb

# default compression methode (none/gzip/bzip2)
defaultcompress = gzip

# lanspeed defines what to your LAN belongs: if sending to hosts is faster
# than this value (KB/s), then files will not be compressed. If you set
# lanspeed = 0 then this feature will be disabled and every file which
# is not in the dontcompress list (see above) will be compressed.  
lanspeed = 100

# notification by message, mail, both or none when a file arrives
notification = message

# ring the gong when a message arrives (on/off)
bell = on

# keep files in spool at least xx days, then delete them (0=infinity)
keep = 0

# delete aborted or corrupted spool files after xx days (0=never)
deljunk = 10

# global logging of file transfer (in/out/both/none)
log = in

# packet size to send/receive in one chunk
# if you are on a fast and reliable network, set this option to 1024 or 4096
packet = 512

# logging of incoming messages in the user log file (on/off)
msglog = off

# allow O-SAFT fetchfile extension (on/off)
fetchfile = on

# internal environment variable PATH for sendfiled
PATH = /usr/local/bin:/bin:/usr/bin:/opt/bin

# where is the pgp program? (used for O-SAFT/fetchfile)
# optional, if pgp is found in the PATH variable above
# pgp = /usr/local/bin/pgp

# enforce secure incoming file transfer with pgp (sign/encrypt/both/none)
forcepgp = none

# allow spooling of outgoing files (on/nostart/off)
# (nostart means a client is not allowed to start the spool daemon)
spooling = on

# allow parallel sending of spooled files = multiple daemons (on/off)
parallel = on

# limit thruput of outgoing files to xx KB/s (0 = no limit)
# this makes only sense if parallel = off (see above)
maxthruput = 0

# bounce files from outgoing spool back to the local user after xx days
# (when remote host is unreachable)
bounce = 5

# retry delivering of files from outgoing spool after xx minutes
retry = 10

# timeout for clients in seconds
timeout = 300
EOD
  init_append
}


init_deny() {
  cat <<EOD> $FILE.tmp || exit 1
## This is the exclusion list for sendfiled. 
## Users which are listed here are not allowed to receive files or messages.
## Warning: if sendfile.allow contains any user names it will be used as an
## allow-only list and this file here will be ignored.
## Text after a # is a comment.
daemon
bin
news
ftp
lpr
uucp
anonymous
nobody
OUTGOING
LOG
EOD
  init_append
}


init_allow() {
  cat <<EOD> $FILE.tmp || exit 1
## This is the allow-only list for sendfiled. 
## Only users which are listed here are allowed to receive files or messages.
## If this files contains no user names at all, it will be ignored and
## senfile.deny will be used instead as an exclusion list.
## Text after a # is a comment.
EOD
  init_append
}


#args=`getopt h?lei $*` || usage
#set -- x $args
#while shift; do
#  case $1 in
#    -\?|-h) usage;;
#    -l)	    list=true;;
#    -e)     edit=true;;
#    -i)     init=true;;
#    --)     shift; break;;
#  esac
#done

list=
edit=
init=
rmode=

case "$1" in
  -l) list=true;;
  -e) edit=true;;
  -i) init=true;;
  -r) rmode=true;;
  *)  usage;;
esac

if [ "$rmode" = true ]; then
  case "$2" in
    e|enable|y)		rmode=enable;;
    d|disable|n)	rmode=disable;;
    *)			rmode=show;;
  esac
else
  case "$2" in
    r|redirect)		FILE=$ALIASES;;
    c|conf|config)	FILE=$CONFIG;;
    a|allow)		FILE=$ALLOW;;
    d|deny)		FILE=$DENY;;
    i|inlog)		FILE=$INLOG; log=true;;
    o|outlog)		FILE=$OUTLOG; log=true;;
    *)			usage;;
  esac
fi


if [ "$rmode" ]; then
  status=0
  case "$rmode" in
    enable)	rm -f $SPOOL/.nosendfile; status=$?;;
    disable)	touch $SPOOL/.nosendfile; status=$?;;
  esac
  if [ -f $SPOOL/.nosendfile ]; then
    echo "receiving of files is disabled"
  else
    echo "receiving of files is enabled"
  fi
  exit $status
fi

if [ "$log" = true ]; then 
  if [ "$list" != true ]; then usage; fi
  testfile $FILE
  utf7decode $FILE | $PAGER  
  exit $?
fi

if [ "$list" = true ]; then 
  testfile $FILE
  $PAGER $FILE
  exit $?
fi

if [ "$edit" = true ]; then 
  if [ -f $FILE ]; then
    $EDITOR $FILE
    exit $?
  else
    echo "%$PRG-Error: $FILE does not exist" >&2
    echo "%$PRG-Info: you can create it with: $PRG -i $2" >&2
    exit 1
  fi
fi

if [ "$init" = true ]; then 
  trap "rm -f $FILE.tmp;exit" 1 2 3 15
  case $FILE in
    *aliases)		init_aliases;;
    *cf)		init_config;;
    *allow)		init_allow;;
    *deny)		init_deny;;
  esac
  $EDITOR $FILE
  exit $?
fi
