#!/bin/sh

# /etc/rc - master bootup script, invokes php setup
# part of pfSense by Scott Ullrich
# Copyright (C) 2004 Scott Ullrich, All rights reserved.
# originally based on m0n0wall (http://neon1.net/m0n0wall)
# Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
# All rights reserved.

#/bin/stty status '^T'
#/bin/stty susp '^-' intr '^-' quit '^-'

#trap : 2
#trap : 3

# Set our operating platform
PLATFORM=`/bin/cat /etc/platform`

HOME=/
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
export HOME PATH

# Set our current version
version=`/bin/cat /etc/version`

if [ "$PLATFORM" = "cdrom" ]; then
	/etc/rc.cdrom
fi

if [ "$PLATFORM" = "embedded" ]; then
	/etc/rc.embedded
fi

if [ "$PLATFORM" = "nanobsd" ]; then
	/etc/rc.embedded
fi

if [ "$PLATFORM" = "pfSense" ]; then
	mdmfs -S -M -s 4m md /var/run
fi

product=`/bin/cat /etc/inc/globals.inc | /usr/bin/grep product_name | /usr/bin/cut -d'"' -f4`
hideplatform=`cat /etc/inc/globals.inc | grep hideplatform | wc -l`

if [ "$hideplatform" -gt "0" ]; then
	platformbanner="" # hide the platform
else
	platformbanner=" on the '${PLATFORM}' platform"
fi

echo
cat /etc/ascii-art/pfsense-logo-small.txt
echo
echo
echo "Welcome to ${product} ${version}${platformbanner}..."
echo

# Enable console output if its muted.
/sbin/conscontrol mute off >/dev/null

# Mount memory file system if it exists
echo -n "Mounting filesystems..."
/sbin/mount -a

# Mount /. If it fails run a fsck.
if [ ! "$PLATFORM" = "cdrom" ] ; then
	/sbin/mount -uw / || (/sbin/fsck -fy; /sbin/mount -uw /)

	# If /conf is a directory, convert it to a symlink
	# to /cf/conf
	if [ -d "/conf" ]; then
		# If item is not a symlink then rm and recreate
		CONFPOINTSTO=`readlink /conf` 
		if ! test "x$CONFPOINTSTO" = "x/cf/conf"; then 
			rm -rf /conf
			ln -s /cf/conf /conf
		fi
	fi
fi

# Check to see if a compact flash mountpoint exists
# If it fails to mount then run a fsck -fy
if grep -q cf /etc/fstab; then
    /sbin/mount -uw /cf || \
	(/sbin/umount /cf; /sbin/fsck -fy /cf; /sbin/mount -w /cf)
fi

if [ "$PLATFORM" = "cdrom" ] ; then
	# do nothing for cdrom platform
elif [ "$PLATFORM" = "embedded" ] ; then
    # do nothing for embedded platform
elif [ "$PLATFORM" = "nanobsd" ] ; then
	# Ensure that packages can be persistent across reboots
	/bin/mkdir -p /root/var/db/pkg
	rm -rf /var/db/pkg
	ln -s /root/var/db/pkg/ /var/db/pkg
else
    SWAPDEVICE=`cat /etc/fstab | grep swap | cut -f1`
    /sbin/swapon -a 2>/dev/null >/dev/null
fi
echo " done."

echo -n "Creating symlinks..."
# Make sure symlink is correct on embedded
if [ "$PLATFORM" = "embedded" ] ; then
	rm /conf
	ln -s /cf/conf/ /conf
fi

# Make sure symlink is correct on embedded
if [ "$PLATFORM" = "nanobsd" ] ; then
	rm /conf
	ln -s /cf/conf/ /conf
fi

# Repair symlinks if they are broken
if [ ! -L /etc/syslog.conf ]; then
	rm -rf /etc/syslog.conf
	ln -s /var/etc/syslog.conf /etc/syslog.conf
fi

# Repair symlinks if they are broken
if [ ! -L /etc/hosts ]; then
	rm -rf /etc/hosts
	ln -s /var/etc/hosts /etc/hosts
fi

if [ ! -L /etc/resolv.conf ]; then
    rm -rf /etc/resolv.conf
    ln -s /var/etc/resolv.conf /etc/resolv.conf
fi

# Setup compatibility link for packages that
# have trouble overriding the PREFIX configure
# argument since we build our packages in a
# seperated PREFIX area
# Only create if symlink does not exist. 
if [ ! -h /tmp/tmp ]; then
    ln -hfs / /tmp/tmp
fi

# Make sure our /tmp is 777 + Sticky
chmod 1777 /tmp

# Malloc debugging check
if [ ! -L /etc/malloc.conf ]; then
    ln -s aj /etc/malloc.conf
fi

if [ ! -L /etc/dhclient.conf ]; then
    rm -rf /etc/dhclient.conf
fi

if [ ! -L /etc/sasyncd.conf ]; then
    mkdir -p /var/etc/
    touch /var/etc/sasyncd.conf
    rm -rf /etc/sasyncd.conf
    ln -s /var/etc/sasyncd.conf /etc/sasyncd.conf
    chown root:wheel /var/etc/sasyncd.conf
    chmod 0600 /var/etc/sasyncd.conf
fi

[ ! -d /var/tmp ] || mkdir -p /var/tmp 2>/dev/null

[ ! -d /cf/conf/backup/ ] || mkdir -p /cf/conf/backup/ 2>/dev/null

[ ! -f /var/db/ez-ipupdate.cache ] || touch /var/db/ez-ipupdate.cache 2>/dev/null

set -T
trap "echo 'Reboot interrupted'; exit 1" 3

