#!/bin/sh
# You NEED the dialog program to use this!
MYPID=$$
TMPFILE=.bnc$MYPID.tmp
RSLFILE=.bnc$MYPID.rsl
VHTFILE=.bnc$MYPID.vht
BSVERSION="0.7 for BNC2.9.4"
SAVEFILE=./bnc.conf

bsclean()
{
  rm -f $TMPFILE $RSLFILE $VHTFILE
  exit $1
}

saveit()
{
echo "Writing your configuration to $SAVEFILE"
LISTENEXOPTS=
if [ ! -z $BNCHOST ]
then
LISTENEXOPTS="$LISTENEXOPTS --localhost $BNCHOST"
fi

if [ -z $BNCPASS ]
then
usepass=
else
usepass="password $BNCPASS"
fi

if [ $MAXUSERS -ne 0 ]
then
LISTENEXOPTS="$LISTENEXOPTS --limit $MAXUSERS"
fi
cat << EOF > $TMPFILE
#!/home/myhome/bnc
# BNCSetup $BSVERSION by IceWizard and pharos
# automaticly generated configuration file.
# <REQUIRED>
# listen <port> [maxusers]	Port to listen for connections on
# Or..
# listenex [--ssl] [--limit <maxusers>] [--localhost <hostname>] <port>
# adminpass <password>		Password for Supervisor Access	
#
# [Optional]
# privatekey <filename>       Required if SSL server is used
# publickey <filename>        Required if SSL server is used
# password <password>		Password required for general connection (recommended)
# allow <address> [address ...]	list of ip/dns's that can use the bnc
# vhost <address> [address ...] Other Vhosts That Can Be Used (display only)
# defaultvhost <address>	Default Virtual Host to Use
# pidfile <filename>		Name of PID File (recommended if you use crontab)
# motdfile <filename>		Message of the day, displayed on connection of valid user
# logfile <filename>		Filename where to store logs
#
# EXAMPLE CONFIG - CHANGE THIS:
# if your pass starts with + it is encrypted, if not it is a normal pass
#
listenex $LISTENEXOPTS $BNCPORT 
adminpass $SUPERPASS
$usepass
EOF

# S:$SUPERPASS
# D:$BNCPORT:$MAXUSERS:$BNCPASS



cat $TMPFILE $VHTFILE > $SAVEFILE
echo "Finished! You may now edit $SAVEFILE to make any special changes"

bsclean 0
}

altconf()
{
  echo "##" > $VHTFILE
  echo "Running alt configuration tool"
  echo -n "Port to listen on> " 
  read BNCPORT
  echo "How many users would you like to be allowed on BNC at the same time?"
  echo "Please note, if you would like to allow unlimited users, enter a 0"
  echo -n "> "
  read MAXUSERS
  echo -n "Pick a password >"
  read BNCPASS
  echo -n "Pick a password for the superuser >"
  read SUPERPASS
  saveit

}


echo "BAD\$" > $TMPFILE
echo "Test" | mkpasswd -s >> $TMPFILE
CRCYS=$(cat $TMPFILE | grep '\$' | tail -1 )


if [ "BAD\$" = "$CRCYS" ]
then
  CRCY=ON
else
  echo "Your OS is using an unstandard Crypt lib, disabling encryption."
  CRCY=OFF
  echo "Continuing script in 4 seconds"
  sleep 4
fi

dialog --clear
ERRORLEVEL=$?
if [ x$ERRORLEVEL != x0 ]
then
  echo "You appear to not have the program named dialog"
  echo "This script depends on it heavily"
  altconf
  bsclean 0
fi



cat << EOF > $TMPFILE
BNCSETUP v$BSVERSION

Written by IceWizard
Rewritten by Pharos
BNCSetup is a quick BNC conf generator

EOF
clear
dialog --title "BNCSETUP by IceWizard" --msgbox "$(cat $TMPFILE)" 10 50

cat << EOF > $TMPFILE
What port would you like BNC to listen on?
This must be a number greater than 1024 and must not be in use.

EOF

