setup-alpine-web.in 3.06 KB
Newer Older
1 2
#!/bin/sh

3 4
PROGRAM=${0##*/}

5 6
BRNUM=0
CNET="10.$BRNUM.0"
7 8
HNAME=alpine
DOMAIN="bootstrap.invalid"
9 10 11 12 13 14 15 16 17 18 19 20 21 22
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 ))"
23
	local poolstart="$CNET.$(( $num + 3 ))"
24 25 26 27 28 29 30 31 32 33 34 35
	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() {
36
	echo "option domain-name \"$DOMAIN\";"
37 38 39 40 41
	echo "option domain-name-servers $CNET.1;"
	echo "ddns-update-style none;"
	echo ""
}

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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
Natanael Copa's avatar
Natanael Copa committed
62
	FORCE_SETUP_WEBCONF=yes setup-webconf "$@"
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

	# set up http listener/forwarder
	mkdir -p /var/www/redirect
	cat <<EOF >/var/www/redirect/index.html
<html>
 <head>
  <meta HTTP-EQUIV="REFRESH" content="0; url=https://$(hostname)">
 </head>
 <body></body>
</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 <<EOF
subnet $CNET.0 netmask 255.255.255.0 {
	range $CNET.3 $CNET.14;
	option routers $CNET.1;
}
EOF

	# shut down all interfaces
	for i in $(get_interfaces); do
		ip addr flush dev $i
		[ "$i" = "$iface" ] && continue
		ip link set dev $i down
	done

	# bring interface up again and flush iptables
	ip addr add $oldip dev $iface
	ip addr add $CNET.1/24 dev $iface
	iptables -t nat -F PREROUTING

	/etc/init.d/dhcpd restart
}

usage() {
	cat <<EOF
usage: $PROGRAM [-c X.Y.Z] [-H HOSTNAME] [-R IFACE]
options:
 -c  Use X.Y.Z as network prefix rather than $CNET
 -H  set hostname
 -R  reset previously configured initerfaces and configure IFACE

EOF
	exit 0
}

126
# parse args
127
while getopts "b:c:d:H:hR:" opts; do
128 129
	case "$opts" in
		b) BRNUM="$OPTARG";;
130 131 132
		c) CNET="$OPTARG";;
		d) DOMAIN="$OPTARG";;
		H) HNAME="$OPTARG";;
133
		h) usage;;
134
		R) KEEP_IFACE="$OPTARG";;
135 136 137 138
	esac
done
shift $(( $OPTIND - 1 ))

139 140 141 142
if [ -z "$KEEP_IFACE" ]; then
	do_setup "$@"
	exit 0
fi
143

144
do_reset "$KEEP_IFACE"
145