# Remove old nameserver resolution files
rm -f /var/etc/nameserver*

# Create uploadbar tmp directory
mkdir -p /tmp/uploadbar
chmod 777 /tmp/uploadbar

# make some directories in /var
mkdir -p /var/run /var/log /var/etc /var/db/entropy /var/at/jobs/ /var/empty 2>/dev/null
rm /var/log/* 2>/dev/null
rm -rf /var/run/*

echo -n "."
# generate circular logfiles
if [ ! "$PLATFORM" = "cdrom" ]; then
	clog -i -s 512144 /var/log/system.log
	clog -i -s 512144 /var/log/filter.log
	clog -i -s 65535 /var/log/dhcpd.log
	clog -i -s 65535 /var/log/vpn.log
	clog -i -s 65535 /var/log/openvpn.log
	clog -i -s 65535 /var/log/portalauth.log
	clog -i -s 65535 /var/log/ipsec.log
	clog -i -s 65535 /var/log/slbd.log
	clog -i -s 65535 /var/log/lighttpd.log
	clog -i -s 65535 /var/log/ntpd.log
	clog -i -s 65535 /var/log/apinger.log
else
    clog -i -s 65535 /var/log/system.log
    clog -i -s 65535 /var/log/filter.log
    clog -i -s 65535 /var/log/dhcpd.log
    clog -i -s 65535 /var/log/vpn.log
	clog -i -s 65535 /var/log/openvpn.log
    clog -i -s 65535 /var/log/portalauth.log
    clog -i -s 65535 /var/log/ipsec.log
    clog -i -s 65535 /var/log/slbd.log
	clog -i -s 65535 /var/log/ntpd.log
	clog -i -s 65535 /var/log/apinger.log
fi

# change permissions on newly created clog files.
chmod 0600 /var/log/system.log /var/log/filter.log /var/log/dhcpd.log /var/log/vpn.log /var/log/portalauth.log /var/log/slbd.log

echo -n "."
DEVFS=`mount | grep devfs | wc -l | cut -d" " -f8`
if [ "$DEVFS" = "0" ]; then
    mount_devfs devfs /dev
fi

# Create an initial utmp file
cd /var/run && cp /dev/null utmp && chmod 644 utmp

echo -n "."
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib

# Make sure /etc/rc.conf doesn't exist.
if [ -f /etc/rc.conf ]; then
    rm -rf /etc/rc.conf
fi

# Launching kbdmux(4)
if [ -f "/dev/kbdmux0" ]; then
	echo -n "."
	kbdcontrol -k /dev/kbdmux0 < /dev/console
	[ -c "/dev/atkbd0" ] && kbdcontrol -a atkbd0 < /dev/console
	[ -c "/dev/ukbd0" ] && kbdcontrol -a ukbd0 < /dev/console
fi

# Fire up unionfs if mount points exist.
if [ -f /dist/uniondirs ]; then
	echo -n "."
	/etc/rc.d/unionfs start
fi

echo "done."

# Recreate capabilities DB
cap_mkdb /etc/login.conf

if [ "$PLATFORM" = "nanobsd" -o "$PLATFORM" = "embedded" ]; then
	upload_tmp_dir="/root"
else
	upload_tmp_dir="/tmp"
fi

if [ "$PLATFORM" != "cdrom" ]; then
# Populate a dummy php.ini to avoid
# the file being clobbered and the firewall
# not being able to boot back up.
cat >/usr/local/lib/php.ini <<EOF
output_buffering = "0"
expose_php = Off
implicit_flush = true
magic_quotes_gpc = Off
max_execution_time = 99999999
max_input_time = 99999999
register_argc_argv = On
file_uploads = On
upload_tmp_dir = ${upload_tmp_dir}
upload_max_filesize = 90M
post_max_size = 90M
html_errors = Off
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg"
apc.enabled="1"
apc.enable_cli="1"
apc.shm_size="30"
extension_dir=/usr/local/lib/php/extensions/no-debug-non-zts-20020429/
extension=apc.so
extension=bcmath.so
extension=ctype.so
extension=curl.so
extension=mbstring.so
extension=overload.so
extension=pcntl.so
extension=pcre.so
extension=posix.so
extension=radius.so
extension=readline.so
extension=session.so
extension=sysvsem.so
extension=tokenizer.so
extension=xml.so

EOF
fi

rm -f /cf/conf/backup/backup.cache

# Copy php.ini to alternate location after generation
cp /usr/local/lib/php.ini /usr/local/etc/php.ini

# let the PHP-based configuration subsystem set up the system now
echo -n "Launching the init system..."
/etc/rc.bootup

# If a shell was selected from recovery 
# console then just drop to the shell now.
if [ -f "/tmp/donotbootup" ]; then
	echo "Dropping to recovery shell."
	exit 0
fi

echo -n "Starting CRON... "
cd /tmp && /usr/sbin/cron -s 2>/dev/null
echo "done."

# Start packages
/etc/rc.start_packages

rm -rf /usr/local/pkg/pf/CVS

# Remove stale files that have already been processed by bootup
# scripts
rm -f /tmp/filter_dirty
rm -f /tmp/rc.linkup
nohup /usr/bin/nice -n20 /usr/local/sbin/check_reload_status &

# Start ping handler for every 240 seconds
minicron 240 /var/run/ping_hosts.pid /usr/local/bin/ping_hosts.sh

/usr/local/bin/beep.sh start 2>&1 >/dev/null

# Reset the cache.  read-only requires this.
rm /tmp/config.cache

/etc/rc.conf_mount_ro

echo "Bootup complete"

exit 0