BNCPORT=
while [ -z "$BNCPORT" ]
do

  dialog --title "Port to listen on" --inputbox "$(cat $TMPFILE)" 10 68 "6669" 2> $RSLFILE
  ERRORLEVEL=$?
  BNCPORT="$(head -1 $RSLFILE)"
  if [ x$ERRORLEVEL = x1 ]
  then
    clear
    echo "BNC Configuration was cancelled..."
    bsclean 0
  fi
done

BNCHOST=
cat << EOF > $TMPFILE
Enter the host IP or DNS that bnc will use.
EOF

dialog --yesno "Enter a host for BNC to listen on (default all hosts)?" 5 60
ERRORLEVEL=$?

if [ x$ERRORLEVEL = x0 ]
then 
  dialog --title "Enter host" --inputbox "$(cat $TMPFILE)" 10 62 2> $RSLFILE
  BNCHOST="$(head -1 $RSLFILE)"
fi



cat << EOF > $TMPFILE
How many users would you like to be allowed on BNC at the same time?
Please note, if you would like to allow unlimited users, enter a 0

EOF

MAXUSERS=
while [ -z "$MAXUSERS" ]
do
  dialog --title "Maximum users" --inputbox "$(cat $TMPFILE)" 10 78 "0" 2> $RSLFILE
  ERRORLEVEL=$?
  MAXUSERS="$(head -1 $RSLFILE)"
  if [ x$ERRORLEVEL = x1 ]
  then
    clear
    echo "BNC Configuration was cancelled..."
    bsclean 0
  fi
done

cat << EOF > $TMPFILE
Pick a password you would like to use for BNC?
This is the pass that will be used upon
/quote pass PASS
when using bnc.

EOF

BNCPASS=
while [ -z "$BNCPASS" ]
do

  dialog --title "Pick a password" --inputbox "$(cat $TMPFILE)" 12 60 2> $RSLFILE
  ERRORLEVEL=$?
  BNCPASS="$(head -1 $RSLFILE)"
  if [ x$ERRORLEVEL = x1 ]
  then
    clear
    echo "BNC Configuration was cancelled..."
    bsclean 0
  fi
done

cat << EOF > $TMPFILE
Pick a superviser password you would like to use for BNC?
If you are the only person to use your BNC,
feel free to enter the same password.

EOF

SUPERPASS=
while [ -z "$SUPERPASS" ]
do
  dialog --title "Pick a superviser password" --inputbox "$(cat $TMPFILE)" 10 62 2> $RSLFILE
  ERRORLEVEL=$?
  SUPERPASS="$(head -1 $RSLFILE)"
  if [ x$ERRORLEVEL = x1 ]; then
    clear
    echo "BNC Configuration was cancelled..."
    bsclean 0
  fi
done

if [ xON = x$CRCY ]
then

dialog --yesno "Use encrypted passes in config file (Securety)" 5 60
ERRORLEVEL=$?

if [ x$ERRORLEVEL = x0 ]
then 
  if [ -x /usr/local/bin/mkpasswd ]
  then
    SUPERPASS="+"$(echo "$SUPERPASS" | mkpasswd -s)
    BNCPASS="+"$(echo "$BNCPASS" | mkpasswd -s)
  else
    clear
    echo "You have not did ./configure and make so there is no mkpasswd"
    echo "This program is required to produce the encryption"
    bsclean 0
  fi
fi
fi

rm -f $VHTFILE
dialog --radiolist "Choose Allow method" 10 70 3 \
"0" "Allow ANY ip to use bnc if they know the pass" "on" \
"1" "Go to menu for creating ip lists" "off" 2> $RSLFILE
ERRORLEVEL=$?
if [ x$ERRORLEVEL = x1 ]; then
  clear
  echo "BNC Configuration was cancelled..."
  bsclean 0
fi
ERRORLEVEL="$(head -1 $RSLFILE)"

cat << EOF > $TMPFILE
Enter an IP to be allowed to use BNC.
this ip can include *'s and ?'s and other wildcard stuff.
enter q alone to end entering IP's
EOF

