#!/bin/sh
# %%% copyright-nrl-98
# This software is Copyright 1998 by Randall Atkinson, Ronald Lee,
# Daniel McDonald, Bao Phan, and Chris Winters. All Rights Reserved. All
# rights under this copyright have been assigned to the US Naval Research
# Laboratory (NRL). The NRL Copyright Notice and License Agreement Version
# 1.1 (January 17, 1995) applies to this software.
# You should have received a copy of the license with this software. If you
# didn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>.
#
exec 2>> setup-6bone.log
echo >&2
echo "$0 $* invoked `date`" >&2
echo >&2
#
I=/usr/inet6
XPWD=`pwd`
#
echo "6Bone Gateway Configuration, Version 1"
echo "======================================"
echo " "
echo "This script will configure your system to be an IPv6 gateway for the 6Bone."
echo "It REQUIRES that the NRL IPv6+IPsec software or the Linux IPv6 software be"
echo "already installed."
echo " "
echo "This script does no hand-holding. Errors are not checked. Existing files"
echo "will be overwritten. If it destroys your system, it's your problem."
echo " "
echo -n "Do you wish to do this? (y/n) [n] "
read FOO
case "$FOO"
in
  y*) FOO=yes ;;
  *) FOO=no ;;
esac
if [ "$FOO" = no ];
then
  exit 0
fi
echo " "
echo "Okay, here goes!"
echo " "
if [ ! -d $I ];
then
  echo "No $I."
  exit 1
fi

DEFIF=`netstat -in | cut -f1 -d' ' | cut -f1 -d'*' | grep '[0-9]' | grep -v '^lo' | grep -v '^sl' | grep -v '^ppp' | grep -v '^tun' | grep -v '^sit' | sort | uniq | head -1`
echo -n "What's the name of your local network interface? [$DEFIF] "
read IF
if [ -z "$IF" ];
then
  IF="$DEFIF"
fi
echo -n "What's the IPv6 prefix given to you by your 6Bone provider? "
read PREFIX
if [ -z "$PREFIX" ];
then
  echo "You must have an IPv6 prefix from your provider."
  exit 1
fi
PREFIXLEN=`echo $PREFIX | cut -f2 -d/`
if [ -z $PREFIXLEN ];
then
  echo -n "What's the IPv6 prefix length given to you by your 6Bone provider? [48] "
  read PREFIXLEN
  if [ -z "$PREFIXLEN" ];
  then
    echo "You must have an IPv6 prefix length from your provider."
    exit 1
  fi
  if [ `expr $PREFIXLEN % 4` != 0 ];
  then
    echo "Your prefix length is not a multiple of four bits, which causes DNS problems."
    exit 1
  fi
fi
echo -n "What's the IPv4 address of your 6Bone provider? "
read ENDPOINT
if [ -z "$ENDPOINT" ];
then
  echo "You must have the IPv4 address of your provider."
  exit 1
fi

PREFIX=`echo $PREFIX | sed 's/::/X/' | cut -f1 -dX`
IP6INTDOTS=`expr 33 - '(' $PREFIXLEN / 4 ')'`
IP6INT=`/usr/inet6/bin/ip6int $PREFIX::0 | cut -f$IP6INTDOTS- -d. | sed 's:\.$::g'`
IP6INTZEROS=`/usr/inet6/bin/ip6int $PREFIX::0 | cut -f-$IP6INTDOTS -d. | cut -f3- -d.`
HOSTNAME=`hostname`
DOMAIN=`dig +pfmin 1.0.0.127.in-addr.arpa. in ptr | grep -v ';;' | grep localhost | cut -f8- -d. | sed 's:\.$::g'`
FORWARD=`cat /etc/resolv.conf | grep nameserver | grep -v '#' | cut -f2- -d' ' | sed 's:$:;:g'`
USER=`whoami`

echo " "
F=$I/etc/rc.inet6
echo -n "Building $F... "

if [ `uname` = Linux ];
then
  cat >$F << EOF
#!/bin/sh
# Automatically generated by setup-6bone -- but please edit me!

echo 0 >/proc/sys/net/ipv6/conf/default/accept_ra
echo 0 >/proc/sys/net/ipv6/conf/default/accept_redirects
echo 0 >/proc/sys/net/ipv6/conf/default/autoconf
echo 1 >/proc/sys/net/ipv6/conf/default/forwarding

/sbin/ifconfig $IF add $PREFIX::1/64
/sbin/ifconfig sit0 up tunnel ::$ENDPOINT
/sbin/ifconfig sit1 up

/sbin/route -A inet6 add 3ffe::0/15 gw fe80::$ENDPOINT dev sit1

/usr/inet6/bin/radvd -C /usr/inet6/etc/radvd.conf

/usr/sbin/named -b /usr/inet6/etc/named.conf
EOF
else
  cat >$F << EOF
#!/bin/sh
# Automatically generated by setup-6bone -- but please edit me!

/usr/inet6/bin/ifconfig lo0 inet6 auto
/usr/inet6/bin/ifconfig $IF inet6 $PREFIX::1/64

/usr/inet6/bin/route add -net 3ffe::0/15 -tunnel -inet $ENDPOINT

/usr/inet6/bin/radvd -C /usr/inet6/etc/radvd.conf

