hatestheinternet
post image is Old Modem Front by Rex Roof

Useful Scripts

Having to deal with the mechanics of creating and setting permissions on tunnel interfaces is quite often a pain, but luckily it's something that we can automate away.

/etc/sudoers

Before we can do anything else, we need to make sure the user that's running the BBS can create TAP interfaces and execute the qemu-ifup script as root.  To do this is very simple:

jay ALL=(root) NOPASSWD: /etc/qemu-ifup, /usr/bin/tunctl

In this case, 'jay' is the name of the user that's going to be running the BBS.  If you run into problems, you can replace the names of the commands with ALL, but this will allow the specified user to run everything as root.  This is a Bad IdeaTM.

/etc/qemu-ifup

This script is called whenever qemu brings up a tap interface.  The quick little script below gives it an IP out in left field based on the numeric part of the tap interface's name, then adds a route to it.  If this script isn't run as root, it'll invoke itself again via sudo:

#!/bin/sh
# Make sure we're root if [ $UID -ne 0 ]; then sudo $0 $1 exit fi # Figure out our "node" and IPs NODE=`echo $1 | sed s/tap//g` MY_IP=`expr $NODE + 1` THEIR_IP=`expr $NODE + 2` # Configure the TAP interface and add a route /sbin/ifconfig $1 10.1.254.$MY_IP netmask 255.255.255.255 up /sbin/route add 10.1.254.$THEIR_IP gw 10.1.254.$MY_IP

Please note that for this to work you need to have tun/tap support built in to your kernel.  If you're looking at me with a blank stare, click that blue link.  Be warned, this is not for the faint of heart.

/opt/bbs/bbs

My BBS lives in /opt/bbs and to keep life simple, I named the script to start it bbs.  There's not really much to it, but it got a LOT longer when I added some commenting for you folks:

#!/bin/sh
# Remove the tap interface on unclean exits
killtap() {
    sudo tunctl -d $TAP 2>&1 > /dev/null
    exit
}

# If the BBS is already running, call it a day
bbschk() {
    PID=`ps auxww | grep qemu | grep -v grep | awk '{print $2}'`
    if [ ! "x$PID" = "x" ]; then
        echo "BBS is already running as PID $PID!"
        exit
    fi
}

# Find out what we're doing
case "$1" in
    # Used internally when it's time to start the BBS
    "gotime")
        # Trap unclean exits
        trap killtap INT TERM EXIT

        # Make a TAP interface for us to use
	U=`whoami`
	TAP=`sudo tunctl -b -u $U`

        # Start QEmu
	qemu -vnc :24100 -hda c.hd -m 8 -std-vga -net nic \\
            -net tap,ifname=$TAP -localtime 2> /dev/null

        # Clean up after ourselves
	trap - INT TERM EXIT
	sudo tunctl -d $TAP 2>&1 > /dev/null
	;;<

	# Start the BBS if it's not running
	"start")
	    bbschk
	    /usr/bin/nohup $0 gotime 2> /dev/null > /dev/null &
            ;;

        # Shut down our BBS :(
        "stop")
	    PID=`ps auxww | grep qemu | grep -v grep | awk '{print $2}'`
	    echo "Killing BBS PID $PID..."
	    kill $PID
	    ;;

        # Print usage
	*)
	    echo "HateTheInternet Virtual BBS Script"
	    echo "Usage: $0 [start|stop]"
	    ;;
esac

That's it, now all you need to do is add /opt/bbs/bbs start to your /etc/rc.d/rc.local and every time your machine reboots your BBS will be running automagically in the background.

Caveat Hackor

I am one of the many brave souls who run a distribution of Linux called Slackware.  Your system's start up scripts will undoubtedly be different and there might even be a slight chance that Bash, for whatever reason, isn't your default shell.  If nothing listed here works, spend some time trolling around /etc and see what you come up with.