From 8c11e39a58e69de9b1912756082609f5ffb0dcb1 Mon Sep 17 00:00:00 2001 From: Lassulus Date: Sun, 9 Dec 2012 03:06:10 +0100 Subject: new version, still WIP --- retiolum/scripts/tinc_setup/new_install.sh | 213 +++++++++++++++++++++++++---- 1 file changed, 188 insertions(+), 25 deletions(-) (limited to 'retiolum') diff --git a/retiolum/scripts/tinc_setup/new_install.sh b/retiolum/scripts/tinc_setup/new_install.sh index ab42aedc..52bb4ddb 100755 --- a/retiolum/scripts/tinc_setup/new_install.sh +++ b/retiolum/scripts/tinc_setup/new_install.sh @@ -1,5 +1,12 @@ #!/bin/sh +#get sudo +if test "${nosudo-false}" != true -a `id -u` != 0; then + echo "we're going sudo..." >&2 + exec sudo "$0" "$@" + exit 23 # go to hell +fi + usage() { cat << EOF @@ -10,15 +17,35 @@ all parameters are optional Options: -h Show this message(haha) -4 \$ipv4 specify an ip(version 4), this also disables random ip mode, default is random + -6 \$ipv6 specify an ip(version 6), this also disables random ip mode, default is random + -s \$SUBNET Choose another Subnet(version4), default is 10.243 + -x \$SUBNET Choose another Subnet(version6), default is 42 + -m \$MASK Choose another Subnet Mask(version4), default is 16 + -j \$MASK Choose another Subnet Mask(version6), default is 16 -t \$DIR Choose another Temporary directory, default is /tmp/tinc-install-fu -o \$HOST Choose another Hostname, default is your system hostname -n \$NET Choose another tincd netname,this also specifies the path to your tinc config, default is retiolum - -s \$SUBNET Choose another Subnet(version4), default is 10.243. - -m \$MASK Choose another Subnet Mask(version4), default is /16 -u \$URL specify another hostsfiles.tar.gz url, default is euer.krebsco.de/retiolum/hosts.tar.gz + -l \$OS specify an OS, numeric parameter.0=Automatic 1=ArchLinux 2=OpenWRT, disables automatic OS-finding, default is 0 + -r \$ADDR give the node an reachable remote address, ipv4 or dns EOF } +#convert hostmask to subnetmask only version 4 +host2subnet() +{ + NEEDDOTSINSUB=$(expr 3 - $(echo $SUBNET4 | sed 's/[0-9]*//g')) + FULLSUBNET=$(echo $SUBNET4$(eval "printf '.0'%.0s {1..${#NEEDDOTSINSUB}}"s)) + + result=$(($(($((1 << $1)) - 1)) << $((32 - $1)))) + byte="" + for ((i=0;i<3;i+=1)); do + byte=.$(($result % 256))$byte + result=$(($result / 256)) + done + RETARDEDMASK=$result$byte +} + #check if ip is valid ipv4 function check_ip_valid4() { @@ -30,20 +57,29 @@ check_ip_valid4() fi } +#check if ip is valid ipv6 function +check_ip_valid6() +{ + if [ "$(echo $1 | awk -F"." ' $0 ~ /^([0-9a-fA-F]{1,4}\:){7}[0-9a-fA-F]{1,4}$/' 2>/dev/null)" == $1 ] && [ ${1:0:${#SUBNET6}} == $SUBNET6 ] + then + return 0 + else + return 1 + fi +} + #check if ip is taken function check_ip_taken() { if grep -q -E "$1(#|/)" $TEMPDIR/hosts/* ;then - echo $1 is taken return 1 else - echo $1 seems free return 0 fi } #if hostname is taken, count upwards until it isn't taken function -check_hostname() +get_hostname() { TSTFILE=$TEMPDIR/hosts/$1 LCOUNTER=0 @@ -58,38 +94,52 @@ check_hostname() fi } +#os autodetection +find_os() +{ + if grep -q "Arch Linux" /etc/*release; then + OS=1 + elif grep -q "OpenWrt" /etc/*release; then + OS=2 + fi +} + +SUBNET4=10.243 +SUBNET6=42 TEMPDIR=/tmp/tinc-install-fu HOSTN=$(hostname) NETNAME=retiolum -SUBNET4=10.243. -MASK4=/16 -RAND=1 +MASK4=16 +MASK6=16 +RAND4=1 +RAND6=1 URL=euer.krebsco.de/retiolum/hosts.tar.gz +OS=0 #check if everything is installed -if $(! test -e "/usr/sbin/tincd"); then +if ! which tincd&>/dev/null; then echo "Please install tinc" exit 1 fi -if $(! test -e /usr/bin/awk); then +if ! which awk&>/dev/null; then echo "Please install awk" exit 1 fi -if $(! test -e /usr/bin/curl); then +if ! which curl&>/dev/null; then echo "Please install curl" exit 1 fi -if $(! /bin/ping -c 1 euer.krebsco.de -W 5 &>/dev/null) ;then +if ! $(/bin/ping -c 1 euer.krebsco.de -W 5 &>/dev/null) ;then echo "Cant reach euer, check if your internet is working" exit 1 fi #parse options -while getopts "h4:t:o:n:s:m:u:" OPTION +while getopts "h4:6:s:x:m:j:t:o:n:u:l:" OPTION do case $OPTION in h) @@ -98,8 +148,25 @@ do ;; 4) IP4=$OPTARG - RAND=0 - if ! check_ip_valid4 $IP4; then echo "ip is invalid" && exit 1; fi + RAND4=0 + if ! check_ip_valid4 $IP4; then echo "ipv4 is invalid" && exit 1; fi + ;; + 6) + IP6=$OPTARG + RAND6=0 + if ! check_ip_valid6 $IP6; then echo "ipv6 is invalid" && exit 1; fi + ;; + s) + SUBNET4=$OPTARG + ;; + x) + SUBNET6=$OPTARG + ;; + m) + MASK4=$OPTARG + ;; + j) + MASK6=$OPTARG ;; t) TEMPDIR=$OPTARG @@ -110,12 +177,6 @@ do n) NETNAME=$OPTARG ;; - s) - SUBNET4=$OPTARG - ;; - m) - MASK4=$OPTARG - ;; u) URL=$OPTARG if $(! curl -s --head $URL | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null); then @@ -123,10 +184,22 @@ do exit 1 fi ;; + l) + OS=$OPTARG + if ! [ "$(echo $OS | awk -F"." ' $0 ~ /^[0-2]$/' )" == $OS ]; then + echo "invalid input for OS" + exit 1 + fi + ;; + r) + ADDR=$OPTARG + ;; esac done +#generate full subnet information for v4 + #test if tinc directory already exists if test -e /etc/tinc/$NETNAME; then echo "tinc config directory /etc/tinc/$NETNAME does already exist. (backup and) delete config directory and restart" @@ -138,9 +211,10 @@ mkdir -p $TEMPDIR/hosts curl euer.krebsco.de/retiolum/hosts.tar.gz | tar zx -C $TEMPDIR/hosts/ #check for free ip +#version 4 until check_ip_taken $IP4; do - if [ $RAND -eq 1 ]; then - IP4="10.243.$((RANDOM%255)).$((RANDOM%255))" + if [ $RAND4 -eq 1 ]; then + IP4="$SUBNET4.$((RANDOM%255)).$((RANDOM%255))" else printf 'choose new ip: ' read IP4 @@ -151,8 +225,97 @@ until check_ip_taken $IP4; do fi done +#version 6 +until check_ip_taken $IP6; do + if [ $RAND6 -eq 1 ]; then + IP6="$SUBNET6$(openssl rand -hex 14 | sed 's/..../:&/g')" #todo: generate ip length from hostmask + else + printf 'ip taken, choose new ip: ' + + read IP6 + while ! check_ip_valid6 $IP6; do + printf 'the ip is invalid, retard, choose a valid ip: ' + read IP6 + done + fi +done + + #check for free hostname -check_hostname $HOSTN +get_hostname $HOSTN -echo "your ip is $IP4" +#check for OS +if [ $OS -eq 0 ]; then + echo $OS + find_os +fi + +#create the configs +mkdir -p /etc/tinc/$NETNAME +cd /etc/tinc/$NETNAME + +mv $TEMPDIR/hosts ./ + +echo "Subnet = $IP4" > hosts/$HOSTN +echo "Subnet = $IP6" >> hosts/$HOSTN + +cat>tinc.conf</dev/null; then + echo 'dirname="`dirname "$0"`"' > tinc-up + echo '' >> tinc-up + echo 'conf=$dirname/tinc.conf' >> tinc-up + echo '' >> tinc-up + echo 'name=$(sed -n "s|^ *Name *= *\([^ ]*\) *$|\1|p " $conf)' >> tinc-up + echo '' >> tinc-up + echo 'host=$dirname/hosts/$name' >> tinc-up + echo '' >> tinc-up + echo 'ip link set $INTERFACE up' >> tinc-up + echo '' >> tinc-up + echo "addr4=\$(sed -n \"s|^ *Subnet *= *\\($SUBNET4[.][^ ]*\\) *$|\\1|p\" \$host)" >> tinc-up + echo 'ip -4 addr add $addr4 dev $INTERFACE' >> tinc-up + echo "ip -4 route add $FULLSUBNET/$MASK4 dev \$INTERFACE" >> tinc-up + echo '' >> tinc-up + echo "addr6=\$(sed -n \"s|^ *Subnet *= *\\($SUBNET6[:][^ ]*\\) *$|\\1|p\" \$host)" >> tinc-up + echo 'ip -6 addr add $addr6 dev $INTERFACE' >> tinc-up + echo "ip -6 route add $SUBNET6::/$MASK6 dev \$INTERFACE" >> tinc-up +else + echo 'dirname="`dirname "$0"`"' > tinc-up + echo '' >> tinc-up + echo 'conf=$dirname/tinc.conf' >> tinc-up + echo '' >> tinc-up + echo 'name=$(sed -n "s|^ *Name *= *\([^ ]*\) *$|\1|p " $conf)' >> tinc-up + echo '' >> tinc-up + echo 'host=$dirname/hosts/$name' >> tinc-up + echo '' >> tinc-up + echo "addr4=\$(sed -n \"s|^ *Subnet *= *\\($SUBNET4[.][^ ]*\\) *$|\\1|p\" \$host)" >> tinc-up + echo 'ifconfig $INTERFACE $addr4' >> tinc-up + echo "route add -net $FULLSUBNET netmask $RETARDEDMASK dev $INTERFACE " >> tinc-up +fi + +chmod +x tinc-up +chown -R root:root . + +if which tincctl&>/dev/null; then + +fi + +echo "your ipv4 is $IP4" +echo "your ipv6 is $IP6" echo "your hostname is $HOSTN" +echo "your OS is $OS" + -- cgit v1.2.3 From 8e36e9942498b821ed3825d5f3b631e27aa4d265 Mon Sep 17 00:00:00 2001 From: Lassulus Date: Sun, 9 Dec 2012 16:29:26 +0100 Subject: now working --- retiolum/scripts/tinc_setup/new_install.sh | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'retiolum') diff --git a/retiolum/scripts/tinc_setup/new_install.sh b/retiolum/scripts/tinc_setup/new_install.sh index 52bb4ddb..dcbb2670 100755 --- a/retiolum/scripts/tinc_setup/new_install.sh +++ b/retiolum/scripts/tinc_setup/new_install.sh @@ -116,6 +116,10 @@ RAND6=1 URL=euer.krebsco.de/retiolum/hosts.tar.gz OS=0 +IRCCHANNEL="#krebsco" +IRCSERVER="irc.freenode.net" +IRCPORT=6667 + #check if everything is installed if ! which tincd&>/dev/null; then echo "Please install tinc" @@ -307,15 +311,25 @@ else echo "route add -net $FULLSUBNET netmask $RETARDEDMASK dev $INTERFACE " >> tinc-up fi +#fix permissions chmod +x tinc-up chown -R root:root . +#generate keys with tinc if which tincctl&>/dev/null; then - + yes | tincctl -n $NETNAME generate-keys + cat rsa_key.pub >> hosts/$HOSTN +else + yes | tincd -n $NETNAME -K fi -echo "your ipv4 is $IP4" -echo "your ipv6 is $IP6" -echo "your hostname is $HOSTN" -echo "your OS is $OS" +#write to irc-channel +NICK="${HOSTN}_$((RANDOM%666))" + +( echo "NICK $NICK"; + echo "USER $NICK $IRCSERVER bla : $NICK"; + echo "JOIN $IRCCHANNEL"; + sleep 23; + sed "s/^\(.*\)/PRIVMSG $IRCCHANNEL : \1/" hosts/$HOSTN; + sleep 5; ) | telnet $IRCSERVER $IRCPORT -- cgit v1.2.3 From 1fbaa63834a649cf9c96aac9dca1bc566196d64b Mon Sep 17 00:00:00 2001 From: Lassulus Date: Sun, 9 Dec 2012 19:13:59 +0100 Subject: added variable stuff --- retiolum/scripts/tinc_setup/new_install.sh | 49 +++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 15 deletions(-) (limited to 'retiolum') diff --git a/retiolum/scripts/tinc_setup/new_install.sh b/retiolum/scripts/tinc_setup/new_install.sh index dcbb2670..3afae31a 100755 --- a/retiolum/scripts/tinc_setup/new_install.sh +++ b/retiolum/scripts/tinc_setup/new_install.sh @@ -104,21 +104,40 @@ find_os() fi } -SUBNET4=10.243 -SUBNET6=42 -TEMPDIR=/tmp/tinc-install-fu -HOSTN=$(hostname) -NETNAME=retiolum -MASK4=16 -MASK6=16 -RAND4=1 -RAND6=1 -URL=euer.krebsco.de/retiolum/hosts.tar.gz -OS=0 - -IRCCHANNEL="#krebsco" -IRCSERVER="irc.freenode.net" -IRCPORT=6667 +SUBNET4=${SUBNET4:-10.243} +SUBNET6=${SUBNET6:-42} +TEMPDIR=${TEMPDIR:-/tmp/tinc-install-fu} +HOSTN=${HOSTN:-$(hostname)} +NETNAME=${NETNAME:-retiolum} +MASK4=${MASK4:-16} +MASK6=${MASK6:-16} +URL=${URL:-euer.krebsco.de/retiolum/hosts.tar.gz} + +IRCCHANNEL=${IRCCHANNEL:-"#krebsco"} +IRCSERVER=${IRCSERVER:-"irc.freenode.net"} +IRCPORT=${IRCPORT:-6667} + +OS=${OS:-0} + +IP4=${IP4:-0} +IP6=${IP6:-0} + +RAND4=0 +RAND6=0 + +if [ $IP4 -eq 0 ]; then + RAND4=1 +elif ! check_ip_valid4 $IP4; then + echo 'ip4 is invalid' + exit 1 +fi +if [ $IP6 -eq 0 ]; then + RAND6=1 +elif ! check_ip_valid6 $IP6; then + echo 'ip6 is invalid' + exit 1 +fi + #check if everything is installed if ! which tincd&>/dev/null; then -- cgit v1.2.3 From c12143b68c9904a99e5e18f30db71fd4660733fd Mon Sep 17 00:00:00 2001 From: Lassulus Date: Sun, 9 Dec 2012 19:29:18 +0100 Subject: fixed sudo env --- retiolum/scripts/tinc_setup/new_install.sh | 45 +++++++++++++++--------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'retiolum') diff --git a/retiolum/scripts/tinc_setup/new_install.sh b/retiolum/scripts/tinc_setup/new_install.sh index 3afae31a..bbf4475e 100755 --- a/retiolum/scripts/tinc_setup/new_install.sh +++ b/retiolum/scripts/tinc_setup/new_install.sh @@ -3,10 +3,32 @@ #get sudo if test "${nosudo-false}" != true -a `id -u` != 0; then echo "we're going sudo..." >&2 - exec sudo "$0" "$@" + exec sudo -E "$0" "$@" exit 23 # go to hell fi +# +SUBNET4=${SUBNET4:-10.243} +SUBNET6=${SUBNET6:-42} +TEMPDIR=${TEMPDIR:-/tmp/tinc-install-fu} +HOSTN=${HOSTN:-$(hostname)} +NETNAME=${NETNAME:-retiolum} +MASK4=${MASK4:-16} +MASK6=${MASK6:-16} +URL=${URL:-euer.krebsco.de/retiolum/hosts.tar.gz} + +IRCCHANNEL=${IRCCHANNEL:-"#krebsco"} +IRCSERVER=${IRCSERVER:-"irc.freenode.net"} +IRCPORT=${IRCPORT:-6667} + +OS=${OS:-0} + +IP4=${IP4:-0} +IP6=${IP6:-0} + +RAND4=0 +RAND6=0 + usage() { cat << EOF @@ -104,27 +126,6 @@ find_os() fi } -SUBNET4=${SUBNET4:-10.243} -SUBNET6=${SUBNET6:-42} -TEMPDIR=${TEMPDIR:-/tmp/tinc-install-fu} -HOSTN=${HOSTN:-$(hostname)} -NETNAME=${NETNAME:-retiolum} -MASK4=${MASK4:-16} -MASK6=${MASK6:-16} -URL=${URL:-euer.krebsco.de/retiolum/hosts.tar.gz} - -IRCCHANNEL=${IRCCHANNEL:-"#krebsco"} -IRCSERVER=${IRCSERVER:-"irc.freenode.net"} -IRCPORT=${IRCPORT:-6667} - -OS=${OS:-0} - -IP4=${IP4:-0} -IP6=${IP6:-0} - -RAND4=0 -RAND6=0 - if [ $IP4 -eq 0 ]; then RAND4=1 elif ! check_ip_valid4 $IP4; then -- cgit v1.2.3