From de1286293437d4fe88121399eae1044d6f728691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Sun, 25 Jan 2015 18:54:42 +0000 Subject: [PATCH] add tincrc --- tinc-generate-mac | 16 +++++++++------- tinc-up | 39 +++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/tinc-generate-mac b/tinc-generate-mac index c094dfd..48181cb 100755 --- a/tinc-generate-mac +++ b/tinc-generate-mac @@ -2,10 +2,13 @@ set -eu -DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd) -MACADDR_FILE="$DIR/tinc-macaddr" -if [ -e "$MACADDR_FILE" ]; then - echo "Skip generating MAC: '$MACADDR_FILE' does already exists." +DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" +TINCRC="$DIR/tincrc" + +source "$TINCRC" || true + +if [ -z "$MAC_ADDRESS" ]; then + echo "Skip generating MAC: MACDDR with value '$MAC_ADDRESS' already defined in '$TINCRC'." exit 1 else # Locally Administered Address Ranges: @@ -14,8 +17,7 @@ else #xA-xx-xx-xx-xx-xx #xE-xx-xx-xx-xx-xx # Replacing x with any hex value. - printf '02:1F:%02X:%02X:%02X:%02X\n' \ + printf 'export MAC_ADDRESS=02:1F:%02X:%02X:%02X:%02X\n' \ $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] \ - > "$MACADDR_FILE" + | tee -a "$TINCRC" fi -cat $MACADDR_FILE diff --git a/tinc-up b/tinc-up index 763fa6e..238fdef 100755 --- a/tinc-up +++ b/tinc-up @@ -14,6 +14,11 @@ set_mac() { local interface=$1 local mac_address=$2 if has ifconfig; then + current_mac=$(ifconfig "$interface" | awk '/ether/ {print $2}') + if [[ "${current_mac,,}" == "${mac_address,,}" ]]; then + return + fi + ifconfig "$interface" down if [[ "$OSTYPE" == "linux-gnu" ]]; then ifconfig "$interface" hw ether "$mac_address" @@ -104,27 +109,37 @@ disable_ipv6_privacy() { DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd) MAC_ADDRESS_FILE="${DIR}/tinc-macaddr" -SUBNET_FILE="${DIR}/tinc-subnet" LOCAL_HOOK_FILE="${DIR}/tinc-up.local" +TINCRC="${DIR}/tincrc" -if [ ! -f "$MAC_ADDRESS_FILE" ]; then - die "${MAC_ADDRESS_FILE} does not exists. Generate it with ./tinc-generate-mac" +MAC_ADDRESS="" +SUBNET="" +DHCP="YES" +IPV6_PRIVACY="YES" + +source "${TINCRC}" || true + +if [ -z "$MAC_ADDRESS" ]; then + echo "${MAC_ADDRESS_FILE} does not exists. Generate it with ./tinc-generate-mac..." + "$DIR/tinc-generate-mac" + source "$TINCRC" fi -read -r MAC_ADDRESS <"$MAC_ADDRESS_FILE" set_mac "$INTERFACE" "$MAC_ADDRESS" -if [ -e $SUBNET_FILE ]; then - read -a SUBNET <"$SUBNET_FILE" - IP=${SUBNET[0]} - NETMASK=${SUBNET[1]} - [ -z "$IP" ] && die "no ip set in '$SUBNET_FILE'" - [ -z "$NETMASK" ] && die "no netmask set in '$SUBNET_FILE'" +if [ -n "$SUBNET" ]; then + IFS=' ' read IP NETMASK <<< "SUBNET" + [ -z "$IP" ] && die "no ip set in SUBNET" + [ -z "$NETMASK" ] && die "no netmask set in SUBNET" set_ip "$INTERFACE" "$IP" "$NETMASK" -else +fi + +if [ "$DHCP" = "YES" ]; then start_dhcp "$INTERFACE" fi -disable_ipv6_privacy "$INTERFACE" +if [[ "$IPV6_PRIVACY" != "YES" ]]; then + disable_ipv6_privacy "$INTERFACE" +fi [ -x "$LOCAL_HOOK_FILE" ] && "$LOCAL_HOOK_FILE" || true