if [ x$ERRORLEVEL = x1 ]
then
BDONE=
  while ! [ x$BDONE = xq ]
  do
    dialog --title "Enter Wildcard IP" --inputbox "$(cat $TMPFILE)" 10 62 2> $RSLFILE
    BDONE="$(head -1 $RSLFILE)"
    if ! [ -z BDONE ]
    then
      if ! [ x$BDONE = xq ]
      then
        echo "allow $BDONE" >> $VHTFILE
      fi
    fi
  done
fi

if ! [ -e "$VHTFILE" ]
then
  echo "allow *" > $VHTFILE
fi


cat << EOF > $TMPFILE
Enter a Default Vhost IP or DNS that bnc will use.
EOF

dialog --yesno "Enter a default Vhost to use?" 5 60
ERRORLEVEL=$?

if [ x$ERRORLEVEL = x0 ]
then 
  dialog --title "Enter Default Vhost" --inputbox "$(cat $TMPFILE)" 10 62 2> $RSLFILE
  BDONE="$(head -1 $RSLFILE)"
  if ! [ -z BDONE ]
  then
    echo "defaultvhost $BDONE" >> $VHTFILE
  fi
fi

dialog --yesno "Enter a list of Vhost possiblities?" 5 60
ERRORLEVEL=$?

cat << EOF > $TMPFILE
Enter an Vhost that can be used by this bnc.
This has no bearing on how bnc functions but is simply
what is listed when the user does:
/quote vip
enter q alone to end entering IP's
EOF

if [ x$ERRORLEVEL = x0 ]
then
BDONE=
  while ! [ x$BDONE = xq ]
  do
    dialog --title "Enter Vhost possibility" --inputbox "$(cat $TMPFILE)" 15 62 2> $RSLFILE
    BDONE="$(head -1 $RSLFILE)"
    if ! [ -z BDONE ]
    then
      if ! [ x$BDONE = xq ]
      then
        echo "vhost $BDONE" >> $VHTFILE
      fi
    fi
  done
fi


dialog --yesno "Would you like bnc to log?" 5 60
ERRORLEVEL=$?

cat << EOF > $TMPFILE
Pick a filename for bnc to write logging info into.
if you do not want to use the default

EOF

if [ x$ERRORLEVEL = x0 ]
then 
  dialog --title "Enter logfile Filename" --inputbox "$(cat $TMPFILE)" 10 62 "bnc.log" 2> $RSLFILE
  BDONE="$(head -1 $RSLFILE)"
  if ! [ -z BDONE ]
  then
    echo "logfile $BDONE" >> $VHTFILE
  fi
fi

dialog --yesno "Would you like bnc to display a motd?" 5 60
ERRORLEVEL=$?

cat << EOF > $TMPFILE
Pick a filename for bnc to show as a motd
if you do not want to use the default

EOF

if [ x$ERRORLEVEL = x0 ]
then 
  dialog --title "Enter motd Filename" --inputbox "$(cat $TMPFILE)" 10 62 "motd" 2> $RSLFILE
  BDONE="$(head -1 $RSLFILE)"
  if ! [ -z BDONE ]
  then
    echo "motdfile $BDONE" >> $VHTFILE
  fi
fi

dialog --title "Requires Identwd to be installed." --yesno "Do you want bnc's dynamic ident support?" 5 60
ERRORLEVEL=$?
if [ x$ERRORLEVEL = x0 ]
then
  echo "useidentwd 1" >> $VHTFILE
fi




cat << EOF > $TMPFILE
Pick a file to save the configuration in
if you do not want to use the default

EOF

SAVEFILE=
while [ -z "$SAVEFILE" ]
do
  dialog --title "Save as" --inputbox "$(cat $TMPFILE)" 10 45 "bnc.conf" 2> $RSLFILE
  ERRORLEVEL=$?
  SAVEFILE="$(head -1 $RSLFILE)"
  if [ x$ERRORLEVEL = x1 ]; then
    clear
    echo "BNC Configuration was cancelled..."
    bsclean 0
  fi
done
clear
if [ -e "$SAVEFILE" ]
then
  echo "BNCsetup found a configuration file already... saving it as $SAVEFILE.old"
  mv $SAVEFILE "$SAVEFILE".old
fi

saveit



syntax highlighted by Code2HTML, v. 0.9.1