/usr/sbin/named -b /usr/inet6/etc/named.conf
EOF
fi
echo "done."

mkdir -p $I/etc
F=$I/etc/named.conf
echo -n "Building $F... "
cat >$F << EOF
# Automatically generated by setup-6bone -- but please edit me!

options {
        directory "$I/lib/domain";
        recursion yes;
        listen-on port 53 { any; };
        allow-transfer {
                127.0.0.1;
        };
	forwarders {
		$FORWARD
	};
};

logging {
        channel syslog_it {
                syslog user;
                severity info;
        };
        category queries { syslog_it; };
        category xfer-in { syslog_it; };
        category xfer-out { syslog_it; };
        category security { syslog_it; };
};

zone "0.0.0.0.0.0.0.0.ip6.int" {
        type master;
        file "master/0.0.0.0.0.0.0.0.ip6.int";
        allow-query {
                any;
        };
};

zone "$IP6INT" {
        type master;
        file "master/$IP6INT";
        allow-query {
                any;
        };
};

zone "ipv6.$DOMAIN" {
        type master;
        file "master/ipv6.$DOMAIN";
        allow-query {
                any;
        };
};
EOF
echo "done."

mkdir -p $I/lib/domain/master
F=$I/lib/domain/master/0.0.0.0.0.0.0.0.ip6.int
echo -n "Building $F... "
cat >$F << EOF
; Automatically generated by setup-6bone -- but please edit me!

@               IN      SOA     $HOSTNAME.$DOMAIN. $USER.HOSTNAME.DOMAIN. (
                                1       ; Serial
                                3600    ; Refresh 1 hour
                                900     ; Retry   15 min.
                                604800  ; Expire  48 hours
                                86400 ) ; Minimum 24 hours
                IN      NS      $HOSTNAME.$DOMAIN.

; 1 2 3 4 5 6 7 8 9 a b c d e f 0 1 2 3 4 5 6 7
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 IN PTR localhost.ipv6.$DOMAIN.
EOF
echo "done."

F=$I/lib/domain/master/$IP6INT
echo -n "Building $F... "
cat >$F << EOF
; Automatically generated by setup-6bone -- but please edit me!

@               IN      SOA     $HOSTNAME.$DOMAIN. $USER.HOSTNAME.DOMAIN. (
                                1       ; Serial
                                3600    ; Refresh 1 hour
                                900     ; Retry   15 min.
                                604800  ; Expire  48 hours
                                86400 ) ; Minimum 24 hours
                IN      NS      $HOSTNAME.$DOMAIN.

; 1 2 3 4 5 6 7 8 9 a b c d e f 0 1 2 3 4 5 6 7 8 9 a b c d e f
1.$IP6INTZEROS IN PTR $HOSTNAME.ipv6.$DOMAIN.
EOF
echo "done."

F=$I/lib/domain/master/ipv6.$DOMAIN
echo -n "Building $F... "
cat >$F << EOF
; Automatically generated by setup-6bone -- but please edit me!

@               IN      SOA     $HOSTNAME.$DOMAIN. $USER.HOSTNAME.DOMAIN. (
                                1       ; Serial
                                3600    ; Refresh 1 hour
                                900     ; Retry   15 min.
                                604800  ; Expire  48 hours
                                86400 ) ; Minimum 24 hours
                IN      NS      $HOSTNAME.$DOMAIN.

localhost	IN	AAAA	::1

$HOSTNAME	IN	AAAA	$PREFIX::1
EOF
echo "done."

F=$I/etc/radvd.conf
echo -n "Building $F... "
cat >$F << EOF
# Automatically generated by setup-6bone -- but please edit me!

interface $IF {
        AdvSendAdvert on;
        MaxRtrAdvInterval 600;
        MinRtrAdvInterval 300;
        AdvManagedFlag off;
        AdvOtherConfigFlag off;
        AdvLinkMTU 1500;
        AdvReachableTime 0;
        AdvRetransTimer 0;
        AdvCurHopLimit 64;
        AdvDefaultLifetime 9000;
        AdvSourceLLAddress on;
        prefix $PREFIX::0/64 {
                AdvOnLink on;
                AdvAutonomous on;
                AdvValidLifetime 18000;
                AdvPreferredLifetime 12000;
        };
};
EOF
echo "done."

if [ `uname` != Linux ];
then
  echo " "
  if grep rc.inet6 /etc/rc.local;
  then
    echo "rc.inet6 appears to already be called from your rc.local - not adding it"
  else
    F=$I/etc/radvd.conf
    echo -n "Adding $I/rc.inet6 to /etc/rc.local... "

    cat >$F << EOF

# Automatically added by setup-6bone -- but please edit me!
/usr/inet6/etc/rc.inet6

EOF
    echo "done."
  fi
fi

echo " "
echo "To finish configuration of the system, you need to:"
echo " "
echo " * EDIT the files that were just created"
if [ `uname` != Linux ];
then
  echo " * Build and install an IPv6 kernel with the IPV6GATEWAY option enabled"
else
  echo " * Add /usr/inet6/etc/rc.inet6 to your boot rc scripts"
fi
echo " * Have the administrators of $DOMAIN delegate"
echo "   ipv6.$DOMAIN to the name server on this host."
echo " "
exit 0
