#!/bin/sh PROGRAM=${0##*/} BRNUM=0 CNET="10.$BRNUM.0" HNAME=alpine DOMAIN="bootstrap.invalid" IFACE_LIST=/tmp/interfaces get_interfaces() { [ -f "$IFACE_LIST" ] || tail -n +3 /proc/net/dev \ | awk -F: '$1 !~ /lo/ { print $1 }' > "$IFACE_LIST" cat "$IFACE_LIST" } make_dhcp_subnet() { local num=$1 local iface=$2 local network="$CNET.$num" local netmask=255.255.255.240 local router="$CNET.$(( $num + 1 ))" local poolstart="$CNET.$(( $num + 3 ))" local poolend="$CNET.$(( $num + 14 ))" echo "subnet $network netmask $netmask {" echo " range $poolstart $poolend;" echo " option routers $router;" echo "}" echo "" ip addr add $router/28 dev $iface || echo "Failed to set address $router/28 on $iface" >&2 ip link set dev $iface up iptables -t nat -A PREROUTING -i $iface -j DNAT --to-destination $router } make_dhcp_global() { echo "option domain-name \"$DOMAIN\";" echo "option domain-name-servers $CNET.1;" echo "ddns-update-style none;" echo "" } do_setup() { local i local count hostname $HNAME # install needed packages apk_add dhcp iptables "$@" # config dhcp server make_dhcp_global > /etc/dhcp/dhcpd.conf count=0 for i in $(get_interfaces); do # maximum 16 network interfaces [ $count -ge 16 ] && break make_dhcp_subnet $(( $count * 16 )) $i >> /etc/dhcp/dhcpd.conf count=$(( $count + 1 )) done /etc/init.d/syslog start /etc/init.d/dhcpd start FORCE_SETUP_WEBCONF=yes setup-acf "$@" # set up http listener/forwarder mkdir -p /var/www/redirect cat </var/www/redirect/index.html EOF echo "E404:/var/www/redirect/index.html" > /etc/httpd.conf echo "HTTPD_OPTS=\"-h /var/www/redirect\"" > /etc/conf.d/httpd /etc/init.d/httpd start # dummy dns echo "* $CNET.1" > /etc/dnsd.conf /etc/init.d/dnsd start } # reconf dhcp and kill all interfaces but $1 do_reset() { local iface=$1 local i local oldip=$(ip addr show dev $iface | awk '/inet / { print $2 } ' | head -n 1) # setup new dhcpd.conf make_dhcp_global > /etc/dhcp/dhcpd.conf cat >> /etc/dhcp/dhcpd.conf <&2 exit 1 fi # parse args while getopts "b:c:d:H:hR:" opts; do case "$opts" in b) BRNUM="$OPTARG";; c) CNET="$OPTARG";; d) DOMAIN="$OPTARG";; H) HNAME="$OPTARG";; h) usage;; R) KEEP_IFACE="$OPTARG";; esac done shift $(( $OPTIND - 1 )) if [ -z "$KEEP_IFACE" ]; then do_setup "$@" exit 0 fi do_reset "$KEEP_IFACE"