2014-12-25 23:33:08 +00:00
|
|
|
#!/usr/bin/env bash
|
2014-12-25 20:47:26 +00:00
|
|
|
set -eu
|
|
|
|
|
|
|
|
DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
|
|
|
|
MAC_ADDRESS_FILE=${DIR}/tinc-macaddr
|
2014-12-25 23:30:02 +00:00
|
|
|
TINC_SUBNET_FILE=${DIR}/tinc-subnet
|
2014-12-25 20:47:26 +00:00
|
|
|
|
|
|
|
has() {
|
|
|
|
command -v "$1" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
die() {
|
|
|
|
echo $1 &>2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
set_mac() {
|
|
|
|
local interface=$1
|
|
|
|
local mac_address=$2
|
|
|
|
if has ifconfig; then
|
|
|
|
ifconfig "$interface" down
|
|
|
|
if [[ "$OSTYPE" == "linux-gnu" ]]; then
|
|
|
|
ifconfig "$interface" hw ether "$mac_address"
|
|
|
|
else
|
|
|
|
ifconfig "$interface" ether "$mac_address"
|
|
|
|
fi
|
2014-12-25 23:33:08 +00:00
|
|
|
ifconfig "$interface" up
|
2014-12-25 20:47:26 +00:00
|
|
|
elif has ip; then
|
|
|
|
ip link set dev "$interface" down
|
|
|
|
ip link set dev "$interface" address "$mac_address"
|
2014-12-25 23:33:08 +00:00
|
|
|
ip link set dev "$interface" up
|
2014-12-25 20:47:26 +00:00
|
|
|
else
|
|
|
|
die "no suitable program found to configure the network interface, need iproute2 or ifconfig"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-12-25 23:30:02 +00:00
|
|
|
mask2cidr() {
|
|
|
|
local netmask=$1
|
|
|
|
local nbits=0
|
|
|
|
IFS=.
|
|
|
|
for dec in "$netmask"; do
|
|
|
|
case $dec in
|
|
|
|
255) let nbits+=8;;
|
|
|
|
254) let nbits+=7;;
|
|
|
|
252) let nbits+=6;;
|
|
|
|
248) let nbits+=5;;
|
|
|
|
240) let nbits+=4;;
|
|
|
|
224) let nbits+=3;;
|
|
|
|
192) let nbits+=2;;
|
|
|
|
128) let nbits+=1;;
|
|
|
|
0);;
|
|
|
|
*) die "Error: $dec is not recognised"
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
echo "$nbits"
|
|
|
|
}
|
|
|
|
|
|
|
|
set_ip() {
|
|
|
|
local interface=$1
|
|
|
|
local ip=$2
|
|
|
|
local netmask=$3
|
|
|
|
if has ifconfig; then
|
|
|
|
ifconfig "$interface" "$ip" netmask "$netmask"
|
|
|
|
elif has ip; then
|
|
|
|
ip addr add "$ip" dev "$interface"
|
|
|
|
else
|
|
|
|
die "no suitable program found to set ip address, need iproute2 or ifconfig"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-01-01 20:02:33 +00:00
|
|
|
start_dhclient() {
|
|
|
|
local interface=$1
|
|
|
|
if [[ "$OSTYPE" == "linux-gnu" ]]; then
|
|
|
|
dhclient -nw "$interface"
|
|
|
|
elif [[ "$OSTYPE" == "freebsd"* ]]; then
|
|
|
|
dhclient -b "$interface"
|
|
|
|
else
|
|
|
|
nohup setsid dhclient "$interface" >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-12-25 20:47:26 +00:00
|
|
|
start_dhcp() {
|
|
|
|
local interface=$1
|
|
|
|
if has dhcpcd; then
|
2015-01-01 20:02:33 +00:00
|
|
|
dhcpcd -b -n "$interface"
|
2014-12-25 20:47:26 +00:00
|
|
|
elif has dhclient; then
|
2015-01-01 20:02:33 +00:00
|
|
|
start_dhclient "$interface"
|
|
|
|
elif [[ "$OSTYPE" == "darwin"* ]]; then # TODO untested
|
2014-12-25 20:47:26 +00:00
|
|
|
ipconfig set "$interface" DHCP
|
|
|
|
else
|
|
|
|
die "no suitable dhcp program found, need dhcpcd or dhclient"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
[ -z "${INTERFACE:-}" ] && die "no INTERFACE environment variable set"
|
|
|
|
|
|
|
|
if [ ! -f "$MAC_ADDRESS_FILE" ]; then
|
|
|
|
die "${MAC_ADDRESS_FILE} does not exists. Generate it with ./tinc-generate-mac"
|
|
|
|
fi
|
|
|
|
|
|
|
|
read -r MAC_ADDRESS <"$MAC_ADDRESS_FILE"
|
|
|
|
set_mac "$INTERFACE" "$MAC_ADDRESS"
|
|
|
|
|
2014-12-25 23:30:02 +00:00
|
|
|
if [ -e $TINC_SUBNET_FILE ]; then
|
|
|
|
read -a SUBNET <"$TINC_SUBNET_FILE"
|
|
|
|
IP=${SUBNET[0]}
|
|
|
|
NETMASK=${SUBNET[1]}
|
|
|
|
[ -z "$IP" ] && die "no ip set in '$TINC_SUBNET_FILE'"
|
|
|
|
[ -z "$NETMASK" ] && die "no netmask set in '$TINC_SUBNET_FILE'"
|
|
|
|
set_ip "$INTERFACE" "$IP" "$NETMASK"
|
|
|
|
else
|
|
|
|
start_dhcp "$INTERFACE"
|
|
|
|
fi
|